-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtcp.py
187 lines (172 loc) · 7.48 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
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
import socket
from ctypes import create_string_buffer
from struct import pack, pack_into, unpack, calcsize
from utils import checksum
TCP_HDR_FMT = '!HHLLBBHHH'
TCP_PSH_FMT = '!4s4sBBH'
'''
Simple Python model for a TCP segment
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Port | Destination Port |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Sequence Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Acknowledgment Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Data | |U|A|P|R|S|F| |
| Offset| Reserved |R|C|S|S|Y|I| Window |
| | |G|K|H|T|N|N| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Checksum | Urgent Pointer |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options | Padding |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| data |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
'''
class TCPSegment:
'''
Simple Python model for a TCP segment
'''
def __init__(self, ip_src_addr, ip_dest_addr, tcp_src_port=12138,
tcp_dest_port=80, tcp_seq=1, tcp_ack_seq=0, tcp_doff=5,
tcp_furg=0, tcp_fack=1, tcp_fpsh=0, tcp_frst=0, tcp_fsyn=0,
tcp_ffin=0, tcp_adwind=1, tcp_urg_ptr=0, tcp_opts=None,
data=''):
# vars for TCP pseudo-header
# all IP addresses has been encoded
self.ip_src_addr = ip_src_addr
self.ip_dest_addr = ip_dest_addr
self.zeros = 0 # padding placeholder for protocol
self.protocol = socket.IPPROTO_TCP
self.tcp_len = 0 # the length of TCP headers and data, computed
# vars for TCP header
self.tcp_src_port = tcp_src_port
self.tcp_dest_port = tcp_dest_port
self.tcp_seq = tcp_seq
self.tcp_ack_seq = tcp_ack_seq
self.tcp_doff = tcp_doff
self.tcp_resvd = 0 # for future use and should be set to 0
self.tcp_ffin = tcp_ffin
self.tcp_fsyn = tcp_fsyn
self.tcp_frst = tcp_frst
self.tcp_fpsh = tcp_fpsh
self.tcp_fack = tcp_fack
self.tcp_furg = tcp_furg
self.tcp_adwind = tcp_adwind
self.tcp_cksum = 0 # to be computed
self.tcp_urg_ptr = tcp_urg_ptr
self.tcp_opts = tcp_opts
# all HTTP stuff goes here
self.data = data
def __repr__(self):
repr = ('TCPSegment: ' +
'[src_port: %d, dest_port: %d, seq: %d, ack_seq: %d,' +
' doff: %d, resvd: %d, urg: %d, ack: %d, psh: %d, rst: %d,' +
' syn: %d, fin: %d, adwind: %d, checksum: 0x%04x, ' +
' urg_ptr: %d, options: %s, len(HTTP): %d]') \
% (self.tcp_src_port, self.tcp_dest_port, self.tcp_seq,
self.tcp_ack_seq, self.tcp_doff, self.tcp_resvd, self.tcp_furg,
self.tcp_fack, self.tcp_fpsh, self.tcp_frst, self.tcp_fsyn,
self.tcp_ffin, self.tcp_adwind, self.tcp_cksum,
self.tcp_urg_ptr, 'Yes' if self.tcp_opts else None,
len(self.data))
return repr
def _shift_flags(self, fin, syn, rst, psh, ack, urg):
'''
Shift the TCP flags to their bitwise locations
'''
return fin + (syn << 1) + (rst << 2) + (psh << 3) \
+ (ack << 4) + (urg << 5)
def _deshift_flags(self, tcp_flags):
'''
De-shift the TCP flags to a string repr
'''
return (tcp_flags & 0x01,
(tcp_flags >> 1) & 0x01,
(tcp_flags >> 2) & 0x01,
(tcp_flags >> 3) & 0x01,
(tcp_flags >> 4) & 0x01,
(tcp_flags >> 5) & 0x01,)
def _tcp_headers_buf(self):
'''
Pack the real TCP header.
'''
# arrange TCP flags
tcp_flags = self._shift_flags(self.tcp_ffin, self.tcp_fsyn,
self.tcp_frst, self.tcp_fpsh,
self.tcp_fack, self.tcp_furg)
# concatenate TCP data offset and reserved field
tcp_doff_resvd = (self.tcp_doff << 4) + self.tcp_resvd
# pack real TCP header with checksum set to 0
tcp_hdr_buf = create_string_buffer(calcsize(TCP_HDR_FMT))
pack_into(TCP_HDR_FMT, tcp_hdr_buf, 0,
self.tcp_src_port, self.tcp_dest_port,
self.tcp_seq, self.tcp_ack_seq,
tcp_doff_resvd, tcp_flags,
self.tcp_adwind, self.tcp_cksum,
self.tcp_urg_ptr)
return tcp_hdr_buf
def _tcp_pseudo_headers(self, tcp_headers):
'''
Pack the TCP pseudo-header.
'''
self.tcp_len = len(tcp_headers) + len(self.data)
tcp_psh = pack(TCP_PSH_FMT,
self.ip_src_addr, self.ip_dest_addr,
self.zeros, self.protocol,
self.tcp_len)
return tcp_psh
def pack(self):
'''
Pack the TCPSegment object to a TCP segment string.
'''
tcp_hdr_buf = self._tcp_headers_buf()
tcp_psh = self._tcp_pseudo_headers(tcp_hdr_buf.raw)
self.tcp_cksum = checksum(''.join(
[tcp_psh, tcp_hdr_buf.raw, self.data]))
pack_into('!H', tcp_hdr_buf,
calcsize(TCP_HDR_FMT[:8]),
self.tcp_cksum)
tcp_segment = ''.join([tcp_hdr_buf.raw, self.data])
return tcp_segment
def unpack(self, tcp_segment):
'''
Unpack the given TCP segment string, the unpacked
data would be stored in the current object.
'''
tcp_header_size = calcsize(TCP_HDR_FMT)
tcp_headers = tcp_segment[:tcp_header_size]
hdr_fields = unpack(TCP_HDR_FMT, tcp_headers)
self.tcp_src_port = hdr_fields[0]
self.tcp_dest_port = hdr_fields[1]
self.tcp_seq = hdr_fields[2]
self.tcp_ack_seq = hdr_fields[3]
tcp_doff_resvd = hdr_fields[4]
self.tcp_doff = tcp_doff_resvd >> 4 # get the data offset
self.tcp_adwind = hdr_fields[6]
self.tcp_urg_ptr = hdr_fields[7]
# parse TCP flags
tcp_flags = hdr_fields[5]
self.tcp_ffin, self.tcp_fsyn, self.tcp_frst, \
self.tcp_fpsh, self.tcp_fack, \
self.tcp_furg = self._deshift_flags(tcp_flags)
# process the TCP options if there are
# currently just skip it
if self.tcp_doff > 5:
opts_size = (self.tcp_doff - 5) * 4
tcp_header_size += opts_size
tcp_headers = tcp_segment[:tcp_header_size]
# get the TCP data
self.data = tcp_segment[tcp_header_size:]
# compute the checksum of the recv packet with psh
tcp_psh = self._tcp_pseudo_headers(tcp_headers)
self.tcp_cksum = checksum(''.join(
[tcp_psh, tcp_headers, self.data]))
def verify_checksum(self):
'''
Return True if the verified the received TCP packet.
'''
return self.tcp_cksum == 0x0000