-
Notifications
You must be signed in to change notification settings - Fork 538
/
Copy pathLocker2.c
395 lines (350 loc) · 9.45 KB
/
Locker2.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include <Windows.h>
#include <wchar.h>
#include <ShlObj_core.h>
#include <stdio.h>
#pragma comment(lib, "Ntdll.lib")
#define TARGET_FILENAMEW L"secret.txt"
#define FILE_CONTENT "Lbh qvq vg. Pbatengf! ;)"
#define PARAM_1W L"1st"
#define PARAM_2W L"2nd"
#define TARGET_PATH L"%temp%\\svсհоѕt.exe"
#define FINAL_DELAY_MS 500
typedef enum _PROCESSINFOCLASS
{
ProcessBasicInformation = 0,
} PROCESSINFOCLASS;
typedef struct _UNICODE_STRING
{
USHORT Length;
USHORT MaximumLength;
PWSTR Buffer;
} UNICODE_STRING, *PUNICODE_STRING;
VOID RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString);
NTSTATUS NtQueryInformationProcess(HANDLE ProcessHandle,
PROCESSINFOCLASS ProcessInformationClass,
PVOID ProcessInformation,
ULONG ProcessInformationLength,
PULONG ReturnLength);
WCHAR pwszNewCmdLine[MAX_PATH];
WCHAR pwszNewPath[MAX_PATH];
WCHAR pwszNewWorkingDir[MAX_PATH];
UNICODE_STRING usCmd;
UNICODE_STRING usPath;
UNICODE_STRING usCurrentDir;
VOID FakeOwnCmdline(PWSTR NewCmdLine, PWSTR NewPath, PWSTR NewWorkingDir)
{
//minimal error checking, should be ok.
typedef struct _PEB_LDR_DATA
{
ULONG Length;
BOOLEAN Initialized;
HANDLE SsHandle;
LIST_ENTRY InLoadOrderModuleList;
LIST_ENTRY InMemoryOrderModuleList;
LIST_ENTRY InInitializationOrderModuleList;
PVOID EntryInProgress;
} PEB_LDR_DATA, *PPEB_LDR_DATA;
typedef struct _STRING
{
USHORT Length;
USHORT MaximumLength;
PCHAR Buffer;
} STRING;
typedef struct _RTL_DRIVE_LETTER_CURDIR
{
USHORT Flags;
USHORT Length;
ULONG TimeStamp;
STRING DosPath;
} RTL_DRIVE_LETTER_CURDIR, *PRTL_DRIVE_LETTER_CURDIR;
typedef struct _CURDIR
{
UNICODE_STRING DosPath;
HANDLE Handle;
} CURDIR, *PCURDIR;
typedef struct _RTL_USER_PROCESS_PARAMETERS
{
ULONG MaximumLength;
ULONG Length;
ULONG Flags;
ULONG DebugFlags;
HANDLE ConsoleHandle;
ULONG ConsoleFlags;
HANDLE StandardInput;
HANDLE StandardOutput;
HANDLE StandardError;
CURDIR CurrentDirectory;
UNICODE_STRING DllPath;
UNICODE_STRING ImagePathName;
UNICODE_STRING CommandLine;
// REST OF MEMBERS REMOVED FOR SIMPLICITY
} RTL_USER_PROCESS_PARAMETERS, *PRTL_USER_PROCESS_PARAMETERS;
typedef struct _PEB
{
BOOLEAN InheritedAddressSpace;
BOOLEAN ReadImageFileExecOptions;
BOOLEAN BeingDebugged;
BOOLEAN SpareBool;
HANDLE Mutant;
PVOID ImageBaseAddress;
PPEB_LDR_DATA Ldr;
PRTL_USER_PROCESS_PARAMETERS ProcessParameters;
// REST OF MEMBERS REMOVED FOR SIMPLICITY
} PEB, *PPEB;
typedef struct _PROCESS_BASIC_INFORMATION
{
PVOID Reserved1;
PPEB PebBaseAddress;
PVOID Reserved2[2];
ULONG_PTR UniqueProcessId;
PVOID Reserved3;
} PROCESS_BASIC_INFORMATION;
NTSTATUS status;
PROCESS_BASIC_INFORMATION procInfo = {0};
ULONG cbNeeded2 = 0;
//copy to global vars
//reasonably assuming lenghts are < max_path.
wcscpy_s(pwszNewCmdLine, _countof(pwszNewCmdLine), NewCmdLine);
wcscpy_s(pwszNewPath, _countof(pwszNewPath), NewPath);
wcscpy_s(pwszNewWorkingDir, _countof(pwszNewWorkingDir), NewWorkingDir);
//printf("PID: %ld\r\n", GetCurrentProcessId());
//RtlInitUnicodeString(&usCmd, pwszCmd);
//RtlInitUnicodeString(&usPath, pwszPath);
//RtlInitUnicodeString(&usCurrentDir, pwszCurrentDir);
RtlInitUnicodeString(&usCmd, pwszNewCmdLine);
RtlInitUnicodeString(&usPath, pwszNewPath);
RtlInitUnicodeString(&usCurrentDir, pwszNewWorkingDir);
status = NtQueryInformationProcess(
GetCurrentProcess(),
ProcessBasicInformation,
&procInfo,
sizeof(procInfo),
&cbNeeded2);
if ((0 == status) && (0 != procInfo.PebBaseAddress))
{
WriteProcessMemory(
GetCurrentProcess(),
&procInfo.PebBaseAddress->ProcessParameters->CommandLine.Buffer,
&usCmd.Buffer,
sizeof(LPVOID),
NULL);
WriteProcessMemory(
GetCurrentProcess(),
&procInfo.PebBaseAddress->ProcessParameters->CommandLine.Length,
&usCmd.Length,
sizeof(USHORT),
NULL);
WriteProcessMemory(
GetCurrentProcess(),
&procInfo.PebBaseAddress->ProcessParameters->CommandLine.MaximumLength,
&usCmd.MaximumLength,
sizeof(USHORT),
NULL);
WriteProcessMemory(
GetCurrentProcess(),
&procInfo.PebBaseAddress->ProcessParameters->ImagePathName.Buffer,
&usPath.Buffer,
sizeof(LPVOID),
NULL);
WriteProcessMemory(
GetCurrentProcess(),
&procInfo.PebBaseAddress->ProcessParameters->ImagePathName.Length,
&usPath.Length,
sizeof(USHORT),
NULL);
WriteProcessMemory(
GetCurrentProcess(),
&procInfo.PebBaseAddress->ProcessParameters->ImagePathName.MaximumLength,
&usPath.MaximumLength,
sizeof(USHORT),
NULL);
WriteProcessMemory(
GetCurrentProcess(),
&procInfo.PebBaseAddress->ProcessParameters->CurrentDirectory.DosPath.Buffer,
&usCurrentDir.Buffer,
sizeof(LPVOID),
NULL);
WriteProcessMemory(
GetCurrentProcess(),
&procInfo.PebBaseAddress->ProcessParameters->CurrentDirectory.DosPath.Length,
&usCurrentDir.Length,
sizeof(USHORT),
NULL);
WriteProcessMemory(
GetCurrentProcess(),
&procInfo.PebBaseAddress->ProcessParameters->CurrentDirectory.DosPath.MaximumLength,
&usCurrentDir.MaximumLength,
sizeof(USHORT),
NULL);
}
}
char* rot13(const char* src)
{
if (src == NULL)
{
return NULL;
}
#define SHIFT 13
char* result = LocalAlloc(LPTR, strlen(src) + 1);
if (result != NULL)
{
strcpy_s(result, LocalSize(result), src);
char* current_char = result;
while (*current_char != '\0')
{
if ((*current_char >= 97 && *current_char <= 122) || (*current_char >= 65 && *current_char <= 90))
{
if (*current_char > 109 || (*current_char > 77 && *current_char < 91))
{
*current_char -= SHIFT;
}
else
{
*current_char += SHIFT;
}
}
current_char++;
}
}
return result;
}
int wmain(int argc, WCHAR** argv, WCHAR** envp)
{
PWSTR pwszDesktopPath; //always wchar
WCHAR pwszFullPath[MAX_PATH];
HRESULT hr;
int iRes;
HANDLE hTargetFile;
DWORD dwRes;
BOOL bRes;
WCHAR pwszCmd[MAX_PATH + _countof(PARAM_1W)];
STARTUPINFOW si = {0};
PROCESS_INFORMATION pi = {0};
hr = SHGetKnownFolderPath(&FOLDERID_Desktop, KF_FLAG_DEFAULT, NULL, &pwszDesktopPath);
if (S_OK != hr)
{
return hr;
}
iRes = swprintf(pwszFullPath, _countof(pwszFullPath), L"%s\\%s", pwszDesktopPath, TARGET_FILENAMEW);
if (-1 == iRes)
{
return errno;
}
if (1 == argc) //first pass, no params
{
// check if target exists, fail if yes.
//wprintf(L"Phase 0.\r\n");
hTargetFile = CreateFileW(pwszFullPath, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hTargetFile)
{
dwRes = GetLastError();
if (ERROR_FILE_EXISTS == dwRes) //most common case, user just dbclicks, lets wait
{
wprintf(L"File already exists. Press Enter.\r\n");
(VOID)getchar();
}
wprintf(L"Error %d creating file %s. Exiting.\r\n", dwRes, pwszFullPath);
return (int)dwRes;
}
DWORD dwWritten;
char* szFileContent;
szFileContent = rot13(FILE_CONTENT);
bRes = WriteFile(hTargetFile, szFileContent, (DWORD)strlen(szFileContent), &dwWritten, NULL);
if (!bRes)
{
dwRes = GetLastError();
CloseHandle(hTargetFile);
wprintf(L"Error %d writing file %s. Exiting.\r\n", dwRes, pwszFullPath);
return (int)dwRes;
}
LocalFree(szFileContent);
CloseHandle(hTargetFile);
WCHAR pwszTargetExePath[MAX_PATH];
ExpandEnvironmentStringsW(TARGET_PATH, pwszTargetExePath, _countof(pwszTargetExePath));
bRes = CopyFileW(argv[0], pwszTargetExePath, FALSE);
if (!bRes)
{
dwRes = GetLastError();
wprintf(L"Error %d copying file %s. Exiting.\r\n", dwRes, pwszTargetExePath);
return (int)dwRes;
}
iRes = swprintf(pwszCmd, _countof(pwszCmd), L"%s %s", pwszTargetExePath, PARAM_1W);
if (-1 == iRes)
{
return errno;
}
bRes = CreateProcessW(
NULL,
pwszCmd,
NULL,
NULL,
FALSE,
NORMAL_PRIORITY_CLASS | CREATE_NEW_PROCESS_GROUP,
NULL,
NULL,
&si,
&pi);
if (!bRes)
{
dwRes = GetLastError();
wprintf(L"Error %d creating process. Exiting.\r\n", dwRes);
return (int)dwRes;
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
//wprintf(L"Phase 0 ends.\r\n");
}
else //params present!
{
FakeOwnCmdline(
L"C:\\Windows\\System32\\svchost.exe -k netsvcs",
L"C:\\Windows\\System32\\svchost.exe",
L"C:\\Windows\\System32\\");
if (0 == wcscmp(argv[1],PARAM_1W))
{
// nothing to do, just run a child and die.
//wprintf(L"Phase 1.\r\n");
iRes = swprintf(pwszCmd, _countof(pwszCmd), L"%s %s", argv[0], PARAM_2W);
if (-1 == iRes)
{
return errno;
}
bRes = CreateProcessW(
NULL,
pwszCmd,
NULL,
NULL,
FALSE,
NORMAL_PRIORITY_CLASS | CREATE_NEW_PROCESS_GROUP,
NULL,
NULL,
&si,
&pi);
if (!bRes)
{
dwRes = GetLastError();
wprintf(L"Error %d creating process. Exiting.\r\n", dwRes);
return (int)dwRes;
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
//wprintf(L"Phase 1 ends.\r\n");
}
if (0 == wcscmp(argv[1],PARAM_2W))
{
//wprintf(L"Phase 2.\r\n");
hTargetFile = CreateFileW(pwszFullPath, GENERIC_ALL, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hTargetFile)
{
dwRes = GetLastError();
wprintf(L"Error %d creating file %s. Exiting.\r\n", dwRes, pwszFullPath);
return (int)dwRes;
}
wprintf(L"Done. The file \"%s\" was created on your Desktop.\r\n", TARGET_FILENAMEW);
Sleep(INFINITE);
CloseHandle(hTargetFile);
//wprintf(L"Phase 2 ends.\r\n");
}
}
Sleep(FINAL_DELAY_MS);
}