forked from DataSoft/Honeyd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
honeydstats_main.c
438 lines (373 loc) · 10.3 KB
/
honeydstats_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
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
/*
* Copyright (c) 2002, 2003, 2004 Niels Provos <[email protected]>
* All rights reserved.
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <sys/param.h>
#include <sys/types.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_IOCCOM_H
#include <sys/ioccom.h>
#endif
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/tree.h>
#include <sys/wait.h>
#include <sys/queue.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <signal.h>
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#include <fcntl.h>
#include <unistd.h>
#include <getopt.h>
#include <dnet.h>
#undef timeout_pending
#undef timeout_initialized
#include <event.h>
#include "honeyd.h"
#include "tagging.h"
#include "untagging.h"
#include "stats.h"
#include "histogram.h"
#include "honeydstats.h"
#include "analyze.h"
#include "keycount.h"
/* Prototypes */
int
make_socket(int (*f)(int, const struct sockaddr *, socklen_t), int type,
char *address, uint16_t port);
extern int checkpoint_fd;
extern struct evbuffer *checkpoint_evbuf;
extern struct usertree users;
static int fd_recv;
static struct evbuffer *evbuf_recv;
static char *checkpoint_filename = NULL;
static char *config_filename = "honeydstats.config";
static void
read_cb(int fd, short what, void *unused)
{
static u_char buf[4096];
struct addr src;
struct sockaddr_storage from;
socklen_t fromsz = sizeof(from);
int nread;
/* Reschedule the event */
struct event *ev_recv = event_new(stats_libevent_base, fd_recv, EV_READ, read_cb, NULL);
event_add(ev_recv, NULL);
nread = recvfrom(fd, buf, sizeof(buf), MSG_WAITALL,
(struct sockaddr *)&from, &fromsz);
if (nread == -1) {
warn("%s: recvfrom", __func__);
return;
}
addr_ston((struct sockaddr *)&from, &src);
syslog(LOG_INFO, "Received report from %s: %d",
addr_ntoa(&src), nread);
evbuffer_drain(evbuf_recv, evbuffer_get_length(evbuf_recv));
evbuffer_add(evbuf_recv, buf, nread);
signature_process(evbuf_recv);
}
struct _unittest {
char *name;
void (*cb)(void);
} unittests[] = {
{ "histogram", histogram_test },
{ "stats", stats_test },
{ "analyze", analyze_test },
{ NULL, NULL}
};
void
unittest(void)
{
struct _unittest *ut;
fprintf(stderr, "Running unittests ...\n");
for (ut = unittests; ut->name != NULL; ut++) {
fprintf(stderr, " ---- %s TEST ---- \n", ut->name);
(*ut->cb)();
fprintf(stderr, " ---- %s OK ---- \n", ut->name);
}
fprintf(stderr, "All unitests are OK\n");
exit(0);
}
void
usage(void)
{
fprintf(stderr,
"Usage: honeydstats [OPTIONS]\n\n"
"where options include:\n"
" --os_report <filename> Report os versions to this file.\n"
" --port_report <filename> Report port distribution to file.\n"
" --spammer_report <filename> Report spammer IPs to this file.\n"
" --country_report <filename> Report country codes to this file.\n"
" -V, --version Print program version and exit.\n"
" -h, --help Print this message and exit.\n"
" -l <address> Address to bind listen socket to.\n"
" -p <port> Port number to bind to.\n"
" -f <config> Name of configuration file.\n"
" -c <checkpoint> Name of checkpointing file.\n"
);
exit(1);
}
void
setup_socket(char *address, int port)
{
if ((evbuf_recv = evbuffer_new()) == NULL){
syslog(LOG_ERR, "%s: evbuffer_new", __func__);
exit(EXIT_FAILURE);
}
if ((fd_recv = make_socket(bind, SOCK_DGRAM, address, port)) == -1){
syslog(LOG_ERR, "%s: make_socket", __func__);
exit(EXIT_FAILURE);
}
syslog(LOG_NOTICE, "Listening on %s:%d", address, port);
struct event *ev_recv = event_new(stats_libevent_base, fd_recv, EV_READ, read_cb, NULL);
event_add(ev_recv, NULL);
}
void
honeydstats_signal(int fd, short what, void *arg)
{
syslog(LOG_NOTICE, "exiting on signal %d", fd);
exit(EXIT_SUCCESS);
}
void
honeydstats_sighup(int fd, short what, void *arg)
{
syslog(LOG_NOTICE,
"rereading configuration/rotating files on signal %d", fd);
if (config_filename != NULL)
user_read_config(config_filename);
if (checkpoint_fd != -1) {
close(checkpoint_fd);
checkpoint_fd = open(checkpoint_filename,
O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP);
}
}
int
main(int argc, char *argv[])
{
static int show_version = 0;
static int show_usage = 0;
static int report_os = 0;
static int report_port = 0;
static int report_spammer = 0;
static int report_country = 0;
static struct option stats_long_opts[] = {
{"version", 0, &show_version, 1},
{"help", 0, &show_usage, 1},
{"os_report", required_argument, &report_os, 1},
{"port_report", required_argument, &report_port, 1},
{"spammer_report", required_argument, &report_spammer, 1},
{"country_report", required_argument, &report_country, 1},
{0, 0, 0, 0}
};
char *replay_filename = NULL;
char *address = "0.0.0.0";
char **orig_argv;
int orig_argc;
int debug = 0;
int want_unittest = 0;
u_short port = 9000;
int c;
orig_argc = argc;
orig_argv = argv;
fprintf(stderr,
"HoneydStats Collector V%s Copyright (c) 2004 Niels Provos\n",
VERSION);
while ((c = getopt_long(argc, argv, "TVdc:r:l:p:f:h?",
stats_long_opts, NULL)) != -1) {
switch (c) {
case 'V':
show_version = 1;
break;
case 'T':
debug = 1;
want_unittest = 1;
break;
case 'd':
debug = 1;
break;
case 'f':
config_filename = optarg;
break;
case 'c':
checkpoint_filename = optarg;
break;
case 'r':
replay_filename = optarg;
break;
case 'l':
address = optarg;
break;
case 'p':
if ((port = atoi(optarg)) == 0) {
syslog(LOG_ERR, "Bad port number: %s\n",optarg);
fprintf(stderr, "Bad port number: %s\n",
optarg);
usage();
}
break;
case 0:
/* long option handled */
if (report_os) {
extern char *os_report_file;
os_report_file = optarg;
report_os = 0;
}
if (report_port) {
extern char *port_report_file;
port_report_file = optarg;
report_port = 0;
}
if (report_spammer) {
extern char *spammer_report_file;
spammer_report_file = optarg;
report_spammer = 0;
}
if (report_country) {
extern char *country_report_file;
country_report_file = optarg;
report_country = 0;
}
break;
default:
usage();
break;
}
}
if (show_version)
exit(0);
if (show_usage) {
usage();
/* not reached */
}
SPLAY_INIT(&users);
if (user_read_config(config_filename) == -1) {
if (!want_unittest)
{
syslog(LOG_ERR, "config file '%s' not found", config_filename);
exit(EXIT_FAILURE);
}
else
{
syslog(LOG_WARNING, "config file '%s' not found", config_filename);
}
}
syslog_init(orig_argc, orig_argv);
/* Start the stats daemon in the background if necessary */
if (!debug) {
setlogmask(LOG_UPTO(LOG_INFO));
fprintf(stderr, "Starting as background process\n");
if (daemon(1, 0) < 0)
{
syslog(LOG_ERR, "daemon");
exit(EXIT_FAILURE);
}
}
stats_libevent_base = event_base_new();
count_init();
analyze_init();
timeseries_init();
if (want_unittest)
unittest();
if (replay_filename != NULL) {
char *p;
int fd;
while ((p = strsep(&replay_filename, ",")) != NULL) {
if ((fd = open(p, O_RDONLY, 0)) == -1)
{
syslog(LOG_ERR, "%s: open(%s)", __func__,p);
exit(EXIT_FAILURE);
}
checkpoint_replay(fd);
}
}
if (checkpoint_filename != NULL) {
int fd;
/*
* First check if we can use the file name to replay
* log information.
*/
fd = open(checkpoint_filename, O_RDONLY, 0);
if (fd != -1)
checkpoint_replay(fd);
/*
* Open file descriptor into which we log information for
* replay.
*/
checkpoint_fd = open(checkpoint_filename,
O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP);
checkpoint_evbuf = evbuffer_new();
}
setup_socket(address, port);
struct event *sigterm_ev, *sigint_ev, *sighup_ev;
sigterm_ev = evsignal_new(stats_libevent_base, SIGTERM, honeydstats_signal, NULL);
sigint_ev = evsignal_new(stats_libevent_base, SIGINT, honeydstats_signal, NULL);
sighup_ev = evsignal_new(stats_libevent_base, SIGHUP, honeydstats_sighup, NULL);
event_add(sigterm_ev, NULL);
event_add(sigint_ev, NULL);
event_add(sighup_ev, NULL);
event_base_dispatch(stats_libevent_base);
syslog(LOG_ERR, "Kqueue does not recognize bpf filedescriptor.");
return (0);
}
void
syslog_init(int argc, char *argv[])
{
int options, i;
char buf[MAXPATHLEN];
#ifdef LOG_PERROR
options = LOG_PERROR|LOG_PID|LOG_CONS;
#else
options = LOG_PID|LOG_CONS;
#endif
openlog("honeydstats", options, LOG_DAEMON);
/* Create a string containing all the command line
* arguments and pass it to syslog:
*/
buf[0] = '\0';
for (i = 1; i < argc; i++) {
if (i > 1 && strlcat(buf, " ", sizeof(buf)) >= sizeof(buf))
break;
if (strlcat(buf, argv[i], sizeof(buf)) >= sizeof(buf))
break;
}
syslog(LOG_NOTICE, "started with %s", buf);
}