-
Notifications
You must be signed in to change notification settings - Fork 1
/
Publisher.py
51 lines (40 loc) · 1.24 KB
/
Publisher.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
import socket
from scapy.all import sniff
from scapy.layers.inet import UDP, IP
import json5
def port_to_bytes(n: int):
return n.to_bytes(length=2, byteorder='big')
class Publisher:
def __init__(self):
with open('./config.json5') as f:
cfg = json5.loads(f.read())
self.serv_ip = cfg['server_ip']
self.serv_port = cfg['server_port']
self.iface = cfg['iface']
self.debug = cfg['debug']
self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def prn(self, packet):
sport = 65535
dport = 65535
try:
sport = packet[UDP].sport
dport = packet[UDP].dport
if self.debug == True:
print(f'catch: {packet[IP].src}:{sport} ->{packet[IP].dst}:{dport}')
except Exception as e:
if self.debug == True:
print('Layer [UDP] not found')
if len(packet[UDP].payload) < 50:
data = port_to_bytes(sport)+port_to_bytes(dport)+bytes(packet[UDP].payload)
self.s.sendto(data, (self.serv_ip, self.serv_port))
if self.debug == True:
print(f'send to: {self.serv_ip}:{self.serv_port}, data len:{len(data)}')
def run(self):
f = 'udp'
print('sniff:', f)
sniff(filter=f, prn=self.prn, iface=self.iface)
def main():
p = Publisher()
p.run()
if __name__ == '__main__':
main()