-
Notifications
You must be signed in to change notification settings - Fork 2
/
udp_gun.py
75 lines (54 loc) · 1.71 KB
/
udp_gun.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
import socket
SOURCE_PORT = 27183
TARGET_IP = "172.18.0.255"
TARGET_PORT = 25000
DEFAULT_PAYLOAD = b"Not hello"
MAGIC_BYTES = b"\x26\x2a"
PUT = b"\x01"
GET = b"\x02"
PING = b"\x03"
PONG = b"\x04"
FPING = b"\x05"
KEY = b"7" * 32
VALUE = b"2" * 32
def str_to_mac(s):
"""
Convert a MAC address string to 6 byte-octets
>>> str_to_mac("020000FFFF01")
b'\x02\x00\x00\xff\xff\x01'
"""
res = []
for i in range(6):
first, second = int("0x" + s[i * 2], 16), int("0x" + s[i * 2 + 1], 16)
res.append(((first << 4) | second).to_bytes(1, byteorder="big"))
return b"".join(res)
def str_to_ip(s):
return b"".join([(int(x)).to_bytes(1, byteorder="big") for x in s.split(".")])
def build_message(action, key, value, payload=DEFAULT_PAYLOAD):
return MAGIC_BYTES + action + key + value + payload
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
# s.bind(("172.17.0.1", SOURCE_PORT))
s.bind(("", SOURCE_PORT))
# s.bind(("172.18.0.0", SOURCE_PORT))
mac_payload = str_to_mac("020000FFFF00")
ip_payload = str_to_ip("10.100.1.10")
forge_payload = mac_payload + ip_payload
forge_payload = b"\x26\x2a" + b"\x07" * 100
# print(forge_payload)
s.sendto(
# build_message(FPING, KEY, VALUE, payload=forge_payload),
forge_payload, (TARGET_IP, TARGET_PORT)
)
# s.sendto(build_message(GET, KEY, VALUE), (TARGET_IP, TARGET_PORT))
received_data, origin = s.recvfrom(1024)
print("received ... something?")
# magic, action, key, value, payload = (
# received_data[:2],
# received_data[2:3],
# received_data[3:35],
# received_data[35:67],
# received_data[67:],
# )
# assert magic == MAGIC_BYTES
# print(action, key, value, payload)