-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_syn.py
81 lines (73 loc) · 2.25 KB
/
example_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
import random
import sys
import sockets
from ipv4 import IPv4Packet, IPv4Flags, IPv4Address, IPv4Protocol, IPV4TOS_NULL
from ethernet import MACAddress, EtherType, EthernetFrame
from tcp import TCPPacket, TCP_FLAGS_SYN
def syn(
interface: str,
source_mac: MACAddress,
gateway_mac: MACAddress,
source_ip: IPv4Address,
destination_ip: IPv4Address,
) -> None:
tcp_syn_packet: TCPPacket = TCPPacket(
random.randint(1024, 65535), # Source port
80, # Destination port
0, # Sequence number
0, # Acknowledgment number
0, # Data offset
0, # Reserved
TCP_FLAGS_SYN,
0, # window
0, # checksum
0, # urgent_pointer
[],
b"",
)
tcp_syn_packet.fix_padding()
tcp_syn_packet.fix_data_offset()
tcp_syn_packet.fix_checksum(source_ip, destination_ip)
# This is the IP packet.
ip_packet: IPv4Packet = IPv4Packet(
4, # Version
0, # IHL
IPV4TOS_NULL, # ToS
0, # Total length
0, # ID
IPv4Flags(False, True, False),
0, # Fragment
64, # TTL
IPv4Protocol.TCP.value, # Protocol
0, # Checksum
source_ip,
destination_ip,
[], # Options
tcp_syn_packet.serialize(), # Payload
)
ip_packet.fix() # Fix the padding, IHL, total length, and checksum
# This is the ethernet frame. Note that its payload is the IP packet.
ethernet_frame: EthernetFrame = EthernetFrame(
gateway_mac,
source_mac,
EtherType.IPV4.value,
ip_packet.serialize(),
)
# Send the ethernet frame on the loopback interface.
sock = sockets.make_raw_socket()
serialized_ethernet_frame = ethernet_frame.serialize()
assert sock.sendto(serialized_ethernet_frame, (interface, 0)) == len(serialized_ethernet_frame)
if __name__ == "__main__":
if len(sys.argv) != 6:
print(
f"Usage: python3 {sys.argv[0]} <interface> <source_mac> <gateway_mac> <source_ip> <destination_ip>",
file=sys.stderr,
)
sys.exit(1)
syn(
sys.argv[1],
MACAddress(sys.argv[2]),
MACAddress(sys.argv[3]),
IPv4Address(sys.argv[4]),
IPv4Address(sys.argv[5]),
)