-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.1_TCP_SYN.py
148 lines (117 loc) · 3.81 KB
/
4.1_TCP_SYN.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
#!/usr/bin/python
from scapy.all import *
eth_inteface = "Intel(R) Ethernet Connection (13) I219-LM"
src_mac='00:50:56:AA:AA:AA'
dst_mac='00:50:56:FF:FF:FF'
broadcast_mac='FF:FF:FF:FF:FF:FF'
src_ip='10.10.10.10'
dst_ip='20.20.20.20'
user_seq = 1000000 #random num starting the seq
server_seq = 2000000 #random num starting the seq
############# Handshake #############
#USER: SYN
eth_frame_src = Ether(dst=dst_mac,src=src_mac, type=0x0800)
ip_packet_src = IP(dst=dst_ip,src=src_ip)
src_port = 1025
dst_port = 80
seq=user_seq
ack = 0 #no ack yet were just starting
# FIN = 0x01
# SYN = 0x02
# RST = 0x04
# PSH = 0x08
# ACK = 0x10
# URG = 0x20
# ECE = 0x40
# CWR = 0x80
flags = 0x02
tcp_src= TCP(sport=src_port, dport=dst_port, seq=seq, ack=ack,flags=flags)
sendp(eth_frame_src/ip_packet_src/tcp_src, iface=eth_inteface)
#SERVER: SYN-ACK
eth_frame_rsp = Ether(dst=src_mac,src=dst_mac, type=0x0800)
ip_packet_rsp = IP(dst=src_ip,src=dst_ip)
src_port = 80
dst_port = 1025
seq=server_seq
user_seq=user_seq+1
ack = user_seq
flags = 0x12
tcp_resp = TCP(sport=src_port, dport=dst_port, seq=seq, ack=ack,flags=flags)
sendp(eth_frame_rsp/ip_packet_rsp/tcp_resp, iface=eth_inteface)
#USER: ACK
eth_frame_src = Ether(dst=dst_mac,src=src_mac, type=0x0800)
ip_packet_src = IP(dst=dst_ip,src=src_ip)
src_port = 1025
dst_port = 80
seq=user_seq
server_seq=server_seq+1
ack = server_seq #sender seq+1
flags = 0x10
tcp_src= TCP(sport=src_port, dport=dst_port, seq=seq, ack=ack,flags=flags)
sendp(eth_frame_src/ip_packet_src/tcp_src, iface=eth_inteface)
############# Data Transmission #############
#USER: Data request
eth_frame_src = Ether(dst=dst_mac,src=src_mac, type=0x0800)
ip_packet_src = IP(dst=dst_ip,src=src_ip)
src_port = 1025
dst_port = 80
seq=user_seq
ack = server_seq
flags = 0x10
tcp_src= TCP(sport=src_port, dport=dst_port, seq=seq, ack=ack,flags=flags)
sendp(eth_frame_src/ip_packet_src/tcp_src/"give me data please.", iface=eth_inteface)
#SERVER: Data given
eth_frame_rsp = Ether(dst=src_mac,src=dst_mac, type=0x0800)
ip_packet_rsp = IP(dst=src_ip,src=dst_ip)
src_port = 80
dst_port = 1025
seq=server_seq
user_seq=user_seq+20
ack = user_seq
flags = 0x10
tcp_resp = TCP(sport=src_port, dport=dst_port, seq=seq, ack=ack,flags=flags)
sendp(eth_frame_rsp/ip_packet_rsp/tcp_resp/"here is some data", iface=eth_inteface)
#USER: Data Ack
eth_frame_src = Ether(dst=dst_mac,src=src_mac, type=0x0800)
ip_packet_src = IP(dst=dst_ip,src=src_ip)
src_port = 1025
dst_port = 80
seq=user_seq
server_seq = server_seq+17
ack = server_seq
flags = 0x10
tcp_src= TCP(sport=src_port, dport=dst_port, seq=seq, ack=ack,flags=flags)
sendp(eth_frame_src/ip_packet_src/tcp_src, iface=eth_inteface)
#SERVER: FIN
eth_frame_rsp = Ether(dst=src_mac,src=dst_mac, type=0x0800)
ip_packet_rsp = IP(dst=src_ip,src=dst_ip)
src_port = 80
dst_port = 1025
seq=server_seq
user_seq=user_seq
ack = user_seq
flags = 0x01
tcp_resp = TCP(sport=src_port, dport=dst_port, seq=seq, ack=ack,flags=flags)
sendp(eth_frame_rsp/ip_packet_rsp/tcp_resp, iface=eth_inteface)
#USER: FIN + ACK
eth_frame_src = Ether(dst=dst_mac,src=src_mac, type=0x0800)
ip_packet_src = IP(dst=dst_ip,src=src_ip)
src_port = 1025
dst_port = 80
seq=user_seq
server_seq = server_seq + 1
ack = server_seq
flags = 0x11
tcp_src= TCP(sport=src_port, dport=dst_port, seq=seq, ack=ack,flags=flags)
sendp(eth_frame_src/ip_packet_src/tcp_src, iface=eth_inteface)
#SERVER: ACK
eth_frame_rsp = Ether(dst=src_mac,src=dst_mac, type=0x0800)
ip_packet_rsp = IP(dst=src_ip,src=dst_ip)
src_port = 80
dst_port = 1025
seq=server_seq
user_seq=user_seq +1
ack = user_seq
flags = 0x10
tcp_resp = TCP(sport=src_port, dport=dst_port, seq=seq, ack=ack,flags=flags)
sendp(eth_frame_rsp/ip_packet_rsp/tcp_resp, iface=eth_inteface)