-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathExtractArray.cpp
More file actions
94 lines (84 loc) · 2.56 KB
/
Copy pathExtractArray.cpp
File metadata and controls
94 lines (84 loc) · 2.56 KB
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
94
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <stdio.h>
/*
convert binary file to C array
*/
BOOL ReadFileData(WCHAR *filename, BYTE **buff, DWORD *size);
const IMAGE_NT_HEADERS* GetNtHeader(const BYTE* image, const DWORD imageSize);
BOOL WriteFileData(HANDLE hFile, BYTE *data, DWORD size)
{
if (data == NULL)
return FALSE;
DWORD numWritten = 0;
while (size > 0) {
if (!WriteFile(hFile, data, size, &numWritten, NULL))
return FALSE;
data += numWritten;
size -= numWritten;
}
return TRUE;
}
int wmain(int argc, WCHAR *argv[])
{
if (argc < 4) {
wprintf(L"usage: ExtractArray.exe <PEFILE> <NAME> <ARRAY.C>\n");
return 1;
}
DWORD imageSize = 0;
BYTE *image = NULL;
if (!ReadFileData(argv[1], &image, &imageSize)) {
wprintf(L"Failed to read file: %s\n", argv[1]);
return 1;
}
// get .text section
const IMAGE_NT_HEADERS *ntHeader = GetNtHeader(image, imageSize);
if (ntHeader == NULL)
return 1;
IMAGE_SECTION_HEADER *section = IMAGE_FIRST_SECTION(ntHeader);
IMAGE_SECTION_HEADER *codeSection = NULL;
for (size_t i = 0; i < ntHeader->FileHeader.NumberOfSections; i++, section++) {
if ((BYTE*)section > (image + imageSize - sizeof(IMAGE_SECTION_HEADER))) {
wprintf(L"Invalid section header\n");
return 1;
}
if (memcmp(".text\x00", section->Name, 6) == 0) {
codeSection = section;
break;
}
}
if (codeSection == NULL) {
wprintf(L"Failed to find code section\n");
return 1;
}
// write .text section to file
const BYTE *text = image + codeSection->PointerToRawData;
if (text < image) {
wprintf(L"Invalid .text section\n");
return 1;
}
if ((text + codeSection->Misc.VirtualSize < text) || (text + codeSection->Misc.VirtualSize) > (image + imageSize)) {
wprintf(L"Invalid .text section\n");
return 1;
}
HANDLE hFile = CreateFileW(argv[3], GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
wprintf(L"Failed to open file: %s\n", argv[3]);
return 1;
}
char buff[256+1];
sprintf_s(buff, 256,
"#pragma once\r\n"
"const unsigned char %S[] = {",
argv[2]);
WriteFileData(hFile, (BYTE*)buff, lstrlenA(buff));
for (size_t i = 0; i < codeSection->Misc.VirtualSize; i++) {
if (i % 16 == 0)
WriteFileData(hFile, (BYTE*)"\r\n ", 6);
sprintf_s(buff, 256, "0x%02x, ", text[i]);
WriteFileData(hFile, (BYTE*)buff, lstrlenA(buff));
}
WriteFileData(hFile, (BYTE*)"\r\n};", 4);
//wprintf(L"Wrote %lu bytes to %s\n", codeSection->Misc.VirtualSize, argv[3]);
return 0;
}