-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
148 lines (122 loc) · 3.3 KB
/
main.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
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <assert.h>
#include <signal.h>
#include <arpa/inet.h>
#include "util.h"
#include "shb.h"
#include "idb.h"
#include "pb.h"
#include "file.h"
#include "filter.h"
FILE* jq_file = NULL;
filter_t filter;
void fup_handler(int signal) {
switch (signal) {
case SIGSEGV:
fprintf(stderr, "Caught fault (SIGSEGV)\n");
break;
case SIGBUS:
fprintf(stderr, "Caught fault (SIGBUS)\n");
break;
case SIGILL:
fprintf(stderr, "Caught fault (SIGILL)\n");
break;
default:
raise(signal);
}
// for SIGSEGV and SIGBUS print a dump
dump();
raise(signal);
}
int32_t identify_block(file_t *f, file_t* outfile){
switch(((int32_t*)f->data)[0]){
case 0xa0d0d0a:
return parse_shb(f, outfile);
case 0x00000001:
return parse_idb(f, outfile);
case 0x00000006:
return parse_pb(f, outfile);
return 0;
default:
printf("=> Unknown header 0x%08x\n", ((int32_t*)f->data)[0]);
break;
}
return -1;
}
int little_endian = 1;
int header_num = 1;
// I can't be bothered not making it global
int unknown_fd = 0;
//#include <time.h>
int main(int argc, char *argv[]) {
signal(SIGSEGV, fup_handler);
signal(SIGBUS, fup_handler);
signal(SIGILL, fup_handler);
load_filters();
//srand(time(NULL));
char* orig_data_pos;
uint64_t input_file_size = 0;
if (argc != 3){
fprintf(stderr, "Expecing two arguments - input and output file\n");
fprintf(stderr, "Possible environemnt variables:\n");
fprintf(stderr, "SSHARK_SRC_IP\n");
fprintf(stderr, "SSHARK_DST_IP\n");
fprintf(stderr, "SSHARK_IP_ADD\n");
fprintf(stderr, "SSHARK_SRC_PORT\n");
fprintf(stderr, "SSHARK_DST_PORT\n");
fprintf(stderr, "SSHARK_PORT\n");
fprintf(stderr, "SSHARK_JSON_FILE\n");
return 255;
}
file_t f __attribute__ ((__cleanup__(close_file))) = open_infile(argv[1]);
if(f.data == NULL){
return 1;
}
orig_data_pos = f.data;
input_file_size = f.sb.st_size;
file_t outfile __attribute__ ((__cleanup__(close_file))) = open_outfile(argv[2], f.sb.st_size);
if(outfile.data == NULL){
return 1;
}
unknown_fd = open("unknown.pcapng", O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (unknown_fd == -1) {
perror( "Error opening unknown.pcapng");
goto exit_failure;
}
//hexdump(f.data, 200);
jinit();
//printf("file size: %lu\n", input_file_size);
int32_t len = identify_block(&f, &outfile);
assert(len > 0);
f.data += len;
input_file_size -= len;
len = identify_block(&f, &outfile);
assert(len > 0);
f.data += len;
input_file_size -= len;
jprintf("[{}");
len = identify_block(&f, &outfile);
assert(len > 0);
f.data += len;
input_file_size -= len;
while (input_file_size > 0){
jnew_row();
len = identify_block(&f, &outfile);
assert(len > 0);
f.data += len;
input_file_size -= len;
//printf("\n");
}
jprintf("]\n");
if(input_file_size <= 0){
//printf("Finished processintg %s\n", argv[1]);
}
f.data = orig_data_pos;
return EXIT_SUCCESS;
exit_failure:
return 2;
}