forked from cmand/sundial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsundialClassify.py
executable file
·1514 lines (1359 loc) · 45.5 KB
/
sundialClassify.py
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
#!/usr/bin/env python
#
# Copyright (c) 2019, Erik Rye
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the <organization> nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AN
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Program: $Id: sundialClassify.py $
# Author: Erik Rye <[email protected]>
# Description: https://www.cmand.org/sundial/
#
# Parses pcap_results.txt[.gz] file from timestampAnalyze. Produces the type
# classification breakdown used in the PAM 2019 paper.
#
import sys
import gzip
import logging
import argparse
from tsutils import *
logging.basicConfig(format='%(filename)s:%(funcName)s:%(lineno)d: '\
'%(levelname)s -- %(message)s', level=logging.INFO)
def addEntry(l, ip, origin, rx, tx, type_):
'''
Adds an entry to a list l. The value
is a tuple that contains the origin, rx, tx, and type
of request or reply. d is either a dictionary containing
all of the requests, or all of the replies.
'''
if not ip in d:
d[ip] = [(origin, rx, tx, type_)]
else:
d[ip].append((origin,rx,tx, type_))
def runStats(requests, replies):
'''
runStats
@params:
requests, dict of timestamp requests
ip -> [(origin, rx, tx, type), ...]
replies, dict of timestamp replies
ip -> [(origin, rx, tx, type), ...]
'''
for ip in requests:
reqs = requests[ip]
if ip in replies:
reps = replies[ip]
else:
logging.debug("[+] IP %s: no replies", ip)
continue
fprint = classify(reqs, reps)
if not fprint:
continue
if not fprint in fingerprints:
fingerprints[fprint] = [ip]
else:
fingerprints[fprint].append(ip)
def printStats(results):
'''
printStats
@params:
results, a dict of classifications -> counts
'''
for result in sorted(results.keys()):
print result, results[result]
def writeStats(results, name):
'''
printStats
@params:
results, a dict of classifications -> counts
'''
with open(name, "w") as f:
for result in sorted(results.keys()):
f.write(str(result)+','+str(results[result])+'\n')
def writeFingerprints(fingerprints, name):
'''
writeFingerprints
@params:
results, a dict of fingerprints -> counts
'''
with open(name, "w") as g:
for fprint in fingerprints:
words = ','.join([fingerNames[idx] for idx, val in \
enumerate(fprint) if val])
fname_words = '_'.join([fingerNames[idx] for idx, val in \
enumerate(fprint) if val]) + '.ips'
if words:
print words, ':', len(fingerprints[fprint])
g.write(words+','+str(len(fingerprints[fprint]))+'\n')
with open(fname_words, 'w') as f:
for ip in fingerprints[fprint]:
f.write(ip + '\n')
else:
print "Unknown:", len(fingerprints[fprint])
g.write('unknown,'+str(len(fingerprints[fprint]))+'\n')
with open('unknown.ips', 'w') as f:
for ip in fingerprints[fprint]:
f.write(ip + '\n')
def writeClassifications(fingerprints, name):
'''
'''
with open(name, "w") as g:
for fprint in fingerprints:
words = ','.join([fingerNames[idx] for idx, val in \
enumerate(fprint) if val])
for ip in fingerprints[fprint]:
if words:
g.write(ip + ',' + words + '\n')
else:
g.write(ip + ',unknown\n')
def classify(reqs, reps):
'''
classify
@params:
reqs, list of timestamp requests
[(origin, rx, tx, type), ...]
replies, list of timestamp requests
[(origin, rx, tx, type), ...]
Returns a tuple fingerprint
of each category the IP fits into
'''
#########################
#If we don't have all the requests,
#then skip for now
#########################
if not allRequests(reqs):
return False
#########################
#If no replies,
#skip
#########################
if not reps:
return False
########################
#Get a new fingerprint tuple
########################
fprint = [0 for x in range(len(fingerNames))]
results['total'] += 1
if isNormal(reqs, reps):
logging.debug("[+] Normal Reply")
results['normal'] += 1
fprint[NORMAL] = 1
if isLazy(reqs, reps):
logging.debug("[+] Lazy Reply")
results['lazy'] += 1
fprint[LAZY] = 1
if isChecksumLazy(reqs, reps):
logging.debug("[+] Checksum-Lazy Reply")
results['checksumLazy'] += 1
fprint[CHECKSUMLAZY] = 1
stuck = isStuck(reqs, reps)
if stuck:
logging.debug("[+] Stuck Clock")
if stuck == BOTH:
results['stuck'] += 1
fprint[STUCK] = 1
elif stuck == RX_ONLY:
results['stuckRx'] += 1
fprint[STUCKRX] = 1
elif stuck == TX_ONLY:
results['stuckTx'] += 1
fprint[STUCKTX] = 1
else:
logging.warn("Error! Shouldn't get here.")
exit(1)
####################
#Stuck at 0 clock
####################
const = isConstant(reqs, reps, 0)
if const:
logging.debug("[+] Constant %s Clock", 0)
if const == BOTH:
results['stuck0'] += 1
fprint[STUCK0] = 1
elif const == RX_ONLY:
results['stuck0Rx'] += 1
fprint[STUCK0RX] = 1
elif const == TX_ONLY:
results['stuck0Tx'] += 1
fprint[STUCK0TX] = 1
else:
logging.warn("Error! Shouldn't get here.")
exit(1)
####################
#Stuck at 1 clock
####################
const = isConstant(reqs, reps, 1)
if const:
logging.debug("[+] Constant %s Clock", 1)
if const == BOTH:
results['stuck1'] += 1
fprint[STUCK1] = 1
elif const == RX_ONLY:
results['stuck1Rx'] += 1
fprint[STUCK1RX] = 1
elif const == TX_ONLY:
results['stuck1Tx'] += 1
fprint[STUCK1TX] = 1
else:
logging.warn("Error! Shouldn't get here.")
exit(1)
####################
#Stuck at LE 1 clock
####################
const = isConstant(reqs, reps, swap(1))
if const:
logging.debug("[+] Constant %s Clock", swap(1))
if const == BOTH:
results['stuckLE1'] += 1
fprint[STUCKLE1] = 1
elif const == RX_ONLY:
results['stuckLE1Rx'] += 1
fprint[STUCKLE1RX] = 1
elif const == TX_ONLY:
results['stuckLE1Tx'] += 1
fprint[STUCKLE1TX] = 1
else:
logging.warn("Error! Shouldn't get here.")
exit(1)
echo = isEchoOrigin(reqs, reps)
if echo:
logging.debug("[+] Echo Origin Reply")
if echo == BOTH:
results['echo'] += 1
fprint[ECHO] = 1
elif echo == RX_ONLY:
results['echoRx'] += 1
fprint[ECHORX] = 1
elif echo == TX_ONLY:
fprint[ECHOTX] = 1
results['echoTx'] += 1
else:
logging.warn("Error! Shouldn't get here.")
exit(1)
reflect = isReflection(reqs, reps)
if reflect:
logging.debug("[+] Reflect Reply")
if reflect == BOTH:
results['reflect'] += 1
fprint[REFLECT] = 1
elif reflect == RX_ONLY:
results['reflectRx'] += 1
fprint[REFLECTRX] = 1
elif reflect == TX_ONLY:
results['reflectTx'] += 1
fprint[REFLECTTX] = 1
else:
logging.warn("Error! Shouldn't get here.")
exit(1)
tz, offset = isTimezone(reqs, reps)
if tz:
logging.debug("[+] Timezone Reply")
if tz == BOTH:
results['timezone'] += 1
fprint[TIMEZONE] = 1
if not offset in timezones:
timezones[offset] = 1
else:
timezones[offset] += 1
elif tz == RX_ONLY:
results['timezoneRx'] += 1
fprint[TIMEZONERX] = 1
if not offset in timezones:
timezones[offset] = 1
else:
timezones[offset] += 1
elif tz == TX_ONLY:
fprint[TIMEZONETX] = 1
results['timezoneTx'] += 1
if not offset in timezones:
timezones[offset] = 1
else:
timezones[offset] += 1
else:
logging.warn("Error! Shouldn't get here.")
exit(1)
correct = isCorrect(reqs, reps)
if correct:
logging.debug("[+] Correct Reply")
if correct == BOTH:
fprint[CORRECT] = 1
results['correct'] += 1
elif correct == RX_ONLY:
results['correctRx'] += 1
fprint[CORRECTRX] = 1
elif correct == TX_ONLY:
fprint[CORRECTTX] = 1
results['correctTx'] += 1
else:
logging.warn("Error! Shouldn't get here.")
exit(1)
correctLE = isCorrectLE(reqs, reps)
if correctLE:
logging.debug("[+] Correct LE Reply")
if correctLE == BOTH:
fprint[CORRECTLE] = 1
results['correctLE'] += 1
elif correctLE == RX_ONLY:
fprint[CORRECTLERX] = 1
results['correctLERx'] += 1
elif correctLE == TX_ONLY:
fprint[CORRECTLETX] = 1
results['correctLETx'] += 1
else:
logging.warn("Error! Shouldn't get here.")
exit(1)
msb = isMSB(reqs, reps)
if msb:
logging.debug("[+] MSB Reply")
if msb == BOTH:
fprint[MSB] = 1
results['msb'] += 1
elif msb == RX_ONLY:
fprint[MSBRX] = 1
results['msbRx'] += 1
elif msb == TX_ONLY:
fprint[MSBTX] = 1
results['msbTx'] += 1
else:
logging.warn("Error! Shouldn't get here.")
exit(1)
correctMSB = isCorrectMSB(reqs, reps)
if correctMSB:
logging.debug("[+] Correct MSB Reply")
if correctMSB == BOTH:
fprint[CORRECTMSB] = 1
results['correctMSB'] += 1
elif correctMSB == RX_ONLY:
fprint[CORRECTMSBRX] = 1
results['correctMSBRx'] += 1
elif correctMSB == TX_ONLY:
fprint[CORRECTMSBTX] = 1
results['correctMSBTx'] += 1
else:
logging.warn("Error! Shouldn't get here.")
exit(1)
buggy = isBuggy(reqs, reps)
if buggy:
logging.debug("[+] Buggy Reply")
fprint[BUGGY] = 1
results['buggy'] += 1
ms = isCountingMs(reqs, reps)
if ms:
logging.debug("[+] Millisecond Counting Reply")
if ms == BOTH:
fprint[MS] = 1
results['millisecond'] += 1
elif ms == RX_ONLY:
fprint[MSRX] = 1
results['millisecondRx'] += 1
elif ms == TX_ONLY:
fprint[MSTX] = 1
results['millisecondTx'] += 1
else:
logging.warn("Error! Shouldn't get here.")
exit(1)
s = isCountingS(reqs, reps)
if s:
logging.debug("[+] Second Counting Reply")
if s == BOTH:
fprint[S] = 1
results['second'] += 1
elif s == RX_ONLY:
fprint[SRX] = 1
results['secondRx'] += 1
elif s == TX_ONLY:
fprint[STX] = 1
results['secondTx'] += 1
else:
logging.warn("Error! Shouldn't get here.")
exit(1)
s = isEpoch(reqs, reps)
if s:
logging.debug("[+] Epoch Reply")
if s == BOTH:
fprint[EPOCH] = 1
results['epoch'] += 1
elif s == RX_ONLY:
fprint[EPOCHRX] = 1
results['epochRx'] += 1
elif s == TX_ONLY:
fprint[EPOCHTX] = 1
results['epochTx'] += 1
else:
logging.warn("Error! Shouldn't get here.")
exit(1)
#########################
#Return the fingerprint tuple
#########################
return tuple(fprint)
def isEpoch(reqs, reps):
'''
Determines whether some destination is replying with
epoch time
@params:
reqs, a list of timestamp requests
[(origin, rx, tx, type), ...]
@return value
BOTH if both rx and tx timestamps are epoch
RX_ONLY
TX_ONLY
We've got the epoch time in the timestamp tuple, so let's just check that
we're within +/- a second
'''
###################
# Get standard request
###################
standardReq = getRequestByType(REQ_STANDARD, reqs)
logging.debug("Standard request: %s", standardReq)
standardReply = getReplyByTimestamp(standardReq[O_TUP], reps)
logging.debug("Standard reply: %s", standardReply)
###################
#If there's a no standard
#request reply, then quit
###################
if not standardReply:
return False
standardRepRx = standardReply[RX_TUP]
standardRepTx = standardReply[TX_TUP]
standardRepEpoch = int(standardReply[TIME_TUP])
if (abs(standardRepRx - standardRepEpoch) <= 1 and
abs(standardRepTx - standardRepEpoch) <= 1):
return BOTH
elif (abs(standardRepRx - standardRepEpoch) <= 1):
return RX_ONLY
elif (abs(standardRepTx - standardRepEpoch) <= 1):
return TX_ONLY
else:
return False
def isCountingS(reqs, reps):
'''
Determines whether some destination is counting
in seconds or not.
@params:
reqs, a list of timestamp requests
[(origin, rx, tx, type), ...]
@return value
BOTH if both rx and tx timestamps are correct
RX_ONLY
TX_ONLY
Determine how long elapsed between two successive replies from a host using
standard and duplicate timestamp requests. Then, compare to what is inferred
from looking at the receive and transmit timestamps in the replies
'''
###################
# Get standard request
###################
standardReq = getRequestByType(REQ_STANDARD, reqs)
logging.debug("Standard request: %s", standardReq)
standardReply = getReplyByTimestamp(standardReq[O_TUP], reps)
logging.debug("Standard reply: %s", standardReply)
###################
#If there's a no standard
#request reply, then quit
###################
if not standardReply:
return False
###################
# Get duplicate timestamp
# request reply
###################
duplicateReq = getRequestByType(REQ_DUPLICATE_TS, reqs)
logging.debug("Duplicate request: %s", duplicateReq)
duplicateReply = getReplyByTimestamp(duplicateReq[O_TUP], reps)
logging.debug("Duplicate reply: %s", duplicateReply)
###################
#If there's a no duplicate
#request reply, then quit
###################
if not duplicateReply:
return False
###Get actual times first###
standardRepRecv = standardReply[TIME_TUP]
duplicateRepRecv = duplicateReply[TIME_TUP]
logging.debug("Standard reply recv: %f, duplicate reply recv: %f",
standardRepRecv, duplicateRepRecv)
#calculate real *seconds* between receives
realDiff = int(duplicateRepRecv - standardRepRecv)
logging.debug("Calculated real time difference between receives: %d",
realDiff)
###Get times from replies###
standardRepRx = standardReply[RX_TUP]
standardRepTx = standardReply[TX_TUP]
duplicateRepRx = duplicateReply[RX_TUP]
duplicateRepTx = duplicateReply[TX_TUP]
#I'm going to assume if something counts in seconds, it doesn't do so modulo
#a day's seconds...because that'd be weird, right?
inferredRxTime = (standardRepRx + realDiff)
inferredTxTime = (standardRepTx + realDiff)
if abs(inferredRxTime - duplicateRepRx) < 2 * ERROR_MARGIN and\
abs(inferredTxTime - duplicateRepTx) < 2 * ERROR_MARGIN:
return BOTH
elif abs(inferredRxTime - duplicateRepRx) < 2 * ERROR_MARGIN:
return RX_ONLY
elif abs(inferredTxTime - duplicateRepTx) <2 * ERROR_MARGIN:
return TX_ONLY
else:
return False
def isCountingMs(reqs, reps):
'''
Determines whether some destination is counting
in milliseconds or not.
@params:
reqs, a list of timestamp requests
[(origin, rx, tx, type), ...]
@return value
BOTH if both rx and tx timestamps are correct
RX_ONLY
TX_ONLY
Determine how long elapsed between two successive replies from a host using
standard and duplicate timestamp requests. Then, compare to what is inferred
from looking at the receive and transmit timestamps in the replies
'''
###################
# Get standard request
###################
standardReq = getRequestByType(REQ_STANDARD, reqs)
logging.debug("Standard request: %s", standardReq)
standardReply = getReplyByTimestamp(standardReq[O_TUP], reps)
logging.debug("Standard reply: %s", standardReply)
###################
#If there's a no standard
#request reply, then quit
###################
if not standardReply:
return False
###################
# Get duplicate timestamp
# request reply
###################
duplicateReq = getRequestByType(REQ_DUPLICATE_TS, reqs)
logging.debug("Duplicate request: %s", duplicateReq)
duplicateReply = getReplyByTimestamp(duplicateReq[O_TUP], reps)
logging.debug("Duplicate reply: %s", duplicateReply)
###################
#If there's a no duplicate
#request reply, then quit
###################
if not duplicateReply:
return False
###Get actual times first###
standardRepRecv = standardReply[TIME_TUP]
duplicateRepRecv = duplicateReply[TIME_TUP]
logging.debug("Standard reply recv: %f, duplicate reply recv: %f",
standardRepRecv, duplicateRepRecv)
#calculate real *milliseconds* between receives
realDiff = int((duplicateRepRecv - standardRepRecv) * 1000)
logging.debug("Calculated real time difference between receives: %d",
realDiff)
###Get times from replies###
standardRepRx = standardReply[RX_TUP]
standardRepTx = standardReply[TX_TUP]
duplicateRepRx = duplicateReply[RX_TUP]
duplicateRepTx = duplicateReply[TX_TUP]
inferredRxTime = (standardRepRx + realDiff) % DAY_MILLISECONDS
inferredTxTime = (standardRepTx + realDiff) % DAY_MILLISECONDS
if abs(inferredRxTime - duplicateRepRx) < 2 * ERROR_MARGIN and\
abs(inferredTxTime - duplicateRepTx) < 2 * ERROR_MARGIN:
return BOTH
elif abs(inferredRxTime - duplicateRepRx) < 2 * ERROR_MARGIN:
return RX_ONLY
elif abs(inferredTxTime - duplicateRepTx) <2 * ERROR_MARGIN:
return TX_ONLY
else:
return False
def isCorrect(reqs, reps):
'''
Determines whether a standard reply
is correct or not
@params:
reqs, a list of timestamp requests
[(origin, rx, tx, type), ...]
@return value
Correctness defined in ERROR_MARGIN
BOTH if both rx and tx timestamps are correct
RX_ONLY
TX_ONLY
Correct -- can be determined w/standard request/reply
'''
###################
# Get standard request
###################
standardReq = getRequestByType(REQ_STANDARD, reqs)
logging.debug("Standard request: %s", standardReq)
standardReply = getReplyByTimestamp(standardReq[O_TUP], reps)
logging.debug("Standard reply: %s", standardReply)
###################
#If there's a no standard
#request reply, then quit
###################
if not standardReply:
return False
rx = standardReply[RX_TUP]
tx = standardReply[TX_TUP]
origin = standardReply[O_TUP]
if abs(rx - origin) < ERROR_MARGIN and \
abs(tx - origin) < ERROR_MARGIN:
return BOTH
elif abs(rx - origin) < ERROR_MARGIN:
return RX_ONLY
elif abs(tx - origin) < ERROR_MARGIN:
return TX_ONLY
return False
def isCorrectLE(reqs, reps):
'''
Determines whether a standard reply
is correct or not when reply is LE
@params:
reqs, a list of timestamp requests
[(origin, rx, tx, type), ...]
@return value
Correctness defined in ERROR_MARGIN
BOTH if both rx and tx timestamps are correct
RX_ONLY
TX_ONLY
Correct -- can be determined w/standard request/reply
'''
###################
# Get standard request
###################
standardReq = getRequestByType(REQ_STANDARD, reqs)
logging.debug("Standard request: %s", standardReq)
standardReply = getReplyByTimestamp(standardReq[O_TUP], reps)
logging.debug("Standard reply: %s", standardReply)
###################
#If there's a standard request reply,
#then check that 0 != rx != tx != 0
###################
if not standardReply:
return False
rx = swap(standardReply[RX_TUP])
tx = swap(standardReply[TX_TUP])
origin = standardReply[O_TUP]
if abs(rx - origin) < ERROR_MARGIN and \
abs(tx - origin) < ERROR_MARGIN:
return BOTH
elif abs(rx - origin) < ERROR_MARGIN:
return RX_ONLY
elif abs(tx - origin) < ERROR_MARGIN:
return TX_ONLY
return False
def isMSB(reqs, reps):
'''
Determines whether a standard reply
returns a MSB-set timestamp
@params:
reqs, a list of timestamp requests
[(origin, rx, tx, type), ...]
@return value
BOTH if both rx and tx timestamps have msb set
RX_ONLY
TX_ONLY
Can be determined w/standard request/reply
'''
###################
# Get standard request
###################
standardReq = getRequestByType(REQ_STANDARD, reqs)
logging.debug("Standard request: %s", standardReq)
standardReply = getReplyByTimestamp(standardReq[O_TUP], reps)
logging.debug("Standard reply: %s", standardReply)
###################
#If there's a standard request reply,
#then check that 0 != rx != tx != 0
###################
if not standardReply:
return False
rx = standardReply[RX_TUP]
tx = standardReply[TX_TUP]
if rx < MIN_NONUTC and tx < MIN_NONUTC:
return False
elif rx >= MIN_NONUTC and tx >= MIN_NONUTC:
return BOTH
elif rx >= MIN_NONUTC:
return RX_ONLY
elif tx >= MIN_NONUTC:
return TX_ONLY
else:
logging.warn("Shouldn't reach this point. Exiting")
sys.exit(1)
def isBuggyReply(reply):
'''
isBuggyReply
@params reply, a timestamp reply tuple (o, rx, tx, type)
@return True if the reply is buggy, e.g. rx == tx != 0
False else
'''
rx = reply[RX_TUP]
tx = reply[TX_TUP]
rxLower0 = ((rx & 0x0000ffff) == 0) and rx != 0 and rx != swap(1)
txLower0 = ((tx & 0x0000ffff) == 0) and tx != 0 and tx != swap(1)
if rxLower0 and txLower0:
return True
return False
def isBuggy(reqs, reps):
'''
Determines whether a remote host has
the htons() bug
@params:
reqs, a list of timestamp requests
[(origin, rx, tx, type), ...]
@return value
BOTH if both rx and tx timestamps have msb set
RX_ONLY
TX_ONLY
New from PAM -- only classify as buggy if *all* available replies are buggy.
Is buggy if in both reply timestamps the lower two bytes are 00 00
'''
#########################
#Iterate through requests
#########################
for reqType in [REQ_STANDARD, REQ_BAD_CLOCK, REQ_BAD_CHECKSUM,
REQ_DUPLICATE_TS]:
req = getRequestByType(reqType, reqs)
logging.debug("Request: %s", req)
rep = getReplyByTimestamp(req[O_TUP], reps)
logging.debug("Reply: %s", req)
####################
#if there isn't a reply
#for this request, that's ok,
#just goto next type
########################
if not rep:
continue
######################
#if this is *not* buggy, we
#break out and return false now
#######################
if not isBuggyReply(rep):
return False
###################
#If we got here,
#it's buggy
##################
return True
def isCorrectMSB(reqs, reps):
'''
Determines whether a standard reply
is correct or not when MSB turned off, if on
@params:
reqs, a list of timestamp requests
[(origin, rx, tx, type), ...]
@return value
Correctness defined in ERROR_MARGIN
BOTH if both rx and tx timestamps are correct
RX_ONLY
TX_ONLY
Correct MSB -- can be determined w/standard request/reply
'''
###################
# Get standard request
###################
standardReq = getRequestByType(REQ_STANDARD, reqs)
logging.debug("Standard request: %s", standardReq)
standardReply = getReplyByTimestamp(standardReq[O_TUP], reps)
logging.debug("Standard reply: %s", standardReply)
###################
#If there's a standard request reply,
#then check that 0 != rx != tx != 0
###################
if not standardReply:
return False
rx = standardReply[RX_TUP]
tx = standardReply[TX_TUP]
if rx < MIN_NONUTC and tx < MIN_NONUTC:
return False
origin = standardReply[O_TUP]
rx -= MIN_NONUTC
tx -= MIN_NONUTC
if abs(rx - origin) < ERROR_MARGIN and \
abs(tx - origin) < ERROR_MARGIN:
return BOTH
elif abs(rx - origin) < ERROR_MARGIN:
return RX_ONLY
elif abs(tx - origin) < ERROR_MARGIN:
return TX_ONLY
return False
def allRequests(reqs):
'''
allRequests
@params
reqs, a list of timestamp requests
[(origin, rx, tx, type), ...]
@return value
True if all 4 timestamp request types are present
False otherwise
'''
logging.debug("[+] In allRequests()")
types = [x[-1] for x in reqs]
logging.debug("[+] %s", types)
if sorted(types) == range(4):
return True
logging.warn("Skipping target, expected 4 response types, got %s" % str(sorted(types)))
return False
def isNormalReply(reply):
'''
isNormalReply
@params
a timestamp reply tuple (o,rx,tx,type_)
@return True if the reply is "normal"
according to the PAM definition 0 != rx != tx != 0
False else
'''
rx = reply[RX_TUP]
tx = reply[TX_TUP]
if rx != tx and tx != 0 and rx != 0:
return True
return False
def isNormal(reqs, reps):
'''
isNormal
Determines whether a responding host is "normal"
or not
reqs, a list of timestamp requests
[(origin, rx, tx, type), ...]
reps, a list of timestamp replies
[(origin, rx, tx, type), ...]
@return value
True if indicative of a normal responding IP
False otherwise
Normal -- 0 != rx != tx != 0. True if **ANY**
of the replies are normal, not just if the standard
request reply is normal (change from PAM)
'''
#########################
#Iterate through requests
#########################
for reqType in [REQ_STANDARD, REQ_BAD_CLOCK, REQ_BAD_CHECKSUM,
REQ_DUPLICATE_TS]:
req = getRequestByType(reqType, reqs)
logging.debug("Request: %s", req)
rep = getReplyByTimestamp(req[O_TUP], reps)
logging.debug("Reply: %s", req)
####################
#if there isn't a reply
#for this request, that's ok,
#just goto next type
########################
if not rep:
continue
######################
#if this reply is normal,
#we know this is a normal
#box, so return true now
#######################
if isNormalReply(rep):
return True
################################################
#If we get here, then all of the reply timestamps
#were lazy. There had to be at least one, or we
#wouldn't be classifying, so that's enough for me.
################################################
return False
def isLazyReply(reply):
'''
isLazyReply
@params reply, a timestamp reply tuple (o, rx, tx, type)
@return True if the reply is lazy, e.g. rx == tx != 0
False else
'''
rx = reply[RX_TUP]
tx = reply[TX_TUP]
if rx == tx and tx != 0 and rx not in CONSTANTS:
return True
return False
def isLazy(reqs, reps):
'''
isLazy()
Determines whether a responding host is "lazy" or not
reqs, a list of timestamp requests
[(origin, rx, tx, type), ...]
reps, a list of timestamp replies
[(origin, rx, tx, type), ...]
@return value
True if **all** timestamp replies are lazy. This is
a change from PAM. A responder can be classified as lazy
if it's really normal, but all of its rx/tx timestamps are
marked sub-ms. So, we are only going with true if all of the
replies indicate lazy. If *any* are normal, we immediately return
False. As a reminder, Lazy --> 0 != rx == tx
Also new from PAM -- lazy excludes other non-zero constants, e.g.
1/LE 1
'''
#########################
#Iterate through requests
#########################
for reqType in [REQ_STANDARD, REQ_BAD_CLOCK, REQ_BAD_CHECKSUM,
REQ_DUPLICATE_TS]:
req = getRequestByType(reqType, reqs)
logging.debug("Request: %s", req)
rep = getReplyByTimestamp(req[O_TUP], reps)
logging.debug("Reply: %s", req)