-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_initial_syn_data.py
executable file
·57 lines (44 loc) · 1.69 KB
/
tcp_initial_syn_data.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
#!/usr/bin/env python
# -*- coding: ascii -*-
from scapy.all import *
import argparse
cmd_desc = 'Template for sending data with scapy over TCP'
parser = argparse.ArgumentParser(description = cmd_desc)
parser.add_argument("--initial-data", help = "Data to send in initial TCP SYN packet",
type = str, nargs = '?', const = 1, default = "initial syn data")
parser.add_argument("--data", help = "Data to send after 3-way handshake",
type = str, nargs = '?', const = 1, default = "after 3-way handshake")
parser.add_argument('ip', help = 'Destination IP Address')
parser.add_argument('port', help = 'Destination TCP Port', type = int)
args = parser.parse_args()
# IP arguments
dst_ip = args.ip
# TCP arguments
dst_port = args.port
src_port = random.randint(1024,65535)
tcp_seq = random.randint(0,4294967295)
initial_syn_data = args.initial_data
data = args.data
# IP header
ip = IP(dst = dst_ip)
# SYN packet
syn = TCP(sport = src_port, dport = dst_port, flags = 'S', seq = tcp_seq)
tcp_seq = tcp_seq + 1
# SYN-ACK response packet
syn_ack_resp = sr1(ip/syn/initial_syn_data)
tcp_ack = syn_ack_resp.seq
tcp_ack = tcp_ack + 1
# ACK packet
ack = TCP(sport = src_port, dport = dst_port, flags = 'A', seq = tcp_seq, ack = tcp_ack)
send(ip/ack)
# Send stream
data_packet = TCP(sport = src_port, dport = dst_port, flags = 'FA', seq = tcp_seq, ack = tcp_ack)
sr1(ip/data_packet/data)
tcp_seq = tcp_seq + len(data)
# FIN packet
fin = TCP(sport = src_port, dport = dst_port, flags = 'FA', seq = tcp_seq, ack = tcp_ack)
fin_resp = sr1(ip/fin)
tcp_seq = tcp_seq + 1
tcp_ack = tcp_ack + 1
ack = TCP(sport = src_port, dport = dst_port, flags = 'A', seq = tcp_seq, ack = tcp_ack)
send(ip/ack)