-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
157 lines (121 loc) · 2.68 KB
/
main.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
#include <Windows.h>
#include <stdio.h>
#include <TlHelp32.h>
#include <vector>
#include <assert.h>
std::vector<DWORD> EnumThreads(DWORD pid)
{
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (hSnapshot == INVALID_HANDLE_VALUE)
{
return {};
}
THREADENTRY32 te;
te.dwSize = sizeof(te);
Thread32First(hSnapshot, &te);
std::vector<DWORD> tids;
while (Thread32First(hSnapshot, &te))
{
if (te.th32OwnerProcessID == pid)
{
tids.push_back(te.th32ThreadID);
}
}
CloseHandle(hSnapshot);
return tids;
}
DWORD GetThreadInProcess(DWORD pid)
{
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (hSnapshot == INVALID_HANDLE_VALUE)
{
return 0;
}
THREADENTRY32 te;
te.dwSize = sizeof(te);
Thread32First(hSnapshot, &te);
while (Thread32Next(hSnapshot, &te))
{
if (te.th32OwnerProcessID == pid)
{
return te.th32ThreadID;
}
}
CloseHandle(hSnapshot);
return 0;
}
int main(int argc, const char* argv[])
{
if (argc < 3)
{
printf("Usage: %s <pid> <dllpath>\n", argv[0]);
return 0;
}
DWORD pid = atoi(argv[1]);
HANDLE hProcess = OpenProcess(
PROCESS_VM_OPERATION | PROCESS_VM_WRITE,
FALSE,
pid);
if (!hProcess)
{
printf("Error opening process (%u)\n", GetLastError());
return 1;
}
auto tid = GetThreadInProcess(pid);
if (tid == 0)
{
printf("Error getting thread\n");
return 1;
}
HANDLE hThread = OpenThread(
THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_SET_CONTEXT,
FALSE,
tid);
if (!hThread)
{
printf("Error opening thread (%u)\n", GetLastError());
return 1;
}
char* p = (char*)VirtualAllocEx(
hProcess,
nullptr,
1 << 12,
MEM_COMMIT | MEM_RESERVE,
PAGE_READWRITE);
assert(p);
WriteProcessMemory(
hProcess,
p,
argv[2],
strlen(argv[2]) + 1,
nullptr);
BYTE shellcode[] = "\x51\x49\xBB\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\x48\xB9\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\x48\x83\xEC\x20\x41\xFF\xD3\x48\x83\xC4\x20\x59\x49\xBB\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\x41\xFF\xE3";
auto pLoadLib = GetProcAddress(GetModuleHandle(L"kernel32"), "LoadLibraryA");
assert(pLoadLib);
memcpy(shellcode + 3, &pLoadLib, sizeof(pLoadLib));
memcpy(shellcode + 13, &p, sizeof(p));
SuspendThread(hThread);
CONTEXT ctx;
ctx.ContextFlags = CONTEXT_CONTROL;
GetThreadContext(hThread, &ctx);
memcpy(shellcode + 0x23, &ctx.Rip, sizeof(ctx.Rip));
WriteProcessMemory(
hProcess,
p + 2048,
shellcode,
sizeof(shellcode),
nullptr);
ctx.Rip = (DWORD64)p + 2048;
SetThreadContext(hThread, &ctx);
DWORD oldProtect;
VirtualProtectEx(
hProcess,
p,
1 << 12,
PAGE_EXECUTE_READ,
&oldProtect);
ResumeThread(hThread);
CloseHandle(hThread);
CloseHandle(hProcess);
return 0;
}