-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTcp.py
86 lines (66 loc) · 2.46 KB
/
Tcp.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
from Layer import Layer
import struct
# https://datatracker.ietf.org/doc/html/rfc793#section-3.1
from HelperFuncs import RandShort, chksum16bit, ProtocolTypesIP
class TCP(Layer):
_my__protocol = ProtocolTypesIP.TCP
sport = None
dport = None
seq = None
ack = None
offset = None # Amount of 32-bit words in the header. Exists to determine if Options exist.
RESERVED = 0b000000
flagURG = None
flagACK = None
flagPSH = None
flagRST = None
flagSYN = None
flagFIN = None
mtu = None # AKA window
chksum = None
urgPtr = None
def __init__(self, sport=None, dport=None, seq=None, acknum=None):
self.sport = sport
self.dport = dport
self.seq = seq
self.ack = acknum
self.flagURG = None
self.flagACK = None
self.flagPSH = None
self.flagRST = None
self.flagSYN = None
self.flagFIN = None
def __bytes__(self):
self._autocomplete()
pkt = struct.pack(">HHLL", self.sport, self.dport, self.seq, self.ack)
pkt += struct.pack(">H", (self.offset << 12) | (self.RESERVED << 6) | (self.flagURG << 5) | (self.flagACK << 4) |
(self.flagPSH << 3) | (self.flagRST << 2) | (self.flagSYN << 1) | self.flagFIN)
pkt += struct.pack(">H", self.mtu)
# IP pseudo header included in checksum
self.chksum = chksum16bit(pkt + struct.pack("!H", self.urgPtr))# + self.data if hasattr(self, 'data') else b'')
pkt += struct.pack(">HH", self.chksum, self.urgPtr)
# if hasattr(self, 'data'):
# pkt += bytes(self.data)
# Insert checksum
# pkt = pkt[:8] + struct.pack(">H", self.chksum) + pkt[10:]
print(hex(self.chksum))
return pkt
def _autocomplete(self):
if self.sport is None:
self.sport = RandShort()
if self.seq is None:
self.seq = 0
if self.ack is None:
self.ack = 0
if self.mtu is None:
self.mtu = 1500
if self.urgPtr is None:
self.urgPtr = 0
if self.offset is None:
self.offset = 5
if self.chksum is None:
self.chksum = 0
# Set all flags which are None to 0
for attr in self.__dict__.keys():
if attr.startswith('flag') and getattr(self, attr) is None:
setattr(self, attr, 0)