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
|
/*
* Copyright (c) 2017-2024 OARC, Inc.
* Copyright (c) 2011-2017, IIS - The Internet Foundation in Sweden
* All rights reserved.
*
* This file is part of PacketQ.
*
* PacketQ is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PacketQ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PacketQ. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "packet_handler.h"
#include "packetq.h"
#include "pcap.h"
#include "reader.h"
#include "server.h"
#include "sql.h"
#include <algorithm>
#include <list>
#include <stack>
#include <stdexcept>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#ifndef WIN32
#include <getopt.h>
#include <signal.h>
#endif
#define NUM_QUERIES 32
namespace packetq {
static void usage(char* argv0, bool longversion)
{
if (!longversion) {
fprintf(stdout,
"usage: %s [-vhjctxd] [-s stmt] [-l pkts] [-p port] [-w dir] [-r dir] [-m num] <pcapfile ...>\n",
argv0);
return;
}
fprintf(stdout,
"usage: %s [options] pcapfile(s)...\n"
/* -o description .*/
" --select statements |\n"
" -s statement Set the SQL statement, can be given multiple times.\n"
" --limit packets |\n"
" -l packets Set maximum number of packets to process, from all\n"
" files and not per file.\n"
" --version | -v Display version and exit.\n"
" --help | -h Display this help.\n"
"\n"
"Output:\n"
" --json | -j JSON (default)\n"
" --csv | -c CSV\n"
" --table | -t Text table\n"
" --xml | -x XML\n"
" --rfc1035 Output DNS names escaped using RFC1035 format:\n"
" All characters outsize [a-zA-Z0-9_-] are escaped\n"
" like \\012. (Octet value in decimal.)\n"
"\n"
"Web Server:\n"
" --daemon | -d Run web server in daemon mode.\n"
" --port number |\n"
" -p number Set the port number to listen on.\n"
" --webroot dir |\n"
" -w dir Set the root directory for the web content.\n"
" --pcaproot dir |\n"
" -r dir Set the root for the PCAP files to make available.\n"
" --maxconn number |\n"
" -m number Set the maximum number of concurrent connections.\n"
"\n"
"example> packetq --csv -s \"select count(*) as mycount, protocol from dns group by protocol;\" myfile.pcap\n"
"\n"
"Packet fields (available in all tables):\n"
" id, s, us, ether_type, src_addr, src_port, dst_addr, dst_port, protocol,\n"
" ip_ttl, ip_version, fragments\n"
"\"dns\" table fields:\n"
" qname, aname, msg_id, msg_size, opcode, rcode, extended_rcode,\n"
" edns_version, z, udp_size, qd_count, an_count, ns_count, ar_count,\n"
" qtype, qclass, atype, aclass, attl, aa, tc, rd, cd, ra, ad, do, edns0, qr,\n"
" edns0_ecs, edns0_ecs_family, edns0_ecs_source, edns0_ecs_scope,\n"
" edns0_ecs_address\n"
"\"icmp\" table fields:\n"
" type, code, echo_identifier, echo_sequence, du_protocol, du_src_addr,\n"
" du_dst_addr, desc\n",
argv0);
}
#ifdef WIN32
// windows support is merely for development purposes atm
#define PACKAGE_STRING "packetq"
struct option {
char* s;
int args;
int b;
char c;
};
char* optarg = 0;
int optind = 1;
int getopt_long(int argc, char* argv[], const char* str, option* opt, int* option_index)
{
while (optind < argc) {
if (argv[optind][0] != '-')
return -1;
if (argv[optind][1] != '-') {
int i = 0;
while (opt[i].s != NULL) {
if (opt[i].c == argv[optind][1]) {
optarg = argv[optind + opt[i].args];
optind += 1 + opt[i].args;
return opt[i].c;
}
i++;
}
}
optind++;
}
return -1;
}
#endif
void sigproc(int sig)
{
// ignore sig pipe
signal(SIGPIPE, sigproc);
}
PacketQ* g_app = new PacketQ();
} // namespace packetq
using namespace packetq;
// The main funtion
int main(int argc, char* argv[])
{
signal(SIGPIPE, sigproc);
int port = 0;
int limit = 0;
int max_conn = 7;
bool daemon = false;
std::string webroot = "", pcaproot = "";
std::string queries[NUM_QUERIES] = {
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
};
int qcount = 0;
while (1) {
int option_index;
struct option long_options[] = {
{ "select", 1, 0, 's' },
{ "limit", 1, 0, 'l' },
{ "maxconn", 1, 0, 'm' },
{ "webroot", 1, 0, 'w' },
{ "pcaproot", 1, 0, 'r' },
{ "port", 1, 0, 'p' },
{ "daemon", 0, 0, 'd' },
{ "csv", 0, 0, 'c' },
{ "json", 0, 0, 'j' },
{ "table", 0, 0, 't' },
{ "xml", 0, 0, 'x' },
{ "rfc1035", 0, 0, 10000 },
{ "help", 0, 0, 'h' },
{ "version", 0, 0, 'v' },
{ NULL, 0, 0, 0 }
};
int c = getopt_long(argc, argv, "w:r:s:l:p:hHdvcxtjm:", long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 'v':
fprintf(stdout, "%s\n", PACKAGE_STRING);
exit(0);
case 's':
if (qcount < NUM_QUERIES) {
queries[qcount++] = optarg;
} else {
fprintf(stderr, "Warning: can't handle more than %d separate query strings; discarding '%s'\n", NUM_QUERIES, optarg);
}
break;
case 'c':
g_app->set_output(PacketQ::csv);
break;
case 't':
g_app->set_output(PacketQ::csv_format);
break;
case 'x':
g_app->set_output(PacketQ::xml);
break;
case 'j':
g_app->set_output(PacketQ::json);
break;
case 'd':
daemon = true;
break;
case 'w':
webroot = optarg;
break;
case 'r':
pcaproot = optarg;
break;
case 'm':
max_conn = atoi(optarg) + 1;
if (max_conn < 2)
max_conn = 2;
break;
case 'l':
limit = atoi(optarg);
break;
case 'p':
port = atoi(optarg);
break;
case 10000: // rfc1035
g_app->set_escape(true);
break;
default:
fprintf(stderr, "Unknown option: %c\n", c);
usage(argv[0], false);
return 1;
case 'h':
usage(argv[0], true);
return 1;
}
}
init_packet_handlers(g_app->get_escape()); // set up tables
g_app->set_limit(limit);
if (port > 0) {
start_server(port, daemon, pcaproot, webroot, max_conn);
}
if (optind >= argc) {
fprintf(stderr, "Missing input uri\n");
usage(argv[0], false);
return 1;
}
std::vector<std::string> in_files;
while (optind < argc) {
in_files.push_back(argv[optind]);
optind++;
}
Reader reader(in_files, g_app->get_limit());
if (g_app->get_output() == PacketQ::json) {
printf("[\n");
}
for (int i = 0; i < qcount; i++) {
char tablename[32];
snprintf(tablename, 32, "result-%d", i);
try {
Query query(tablename, queries[i].c_str());
query.parse();
query.execute(reader);
Table* result = query.m_result;
switch (g_app->get_output()) {
case (PacketQ::csv_format):
if (result)
result->csv(true);
break;
case (PacketQ::csv):
if (result)
result->csv();
break;
case (PacketQ::xml):
if (result)
result->xml();
break;
case (PacketQ::json):
if (result)
result->json(i < (qcount - 1));
break;
}
} catch (Error& e) {
printf("Error: %s\n", e.m_err.c_str());
fflush(stdout);
exit(1);
} catch (...) {
printf("Error: an unknown error has occured !\n");
fflush(stdout);
}
}
if (g_app->get_output() == PacketQ::json) {
printf("]\n");
}
delete g_app;
destroy_packet_handlers();
return 0;
}
|