forked from CuckooEXE/FakeNTP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackets.py
More file actions
156 lines (131 loc) · 3.68 KB
/
Copy pathpackets.py
File metadata and controls
156 lines (131 loc) · 3.68 KB
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
"""
Packets - Packet Definitions
"""
"""
Imported Libraries
ctypes - Used to create a C structure.
enum - Used to create an enum type.
"""
import ctypes
import enum
class NTPLI(enum.IntEnum):
"""
NTP Leap Indicator
"""
NO_WARNING = 0
LAST_MINUTE_61 = 1
LAST_MINUTE_59 = 2
ALARM = 3
class NTPVN(enum.IntEnum):
"""
NTP Version Number
"""
VERSION_1 = 1
VERSION_2 = 2
VERSION_3 = 3
VERSION_4 = 4
class NTPMode(enum.IntEnum):
"""
NTP Mode
"""
RESERVED = 0
SYMMETRIC_ACTIVE = 1
SYMMETRIC_PASSIVE = 2
CLIENT = 3
SERVER = 4
BROADCAST = 5
NTP_CONTROL_MESSAGE = 6
PRIVATE = 7
class NTPStratum(enum.IntEnum):
"""
NTP Stratum
"""
UNSPECIFIED = 0
PRIMARY_REFERENCE = 1
SECONDARY_REFERENCE = 2
UNSYNCHRONIZED = 3
RESERVED = 4
class StructHelper(object):
def __get_value_str(self, name, fmt="{}"):
val = getattr(self, name)
if isinstance(val, ctypes.Array):
val = list(val)
return fmt.format(val)
def __str__(self):
result = "{}:\n".format(self.__class__.__name__)
maxname = max(len(f[0]) for f in self._fields_)
for field in self._fields_:
if len(field) == 3:
name, type_, _ = field
else:
name, type_ = field
_ = None
result += " {name:<{width}}: {value}\n".format(
name=name,
width=maxname,
value=self.__get_value_str(name),
)
return result
def __repr__(self):
return "{name}({fields})".format(
name=self.__class__.__name__,
fields=", ".join(
"{}={}".format(name, self.__get_value_str(name, "{!r}"))
for name, _ in self._fields_
),
)
@classmethod
def _typeof(cls, field):
"""Get the type of a field
Example: A._typeof(A.fld)
Inspired by stackoverflow.com/a/6061483
"""
for name, type_ in cls._fields_:
if getattr(cls, name) is field:
return type_
raise KeyError
@classmethod
def read_from(cls, f):
result = cls()
if f.readinto(result) != ctypes.sizeof(cls):
raise EOFError
return result
def get_bytes(self):
"""Get raw byte string of this structure
ctypes.Structure implements the buffer interface, so it can be used
directly anywhere the buffer interface is implemented.
https://stackoverflow.com/q/1825715
"""
return bytes(self)
class NTPv3(ctypes.BigEndianStructure, StructHelper):
_pack_ = 1
_fields_ = [
("li", ctypes.c_uint8, 2),
("vn", ctypes.c_uint8, 3),
("mode", ctypes.c_uint8, 3),
("stratum", ctypes.c_uint8),
("poll", ctypes.c_uint8),
("precision", ctypes.c_uint8),
("root_delay", ctypes.c_uint32),
("root_dispersion", ctypes.c_uint32),
("reference_identifier", ctypes.c_uint32),
("reference_timestamp", ctypes.c_uint64),
("originate_timestamp", ctypes.c_uint64),
("receive_timestamp", ctypes.c_uint64),
("transmit_timestamp", ctypes.c_uint64),
]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.li = 0
self.vn = 3
self.mode = 4
self.stratum = 0
self.poll = 0
self.precision = 0
self.root_delay = 0
self.root_dispersion = 0
self.reference_identifier = 0
self.reference_timestamp = 0
self.originate_timestamp = 0
self.receive_timestamp = 0
self.transmit_timestamp = 0