-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathextract.c
93 lines (77 loc) · 2.91 KB
/
extract.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <windows.h>
#include <string.h>
int main() {
const char* input_file_path = "decryptprotect.exe";
HANDLE hFile = CreateFileA(input_file_path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
printf("Failed to open input file.\n");
return 1;
}
HANDLE hMapping = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
if (hMapping == NULL) {
printf("Failed to create file mapping.\n");
CloseHandle(hFile);
return 1;
}
LPVOID baseAddress = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
if (baseAddress == NULL) {
printf("Failed to map view of file.\n");
CloseHandle(hMapping);
CloseHandle(hFile);
return 1;
}
PIMAGE_DOS_HEADER dosHeader = (PIMAGE_DOS_HEADER)baseAddress;
PIMAGE_NT_HEADERS ntHeaders = (PIMAGE_NT_HEADERS)((uint8_t*)baseAddress + dosHeader->e_lfanew);
PIMAGE_SECTION_HEADER textSectionHeader = NULL;
if (ntHeaders->Signature != IMAGE_NT_SIGNATURE) {
printf("Invalid PE file.\n");
UnmapViewOfFile(baseAddress);
CloseHandle(hMapping);
CloseHandle(hFile);
return 1;
}
PIMAGE_SECTION_HEADER sectionHeader = (PIMAGE_SECTION_HEADER)(ntHeaders + 1);
for (int i = 0; i < ntHeaders->FileHeader.NumberOfSections; i++) {
if (strcmp((char*)sectionHeader[i].Name, ".text") == 0) {
textSectionHeader = §ionHeader[i];
break;
}
}
if (textSectionHeader == NULL) {
printf(".text section not found.\n");
UnmapViewOfFile(baseAddress);
CloseHandle(hMapping);
CloseHandle(hFile);
return 1;
}
uint8_t* textSection = (uint8_t*)baseAddress + textSectionHeader->PointerToRawData;
uint32_t textSectionSize = textSectionHeader->SizeOfRawData;
printf("Size of .text section: %u bytes\n", textSectionSize);
const char* output_file_path = "decryptprotect.bin";
HANDLE hOutputFile = CreateFileA(output_file_path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hOutputFile == INVALID_HANDLE_VALUE) {
printf("Failed to create output file.\n");
UnmapViewOfFile(baseAddress);
CloseHandle(hMapping);
CloseHandle(hFile);
return 1;
}
DWORD bytesWritten;
if (WriteFile(hOutputFile, textSection, textSectionSize, &bytesWritten, NULL) == 0 || bytesWritten != textSectionSize) {
printf("Failed to write output file.\n");
CloseHandle(hOutputFile);
UnmapViewOfFile(baseAddress);
CloseHandle(hMapping);
CloseHandle(hFile);
return 1;
}
CloseHandle(hOutputFile);
UnmapViewOfFile(baseAddress);
CloseHandle(hMapping);
CloseHandle(hFile);
printf("Extraction completed successfully.\n");
return 0;
}