-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfileio.c
executable file
·522 lines (387 loc) · 10.7 KB
/
fileio.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#include <windows.h>
#include <commctrl.h>
#include "fileio.h"
#include "structs.h"
#include "globals.h"
#include "utility.h"
#include "rules.h"
#include "common.h"
/*
Function: Load port numbers and their corresponding labels
Parameters: Filename to read from
Returns: TRUE is successful, FALSE otherwise
*/
BOOL LoadPortsFile(char *szFilename)
{
HANDLE hFile;
DWORD nResult, BytesRead;
struct hash_entry he;
hFile = CreateFile( szFilename,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile == INVALID_HANDLE_VALUE)
return FALSE;
while (TRUE)
{
nResult = ReadFile(hFile, &he, sizeof(he), &BytesRead, NULL);
if (BytesRead == 0)
break;
add_to_table(he.port, he.str, he.type);
}
CloseHandle(hFile);
return TRUE;
}
/*
Function: Save udp/tcp port numbers and their corresponding labels
Parameters: Filename to write to (deletes existing file)
Returns: TRUE is successful, FALSE otherwise
*/
BOOL SavePortsFile(char *szFilename)
{
HANDLE hFile;
DWORD BytesWritten;
int index;
struct hash_entry *cur_ptr;
hFile = CreateFile( szFilename,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile == INVALID_HANDLE_VALUE)
return FALSE;
for(index = 0; index < HASHTABLE_SIZE; index++)
{
cur_ptr = &hash_table[index];
while(cur_ptr)
{
WriteFile( hFile, cur_ptr, sizeof(struct hash_entry), &BytesWritten, NULL);
cur_ptr = cur_ptr->next;
}
}
CloseHandle(hFile);
/* Temporery code, TO BE REMOVED
hFile = CreateFile( "c:\\windows\\desktop\\debug.txt",
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile == INVALID_HANDLE_VALUE)
return FALSE;
for(index = 0; index < HASHTABLE_SIZE; index++)
{
cur_ptr = &hash_table[index];
while(cur_ptr)
{
wsprintf(str, "%d ->", cur_ptr->port);
WriteFile( hFile, str, lstrlen(str), &BytesWritten, NULL);
cur_ptr = cur_ptr->next;
}
wsprintf(str, "\r\n");
WriteFile( hFile, str, lstrlen(str), &BytesWritten, NULL);
}
CloseHandle(hFile);
*/
return TRUE;
}
/*
Function: Save rules as fixed length structs
Parameters: Filename to read from
Returns: TRUE is successful, FALSE otherwise
*/
BOOL LoadRules(char *szFilename)
{
HANDLE hFile;
DWORD nResult, BytesRead;
hFile = CreateFile( szFilename,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile == INVALID_HANDLE_VALUE)
return FALSE;
nResult = ReadFile(hFile, &rule_text, sizeof(rule_text), &BytesRead, NULL);
if (BytesRead < sizeof(rule_text))
{
memset(&rule_text, 0, sizeof(rule_text));
return FALSE;
}
CloseHandle(hFile);
return TRUE;
}
/*
Function: Load and populate RulesText structure with rules
Parameters: Filename to write to (deletes existing file)
Returns: TRUE is successful, FALSE otherwise
*/
BOOL SaveRules(char *szFilename)
{
HANDLE hFile;
DWORD BytesWritten;
hFile = CreateFile( szFilename,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile == INVALID_HANDLE_VALUE)
return FALSE;
WriteFile( hFile, rule_text, sizeof(rule_text), &BytesWritten, NULL);
CloseHandle(hFile);
return TRUE;
}
/*
Function: Load a file into binary buffer (used for loading .pkt files)
Parameters: Filename to read from
Pointer to char * where location of read data will be saved
Pointer to int where size of read file will be saved
Returns: TRUE is successful, FALSE otherwise
*/
BOOL LoadFile(char *szFilename, char **data, int *len)
{
HANDLE hFile;
DWORD nResult, BytesRead;
int size;
char *szBuffer;
hFile = CreateFile( szFilename,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile == INVALID_HANDLE_VALUE)
return FALSE;
size = GetFileSize(hFile, NULL);
szBuffer = malloc(size);
nResult = ReadFile(hFile, szBuffer, size, &BytesRead, NULL);
if (BytesRead == 0)
{
free (szBuffer);
return FALSE;
}
CloseHandle(hFile);
*len = size;
*data = szBuffer;
return TRUE;
}
/*
Function: Save binary data to file (used for saving .pkt files)
Parameters: Filename to save to
Pointer to binary data
size of data buffer
Returns: TRUE is successful, FALSE otherwise
*/
BOOL SaveFile(char *szFilename, char *data, int size)
{
HANDLE hFile;
DWORD BytesWritten;
hFile = CreateFile( szFilename,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile == INVALID_HANDLE_VALUE)
return FALSE;
WriteFile( hFile, data, size, &BytesWritten, NULL);
CloseHandle(hFile);
return TRUE;
}
/* Dump the captured packets shown in ListView to file */
DWORD DumpFile(PVOID ptr)
{
int iCount, index;
char *ret, *szBuffer; // ret = 32000, szBuffer = 64000 bytes
LV_ITEM lvI;
struct packet *pkt;
HANDLE hFile;
DWORD BytesWritten;
int nTotalBytesWritten = 0;
PDPARAMS pparams;
pparams = (PDPARAMS) ptr;
hFile = CreateFile( pparams->filename,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile == INVALID_HANDLE_VALUE)
return FALSE;
ret = malloc(32000);
szBuffer = malloc(64000);
lvI.mask = LVIF_PARAM ;
lvI.iItem = 0;
lvI.iSubItem = 0;
SendMessage(pparams->hwnd, MY_SET_RANGE, (WPARAM) bytes_used, (LPARAM) pparams);
iCount = ListView_GetItemCount( hWndAlertList );
for( index = 0; index < iCount; index++ )
{
lvI.iItem = index;
ListView_GetItem(hWndAlertList, &lvI);
pkt = (struct packet *) lvI.lParam;
wsprintf(szBuffer, "- - - - - - - - - - - - - - - - Frame %d - - - - - - - - - - - - - - - - -\r\n\r\n\r\n\r\n", index+1);
lstrcat(szBuffer, "ADDR HEX ASCII\r\n");
PrintRawData(pkt->data, pkt->size, ret);
lstrcat(szBuffer, ret);
WriteFile( hFile, szBuffer, lstrlen(szBuffer), &BytesWritten, NULL);
nTotalBytesWritten += pkt->size;
SendMessage(pparams->hwnd, MY_UPDATE_PROGRESS, (WPARAM) nTotalBytesWritten, 0);
if (pparams->bContinue == FALSE)
break;
}
CloseHandle(hFile);
SendMessage(pparams->hwnd, MY_CLOSE_WINDOW, 0, 0);
SendMessage(hWndStatus, SB_SETTEXT, (WPARAM) 1, (LPARAM) "Dump complete!");
free (pparams);
free (ret);
free (szBuffer);
return TRUE;
}
/*
Dump the packets saved in the linked list to a file
and destroy the linked list
*/
DWORD DumpBuffer(PVOID ptr)
{
char *ret, *szBuffer;
HANDLE hFile;
DWORD BytesWritten;
int nTotalBytesWritten = 0, index;
PDPARAMS pparams;
pparams = (PDPARAMS) ptr;
hFile = CreateFile( pparams->filename,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile == INVALID_HANDLE_VALUE)
return FALSE;
ret = malloc(32000);
szBuffer = malloc(64000);
SendMessage(pparams->hwnd, MY_SET_RANGE, (WPARAM) bytes_used, (LPARAM) pparams);
index = 0;
cur_ptr = head_ptr;
while (cur_ptr != NULL)
{
wsprintf(szBuffer, "- - - - - - - - - - - - - - - - Frame %d - - - - - - - - - - - - - - - - -\r\n\r\n\r\n\r\n", index+1);
lstrcat(szBuffer, "ADDR HEX ASCII\r\n");
PrintRawData(cur_ptr->data, cur_ptr->size, ret);
lstrcat(szBuffer, ret);
WriteFile( hFile, szBuffer, lstrlen(szBuffer), &BytesWritten, NULL);
nTotalBytesWritten += cur_ptr->size;
SendMessage(pparams->hwnd, MY_UPDATE_PROGRESS, (WPARAM) nTotalBytesWritten, 0);
cur_ptr = cur_ptr->next;
++index;
if (pparams->bContinue == FALSE)
break;
}
CloseHandle(hFile);
while (head_ptr != NULL)
{
cur_ptr = head_ptr;
head_ptr = head_ptr->next;
free(cur_ptr->data);
free(cur_ptr);
}
bytes_used = 0;
SendMessage(pparams->hwnd, MY_CLOSE_WINDOW, 0, 0);
SendMessage(hWndStatus, SB_SETTEXT, (WPARAM) 1, (LPARAM) "Buffer and Dump session complete!");
free (pparams);
free (ret);
free (szBuffer);
return TRUE;
}
DWORD LoadSession(PVOID ptr)
{
HANDLE hFile;
DWORD nResult, BytesRead;
int size;
char *szBuffer, *szFilename;
SYSTEMTIME systime;
szFilename = (char *) ptr;
hFile = CreateFile( szFilename,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
SendMessage(hWndStatus, SB_SETTEXT, (WPARAM) 0, (LPARAM) "Error importing data");
MessageBeep(0xffffffff);
return FALSE;
}
szBuffer = malloc(32000);
SendMessage(hWndStatus, SB_SETTEXT, (WPARAM) 0, (LPARAM) "Importing data...");
while(TRUE)
{
nResult = ReadFile(hFile, &systime, sizeof(SYSTEMTIME), &BytesRead, NULL);
if (BytesRead == 0)
break;
ReadFile(hFile, &size, sizeof(int), &BytesRead, NULL);
/*check for invalid file format */
if ((size < 0) || (size > 1514))
break;
ReadFile(hFile, szBuffer, size, &BytesRead, NULL);
ProcessPacket(systime, szBuffer, size, FALSE);
}
CloseHandle(hFile);
free (szBuffer);
SendMessage(hWndStatus, SB_SETTEXT, (WPARAM) 0, (LPARAM) "Data imported.");
return TRUE;
}
DWORD SaveSession(PVOID ptr)
{
HANDLE hFile;
DWORD BytesWritten;
char *szFilename;
int index, num;
LV_ITEM lvItem;
struct packet *pkt;
szFilename = (char *) ptr;
hFile = CreateFile( szFilename,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
SendMessage(hWndStatus, SB_SETTEXT, (WPARAM) 0, (LPARAM) "File error!");
MessageBeep(0xffffffff);
return FALSE;
}
SendMessage(hWndStatus, SB_SETTEXT, (WPARAM) 0, (LPARAM) "Exporting data...");
lvItem.mask = LVIF_PARAM;
lvItem.iSubItem = 0;
num = ListView_GetItemCount(hWndAlertList);
for (index = 0; index < num; index++)
{
lvItem.iItem = index;
ListView_GetItem(hWndAlertList, &lvItem);
pkt = (struct packet *)lvItem.lParam;
WriteFile( hFile, &pkt->time, sizeof(SYSTEMTIME), &BytesWritten, NULL);
WriteFile( hFile, &pkt->size, sizeof(int), &BytesWritten, NULL);
WriteFile( hFile, pkt->data, pkt->size, &BytesWritten, NULL);
}
CloseHandle(hFile);
SendMessage(hWndStatus, SB_SETTEXT, (WPARAM) 0, (LPARAM) "Data exported.");
return TRUE;
}