-
Notifications
You must be signed in to change notification settings - Fork 101
/
personality.c
2770 lines (2490 loc) · 62.3 KB
/
personality.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 Niels Provos <provos@citi.umich.edu>
* 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>
#include "config.h"
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <sys/stat.h>
#include <sys/tree.h>
#include <sys/queue.h>
#include <sys/wait.h>
#include <math.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <dnet.h>
#include <ctype.h>
#undef timeout_pending
#undef timeout_initialized
#include <event.h>
#include "honeyd.h"
#include "personality.h"
#include "xprobe_assoc.h"
#include "template.h"
#include "debug.h"
/* ET - Moved SPLAY_HEAD to personality.h so xprobe_assoc.c could use it. */
int npersons;
/* ET - global from honeyd.c */
struct personate person_drop = {};
static struct timeval tv_periodic;
SPLAY_GENERATE(perstree, personality, node, perscompare);
/* ET - For the Xprobe fingerprint tree */
SPLAY_GENERATE(xp_fprint_tree, xp_fingerprint, node, xp_fprint_compare);
static void
personality_time_evcb(int fd, short what, void *event)
{
gettimeofday(&tv_periodic, NULL);
}
void
xprobe_personality_init(void)
{
SPLAY_INIT(&xp_fprints);
}
void
personality_init(void)
{
npersons = 0;
SPLAY_INIT(&personalities);
/* Start a timer that keeps track of the current system time */
struct event *ev = event_new(libevent_base, -1, EV_PERSIST, personality_time_evcb, NULL);
struct timeval tv;
timerclear(&tv);
tv.tv_usec = 100000; /* every 100 ms */
evtimer_add(ev, &tv);
}
struct personality *
personality_new(const char *name)
{
struct personality *pers, tmp;
tmp.name = (char *)name;
if (SPLAY_FIND(perstree, &personalities, &tmp))
return (NULL);
if ((pers = calloc(1, sizeof(struct personality))) == NULL)
{
syslog(LOG_ERR, "%s: calloc", __func__);
exit(EXIT_FAILURE);
}
if ((pers->name = strdup(name)) == NULL)
{
syslog(LOG_ERR, "%s: stdrup", __func__);
exit(EXIT_FAILURE);
}
/* Initialize defaults */
pers->tstamphz = -1;
npersons++;
SPLAY_INSERT(perstree, &personalities, pers);
/* Find and add the Xprobe fingerprint, if it exists */
correlate_nmap_with_xprobe(pers);
return (pers);
}
struct personality *
personality_clone(const struct personality *person)
{
struct personality *newperson;
if ((newperson = malloc(sizeof(struct personality))) == NULL)
{
syslog(LOG_ERR, "%s: malloc", __func__);
exit(EXIT_FAILURE);
}
memcpy(newperson, person, sizeof(struct personality));
return (newperson);
}
/*
* Frees the reference that a template has to a personality.
*/
void
personality_declone(struct personality *pers)
{
free(pers);
}
void
personality_free(struct personality *pers)
{
SPLAY_REMOVE(perstree, &personalities, pers);
free(pers->name);
free(pers);
}
struct personality *
personality_random(void)
{
extern rand_t *honeyd_rand;
struct personality *pers;
int i;
if (!npersons)
return (NULL);
i = rand_uint32(honeyd_rand) % npersons;
pers = SPLAY_MIN(perstree, &personalities);
while (i--) {
pers = SPLAY_NEXT(perstree, &personalities, pers);
}
return (pers);
}
struct personality *
personality_find(const char *name)
{
struct personality tmp;
tmp.name = (char *)name;
return (SPLAY_FIND(perstree, &personalities, &tmp));
}
/* Not much here, set up ip id accordingly */
void
ip_personality(struct template *tmpl, uint16_t *pid, enum ipid_protocol proto)
{
extern rand_t *honeyd_rand;
struct personality *person;
if (tmpl == NULL)
return;
if ((person = tmpl->person) == NULL)
return;
uint16_t *ipid_cached, *ipid_cached_TCP, *ipid_cached_ICMP;
//If it's a shared sequence...
if( person->ipid_shared_sequence == 1 )
{
ipid_cached_TCP = &tmpl->ipid;
ipid_cached_ICMP = &tmpl->ipid;
}
else
{
ipid_cached_TCP = &tmpl->IPID_last_TCP;
ipid_cached_ICMP = &tmpl->IPID_last_ICMP;
}
enum ipidtype ourType;
if( proto == TCP)
{
//TODO: What are we supposed to do with the CI test? It's unused. Also, we're assuming UDP
// should be handled the same as TCP.
ourType = person->IPID_type_TI;
ipid_cached = ipid_cached_TCP;
}
else if( proto == TCP_CLOSED)
{
ourType = person->IPID_type_CI;
ipid_cached = ipid_cached_TCP;
}
else
{
ourType = person->IPID_type_II;
ipid_cached = ipid_cached_ICMP;
}
if((proto == ICMP))
while (!*ipid_cached)
*ipid_cached = 32768 + rand_uint16(honeyd_rand)/2;
else
while (!*ipid_cached)
*ipid_cached = rand_uint16(honeyd_rand) % 1024;
switch(ourType)
{
case(ID_ZERO):
{
*pid = 0;
break;
}
case(ID_RANDOM):
{
//Random not valid for II, set to 0 if called mistakenly
if(proto != ICMP)
{
*pid = rand_uint16(honeyd_rand);
}
else
*pid = 0;
break;
}
case(ID_CONSTANT):
{
*pid = *ipid_cached;
break;
}
case(ID_RPI):
{
// Id must increment by at least 1000
uint16_t temp = 1025 + (rand_uint16(honeyd_rand) % 255);
//cannot be divisible by 256 unless > 256,000 which is outside uint16_t so we just keep trying
// by shifting right 2 we can quickly constrain the random to < 2^14 == 16384 to avoid
// incrementing by 20,000 and having value of RD.
if((temp % 256 == 0))
temp = 1025 + (rand_uint16(honeyd_rand) % 255);
*ipid_cached += temp;
*pid = *ipid_cached;
break;
}
//This means id is sent in host byte order, increment and return host to network
case(ID_SEQUENTIAL_BROKEN):
{
*ipid_cached = ntohs(*ipid_cached) + 1;
*ipid_cached = htons(*ipid_cached);
*pid = *ipid_cached;
break;
}
//This means id increases by 1
case(ID_SEQUENTIAL):
{
*ipid_cached = *ipid_cached + 1;
*pid = *ipid_cached;
break;
}
case(ID_NONE):
{
//need to know what to implement for this section
break;
}
default:
{
break;
}
}
//TODO this is a fringe case that can sometimes cause bad IP ID results particularly when TI = BI, II = BI and SS = O
if((proto == ICMP) && !person->ipid_shared_sequence)
{
uint16_t temp = tmpl->IPID_last_TCP + 3*person->TCPID_Max_Increment;
if(*ipid_cached < temp)
{
temp = (tmpl->IPID_last_TCP + 1 + 3*person->TCPID_Max_Increment);
*ipid_cached = temp + (rand_uint16(honeyd_rand) % ((~(uint16_t)0) - temp));
if(tmpl->IPID_last_TCP > 32768)
{
tmpl->IPID_last_TCP -= 32768;
}
}
}
}
struct personate *
tcp_personality_test(const struct tcp_con *con, struct personality *person,
uint8_t sndflags)
{
uint8_t flags;
/* ET - The T1 test of NMAP has the SYN and ECE TCP flags set. The T5
* test of NMAP has only the SYN TCP flag set. The sequence number
* test of NMAP has only the SYN TCP flag set.
* Niels: We reuse the T1 even if ECE is not set if the response seems
* sane. This allows us to get TCP options right, too.
*/
flags = con->rcv_flags & (TH_FIN|TH_RST|TH_PUSH|TH_ACK|TH_URG|TH_SYN);
if(con->rcv_flags == (TH_ECE|TH_CWR|TH_SYN))
{
switch (con->state)
{
case TCP_STATE_LISTEN:
case TCP_STATE_SYN_RECEIVED:
return (&person->ecn_test);
default:
return (NULL);
}
}
else if (flags == TH_SYN)
{
int hasece = con->rcv_flags & TH_ECE;
switch (con->state)
{
case TCP_STATE_LISTEN:
case TCP_STATE_SYN_RECEIVED:
{
struct personate *test = &person->t_tests[0];
if (sndflags & TH_RST)
return (NULL);
//This might be one of the 6 Nmap SEQ packets, reply appropriately
//Apply WIN and OPS fields to the t_tests[0] test
switch( con->recv_window )
{
case 1:
{
//SEQ Packet #1
return (&person->seq_tests[0]);
}
case 63:
{
//SEQ Packet #2
return (&person->seq_tests[1]);
}
case 4:
{
//In this case the last byte in the tcp header for seq_test[2]
// is the last byte of the MSS value which is 640, the last byte is 128
if(con->nmap_opt == 128)
{
//SEQ Packet #3
return (&person->seq_tests[2]);
}
//The last byte in the tcp header(data-1) for seq_test[3] is 0, EOL
else
{
//SEQ Packet #4
return (&person->seq_tests[3]);
}
break;
}
case 16:
{
//SEQ Packet #5
return (&person->seq_tests[4]);
}
case 512:
{
//SEQ Packet #6
return (&person->seq_tests[5]);
}
}
/* Check if we can use the ECE response for normal
* SYN, too.
*/
if (hasece || (test->flags == (TH_SYN|TH_ACK) &&
test->forceack == ACK_KEEP))
return (test);
return (NULL);
}
case TCP_STATE_CLOSED:
if (hasece)
return (NULL);
return (&person->t_tests[4]);
default:
return (NULL);
}
}
else if (flags == 0)
{
switch (con->state)
{
case TCP_STATE_LISTEN:
return (&person->t_tests[1]);
default:
return (NULL);
}
}
else if (flags == (TH_SYN|TH_PUSH|TH_FIN|TH_URG))
{
switch (con->state)
{
case TCP_STATE_LISTEN:
case TCP_STATE_SYN_RECEIVED:
return (&person->t_tests[2]);
default:
return (NULL);
}
}
else if (flags == TH_ACK)
{
switch (con->state)
{
case TCP_STATE_LISTEN:
case TCP_STATE_SYN_RECEIVED:
return (&person->t_tests[3]);
case TCP_STATE_CLOSED:
return (&person->t_tests[5]);
default:
return (NULL);
}
}
else if (flags == (TH_FIN|TH_PUSH|TH_URG))
{
switch (con->state)
{
case TCP_STATE_CLOSED:
return (&person->t_tests[6]);
default:
return (NULL);
}
}
else if ((flags & TH_FIN) && (flags & (TH_SYN|TH_ACK)) == 0)
{
/*
* If we get a FIN flag and do not allow fin scanning, then
* we just let the regular state engine run its course.
* Otherwise, we silently drop the packet, which indicates
* that FIN scanning is allowed for TCP_STATE_LISTEN.
*/
if (person->disallow_finscan || con->state != TCP_STATE_LISTEN)
return (NULL);
return (&person_drop);
}
return (NULL);
}
double rand_normal(double mean, double stddev)
{
static double n2 = 0.0;
static int n2_cached = 0;
if (!n2_cached) {
double x, y, r;
do {
x = 2.0*rand()/RAND_MAX - 1;
y = 2.0*rand()/RAND_MAX - 1;
r = x*x + y*y;
} while (r == 0.0 || r > 1.0);
{
double d = sqrt(-2.0*log(r)/r);
double n1 = x*d;
n2 = y*d;
double result = n1*stddev + mean;
n2_cached = 1;
return result;
}
} else {
n2_cached = 0;
return n2*stddev + mean;
}
}
static uint32_t
get_next_isn(struct template *tmpl, const struct personality *person)
{
double mean, max, min, std_dev, seconds_passed;
extern rand_t *honeyd_rand;
uint32_t GCD_delta, ISN_delta = 0;
struct timeval time_now, time_diff;
//Get the current timstamp, save it
gettimeofday( &time_now, NULL);
//If this is our first ISN, then just make one up.
if( tmpl->seq == 0 )
{
tmpl->seq = rand_uint32(honeyd_rand);
//Save the timeval into the template
tmpl->tv_ISN = time_now;
return tmpl->seq;
}
/* if SEQ is constant */
if(person->TCP_ISR_max < 10)
{
//Do nothing
return (tmpl->seq);
}
timersub(&time_now, &tmpl->tv_ISN, &time_diff);
seconds_passed = time_diff.tv_sec + ((double)time_diff.tv_usec / 1000000.0);
//Nmap saves the values as binary log times 8, so undo this.
// (Supposedly, Nmap does this to prevent floating point rounding during calculations)
max = pow(2,((double)person->TCP_ISR_max /(double)8)) * seconds_passed;
min = pow(2,((double)person->TCP_ISR_min /(double)8)) * seconds_passed;
mean = pow(2,((double)person->TCP_ISR /(double)8)) * seconds_passed;
std_dev = pow(2, ((double)person->TCP_SP/(double)8));
max += (std_dev/8);
min -= (std_dev/8);
double temp = rand_normal(0, std_dev);
ISN_delta = mean;
if(person->TCP_ISN_gcd > 9)
{
temp *= person->TCP_ISN_gcd;
max += (std_dev/8)*(person->TCP_ISN_gcd -1);
min -= (std_dev/8)*(person->TCP_ISN_gcd -1);
}
while((max < (mean + temp)) || (min > (mean + temp)))
{
temp = rand_normal(0, std_dev);
if(person->TCP_ISN_gcd > 9)
{
temp *= person->TCP_ISN_gcd;
}
}
ISN_delta += temp;
//Only worry about making things line up for the GCD if it's a significant value
if(person->TCP_ISN_gcd > 9)
{
GCD_delta = (ISN_delta % person->TCP_ISN_gcd);
//Round up
if( GCD_delta > (person->TCP_ISN_gcd/2) )
{
ISN_delta += (person->TCP_ISN_gcd - GCD_delta);
}
//Round down
else
{
ISN_delta -= GCD_delta;
}
}
tmpl->tv_ISN = time_now;
tmpl->seq += ISN_delta;
return tmpl->seq;
}
#define TIME_CORRECT(x,y) do { \
(y)->tv_sec = 0; \
(y)->tv_usec = (y)->tv_usec % (x); \
timersub(&tmpl->tv, y, &tmpl->tv); \
} while (0)
/* Get the correct time for this personality */
void
personality_time(struct template *tmpl, struct timeval *diff)
{
uint32_t ms;
timersub(&tv_periodic, &tmpl->tv_real, diff);
tmpl->tv_real = tv_periodic;
ms = diff->tv_sec * 10000 + (diff->tv_usec / 100);
ms *= tmpl->drift;
diff->tv_sec = ms / 10000;
diff->tv_usec = (ms % 10000) * 100;
timeradd(&tmpl->tv, diff, &tmpl->tv);
}
int
tcp_personality_time(struct template *tmpl, struct timeval *diff)
{
extern rand_t *honeyd_rand;
struct personality *person = tmpl->person;
int slowhz;
if (person == NULL)
return (-1);
gettimeofday(&tv_periodic, NULL);
personality_time(tmpl, diff);
if (person->tstamphz)
{
int tstamphz = person->tstamphz;
int ticks;
if (tstamphz == -1)
tstamphz = 2;
/*
* Adjust so that the remaining subsecond ticks get
* counted next time.
*/
ticks = 1000000L / tstamphz;
if(!ticks)
{
slowhz = diff->tv_sec * tstamphz + diff->tv_usec;
}
else
{
slowhz = diff->tv_sec * tstamphz + diff->tv_usec / ticks;
TIME_CORRECT(ticks, diff);
}
tmpl->timestamp += slowhz;
}
else
{
/*
* This is not the default. Some stacks don't have
* any notion of time.
*/
slowhz = 0;
tmpl->timestamp = 0;
}
return (slowhz);
}
uint32_t
tcp_personality_seq(struct tcp_con * con, struct template *tmpl, struct personality *person)
{
struct timeval tmp;
extern rand_t *honeyd_rand;
tmpl->seqcalls++;
if (!timerisset(&tmpl->tv))
{
gettimeofday(&tv_periodic, NULL);
tmpl->tv_real = tmpl->tv = tv_periodic;
gettimeofday(&tv_periodic, NULL);
if (tmpl->timestamp == 0)
{
tmpl->timestamp = rand_uint32(honeyd_rand) % 1728000;
}
}
tcp_personality_time(tmpl, &tmp);
/*
* This is where new ISNs are generated. The latest ISN is
* stored in tmpl->seq. tmpl->seqcalls is the number of ISNs
* generated so far.
*/
if((con->state == TCP_STATE_LISTEN) && (con->rcv_flags == TH_SYN))
{
con->snd_una = get_next_isn(tmpl, person);
}
return (con->snd_una);
}
/* Default TCP options is timestamp, noop, noop */
static struct tcp_option default_opt_vals[3] = {
{'T', 0, 0, 0},
{'N', 0, 0, 0},
{'N', 0, 0, 0}
};
static struct tcp_options default_opts =
{
3,
default_opt_vals,
};
int
tcp_personality_match(struct tcp_con *con, int flags)
{
struct template *tmpl = con->tmpl;
struct personality *person;
/* Find template and corresponding personality */
if (tmpl == NULL)
return (0);
person = tmpl->person;
if (person == NULL)
return (0);
return (tcp_personality_test(con, person, flags) != NULL);
}
int
tcp_personality(struct tcp_con *con, uint8_t *pflags, int *pwindow, int *pdf,
uint16_t *pid, struct tcp_options *poptions)
{
struct template *tmpl = con->tmpl;
struct personality *person;
struct personate *pers;
uint8_t flags = *pflags;
/* XXX - We need to find some template to use here */
/* Find template and corresponding personality */
if (tmpl == NULL)
return (-1);
person = tmpl->person;
if (person == NULL)
return (-1);
if ((pers = tcp_personality_test(con, person, flags)) == NULL)
{
/* Not a test case - but we still want to pretend */
ip_personality(tmpl, pid, TCP);
/* Set the sequence number only on SYN segments */
if (flags & TH_SYN)
con->snd_una = tcp_personality_seq(con, tmpl, person);
/* If we support timestamps, always set them */
if (person->tstamphz >= 0 && poptions != NULL)
*poptions = default_opts;
return (-1);
}
*pwindow = pers->window;
*pflags = pers->flags;
*pdf = pers->df;
if (poptions != NULL)
*poptions = pers->options;
if(con->rcv_flags != (TH_ECE|TH_CWR|TH_SYN))
{
switch (pers->forceack)
{
case ACK_ZERO:
con->rcv_next = 0;
break;
case ACK_DECREMENT:
con->rcv_next--;
break;
case ACK_KEEP:
break;
case ACK_OTHER:
con->rcv_next += 9;
break;
}
switch (pers->forceseq)
{
case SEQ_ZERO:
con->snd_una = 0;
break;
case SEQ_DECREMENT:
con->snd_una--;
break;
case SEQ_KEEP:
break;
case SEQ_OTHER:
con->snd_una += 9;
break;
}
}
if(con->state == TCP_STATE_CLOSED)
ip_personality(tmpl, pid, TCP_CLOSED);
else
ip_personality(tmpl, pid, TCP);
if ((pers->forceseq != SEQ_ZERO) && (con->state == TCP_STATE_LISTEN))
con->snd_una = tcp_personality_seq(con, tmpl, person);
return (0);
}
#define SET(y, x, w, l) do { \
(x)->opt_type = w; \
(x)->opt_len = l; \
memcpy(y, x, l); \
} while (0)
/*
* Given a character string that describe TCP options, create the
* corresponding packet data.
*/
void
tcp_personality_options(struct tcp_con *con, struct tcp_hdr *tcp,
struct tcp_options *options)
{
extern rand_t *honeyd_rand;
u_char *p = (u_char *)tcp + TCP_HDR_LEN;
struct template *tmpl = con->tmpl;
struct tcp_opt opt;
int optlen = 0, simple = 0;
uint32_t timestamp;
short mss = 1460;
uint i = 0;
for (; i < options->count; i++ )
{
opt.opt_len = 0;
switch(options->options[i].opt_type)
{
case 'M':
mss = options->options[i].value;
if (con->flags & TCP_TARPIT)
{
mss = 64;
}
opt.opt_data.mss = htons(mss);
SET(p, &opt, TCP_OPT_MSS, 4);
break;
case 'W':
opt.opt_data.wscale = options->options[i].value;
SET(p, &opt, TCP_OPT_WSCALE, 3);
break;
case 'T':
if (tmpl != NULL)
{
//struct timeval tv;
//tcp_personality_time(tmpl, &tv);
//gettimeofday(&tv, NULL);
//timestamp = tv.tv_sec;
timestamp = htonl(tmpl->timestamp);
}
else
{
timestamp = rand_uint32(honeyd_rand);
}
//Assign timestamps on the basis of the Nmap T values
// IE: 0 means value is zero, 1 means anything else
if( options->options[i].TSval == '1' )
{
opt.opt_data.timestamp[0] = timestamp;
}
else
{
opt.opt_data.timestamp[0] = 0;
}
if( options->options[i].TSecr == '1' )
{
opt.opt_data.timestamp[1] = timestamp;
}
else
{
opt.opt_data.timestamp[1] = 0;
}
/*if (con->sawtimestamp)
opt.opt_data.timestamp[1] = con->echotimestamp;*/
SET(p, &opt, TCP_OPT_TIMESTAMP, 2 + 4 + 4);
break;
case 'N':
simple++;
SET(p, &opt, TCP_OPT_NOP, 1);
break;
case 'L':
SET(p, &opt, TCP_OPT_EOL, 1);
break;
case 'S':
SET(p, &opt, 4, 2);
break;
default:
opt.opt_len = 0;
break;
}
optlen += opt.opt_len;
p += opt.opt_len;
}
/* Check if we have only unreasonable options */
if (simple == optlen)
optlen = 0;
if (optlen)
tcp->th_off += (optlen + 3) / 4;
}
/* JVR - added '+1' in default case below for situations where IP checksum does not
change after byte order conversion, e.g. IP checksum of 0x6565.
Also added missing 'break' statement in RVAL_ZERO case */
#define RVAL_DO(x, w) do { \
switch (w) { \
case RVAL_OKAY: break; \
case RVAL_ZERO: (x) = 0; break; \
default: (x) = ntohs(x+1); \
} \
} while (0)
int
icmp_error_personality(struct template *tmpl,
struct addr *dst, struct ip_hdr *ip, uint8_t *pdf,
uint8_t *ptos, int *pquotelen, uint8_t *ttl)
{
struct persudp *test;
int iphdr_changed = 0;
if (tmpl == NULL || tmpl->person == NULL)
return (1);
test = &tmpl->person->udptest;
if (!test->response)
return (0);
extern rand_t* honeyd_rand;
if((test->ttl == test->ttl_guess) && (test->ttl_max != test->ttl_min))
{
test->ttl = test->ttl_min + rand_uint32(honeyd_rand)%(test->ttl_max - test->ttl_min);
test->ttl_guess = test->ttl+1;
}
*ttl = test->ttl;
*pdf = test->df;
*ptos = test->tos;
*pquotelen = test->quotelen;
if (test->riplen) {
u_int len = ntohs(ip->ip_len);
ip->ip_len = htons(len + test->riplen);
iphdr_changed = 1;
}
if (test->rid != RVAL_OKAY)
{
iphdr_changed = 1;
ip->ip_id = htons(test->ridVal);
}
/* We need to recompute the ip header checksum in some cases */
if (test->ripck == RVAL_OKAY)
{
if (iphdr_changed)
ip_checksum(ip, ip->ip_hl << 2);
}
else
RVAL_DO(ip->ip_sum, test->ripck);
if (ip->ip_p == IP_PROTO_UDP)
{
struct udp_hdr *udp = (struct udp_hdr *)((u_char *)ip + (ip->ip_hl << 2));
u_char *p = (u_char *)(udp + 1);
if(test->uck != RVAL_OKAY)
{
udp->uh_sum = htons(test->uckVal);
}
if (test->dat == RVAL_BAD)
*p = 0;
}
return (1);