-
Notifications
You must be signed in to change notification settings - Fork 1
/
unmangletest.py
executable file
·55 lines (48 loc) · 1.26 KB
/
unmangletest.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
#!/usr/bin/python
# iptables -N pb_in
# iptables -A pb_in -p icmp -j NFQUEUE --queue-num 2
# iptables -A INPUT -j pb_in
from netfilterqueue import NetfilterQueue
import socket
import dpkt
import dnet
out = dnet.ip()
class PacketError(Exception):
pass
def unnest_payload(bytes):
ip = dpkt.ip.IP(bytes)
if not isinstance(ip.data, dpkt.icmp.ICMP):
raise PacketError
icmp = ip.data
if not isinstance(icmp.data, dpkt.icmp.ICMP.Unreach) and \
not isinstance(icmp.data, dpkt.icmp.ICMP.TimeExceed):
raise PacketError
icmp_payload = icmp.data
if not isinstance(icmp_payload.data, dpkt.ip.IP):
raise PacketError
ip2 = icmp_payload.data
if not isinstance(ip2.data, dpkt.udp.UDP):
raise PacketError
udp = ip2.data
ip3 = dpkt.ip.IP(str(udp.data))
return ip3
def handle_packet(pkt):
print pkt
data = pkt.get_payload()
print data.encode('hex')
try:
ip = unnest_payload(data)
#print str(ip).encode('hex')
print ip.__repr__()
out.send(str(ip))
print 'drop'
pkt.drop()
except PacketError:
print 'accept'
pkt.accept()
nfqueue = NetfilterQueue()
nfqueue.bind(2, handle_packet)
try:
nfqueue.run()
except KeyboardInterrupt:
print