-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdump-sql.c
202 lines (173 loc) · 6.33 KB
/
dump-sql.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pcap.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <mysql/mysql.h>
#define MYSQL_MODE 1
#define DUMP_MODE 2
#define ETH_ADDR_LEN 6
#define ETHERNET_SIZE 14
int main(int argc, char * argv[]) {
// Mode Vars
MYSQL * conn = mysql_init(NULL);
FILE * df;
int mode;
// Pcap vars
pcap_t * handle;
char errbuf[PCAP_ERRBUF_SIZE];
struct pcap_pkthdr * header;
const u_char * packet;
// Ethernet Header
struct ethhdr {
u_char eth_dst_host[ETH_ADDR_LEN];
u_char eth_src_host[ETH_ADDR_LEN];
u_short eth_type;
};
const struct ethhdr * ethernet_hdr;
const struct ip * ip_hdr;
const struct tcphdr * tcp_hdr;
// Check num of args to determine mode
if (argc == 6)
mode = MYSQL_MODE;
else if (argc == 3)
mode = DUMP_MODE;
else {
fprintf(stderr, "Usage:\nMySQL Connect: dump-sql [Capture File] [MySQL Host] [MySQL User] [MySQL Pass] [MySQL DB Name]\nDump Output: dump-sql [Capture File] [Output File]\n");
return 2;
}
// Open packet capture file
if ((handle = pcap_open_offline(argv[1], errbuf)) == NULL) {
fprintf(stderr, "[ERROR] Could not open capture file: %s\n", errbuf);
return 2;
}
// If MYSQL, then connect to DB
if (mode == MYSQL_MODE) {
if (!mysql_real_connect(conn, argv[2], argv[3], argv[4], argv[5], 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
return 2;
}
printf("Connected to %s, populating tables...\n", argv[2]);
}
// else open a file write to
else if (mode == DUMP_MODE) {
if ((df = fopen(argv[2], "w")) == NULL) {
fprintf(stderr, "[ERROR] Could not open file for writing: %s\n", argv[2]);
return 2;
}
}
int packet_num = 1, iter;
while(pcap_next_ex(handle, &header, &packet) != -2) {
/*
* Each packet has the three structures sitting in line, so to find the location
* of the next, just add the size of it to the previous
*/
ethernet_hdr = (struct ethhdr *)(packet);
ip_hdr = (struct ip *)(packet + ETHERNET_SIZE);
tcp_hdr = (struct tcphdr *)(packet + ETHERNET_SIZE + (ip_hdr->ip_hl*4));
char dst_mac[12];
char src_mac[12];
char byte[2];
char eth_type[4];
// null offset
int os = 0;
char * sql_string = (char *)malloc(150 * sizeof(char *));
/*
* Since sprintf returns the number of chars written (excluding null), then
* to append to sql_string, rather than overwrite it, just keep track of
* the offset (os) and add that amount to the char pointer at each successive
* write
*/
// Ethernet
os += sprintf(sql_string, "INSERT INTO eth VALUES(%d, '", packet_num);
for (iter = 0; iter < ETH_ADDR_LEN; iter++)
os += sprintf(sql_string + os, "%.2x", ethernet_hdr->eth_dst_host[iter]);
os += sprintf(strchr(sql_string, 0), "', '");
for (iter = 0; iter < ETH_ADDR_LEN; iter++)
os += sprintf(sql_string + os, "%.2x", ethernet_hdr->eth_src_host[iter]);
os += sprintf(sql_string + os, "', '");
os += sprintf(sql_string + os, "%.4x')", ntohs(ethernet_hdr->eth_type));
// Perform a DB call or write to a file
if (mode == MYSQL_MODE) {
if (mysql_query(conn, sql_string))
fprintf(stderr, "%s\n", mysql_error(conn));
}
else if (mode == DUMP_MODE) {
fprintf(df, "%s;\n", sql_string);
}
os = 0;
free(sql_string);
sql_string = NULL;
sql_string = (char *)malloc(150 * sizeof(char *));
// IP
os += sprintf(sql_string, "INSERT INTO ip VALUES(%d, ", packet_num);
os += sprintf(sql_string + os, "%d, ", ip_hdr->ip_v);
os += sprintf(sql_string + os, "%d, ", ip_hdr->ip_hl*4);
os += sprintf(sql_string + os, "%d, ", ip_hdr->ip_tos);
os += sprintf(sql_string + os, "%d, ", ntohs(ip_hdr->ip_len));
os += sprintf(sql_string + os, "'%x', ", ntohs(ip_hdr->ip_id));
u_int ip_off = ntohs(ip_hdr->ip_off);
if (ip_off & IP_RF)
os += sprintf(sql_string + os, "1, ");
else
os += sprintf(sql_string + os, "0, ");
if (ip_off & IP_DF)
os += sprintf(sql_string + os, "1, ");
else
os += sprintf(sql_string + os, "0, ");
if (ip_off & IP_MF)
os += sprintf(sql_string + os, "1, ");
else
os += sprintf(sql_string + os, "0, ");
os += sprintf(sql_string + os, "%d, ", (ip_off & IP_OFFMASK) << 3);
os += sprintf(sql_string + os, "%d, ", ip_hdr->ip_ttl);
os += sprintf(sql_string + os, "%.2x, ", ip_hdr->ip_p);
os += sprintf(sql_string + os, "'%x', ", ntohs(ip_hdr->ip_sum));
os += sprintf(sql_string + os, "'%s', ", inet_ntoa(*(struct in_addr *) &ip_hdr->ip_src.s_addr));
os += sprintf(sql_string + os, "'%s')", inet_ntoa(*(struct in_addr *) &ip_hdr->ip_dst.s_addr));
if (mode == MYSQL_MODE) {
if (mysql_query(conn, sql_string))
fprintf(stderr, "%s\n", mysql_error(conn));
}
else if (mode == DUMP_MODE) {
fprintf(df, "%s;\n", sql_string);
}
os = 0;
free(sql_string);
sql_string = NULL;
sql_string = (char *)malloc(150 * sizeof(char *));
// TCP
os += sprintf(sql_string, "INSERT INTO tcp VALUES(%d, ", packet_num);
os += sprintf(sql_string + os, "%d, ", ntohs(tcp_hdr->source));
os += sprintf(sql_string + os, "%d, ", ntohs(tcp_hdr->dest));
os += sprintf(sql_string + os, "%d, ", ntohl(tcp_hdr->seq));
os += sprintf(sql_string + os, "%d, ", ntohl(tcp_hdr->ack_seq));
os += sprintf(sql_string + os, "%d, ", tcp_hdr->doff*4);
os += sprintf(sql_string + os, "%x, ", tcp_hdr->urg);
os += sprintf(sql_string + os, "%x, ", tcp_hdr->ack);
os += sprintf(sql_string + os, "%x, ", tcp_hdr->psh);
os += sprintf(sql_string + os, "%x, ", tcp_hdr->rst);
os += sprintf(sql_string + os, "%x, ", tcp_hdr->syn);
os += sprintf(sql_string + os, "%x, ", tcp_hdr->fin);
os += sprintf(sql_string + os, "%d, ", ntohs(tcp_hdr->window));
os += sprintf(sql_string + os, "'%.4x')", ntohs(tcp_hdr->check));
if (mode == MYSQL_MODE) {
if (mysql_query(conn, sql_string))
fprintf(stderr, "%s\n", mysql_error(conn));
}
else if (mode == DUMP_MODE) {
fprintf(df, "%s;\n", sql_string);
}
free(sql_string);
sql_string = NULL;
packet_num++;
}
// Close out
if (mode == MYSQL_MODE)
mysql_close(conn);
else if (mode == DUMP_MODE)
fclose(df);
return 0;
}