-
Notifications
You must be signed in to change notification settings - Fork 26
/
satoriNTP.py
194 lines (153 loc) · 5.99 KB
/
satoriNTP.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
import untangle
import satoriCommon
from pathlib import Path
from pypacker.layer12 import ethernet
from pypacker.layer3 import ip
from pypacker.layer567 import ntp
from datetime import datetime
from pypacker import pypacker
# https://datatracker.ietf.org/doc/html/rfc5905
# grab the latest fingerprint files:
# wget chatteronthewire.org/download/updates/satori/fingerprints/dhcp.xml -O dhcp.xml
#
# looking for new fingerprints
# python3 satori.py -r dhcp.pcap -m dhcp > output.txt
# cat output.txt | awk -F';' '{print $3, $4, $5, $6, $7}' | sort -u > output2.txt
# cat output.txt | awk -F';' '{print $5";"$6";"$7}' | sort -u > output2.txt
#
def version():
dateReleased='satoriNTP.py - 2023-07-04'
print(dateReleased)
def ntpProcess(pkt, layer, ts, ntpExactList, ntpPartialList):
if layer == 'eth':
src_mac = pkt[ethernet.Ethernet].src_s
else:
#fake filler mac for all the others that don't have it, may have to add some elif above
src_mac = '00:00:00:00:00:00'
ip4 = pkt.upper_layer
udp1 = pkt.upper_layer.upper_layer
ntp1 = pkt[ntp.NTP]
fingerprintNTP = None
timeStamp = datetime.utcfromtimestamp(ts).isoformat()
sport = udp1.sport
leap = ntp1.li
version = ntp1.v
mode = ntp1.mode
stratum = ntp1.stratum
poll = ntp1.interval
precision = ntp1.precision
delay = ntp1.delay
dispersion = ntp1.dispersion
id = pypacker.ip4_bytes_to_str(ntp1.id)
[referenceTime, referenceVal] = ntpTimeConvert(ntp1.update_time, ts)
[originateTime, originateVal] = ntpTimeConvert(ntp1.originate_time, ts)
[receiveTime, receiveVal] = ntpTimeConvert(ntp1.receive_time, ts)
[transmitTime, transmitVal] = ntpTimeConvert(ntp1.transmit_time, ts)
#sport needs to be either 123 or 1024+
if sport > 1024:
sport = 1025
if id != '0.0.0.0':
idVal = 'set'
else:
idVal = 'unset'
# ones with no value that I can find:
# stratum, precision, delay
# minimal use?
# dispersion, it switches based on who it is talking to for time
#mode
# 1 = symmetric active
# 2 = symmetric passive
# 3 = client
# 4 = server
# 5 = broadcastServer
# 6 = broadcastClient
if mode == 1:
fingerprint = 'active;' + str(sport) + ',' + str(leap) + ',' + str(version) + ',' + str(poll) + ',' + idVal + ',' + referenceVal + ',' + transmitVal + ',' + str(get16bitSecs(dispersion))
elif mode == 2:
# probably will remove this one?
fingerprint = 'passive;' + str(sport) + ',' + str(leap) + ',' + str(version) + ',' + str(poll) + ',' + idVal + ',' + referenceVal + ',' + transmitVal + ',' + str(get16bitSecs(dispersion))
elif mode == 3:
fingerprint = 'client;' + str(sport) + ',' + str(leap) + ',' + str(version) + ',' + str(poll) + ',' + idVal + ',' + referenceVal + ',' + transmitVal + ',' + str(get16bitSecs(dispersion))
# elif mode == 4:
#poll seemed to be from client it was replying too
# fingerprint = 'server;' + str(sport) + ',' + str(leap) + ',' + str(version) + ',' + str(get16bitSecs(dispersion)) + ',' + idVal + ',' + referenceVal + ',' + transmitVal
ntpFingerprint = ''
if (fingerprint != ''):
ntpFingerprint = fingerprintLookup(ntpExactList, ntpPartialList, fingerprint)
fingerprintNTP = ''
fingerprintNTP = ip4.src_s + ';' + src_mac + ';NTP;' + fingerprint + ';' + ntpFingerprint
return [timeStamp, fingerprintNTP]
def get16bitSecs(value):
return(value >> 16)
def get16bitFrac(value):
return(value & 0xFFFF)
def ntpTimeConvert(ntpTime, packetTime):
value = ''
#why we need an offset and only part of the info https://tickelton.gitlab.io/articles/ntp-timestamps/
offset = 2208988800
time = int.from_bytes(ntpTime[0:4], "big")
if time > offset:
time = time - offset
if time == 0:
value = '0'
else:
randomAssValue = 2000
secDiff = packetTime - time
if secDiff < -randomAssValue:
value = 'random' #past
elif secDiff < randomAssValue:
value = 'current'
else:
value = 'random' #future
timeStamp = datetime.utcfromtimestamp(time).strftime('%Y-%m-%dT%H:%M:%S')
return (timeStamp, value)
def BuildNTPFingerprintFiles():
# converting from the xml format to a more flat format that will hopefully be faster than walking the entire xml every FP lookup
ntpExactList = {}
ntpPartialList = {}
satoriPath = str(Path(__file__).resolve().parent)
obj = untangle.parse(satoriPath + '/fingerprints/ntp.xml')
fingerprintsCount = len(obj.NTP.fingerprints)
for x in range(0,fingerprintsCount):
os = obj.NTP.fingerprints.fingerprint[x]['name']
testsCount = len(obj.NTP.fingerprints.fingerprint[x].ntp_tests)
test = {}
for y in range(0,testsCount):
test = obj.NTP.fingerprints.fingerprint[x].ntp_tests.test[y]
if test is None: #if testsCount = 1, then untangle doesn't allow us to iterate through it
test = obj.NTP.fingerprints.fingerprint[x].ntp_tests.test
matchtype = test['matchtype']
ntp = test['ntp']
weight = test['weight']
if matchtype == 'exact':
if ntp in ntpExactList:
oldValue = ntpExactList.get(ntp)
ntpExactList[ntp] = oldValue + '|' + os + ':' + weight
else:
ntpExactList[ntp] = os + ':' + weight
else:
if ntp in ntpPartialList:
oldValue = ntpPartialList.get(ntp)
ntpPartialList[ntp] = oldValue + '|' + os + ':' + weight
else:
ntpPartialList[ntp] = os + ':' + weight
return [ntpExactList, ntpPartialList]
def fingerprintLookup(exactList, partialList, value):
exactValue = ''
partialValue = ''
if value in exactList:
exactValue = exactList.get(value)
for key, val in partialList.items():
if value.find(key) > -1:
partialValue = partialValue + '|' + val
if partialValue.startswith('|'):
partialValue = partialValue[1:]
if partialValue.endswith('|'):
partialValue = partialValue[:-1]
fingerprint = exactValue + '|' + partialValue
if fingerprint.startswith('|'):
fingerprint = fingerprint[1:]
if fingerprint.endswith('|'):
fingerprint = fingerprint[:-1]
fingerprint = satoriCommon.sortFingerprint(fingerprint)
return fingerprint