-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloopback_socket.py
45 lines (34 loc) · 1.17 KB
/
loopback_socket.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
from scapy.all import *
from consts import TCP_BUFFER_SIZE
class LoopbackSocket():
"""
Loopback Raw Layer3 socket to sniff and write
packets from or to the ICMP tunnel.
"""
def __init__(self, listen_port, is_server):
"""
Initialize socket.
:param listen_port - TCP port to listen to.
:param is_server - Whether we should sniff incoming or outgoing packets to the listen_port
"""
self.listen_port = listen_port
self.is_server = is_server
print(f"[LoopbackSocket] Creating Raw TCP socket on loopback: {'127.0.0.1', listen_port}")
self.socket = L3RawSocket(iface='lo')
def recv(self):
"""
Receive Packet from raw socker
@returns the packet from the TCP layer and up
"""
packet = self.socket.recv(TCP_BUFFER_SIZE)
if TCP in packet:
if getattr(packet[TCP], "sport" if self.is_server else "dport") == self.listen_port and (packet[IP].src == '127.0.0.1' or packet[IP].dst == '127.0.0.1'):
return raw(packet[TCP])
def send(self, partial_packet):
"""
Send packet to raw socket
:param partial_packet - packet from the TCP layer and up
"""
packet = IP(dst='127.0.0.1') / TCP(partial_packet)
del packet[TCP].chksum
self.socket.send(packet)