-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhoneyd.c
5148 lines (4319 loc) · 144 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]>
* Copyright (c) 2012, 2016 Sven Schindler <[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 <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 "personality6.h"
#include "honeyd.h"
#include "template.h"
#include "subsystem.h"
#include "personality.h"
#include "xprobe_assoc.h"
#include "ipfrag.h"
#include "ip6frag.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 <netinet/icmp6.h>
#include "icmp6.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 "update.h"
#include "util.h"
#include "randomipv6.h"
#ifdef HAVE_PYTHON
#include <Python.h>
#include "pyextend.h"
#include "pydataprocessing.h"
#include "pydatahoneyd.h"
#endif
#include "hihmanager.h"
#include "database.h"
#include "shellcode.h"
#include "submission.h"
#include <pthread.h>
#define IPV6_RANDOM_MODE_DEACTIVATED 0
#define DEFAULT_RANDOM_IPV6_PROBABILITY 0.0
#define MAX_NUMBER_OF_IPV6_RANDOM_HOSTS
#define SIZE_OF_IPV6_FRAGMENT_HEADER 8
/* Prototypes */
void honeyd_tcp_timeout(int, short, void *);
void honeyd_udp_timeout(int, short, void *);
void honeyd_delay_cb(int, short, void *);
void honeyd_delay_cb6(int, short, void *);
enum forward honeyd_route_packet(struct ip_hdr *, u_int, struct addr *, struct addr *, int *);
enum forward honeyd_route_packet6(struct ip6_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);
/* IP6 specific prototypes */
void honeyd_recv_cb6(u_char *, const struct pcap_pkthdr *, const u_char *);
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.prints",
PATH_HONEYDDATA "/xprobe2.conf",
PATH_HONEYDDATA "/nmap.assoc",
PATH_HONEYDDATA "/pf.os",
IPV6_RANDOM_MODE_DEACTIVATED,
DEFAULT_RANDOM_IPV6_PROBABILITY,
MAX_NUMBER_OF_IPV6_RANDOM_HOSTS
};
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;
struct event honeyd_rrd_ev;
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;
int honeyd_needsroot; /* Need different IDs */
int honeyd_disable_webserver = 0;
int honeyd_disable_update = 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;
/* to execute integration tests as soon as libevent has started */
struct event *integration_test_ev;
struct timeval *integration_test_tv;
/* can be used by unittests to do bad stuff */
void (*honeyd_delay_callback)(int, short, void *) = honeyd_delay_cb;
void (*honeyd_delay_callback6)(int, short, void *) = honeyd_delay_cb6;
static char *logfile = NULL; /* Log file names */
static char *servicelog = NULL;
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 },
{ "disable-update", 0, &honeyd_disable_update, 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 },
{ 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"
" -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"
" -f configfile Read configuration from file.\n"
" -c host:port:name:pass Reports starts to collector.\n"
" -H Start high interaction honeypots.\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"
" --disable-update Disables checking for security fixes.\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(1);
}
/* 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));
}
void honeyd_settcp46(struct tcp_con *con, struct ip_hdr *ip,
struct ip6_hdr *ip6, struct tcp_hdr *tcp, int local)
{
struct tuple *hdr = &con->conhdr;
memset(hdr, 0, sizeof(struct tuple));
if (ip != NULL )
{
con->addr_family=AF_INET;
hdr->ip_src = ip->ip_src;
hdr->ip_dst = ip->ip_dst;
hdr->src_addr.addr_type = ADDR_TYPE_NONE;
hdr->dst_addr.addr_type = ADDR_TYPE_NONE;
}
else if (ip6 != NULL )
{
con->addr_family=AF_INET6;
hdr->src_addr.addr_type = ADDR_TYPE_IP6;
hdr->src_addr.addr_bits = IP6_ADDR_BITS;
hdr->src_addr.addr_ip6 = ip6->ip6_src;
hdr->dst_addr.addr_type = ADDR_TYPE_IP6;
hdr->dst_addr.addr_bits = IP6_ADDR_BITS;
hdr->dst_addr.addr_ip6 = ip6->ip6_dst;
}
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;
}
void honeyd_settcp(struct tcp_con *con, struct ip_hdr *ip, struct tcp_hdr *tcp,
int local)
{
honeyd_settcp46(con, ip, NULL, tcp, local);
}
void honeyd_settcp6(struct tcp_con *con, struct ip6_hdr *ip6,
struct tcp_hdr *tcp, int local)
{
honeyd_settcp46(con, NULL, ip6, tcp, local);
}
void honeyd_setudp(struct udp_con *con, struct ip_hdr *ip, struct ip6_hdr *ip6,
struct udp_hdr *udp, int local, int addr_family)
{
struct tuple *hdr = &con->conhdr;
memset(hdr, 0, sizeof(struct tuple));
if (addr_family == AF_INET6)
{
hdr->src_addr.addr_type = ADDR_TYPE_IP6;
hdr->src_addr.addr_bits = IP6_ADDR_BITS;
hdr->src_addr.addr_ip6 = ip6->ip6_src;
hdr->dst_addr.addr_type = ADDR_TYPE_IP6;
hdr->dst_addr.addr_bits = IP6_ADDR_BITS;
hdr->dst_addr.addr_ip6 = ip6->ip6_dst;
}
else
{
hdr->ip_src = ip->ip_src;
hdr->ip_dst = ip->ip_dst;
hdr->src_addr.addr_type = ADDR_TYPE_NONE;
hdr->dst_addr.addr_type = ADDR_TYPE_NONE;
}
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 *arg)
{
static int count;
char line[1024];
struct event *ev = arg;
struct timeval tv;
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);
timerclear(&tv);
tv.tv_sec = 60;
evtimer_add(ev, &tv);
/* 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 )
errx(1, "%s: cannot start rrdtool", __func__);
if ((honeyd_traffic_db = rrdtool_db_start(honeyd_rrd_drv,
honeyd_traffic_filename, 60)) == NULL )
errx(1, "%s: cannot create rrd db: %s", __func__,
honeyd_traffic_filename);
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 */
evtimer_set(&honeyd_rrd_ev, honeyd_rrd_cb, &honeyd_rrd_ev);
honeyd_rrd_cb(-1, EV_TIMEOUT, &honeyd_rrd_ev);
}
/*
* 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 ((pwd = getpwnam("nobody")) != NULL )
{
honeyd_uid = pwd->pw_uid;
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)
err(1, "getrlimit: NOFILE");
rl.rlim_cur = rl.rlim_max;
if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
err(1, "setrlimit: NOFILE");
}
#ifdef RLIMIT_NPROC
if (getrlimit(RLIMIT_NPROC, &rl) == -1)
err(1, "getrlimit: NPROC");
rl.rlim_max = rl.rlim_max / 2;
rl.rlim_cur = rl.rlim_max;
if (setrlimit(RLIMIT_NPROC, &rl) == -1)
err(1, "setrlimit: NPROC");
#endif
stats_network.input_bytes = count_new();
stats_network.output_bytes = count_new();
}
#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
//required by shellcode detection
database_close();
shellcode_destroy();
submission_destroy();
exit(status);
}
/* Encapsulate a packet into Ethernet */
void honeyd_ether_cb46(const struct interface * inter,
struct addr *dst_mac_addr, struct addr *src_mac_addr, void *arg,
int eth_type)
{
struct ip_hdr *ip = NULL;
struct ip6_hdr *ip6 = NULL;
u_char pkt[HONEYD_MTU + 40]; /* XXX - Enough? */
u_int len = 0, iplen = 0;
switch (eth_type)
{
case ETH_TYPE_IP:
ip = arg;
iplen = ntohs(ip->ip_len);
break;
case ETH_TYPE_IPV6:
ip6 = arg;
iplen = ntohs(ip6->ip6_plen) + IP6_HDR_LEN;
break;
}
eth_pack_hdr(pkt, dst_mac_addr->addr_eth,
src_mac_addr->addr_eth,
eth_type);
len = ETH_HDR_LEN + iplen;
if (sizeof(pkt) < len)
{
syslog(LOG_WARNING, "%s: IP packet is larger than buffer: %d", __func__,
len);
goto out;
}
memcpy(pkt + ETH_HDR_LEN, arg, 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: if (eth_type == ETH_TYPE_IPV6)
{
pool_free(pool_pkt, ip6);
}
else
{
pool_free(pool_pkt, ip);
}
}
void honeyd_ether_cb(struct arp_req * req, int success, void *arg)
{
honeyd_ether_cb46(req->inter, &req->ha, &req->src_ha, arg, ETH_TYPE_IP);
}
void honeyd_ether_cb6(struct ndp_neighbor_req * req, int success, void * arg)
{
honeyd_ether_cb46(req->inter, &req->target_mac_addr, &req->source_mac_addr,
arg, ETH_TYPE_IPV6);
}
/*
* Delivers an IP packet to a specific interface.
* Generates ARP request if necessary.
*/
void honeyd_deliver_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);
/* Ethernet delivery if possible */
if ((req = arp_find(dst_pa)) == NULL )
{
arp_request(inter, src_pa, src_ha, dst_pa, honeyd_ether_cb, ip);
}
else if (req->cnt == -1)
{
/*
* 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_cb(req, 1, ip);
}
else
{
/*
* Fall through in case that this packet needs
* to be dropped.
*/
pool_free(pool_pkt, ip);
}
}
/*
* Returs 1 if the passed type id is an ext header id, else false.
*/
int is_ext_hdr_id(int ext_hdr_id)
{
int i;
/* TODO: replace magic numbers */
uint8_t ext_hdr_ids[] =
{ 0, 43, 44, 51, 50, 60, 59 };
for (i = 0; i < sizeof(ext_hdr_ids); i++)
{
if (ext_hdr_ids[i] == ext_hdr_id)
return 1;
}
return 0;
}
int get_ext_hdr_length(struct ip6_ext_hdr *ext_hdr) {
/* ext len given in 8 octets + first 8 bytes */
return ext_hdr->ext_len * 8 + 8;
}
/*
* Sets the pointer to the next (ext) header of the package, and returns its position, if the header
* could not be found then it returns -1
* TODO: check ip len
*/
int get_ip6_next_hdr(u_char **next_hdr, struct ip6_hdr *ip6,
uint8_t requested_next_hdr_id)
{
u_char *pkt_ptr = (u_char *) (ip6 + 1);
uint8_t nxt_ext_hdr_id;
int position_in_pkt = IP6_HDR_LEN;
int ext_hdr_len;
if (ip6->ip6_nxt == requested_next_hdr_id)
{
*next_hdr = pkt_ptr;
return IP6_HDR_LEN;
}
else if (!is_ext_hdr_id(ip6->ip6_nxt))
{
return -1;
}
/* walk through the package and try to find the requested header */
while (pkt_ptr < ((u_char*) ip6) + ip6->ip6_plen)
{
nxt_ext_hdr_id = ((struct ip6_ext_hdr *) pkt_ptr)->ext_nxt;
if (nxt_ext_hdr_id != requested_next_hdr_id && !is_ext_hdr_id(nxt_ext_hdr_id))
{
return -1;
}
ext_hdr_len = get_ext_hdr_length((struct ip6_ext_hdr *)pkt_ptr);
pkt_ptr += ext_hdr_len;
position_in_pkt += ext_hdr_len;
if (nxt_ext_hdr_id == requested_next_hdr_id)
{
*next_hdr = pkt_ptr;
return position_in_pkt;
}
}
return -1;
}
u_int get_number_of_fragments(u_int iplen, u_int unfrag_fraghdr_size, u_int size_of_fragmentable_part)
{
u_int number_of_fragments;
/* calculate the number of fragmens */
if ((iplen - unfrag_fraghdr_size) % (size_of_fragmentable_part) == 0)
{
number_of_fragments = (iplen - unfrag_fraghdr_size)
/ (size_of_fragmentable_part);
}
else
{
number_of_fragments = ((iplen - unfrag_fraghdr_size)
/ (size_of_fragmentable_part)) + 1;
}
return number_of_fragments;
}
void ip6_set_more_fragments_bit(u_int current_fragment_number,
u_int number_of_fragments, struct ip6_ext_hdr* frag_hdr)
{
/* set more fragments bit if needed */
if (current_fragment_number != number_of_fragments - 1)
{
frag_hdr->ext_data.fragment.offlg |= IP6_MORE_FRAG;
}
else
{
frag_hdr->ext_data.fragment.offlg &= ~(IP6_MORE_FRAG);
}
}
void ip6_copy_chunk_to_fragment(u_char *ip6_fragment_pkt, u_char *unfragmented_ip6_pkt,u_int fragment_size,
u_int size_of_unfragmentable_part, u_int bytes_sent)
{
u_char *start_of_fragmentable_part, *start_of_bytes_left;
u_int unfrag_fraghdr_size = size_of_unfragmentable_part + SIZE_OF_IPV6_FRAGMENT_HEADER;
start_of_fragmentable_part = ip6_fragment_pkt + unfrag_fraghdr_size;
start_of_bytes_left = unfragmented_ip6_pkt + size_of_unfragmentable_part + bytes_sent;
memcpy(start_of_fragmentable_part, start_of_bytes_left, fragment_size - unfrag_fraghdr_size);
}
/* TODO: path mtu discovery */
void ip6_send_fragments(const struct interface *inter, struct addr *src_mac_addr,
struct addr *target_mac_addr, struct ip6_hdr *ip6, u_int iplen)
{
struct ip6_hdr *ip6_fragment;
u_char *unfragmented_ip6_pkt, *ip6_fragment_pkt;
struct ip6_ext_hdr *frag_hdr, *last_unfragmentable_ext_hdr = NULL;
u_int number_of_fragments, fragment_size, size_of_fragmentable_part, current_fragment_number;
u_int bytes_left = iplen - IP6_HDR_LEN, bytes_sent = 0, is_not_last_fragment;
u_int size_of_unfragmentable_part = get_size_of_unfragmentable_part(ip6);
u_int unfrag_fraghdr_size = size_of_unfragmentable_part + SIZE_OF_IPV6_FRAGMENT_HEADER;
unfragmented_ip6_pkt = (u_char *) ip6;
/* calculate the size of the fragmentable part (it needs to be a multiple of 8) */
size_of_fragmentable_part = HONEYD_MTU - unfrag_fraghdr_size;
size_of_fragmentable_part -= (size_of_fragmentable_part % 8);
number_of_fragments = get_number_of_fragments(iplen, unfrag_fraghdr_size,size_of_fragmentable_part);
for (current_fragment_number = 0; current_fragment_number < number_of_fragments; current_fragment_number++)
{
ip6_fragment = (struct ip6_hdr *) pool_alloc(pool_pkt);
ip6_fragment_pkt = (u_char*) ip6_fragment;
is_not_last_fragment = bytes_left > size_of_fragmentable_part;
if (is_not_last_fragment)
{
fragment_size = size_of_fragmentable_part + unfrag_fraghdr_size;
bytes_left -= size_of_fragmentable_part;
}
else
{
fragment_size = bytes_left + unfrag_fraghdr_size;
bytes_left = 0;
}
/* copy unfragmentable part */
memset(ip6_fragment_pkt,0,HONEYD_MTU);
memcpy(ip6_fragment_pkt, unfragmented_ip6_pkt, size_of_unfragmentable_part);
ip6_fragment->ip6_plen = htons(fragment_size - IP6_HDR_LEN);
/* create the fragment header */
frag_hdr = (struct ip6_ext_hdr*) (ip6_fragment_pkt + size_of_unfragmentable_part);
get_last_unfragmentable_ext_hdr(ip6_fragment, last_unfragmentable_ext_hdr);
if (last_unfragmentable_ext_hdr == NULL )
{
frag_hdr->ext_nxt = ip6->ip6_nxt;
}
else
{
frag_hdr->ext_nxt = last_unfragmentable_ext_hdr->ext_nxt;
}
/* set the fragment offset */
frag_hdr->ext_data.fragment.offlg |= (htons(bytes_sent) & IP6_OFF_MASK);
/* copy the data chunk */
ip6_copy_chunk_to_fragment(ip6_fragment_pkt,unfragmented_ip6_pkt,fragment_size,size_of_unfragmentable_part,bytes_sent);
bytes_sent += fragment_size - unfrag_fraghdr_size;
/* set the next header to fragment header */
if (last_unfragmentable_ext_hdr == NULL )
{
ip6_fragment->ip6_nxt = IPPROTO_FRAGMENT;
}
else
{
last_unfragmentable_ext_hdr->ext_nxt = IPPROTO_FRAGMENT;
}
ip6_set_more_fragments_bit(current_fragment_number, number_of_fragments,frag_hdr);
honeyd_deliver_ethernet6(inter, src_mac_addr, target_mac_addr, ip6_fragment, fragment_size);
}
return;
}
int is_fragmented_ipv6_packet(struct ip6_hdr *ip6)
{
u_char *next_hdr = NULL;
return get_ip6_next_hdr(&next_hdr, ip6, IPPROTO_FRAGMENT) != -1;
}
/***
* Function requires valid router advertisements
*/
int is_ip6_addr_in_our_network(struct addr *ip6_addr)
{
struct router_advertisement *router_adv = NULL;
struct addr target_net;
struct addr ip6_addr_modified_bits = *ip6_addr;
router_adv = find_router_adv();
if (router_adv != NULL)
{
ip6_addr_modified_bits.addr_bits = router_adv->prefix_len;
addr_net(&ip6_addr_modified_bits, &target_net);
if (addr_cmp(&target_net, &router_adv->prefix) != 0)
{
return 0;
}
}
return 1;
}
void ip6_deliver_packet_to_local_neighbor(const struct interface *inter,struct ip6_hdr *ip6,
struct addr *source_mac_addr, struct addr *source_ip_addr, struct addr *target_ip_addr)
{
struct router *gw = NULL;
struct ndp_neighbor_req *ndp_req = ndp_neighbor_find(target_ip_addr);
struct template *gw_template = NULL;
if (ndp_req != NULL )
{
honeyd_ether_cb46(inter, &ndp_req->target_mac_addr, source_mac_addr, ip6, ETH_TYPE_IPV6);
}
else
{
/* send packet with gateway source or host source depending on whether we use a gateway */
if (router_used)
{
gw = entry_routers_ip6->data;
if (gw != NULL )
{
gw_template = template_find(addr_ntoa(&gw->addr));
if (gw_template != NULL )
{
icmp6_send_neighbor_sol(inter, &gw->addr, gw_template->ethernet_addr, target_ip_addr, honeyd_ether_cb6, ip6);
}
}
}
else
{
icmp6_send_neighbor_sol(inter, source_ip_addr, source_mac_addr, target_ip_addr, honeyd_ether_cb6, ip6);
}
}
}
void ip6_deliver_packet_to_router(const struct interface *inter, struct ip6_hdr *ip6,struct addr *source_mac_addr)
{
struct router_advertisement *router_adv = NULL;
router_adv = find_router_adv();
if (router_adv != NULL )
{
honeyd_ether_cb46(inter, &router_adv->src_eth_addr, source_mac_addr, ip6, ETH_TYPE_IPV6);
}
else
{
syslog(LOG_DEBUG, "cannot send packet because no ethernet address and router advertisement is available...");
}
}
void honeyd_deliver_ethernet6(const struct interface *inter,
struct addr *source_mac_addr, struct addr *target_mac_addr,
struct ip6_hdr *ip6, u_int iplen)
{
struct addr target_ip_addr, source_ip_addr;
int target_in_same_net = 1;
if (!is_fragmented_ipv6_packet(ip6))
{
ip6_checksum(ip6, iplen);
}
/* fragment if necessary */
if (iplen > HONEYD_MTU)
{
ip6_send_fragments(inter, source_mac_addr, target_mac_addr, ip6, iplen);
return;
}
/* check if the target ip address is located within out network */
ip6_addr_t_to_addr(&target_ip_addr,&ip6->ip6_dst);
ip6_addr_t_to_addr(&source_ip_addr,&ip6->ip6_src);
/* if we know the target mac address already just send the packet */
if (target_mac_addr != NULL )
{
honeyd_ether_cb46(inter, target_mac_addr, source_mac_addr, ip6, ETH_TYPE_IPV6);
return;
}
target_in_same_net = is_ip6_addr_in_our_network(&target_ip_addr);
if (target_in_same_net)