-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample.py
More file actions
executable file
·71 lines (51 loc) · 1.21 KB
/
Copy pathexample.py
File metadata and controls
executable file
·71 lines (51 loc) · 1.21 KB
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
#!/usr/bin/python
# need root privileges
import sys
from socket import AF_INET, inet_ntoa
sys.path.append('python')
sys.path.append('build/python')
import nfqueue
sys.path.append('dpkt-1.6')
from dpkt import ip
count = 0
def cb(payload):
global count
print "python callback called !"
count += 1
data = payload.get_data()
pkt = ip.IP(data)
if pkt.p == ip.IP_PROTO_TCP:
print " len %d proto %s src: %s:%s dst %s:%s " % (
payload.get_length(),
pkt.p,
inet_ntoa(pkt.src),
pkt.tcp.sport,
inet_ntoa(pkt.dst),
pkt.tcp.dport,
)
else:
print " len %d proto %s src: %s dst %s " % (
payload.get_length(),
pkt.p,
inet_ntoa(pkt.src),
inet_ntoa(pkt.dst),
)
payload.set_verdict(nfqueue.NF_ACCEPT)
sys.stdout.flush()
return 1
q = nfqueue.queue()
print "setting callback"
q.set_callback(cb)
print "open"
q.fast_open(0, AF_INET)
q.set_queue_maxlen(50000)
print "trying to run"
try:
q.try_run()
except KeyboardInterrupt, e:
print "interrupted"
print "%d packets handled" % count
print "unbind"
q.unbind(AF_INET)
print "close"
q.close()