-
Notifications
You must be signed in to change notification settings - Fork 0
/
InjectDll.cpp
170 lines (137 loc) · 4.57 KB
/
InjectDll.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
160
161
162
163
164
165
166
167
168
169
170
#include "InjectDll.h"
DWORD GetTargetProcessIdFromProcname(char *procName);
DWORD GetTargetThreadIdFromProcname(char *procName);
__declspec(naked) void loadDll(void)
{
_asm {
// Save the flags and registers
pushfd
pushad
// Placeholder for the string address and LoadLibrary
push 0xDEADBEEF
mov eax, 0xDEADBEEF
// Call LoadLibrary with the string parameter
call eax
// Store the dll start address
mov dword ptr ds:[0xDEADBEEF],eax
// Restore the registers and flags
popad
popfd
// Return control to the hijacked thread
ret
// Space for dll start address
nop
nop
nop
nop
}
}
__declspec(naked) void loadDll_end(void) {}
DWORD Inject(char* PROC_NAME, char* DLL_NAME, void* loaderAddress, DWORD* bytesWritten)
{
DWORD CREATE_THREAD_ACCESS = PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION |
PROCESS_VM_OPERATION | PROCESS_VM_WRITE |
PROCESS_VM_READ | PROCESS_VM_OPERATION;
DWORD ORDINARY_ACCESS = PROCESS_VM_WRITE | PROCESS_VM_OPERATION;
bool allocStub = loaderAddress==NULL;
void* stub = NULL;
void* dllString = loaderAddress;
DWORD stubLen = (DWORD)loadDll_end - (DWORD)loadDll;
FARPROC loadLibAddy = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
DWORD procID = GetTargetProcessIdFromProcname(PROC_NAME);
HANDLE hProcess = OpenProcess(CREATE_THREAD_ACCESS, false, procID);
// If nessacary allocate memory
DWORD dllNameLength = (DWORD)strlen(DLL_NAME)+1;
if (bytesWritten) *bytesWritten = stubLen+dllNameLength;
if (allocStub) {
dllString = VirtualAllocEx(hProcess, NULL, stubLen+dllNameLength, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
}
stub = (BYTE*)dllString + dllNameLength;
// Fill in the place holders
DWORD oldprot;
DWORD loadLibRetVal = (DWORD) stub + 23;
VirtualProtect(loadDll, stubLen, PAGE_EXECUTE_READWRITE, &oldprot);
memcpy((void *)((DWORD)loadDll + 3), &dllString, 4);
memcpy((void *)((DWORD)loadDll + 8), &loadLibAddy, 4);
memcpy((void *)((DWORD)loadDll + 16), &loadLibRetVal, 4);
// Paste in the loading function
WriteProcessMemory(hProcess, dllString, DLL_NAME, strlen(DLL_NAME), NULL);
WriteProcessMemory(hProcess, stub, loadDll, stubLen, NULL);
// Create a thread to load the dll
HANDLE quickThread = CreateRemoteThread(hProcess, NULL, NULL, (LPTHREAD_START_ROUTINE)/*loadLibAddy*/stub, (LPVOID)/*dllString*/NULL, NULL, NULL);
CloseHandle(quickThread);
WaitForSingleObject(quickThread, INFINITE);
// Get the dll location
Sleep(3000); // If it reads the memory too early, it crashes
DWORD dllLocation = NULL, bytesRead = NULL;
ReadProcessMemory(hProcess, (void*)loadLibRetVal, (BYTE*)&dllLocation, 4, &bytesRead);
// Free the memory
if (allocStub)
VirtualFreeEx(hProcess, dllString, stubLen+dllNameLength, MEM_DECOMMIT);
// Close the handle
CloseHandle(hProcess);
// Return the location of the dll
return dllLocation;
}
DWORD GetTargetProcessIdFromProcname(char *procName)
{
PROCESSENTRY32 pe;
HANDLE thSnapshot;
BOOL retval, ProcFound = false;
thSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(thSnapshot == INVALID_HANDLE_VALUE)
{
MessageBox(NULL, "Error: unable to create toolhelp snapshot", "Loader", NULL);
return false;
}
pe.dwSize = sizeof(PROCESSENTRY32);
retval = Process32First(thSnapshot, &pe);
while(retval)
{
if(StrStrI(pe.szExeFile, procName) )
{
ProcFound = true;
break;
}
retval = Process32Next(thSnapshot,&pe);
pe.dwSize = sizeof(PROCESSENTRY32);
}
CloseHandle(thSnapshot);
return pe.th32ProcessID;
}
DWORD GetTargetThreadIdFromProcname(char *procName)
{
PROCESSENTRY32 pe;
HANDLE thSnapshot, hProcess;
BOOL retval, ProcFound = false;
void* pTID;
DWORD threadID;
thSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(thSnapshot == INVALID_HANDLE_VALUE)
{
MessageBox(NULL, "Error: unable to create toolhelp snapshot", "Loader", NULL);
return false;
}
pe.dwSize = sizeof(PROCESSENTRY32);
retval = Process32First(thSnapshot, &pe);
while(retval)
{
if(StrStrI(pe.szExeFile, procName) )
{
ProcFound = true;
break;
}
retval = Process32Next(thSnapshot,&pe);
pe.dwSize = sizeof(PROCESSENTRY32);
}
CloseHandle(thSnapshot);
_asm {
mov eax, fs:[0x18]
add eax, 36
mov [pTID], eax
}
hProcess = OpenProcess(PROCESS_VM_READ, false, pe.th32ProcessID);
ReadProcessMemory(hProcess, (const void*)pTID, &threadID, 4, NULL);
CloseHandle(hProcess);
return threadID;
}