-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathioctl_bf.c
executable file
·398 lines (356 loc) · 12.5 KB
/
ioctl_bf.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
396
397
398
// System includes ------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
// Program include ------------------------------------------------------------
#include "getopt.h"
//#include "rng.c"
#include "utilities.h"
// Globals
short int displayerrflg = 0;
short int pausebuff=0;
short int quietflg=0;
short int brute=0;
short int timec=0;
short int valid=0;
short int limit=0;
short int dlimit=0;
short int stage=0;
// Main function --------------------------------------------------------------
int main(int argc, char *argv[])
{
extern char *optarg;
char *singleIoctl = NULL;
char *rangeIoctl = NULL;
char *host = NULL;
short int singleflg = 0;
short int errflg = 0;
short int filteralwaysok = 0;
short int nonull=0;
short int port=0;
HANDLE deviceHandle;
char * deviceName=NULL;
MYWORD beginIoctl=0, endIoctl=0, currentIoctl=0;
DWORD status=0, errorCode=0;
DWORD nbBytes = 0;
pIOCTLlist listIoctls = NULL;
pIOCTLlist posListIoctls = NULL;
int choice = -1;
short int c=0, i=0,j=0;
time_t rawtime;
short int ptm=0,ip[4];
// Parse options from command-line
while((c = getopt(argc, argv, "c:d:i:r:s:q:t:l:nph?efbv")) != -1)
{
switch(c)
{
case 'd':
deviceName= malloc(strlen(optarg)+5);
strcpy(deviceName,"\\\\.\\");
strncat(deviceName, optarg,strlen(optarg));
break;
case 'c':
host=malloc(16);
if(sscanf(optarg,"%d.%d.%d.%d:%d",&ip[0],&ip[1],&ip[2],&ip[3],&port)==EOF)
errflg++;
else
sprintf(host,"%d.%d.%d.%d",ip[0],ip[1],ip[2],ip[3]);
//printf("O:%s| H: %s P:%d\n",optarg,host,port);
break;
case 'i':
if(rangeIoctl)
errflg++;
else
{
singleIoctl = optarg;
singleflg = 1;
}
break;
case 'r':
if(singleIoctl)
errflg++;
else
rangeIoctl = optarg;
break;
case 's':
stage =atoi(optarg);
if(stage <1 || stage >3)
{
myprintf("[-] Invalid stage number: %d \n",stage);
exit(1);
}
break;
case 'q':
quietflg =atoi(optarg);
if(quietflg <1 || quietflg >3)
{
myprintf("[-] Invalid quiet flag: %d \n",quietflg);
exit(1);
}
break;
case 't':
timec =atoi(optarg);
rawtime = time(NULL);
if(timec <1 || rawtime <1)
{
myprintf("[-] Invalid time: %d \n",timec);
exit(1);
}
ptm = (localtime(&rawtime))->tm_min;
break;
case 'e':
displayerrflg++;
break;
case 'f':
filteralwaysok++;
break;
case 'b':
brute++;
break;
case 'n':
nonull++;
break;
case 'p':
pausebuff++;
break;
case 'l':
limit=atoi(optarg);
if(limit<=0)
{
myprintf("[-] Invalid rate:%d\n",limit);
exit(1);
}
dlimit=limit;
break;
case 'h':
case 'v':
valid++;
break;
case '?':
errflg++;
}
}
initializeJunkData();
// Check & parse options from command line
if(deviceName == NULL || (rangeIoctl == NULL && singleIoctl == NULL))
errflg++;
if(!errflg)
{
// IOCTL range mode
if(rangeIoctl)
{
if(strchr(rangeIoctl, '-') == NULL)
errflg++;
else
{
beginIoctl = (MYWORD)parseHex(strtok(rangeIoctl, "-"));
endIoctl = (MYWORD)parseHex(strtok(NULL, "-"));
if(endIoctl < beginIoctl)
errflg++;
}
}
// Function code + Transfer type (14 lowest bits) bruteforce mode
else if(singleIoctl && !singleflg)
{
beginIoctl = (MYWORD)parseHex(singleIoctl) & 0xffffc000;
endIoctl = ((MYWORD)parseHex(singleIoctl) & 0xffffc000) | 0x00003fff;
}
// Single IOCTL mode
else
{
beginIoctl = (MYWORD)parseHex(singleIoctl);
endIoctl = beginIoctl;
}
}
if(errflg)
usage(argv[0]);
else
banner();
if(host && socket_init(host,port))
myprintf("[-] Can't stream to %s:%d!\n", host,port);
else if (host)
myprintf("[*] Streaming to %s:%d!\n", host,port);
// Open handle to the device
myprintf("[*] Openning handle to the device: %s \n", deviceName);
deviceHandle = CreateFile((HANDLE)deviceName,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if(deviceHandle == INVALID_HANDLE_VALUE)
{
myprintf("[-] Error code: %d\n%s\n", GetLastError(),
errorCode2String(GetLastError()));
exit(1);
}
if(!CryptAcquireContext(&hCryptProv,NULL,NULL,PROV_RSA_AES,0))
{
myprintf("[-] Can't get a WinCrypt provider!\n");
exit(1);
}
srand(time(NULL)); // getting a internal seed :)
memset(bufInput, nonull?0x41:0, MAX_BUFSIZE);
memset(bufOutput,0, MAX_BUFSIZE);
// Print summary
myprintf(" Summary:\n");
myprintf(" -------------------------\n");
myprintf(" IOCTL scanning mode : ");
if(rangeIoctl)
myprintf("Range mode 0x%08x - 0x%08x\n", beginIoctl, endIoctl);
else if(singleIoctl && singleflg)
myprintf("Single mode 0x%08x\n", beginIoctl);
else
myprintf("Function + transfer type bf 0x%08x - 0x%08x\n",
beginIoctl, endIoctl);
myprintf(" Filter mode : ");
if(filteralwaysok)
myprintf("Filtering codes that return true for all buffer sizes\n");
else
myprintf("Filter disabled\n");
myprintf(" Symbolic Device Name : %s\n", deviceName);
if(singleIoctl)
myprintf(" Device Type : 0x%08x\n",
(beginIoctl & 0xffff0000) >> 16);
myprintf(" Device handle : 0x%08x\n\n", deviceHandle);
if(singleIoctl && singleflg)
myprintf("[~] Test given IOCTL and determine input size...\n");
else
myprintf("[~] Bruteforce function code + transfer type and determine input sizes...\n");
if(nonull)
myprintf("[~] Non-null input buffer\n");
if(valid)
myprintf("[~] Using valid memory address inside in-buffer\n");
if(limit)
myprintf("[~] Limiting the output into 1/%d messages\n",limit);
// IOCTL code scanning
for(currentIoctl = beginIoctl; currentIoctl<=endIoctl; currentIoctl++)
{
if(!nonull)
{
myprintf("[~] Trying null pointers\r");
status = DeviceIoControl(deviceHandle,
currentIoctl,
NULL,
0,
NULL,
0,
&nbBytes,
NULL);
// No further tests for the current IOCTL if the operation fails with
// one of the following error codes:
// - ERROR_INVALID_FUNCTION 0x1
// - ERROR_ACCESS_DENIED 0x5
// - ERROR_NOT_SUPPORTED 0x50
// cf. winerror.h
if(status == 0)
{
errorCode = GetLastError();
if((displayerrflg && errorCode != ERROR_ACCESS_DENIED && errorCode != ERROR_NOT_SUPPORTED && errorCode !=ERROR_INVALID_FUNCTION))
myprintf("0x%08x -> error code %03d - %s\r", currentIoctl,
errorCode, errorCode2String(errorCode));
else if(!brute)
continue;
}
}
// Filter out IOCTLs that always return status != 0
if(filteralwaysok)
{
status = DeviceIoControl(deviceHandle,
currentIoctl,
&bufInput,
MAX_BUFSIZE,
&bufOutput,
MAX_BUFSIZE,
&nbBytes,
NULL);
if(status != 0)
for(j=0; j<4 && status != 0; j++)
{
status = DeviceIoControl(deviceHandle,
currentIoctl,
&bufInput,
j,
&bufOutput,
j,
&nbBytes,
NULL);
if(status == 0)
myprintf("0x%08x (size %d) -> error code %03d \r", currentIoctl, j, GetLastError());
else
myprintf("0x%08x (size %d) -> status != 0 \r", currentIoctl, j);
if(pausebuff && nbBytes)
{
myprintf("[~] Out Buffer wrote:\n");
Hexdump(bufOutput,j);
memset(bufOutput,0, MAX_BUFSIZE);
myprintf("[Press enter]\n");
getchar();
}
nbBytes = 0;
}
if(j == 4)
//myprintf("Skip 0x%08x\n", currentIoctl);
continue;
}
listIoctls=getIoBuff_minmax(currentIoctl,deviceHandle,listIoctls);
}
myprintf("\n");
if(!getIoctlListLength(listIoctls))
{
if(singleflg)
myprintf("[!] The given IOCTL code seems not to be recognized by the driver !\n");
else
myprintf("[!] No valid IOCTL code has been found !\n");
exit(1);
}
else
{
if(singleflg)
myprintf("[!] The given IOCTL code is recognized by the driver !\n\n");
else
myprintf("[+] %d valid IOCTL have been found\n\n", i);
}
// Fuzzing IOCTL buffer
while(1)
{
// Choice of the IOCTL to fuzz
myprintf(" Valid IOCTLs found \n");
myprintf(" ------------------ \n");
printIoctlList(listIoctls, MAX_BUFSIZE);
myprintf("\n");
if(singleflg && getIoctlListLength(listIoctls)==1)
choice = 0;
else
{
myprintf("[?] Choose an IOCTL to fuzz...\n");
printIoctlChoice(listIoctls);
myprintf("[*] Choice: ");
scanf_s("%d", &choice, 3);
if(choice < 0 || choice >= getIoctlListLength(listIoctls))
goto exit;
}
posListIoctls = getIoctlListElement(listIoctls, choice);
// Start fuzzing
myprintf("\n");
myprintf("[*] Fuzzing IOCTL 0x%08x \n", posListIoctls->IOCTL);
myprintf(" ------------------------ \n");
// Stage 1: Check for trivial kernel overflow
if(stage==1 || !stage)
IoStage1(posListIoctls, deviceHandle);
// Stage 2: Fuzzing with predetermined DWORDs
if (stage==2 || !stage)
IoStage2(posListIoctls, deviceHandle, ptm);
// Stage 3: Fuzzing with fully random data
if(stage==3 || !stage)
IoStage3(posListIoctls, deviceHandle, ptm);
myprintf("[0x%08x] Fuzzing finished\n",posListIoctls->IOCTL);
exit:
myprintf("[?] Exit fuzzer? (y/n)");
if(getch() == 'y')
exitProgram(listIoctls);
myprintf("\n");
}
return 1;
}