-
Notifications
You must be signed in to change notification settings - Fork 0
/
Listener.py
69 lines (53 loc) · 2.3 KB
/
Listener.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
from scapy.all import *
from Socket import *
class FlowListener:
def __init__(self, server_addr, server_port, name, interfaces=None):
self.conn = SocketClient(server_addr, server_port, name)
self.server_addr = server_addr
self.server_port = server_port
self.client_addr = self.conn.sock.getsockname()[0]
self.client_port = self.conn.sock.getsockname()[1]
if interfaces is None:
self.interfaces = None
elif isinstance(interfaces, str):
self.interfaces = ifaces[interfaces]
elif isinstance(interfaces, int):
self.interfaces = ifaces.dev_from_index(interfaces)
elif isinstance(interfaces, (list, tuple)):
self.interfaces = []
for interface in interfaces:
if isinstance(interface, str):
self.interfaces.append(ifaces[interface])
elif isinstance(interface, int):
self.interfaces.append(ifaces.dev_from_index(interface))
else:
raise ValueError("Invalid interface type")
def handler(self, pkg):
dst = None if IP not in pkg else pkg[IP].dst
dport = None if TCP not in pkg else pkg[TCP].dport
src = None if IP not in pkg else pkg[IP].src
sport = None if TCP not in pkg else pkg[TCP].sport
if dst == self.server_addr and dport == self.server_port:
return
if src == self.server_addr and sport == self.server_port:
return
self.conn.send(bytes(pkg))
def run(self, filter=None):
sniff(filter=filter, iface=self.interfaces, prn=self.handler)
def stop(self):
self.conn.disconnect()
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("host", help="Listen address")
parser.add_argument("port", type=int, help="Listen port")
parser.add_argument("name", help="Name of this client")
parser.add_argument("-i", "--interface", type=str, nargs='+', help="Interface to listen on")
args = parser.parse_args()
listener = FlowListener(args.host, args.port, args.name, args.interface)
try:
listener.run()
except KeyboardInterrupt:
listener.stop()
print("Disconnected")
exit(0)