forked from DataSoft/Honeyd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
honeyd.c
4014 lines (3385 loc) · 100 KB
/
honeyd.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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2002, 2003, 2004, 2005 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 <pcap.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
#include <string.h>
#include <syslog.h>
#include <signal.h>
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#include <unistd.h>
#include <getopt.h>
#include <pwd.h>
#include <assert.h>
#undef timeout_pending
#undef timeout_initialized
#include <dnet.h>
#include <event.h>
#include "honeyd.h"
#include "template.h"
#include "subsystem.h"
#include "personality.h"
#include "xprobe_assoc.h"
#include "ipfrag.h"
#include "router.h"
#include "network.h"
#include "tcp.h"
#include "udp.h"
#include "hooks.h"
#include "pool.h"
#include "plugins_config.h"
#include "plugins.h"
#include "interface.h"
#include "arp.h"
#include "gre.h"
#include "log.h"
#include "osfp.h"
#include "parser.h"
#include "ui.h"
#include "ethernet.h"
#include "tagging.h"
#include "stats.h"
#include "dhcpclient.h"
#include "rrdtool.h"
#include "histogram.h"
#include "util.h"
#include "personality.h"
#ifdef HAVE_PYTHON
#include <Python.h>
#include "pyextend.h"
#include "pydataprocessing.h"
#include "pydatahoneyd.h"
#endif
/* Prototypes */
void honeyd_tcp_timeout(int, short, void *);
void honeyd_udp_timeout(int, short, void *);
void honeyd_delay_cb(int, short, void *);
enum forward honeyd_route_packet(struct ip_hdr *, u_int, struct addr *,
struct addr *, int *);
void tcp_retrans_timeout(int, short, void *);
void icmp_error_send(struct template *, struct addr *, uint8_t, uint8_t,
struct ip_hdr *, struct spoof);
struct tree tcpcons;
struct conlru tcplru;
struct tree udpcons;
struct conlru udplru;
struct spoof no_spoof; /* spoof settings for default packet processing */
struct config config = {
NULL,
PATH_HONEYDDATA "/nmap-os-db",
PATH_HONEYDDATA "/xprobe2.conf",
PATH_HONEYDDATA "/nmap.assoc",
PATH_HONEYDDATA "/pf.os",
PATH_HONEYDDATA "/nmap-mac-prefixes"//add the install path for the file
};
struct stats_network stats_network = {
0, /* input bytes */
0 /* output bytes */
};
SPLAY_PROTOTYPE(tree, tuple, node, conhdr_compare);
SPLAY_GENERATE(tree, tuple, node, conhdr_compare);
struct rrdtool_drv *honeyd_rrd_drv;
struct rrdtool_db *honeyd_traffic_db;
FILE *honeyd_servicefp;
struct timeval honeyd_uptime;
static FILE *honeyd_logfp;
static ip_t *honeyd_ip;
struct pool *pool_pkt;
struct pool *pool_delay;
rand_t *honeyd_rand;
int honeyd_sig;
int honeyd_nconnects;
int honeyd_nchildren;
int honeyd_ttl = HONEYD_DFL_TTL;
struct tcp_con honeyd_tmp;
int honeyd_show_include_dir;
int honeyd_show_data_dir;
int honeyd_show_version;
int honeyd_show_usage;
int honeyd_debug;
uid_t honeyd_uid = 32767;
gid_t honeyd_gid = 32767;
char *templateDump = NULL;
int honeyd_needsroot; /* Need different IDs */
int honeyd_disable_webserver = 0;
int honeyd_ignore_parse_errors = 0;
int honeyd_verify_config = 0;
int honeyd_webserver_fix_permissions = 0;
char *honeyd_webserver_address = "127.0.0.1";
int honeyd_webserver_port = 80;
char *honeyd_webserver_root = PATH_HONEYDDATA \
"/webserver/htdocs";
char *honeyd_rrdtool_path = PATH_RRDTOOL;
/* can be used by unittests to do bad stuff */
void (*honeyd_delay_callback)(int, short, void *) = honeyd_delay_cb;
static char *logfile = NULL; /* Log file names */
static char *servicelog = NULL;
/*
* TODO: There is a patch on the Google Code page, Issue 12, that purports to be a performance
* enhancement when using honeyd as a proxy to send data through an SSH daemon. However,
* there are some parts of the patch that are now incompatible with the DataSoft honeyd
* structure. I will put a note about this in the git commits, and possibly a ticket
* for it if we suspect we'll be using honeyd in this fashion.
*/
/*
* TODO: There is another patch, Issue 13, which solves about 4 issues total on the Google Code
* page for honeyd. It is rather large, so I'm merely writing this to remind myself what
* should be done next time I'm here. I may need to pass this off to someone else, however
* given that this honeyd is modified a bit and I may not know how to resolve everything.
*/
/*
* TODO: Issue 20 is rather large, and I don't know if we'd need to functionality that it would
* provide, but I'm not familiar enough with honeyd to be comfortable with putting it in myself.
*/
static struct option honeyd_long_opts[] = {
{"include-dir", 0, &honeyd_show_include_dir, 1},
{"data-dir", 0, &honeyd_show_data_dir, 1},
{"version", 0, &honeyd_show_version, 1},
{"help", 0, &honeyd_show_usage, 1},
{"webserver-address", required_argument, NULL, 'A'},
{"webserver-port", required_argument, NULL, 'W'},
{"webserver-root", required_argument, NULL, 'X'},
{"rrdtool-path", required_argument, NULL, 'Y'},
{"disable-webserver", 0, &honeyd_disable_webserver, 1},
{"verify-config", 0, &honeyd_verify_config, 1},
{"ignore-parse-errors", 0, &honeyd_ignore_parse_errors, 1},
{"fix-webserver-permissions", 0, &honeyd_webserver_fix_permissions, 1},
{"mac-address",0,0,1},
{0, 0, 0, 0}
};
void
usage(void)
{
fprintf(stderr,
"Usage: honeyd [OPTIONS] [net ...]\n\n"
"where options include:\n"
" -d Do not daemonize, be verbose.\n"
" -P Enable polling mode.\n"
" -l logfile Log packets and connections to logfile.\n"
" -s logfile Logs service status output to logfile.\n"
" -t ipFile Dumps currently used DHCP IP addresses to ipFile\n"
" -i interface Listen on interface.\n"
" -p file Read nmap-style fingerprints from file.\n"
" -x file Read xprobe-style fingerprints from file.\n"
" -a assocfile Read nmap-xprobe associations from file.\n"
" -0 osfingerprints Read pf-style OS fingerprints from file.\n"
" -u uid Set the uid Honeyd should run as.\n"
" -g gid Set the gid Honeyd should run as.\n"
" -m file Read nmap-mac-prefixes from file. \n"
" -f configfile Read configuration from file.\n"
" -c host:port:name:pass Reports starts to collector.\n"
" --webserver-address=address Address on which webserver listens.\n"
" --webserver-port=port Port on which webserver listens.\n"
" --webserver-root=path Root of document tree.\n"
" --fix-webserver-permissions Change ownership and permissions.\n"
" --rrdtool-path=path Path to rrdtool.\n"
" --disable-webserver Disables internal webserver\n"
" --verify-config Verify configuration file then exit.\n"
" -V, --version Print program version and exit.\n"
" -h, --help Print this message and exit.\n"
"\n"
"For plugin development:\n"
" --include-dir Prints out header files directory and exits.\n"
" --data-dir Prints out data/plug-in directory and exits.\n");
exit(EXIT_FAILURE);
}
/* XXX ches debug */
void
print_spoof(char *msg, struct spoof s) {
char buf2[100], buf3[100];
if (s.new_src.addr_type == ADDR_TYPE_NONE)
strlcpy(buf2, "**no addr**", sizeof(buf2));
else
addr_ntop(&s.new_src, buf2, sizeof(buf2));
if (s.new_dst.addr_type == ADDR_TYPE_NONE)
strlcpy(buf3, "**no addr**", sizeof(buf3));
else
addr_ntop(&s.new_dst, buf3, sizeof(buf3));
}
/*
* Populates a new tcp_con structure, which holds the state of a TCP connection
* con : connection structure to populate
* ip : IP header of the initial packet
* tcp : TCP header of the initial packet
* local : source of this connection, INITIATED_BY_EXTERNAL or INITIATED_BY_SUBSYSTEM
*/
void
honeyd_settcp(struct tcp_con *con, const struct interface *iface, const struct ip_hdr *ip, const struct tcp_hdr *tcp,
int local)
{
struct tuple *hdr = &con->conhdr;
memset(hdr, 0, sizeof(struct tuple));
hdr->ip_src = ip->ip_src;
hdr->ip_dst = ip->ip_dst;
hdr->iface = iface;
hdr->sport = ntohs(tcp->th_sport);
hdr->dport = ntohs(tcp->th_dport);
hdr->type = SOCK_STREAM;
hdr->local = local;
con->rcv_flags = tcp->th_flags;
con->cmd.pfd = -1;
con->cmd.perrfd = -1;
}
/*
* Populates a new udp_con structure, which holds the state of a UDP connection
* con : connection structure to populate
* ip : IP header of the initial packet
* tcp : TCP header of the initial packet
* local : source of this connection, INITIATED_BY_EXTERNAL or INITIATED_BY_SUBSYSTEM
*/
void
honeyd_setudp(struct udp_con *con, const struct ip_hdr *ip, const struct udp_hdr *udp,
int local)
{
struct tuple *hdr = &con->conhdr;
memset(hdr, 0, sizeof(struct tuple));
hdr->ip_src = ip->ip_src;
hdr->ip_dst = ip->ip_dst;
hdr->iface = NULL;
hdr->sport = ntohs(udp->uh_sport);
hdr->dport = ntohs(udp->uh_dport);
hdr->type = SOCK_DGRAM;
hdr->local = local;
con->softerrors = 0;
con->cmd.pfd = -1;
con->cmd.perrfd = -1;
TAILQ_INIT(&con->incoming);
}
struct tuple *
tuple_find(struct tree *root, struct tuple *key)
{
return SPLAY_FIND(tree, root, key);
}
/*
* Iterate over all connection objects.
*/
int
tuple_iterate(struct conlru *head, int (*f)(struct tuple *, void *), void *arg)
{
struct tuple *conhdr;
TAILQ_FOREACH(conhdr, head, next) {
if ((*f)(conhdr, arg) == -1)
return (-1);
}
return (0);
}
static 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("honeyd", 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);
}
/*
* Update traffic statistics for honeyd.
*/
void
honeyd_rrd_cb(int fd, short what, void *unused)
{
static int count;
char line[1024];
snprintf(line, sizeof(line), "%f:%f",
(double)count_get_minute(stats_network.input_bytes)/60.0,
(double)count_get_minute(stats_network.output_bytes)/60.0);
rrdtool_db_update(honeyd_traffic_db, NULL, line);
/* Create a graph every five minutes */
if (count++ % 5 == 0) {
char filename[1024];
struct timeval tv;
/* Hourly graph */
timerclear(&tv);
tv.tv_sec = -7200;
snprintf(filename, sizeof(filename),
"%s/graphs/traffic_hourly.gif", honeyd_webserver_root);
rrdtool_graph(honeyd_traffic_db, filename, &tv, NULL,
"DEF:inoctets=/tmp/honeyd_traffic.rrd:input:AVERAGE "
"DEF:outoctets=/tmp/honeyd_traffic.rrd:output:AVERAGE "
"AREA:inoctets#00FF00:\"In traffic\" "
"LINE1:outoctets#0000FF:\"Out traffic\"");
/* Daily graph */
timerclear(&tv);
tv.tv_sec = -86400;
snprintf(filename, sizeof(filename),
"%s/graphs/traffic_daily.gif", honeyd_webserver_root);
rrdtool_graph(honeyd_traffic_db, filename, &tv, NULL,
"DEF:inoctets=/tmp/honeyd_traffic.rrd:input:AVERAGE "
"DEF:outoctets=/tmp/honeyd_traffic.rrd:output:AVERAGE "
"AREA:inoctets#00FF00:\"In traffic\" "
"LINE1:outoctets#0000FF:\"Out traffic\"");
}
}
void
honeyd_rrd_start(const char *rrdtool_path)
{
/* Initialize our traffic stats for rrdtool */
char *honeyd_traffic_filename = "/tmp/honeyd_traffic.rrd";
if ((honeyd_rrd_drv = rrdtool_init(rrdtool_path)) == NULL)
{
syslog(LOG_ERR, "%s: cannot start rrdtool", __func__);
exit(EXIT_FAILURE);
}
if ((honeyd_traffic_db = rrdtool_db_start(honeyd_rrd_drv,
honeyd_traffic_filename, 60)) == NULL)
{
syslog(LOG_ERR, "%s: cannot create rrd db(database): %s", __func__, honeyd_traffic_filename);
exit(EXIT_FAILURE);
}
rrdtool_db_datasource(honeyd_traffic_db,
"input", "GAUGE", 600);
rrdtool_db_datasource(honeyd_traffic_db,
"output", "GAUGE", 600);
rrdtool_db_commit(honeyd_traffic_db);
/* Start the periodic traffic update timer */
struct timeval tv;
timerclear(&tv);
tv.tv_sec = 60;
struct event *ev = event_new(libevent_base, -1, EV_PERSIST, honeyd_rrd_cb, NULL);
evtimer_add(ev, &tv);
}
/*
* Initializes data structures pertaining to the daemon
*/
void
honeyd_init(void)
{
struct rlimit rl;
struct passwd *pwd;
/* Record our start time */
gettimeofday(&honeyd_uptime, NULL);
/* Find the correct ids for nobody, if the uid was not set in the
* command line */
if ( honeyd_uid == 32767 && (pwd = getpwnam("nobody")) != NULL) {
honeyd_uid = pwd->pw_uid;
}
if ( honeyd_gid == 32767 && (pwd = getpwnam("nobody")) != NULL) {
honeyd_gid = pwd->pw_gid;
}
/* Initalize ongoing connection state */
SPLAY_INIT(&tcpcons);
TAILQ_INIT(&tcplru);
SPLAY_INIT(&udpcons);
TAILQ_INIT(&udplru);
memset(&honeyd_tmp, 0, sizeof(honeyd_tmp));
/* Raising file descriptor limits */
rl.rlim_max = RLIM_INFINITY;
rl.rlim_cur = RLIM_INFINITY;
if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
/* Linux does not seem to like this */
if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
{
syslog(LOG_ERR, "getrlimit: NOFILE, failed at getting the files resources limit due to file not existing");
exit(EXIT_FAILURE);
}
rl.rlim_cur = rl.rlim_max;
if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
{
syslog(LOG_ERR, "setrlimit: NOFILE, failed to set resource limit due to file not existing");
exit(EXIT_FAILURE);
}
}
#ifdef RLIMIT_NPROC
if (getrlimit(RLIMIT_NPROC, &rl) == -1)
{
syslog(LOG_ERR, "getrlimit: NPROC, failed at getting the process' resource limit due to process not running");
exit(EXIT_FAILURE);
}
rl.rlim_max = rl.rlim_max/2;
rl.rlim_cur = rl.rlim_max;
if (setrlimit(RLIMIT_NPROC, &rl) == -1)
{
syslog(LOG_ERR, "setrlimit: NPROC, failed at setting the process' resource limit due to process not running");
exit(EXIT_FAILURE);
}
#endif
stats_network.input_bytes = count_new();
stats_network.output_bytes = count_new();
//set environment variable for scripts to use
char *sudo_user = getenv("SUDO_USER");
int uid = 0;
char *home_path = NULL;
struct passwd *pass = NULL;
//Try getting the "SUDO_USER".
//If it doesn't exist (not running with sudo), then just default to using the current user
if(sudo_user != NULL)
{
pass = getpwnam(sudo_user);
}
if(pass == NULL)
{
pass = getpwuid(getuid());
if(pass == NULL)
{
syslog(LOG_ERR, "%s: Cannot find a valid user to run as, is your system okay?!", __func__);
exit(EXIT_FAILURE);
}
}
uid = pass->pw_uid;
home_path = pass->pw_dir;
char config_suffix[] = "/.config";
char honeyd_suffix[] = "/honeyd/";
char *full_path = malloc(strlen(home_path) + strlen(config_suffix) + strlen(honeyd_suffix) + 1);
strcpy(full_path, home_path);
strcat(full_path, config_suffix);
//Try to make ~/.config/
if(mkdir(full_path, S_IRWXU|S_IRWXO) != 0)
{
if(errno != EEXIST)
{
perror("Error: Could not create ~/.config");
}
}
strcat(full_path, honeyd_suffix);
//Try to make ~/.config/honeyd
if(mkdir(full_path, S_IRWXU|S_IRWXO) != 0)
{
if(errno != EEXIST)
{
perror("Error: Could not create ~/.config/honeyd");
}
}
if(chown(full_path, uid, 0) != 0)
{
perror("Error: Could not change owner of ~/.config/honeyd");
}
if(chmod(full_path, S_IRWXU|S_IRWXO))
{
perror("Error: Could not change permissions of ~/.config/honeyd");
}
if(setenv("HONEYD_HOME", full_path, 1))
{
perror("Error: Could not set enviromnent variable HONEYD_HOME");
}
}
#ifdef HAVE_PYTHON
static int
honeyd_is_webserver_enabled(void)
{
if (honeyd_webserver_port <= 0)
return 0;
if (honeyd_disable_webserver)
return 0;
return (1);
}
#endif
void
honeyd_exit(int status)
{
honeyd_logend(honeyd_logfp);
honeyd_logend(honeyd_servicefp);
template_free_all(TEMPLATE_FREE_DEALLOCATE);
interface_close_all();
rand_close(honeyd_rand);
ip_close(honeyd_ip);
closelog();
unlink(PIDFILE);
#ifdef HAVE_PYTHON
if (honeyd_is_webserver_enabled())
pyextend_webserver_exit();
pyextend_exit();
#endif
exit(status);
}
/* Encapsulate a packet into Ethernet */
void
honeyd_ether_send_cb(struct arp_req *req, int success, void *arg)
{
if((req == NULL) || (arg == NULL))
{
syslog(LOG_WARNING, "%s: invalid packet to encapsulate", __func__);
return;
}
u_char pkt[HONEYD_MTU + 40]; /* XXX - Enough? */
struct interface *inter = req->inter;
struct ip_hdr *ip = arg;
u_int len, iplen = ntohs(ip->ip_len);
eth_pack_hdr(pkt,
req->ha.addr_eth, /* destination */
req->src_ha.addr_eth, /* source */
ETH_TYPE_IP);
len = ETH_HDR_LEN + iplen;
if (sizeof(pkt) < len) {
syslog(LOG_WARNING, "%s: IP packet is larger than buffer: %d",
__func__, len);
goto out;
}
if(inter != NULL)
{
memcpy(pkt + ETH_HDR_LEN, ip, iplen);
if (eth_send(inter->if_eth, pkt, len) != len)
{
syslog(LOG_ERR, "%s: couldn't send packet size %d: %m",
__func__, len);
}
else
{
count_increment(stats_network.output_bytes, iplen);
}
}
out:
pool_free(pool_pkt, ip);
}
/*
* Delivers an IP packet to a specific interface.
* Generates ARP request if necessary.
*/
void
honeyd_send_ethernet(struct interface *inter,
struct addr *src_pa, struct addr *src_ha,
struct addr *dst_pa, struct ip_hdr *ip, u_int iplen)
{
struct arp_req *req;
ip_checksum(ip, iplen);
// If we haven't done an ARP request yet
if ((req = arp_find(dst_pa)) == NULL) {
arp_request(inter, src_pa, src_ha, dst_pa, honeyd_ether_send_cb,ip);
// If the ARP request finished with success
} else if (req->cnt == ARP_REQUEST_SUCESS) {
/*
* The source MAC of the original requestor does not help
* us here, but we can overwrite it with the MAC of this
* honeypot without causing any harm.
*/
req->src_ha = *src_ha;
honeyd_ether_send_cb(req, 1, ip);
// We couldn't figure out how to deliver this
} else {
/*
* Fall through in case that this packet needs
* to be dropped.
*/
pool_free(pool_pkt, ip);
}
}
/*
* Makes sure that we end up owning the memory referenced by
* the delay descriptor. We either tell to not free the
* memory or just make our own copy.
*/
struct ip_hdr *
honeyd_delay_own_memory(struct delay *delay, struct ip_hdr *ip, u_int iplen)
{
/* If we are not supposed to free the buffer then we do not own it */
if (!(delay->flags & DELAY_FREEPKT)) {
void *tmp = pool_alloc(pool_pkt);
memcpy(tmp, ip, iplen);
ip = tmp;
} else {
/*
* We are handling the memory ourselves: if we
* delegate the memory to the ARP handler, it will get
* freed later.
*/
delay->flags &= ~DELAY_FREEPKT;
}
return (ip);
}
/*
* This function delivers the actual packet to the network.
* It supports internal delivery, external delivery via ip_send
* and external delivery via ethernet encapsulation.
*
* This function handles the following cases:
* - TTL is 0: send ICMP time exceeded in transit message
* - External: Packet is delivered to the real network
* - Tunnel: Packet is GRE encapsulated and sent to a remote location
* - Ethernet: A physical machine has been integrate into the virtual
* routing topology and we need to ethernet encapsulate the packet.
* - Arp: The destination machine is configured to be on the physical link,
* so arp for it and ethernet encapsulate the packet.
* - Everything else: The packet is delivered internally after potential
* fragment reassembly.
*
* It needs to unreference the passed template value.
*/
static __inline void
honeyd_send_normally(struct ip_hdr *ip, u_int iplen)
{
ip_checksum(ip, iplen);
if (ip_send(honeyd_ip, ip, iplen) != iplen) {
int level = LOG_ERR;
if (errno == EHOSTDOWN || errno == EHOSTUNREACH)
level = LOG_DEBUG;
syslog(level, "couldn't send packet: %m");
} else {
count_increment(stats_network.output_bytes, iplen);
}
}
void
honeyd_delay_cb(int fd, short which, void *arg)
{
struct delay *delay = arg;
struct ip_hdr *ip = delay->ip;
struct template *tmpl = delay->tmpl;
u_int iplen = delay->iplen;
if (!ip->ip_ttl) {
/* Fix up TTL */
ip->ip_ttl++;
ip_checksum(ip, ip->ip_hl << 2);
icmp_error_send(tmpl, &delay->src,
ICMP_TIMEXCEED, ICMP_TIMEXCEED_INTRANS, ip, delay->spoof);
} else if (delay->flags & DELAY_UNREACH) {
/* Fix up TTL */
ip->ip_ttl++;
ip_checksum(ip, ip->ip_hl << 2);
icmp_error_send(tmpl, &delay->src,
ICMP_UNREACH, ICMP_UNREACH_NET, ip, delay->spoof);
} else if (delay->flags & DELAY_EXTERNAL) {
struct addr dst;
addr_pack(&dst, ADDR_TYPE_IP, IP_ADDR_BITS,
&ip->ip_dst, IP_ADDR_LEN);
struct interface* inter = interface_find_responsible(&dst);
// Check if the destination is the broadcast address and skip ARP if so
if (inter != NULL && ip->ip_dst == inter->subnetBcastAddress)
{
honeyd_send_normally(ip, iplen);
/* This is the source template */
} else if (tmpl != NULL && tmpl->ethernet_addr != NULL && tmpl->inter != NULL &&
inter == tmpl->inter)
{
struct addr src;
/* To do ARP, we need to know all this information */
addr_pack(&src, ADDR_TYPE_IP, IP_ADDR_BITS,
&ip->ip_src, IP_ADDR_LEN);
ip = honeyd_delay_own_memory(delay, ip, iplen);
/* This function computes the IP checksum for us */
honeyd_send_ethernet(tmpl->inter,
&src, tmpl->ethernet_addr,
&dst, ip, iplen);
} else {
honeyd_send_normally(ip, iplen);
}
} else if (delay->flags & DELAY_TUNNEL) {
ip_checksum(ip, iplen);
if (gre_encapsulate(honeyd_ip, &delay->src, &delay->dst,
ip, iplen) == -1)
syslog(LOG_ERR, "couldn't GRE encapsulate packet: %m");
else
count_increment(stats_network.output_bytes, iplen);
} else if (delay->flags & DELAY_ETHERNET) {
extern struct network *reverse;
struct interface *inter = tmpl->inter;
struct router *router;
struct addr addr;
/*
* If a physical honeypot has been integrated into the
* virtual routing topology, we need to find the
* corresponding router.
*/
addr_pack(&addr, ADDR_TYPE_IP, IP_ADDR_BITS,
&ip->ip_dst, IP_ADDR_LEN);
router = network_lookup(reverse, &addr);
if (router == NULL){
syslog(LOG_ERR, "%s: bad configuration", __func__);
exit(EXIT_FAILURE);
}
/*
* If we are routing for an external sender, then we
* might have to copy the packet into an allocated
* buffer.
*/
ip = honeyd_delay_own_memory(delay, ip, iplen);
/* This function computes the IP checksum for us */
honeyd_send_ethernet(inter,
&router->addr, &inter->if_ent.intf_link_addr,
&addr, ip, iplen);
} else {
struct addr addr;
uint16_t ipoff;
template_free(tmpl);
addr_pack(&addr, ADDR_TYPE_IP, IP_ADDR_BITS,
&ip->ip_dst, IP_ADDR_LEN);
/* Internal delivery */
tmpl = template_find_best(addr_ntoa(&addr), ip, iplen);
tmpl = template_ref(tmpl);
/* Check for fragmentation */
ipoff = ntohs(ip->ip_off);
if ((ipoff & IP_OFFMASK) || (ipoff & IP_MF)) {
struct ip_hdr *nip;
u_short niplen;
if (ip_fragment(tmpl, ip, iplen, &nip, &niplen) == 0)
honeyd_dispatch(tmpl, delay->iface, nip, niplen);
} else
honeyd_dispatch(tmpl, delay->iface, ip, iplen);
}
if (delay->flags & DELAY_FREEPKT)
pool_free(pool_pkt, ip);
template_free(tmpl);
if (delay->flags & DELAY_NEEDFREE)
pool_free(pool_delay, delay);
}
/*
* Delays a packet for a specified amount of ms to simulate routing delay.
* Host is used for the router that might generate a XCEED message.
*/
void
honeyd_delay_packet(struct template *tmpl, const struct interface* iface, struct ip_hdr *ip, u_int iplen,
const struct addr *src, const struct addr *dst, int ms, int flags,
struct spoof spoof)
{
struct delay *delay, tmp_delay;
struct timeval tv;
if (ms) {
delay = pool_alloc(pool_delay);
flags |= DELAY_NEEDFREE;
/*
* If the IP packet is not allocated separately, we
* need to allocate it here.
*/
if ((flags & DELAY_FREEPKT) == 0) {
void *tmp;
if (iplen < HONEYD_MTU)
tmp = pool_alloc(pool_pkt);
else
tmp = pool_alloc_size(pool_pkt, iplen);
memcpy(tmp, ip, iplen);
ip = tmp;
flags |= DELAY_FREEPKT;
}
} else {
memset(&tmp_delay, 0, sizeof(tmp_delay));
delay = &tmp_delay;
}
delay->ip = ip;
delay->iplen = iplen;
delay->iface = iface;
if (src != NULL)
delay->src = *src;
if (dst != NULL)
delay->dst = *dst;
delay->tmpl = template_ref(tmpl);
delay->flags = flags;
delay->spoof = spoof;
if (ms) {
delay->timeout = evtimer_new(libevent_base, honeyd_delay_callback, delay);
timerclear(&tv);
tv.tv_sec = ms / 1000;
tv.tv_usec = (ms % 1000) * 1000;
evtimer_add(delay->timeout, &tv);
} else
honeyd_delay_callback(-1, EV_TIMEOUT, delay);
}
/*
* This function allows us to deliver packets to virtual hosts as well
* as to external hosts. If virtual routing topologies are enabled,
* characteristics like packet loss, latency and ttl decrements are
* taken into consideration.
*/
void
honeyd_ip_send(u_char *pkt, u_int iplen, struct spoof spoof)
{
struct template *tmpl = NULL;
struct ip_hdr *ip = (struct ip_hdr *)pkt;
enum forward res = FW_EXTERNAL;
int delay = 0, flags = 0;
struct addr addr, src;
print_spoof("honeyd_ip_send", spoof);
if (iplen > HONEYD_MTU) {
u_short off = ntohs(ip->ip_off);
if ((off & IP_DF) == 0)
ip_send_fragments(HONEYD_MTU, ip, iplen, spoof);
goto drop;
}
if (spoof.new_dst.addr_type != ADDR_TYPE_NONE)
ip->ip_dst = spoof.new_dst.addr_ip;
addr_pack(&addr, ADDR_TYPE_IP, IP_ADDR_BITS, &ip->ip_dst, IP_ADDR_LEN);
addr_pack(&src, ADDR_TYPE_IP, IP_ADDR_BITS, &ip->ip_src, IP_ADDR_LEN);
/* Find the template for the external address */
tmpl = template_find_best(addr_ntoa(&addr), ip, iplen);
if ((tmpl != NULL) && (tmpl->flags & TEMPLATE_EXTERNAL))
flags |= DELAY_ETHERNET;
/* But all sending decisions are really based on the source template */
tmpl = template_find_best(addr_ntoa(&src), ip, iplen);
if (router_used) {
extern struct network *reverse;
struct router *router;
router = network_lookup(reverse, &src);
if (router == NULL) {
syslog(LOG_NOTICE, "No reverse routing map for %s",
addr_ntoa(&src));
goto drop;
}