| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a0824db commit ac24ebc
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,60 @@ | |||
| 1 | + #!/usr/bin/python | ||
| 2 | + | ||
| 3 | + # need root privileges | ||
| 4 | + | ||
| 5 | + import struct | ||
| 6 | + import sys | ||
| 7 | + import time | ||
| 8 | + import asyncore | ||
| 9 | + | ||
| 10 | + from socket import AF_INET, AF_INET6, inet_ntoa | ||
| 11 | + | ||
| 12 | + sys.path.append('python') | ||
| 13 | + sys.path.append('build/python') | ||
| 14 | + import nfqueue | ||
| 15 | + | ||
| 16 | + sys.path.append('dpkt-1.6') | ||
| 17 | + from dpkt import ip | ||
| 18 | + | ||
| 19 | + def cb(i,payload): | ||
| 20 | + print "python callback called !", i | ||
| 21 | + | ||
| 22 | + print "payload len ", payload.get_length() | ||
| 23 | + data = payload.get_data() | ||
| 24 | + pkt = ip.IP(data) | ||
| 25 | + print "proto:", pkt.p | ||
| 26 | + print "source: %s" % inet_ntoa(pkt.src) | ||
| 27 | + print "dest: %s" % inet_ntoa(pkt.dst) | ||
| 28 | + if pkt.p == ip.IP_PROTO_TCP: | ||
| 29 | + print " sport: %s" % pkt.tcp.sport | ||
| 30 | + print " dport: %s" % pkt.tcp.dport | ||
| 31 | + payload.set_verdict(nfqueue.NF_DROP) | ||
| 32 | + | ||
| 33 | + sys.stdout.flush() | ||
| 34 | + return 1 | ||
| 35 | + | ||
| 36 | + class AsyncNfQueue(asyncore.file_dispatcher): | ||
| 37 | + """An asyncore dispatcher of nfqueue events. | ||
| 38 | + | ||
| 39 | + """ | ||
| 40 | + | ||
| 41 | + def __init__(self, cb, nqueue=0, family=AF_INET, maxlen=5000, map=None): | ||
| 42 | + self._q = nfqueue.queue() | ||
| 43 | + self._q.set_callback(cb) | ||
| 44 | + self._q.fast_open(nqueue, family) | ||
| 45 | + self._q.set_queue_maxlen(maxlen) | ||
| 46 | + self.fd = self._q.get_fd() | ||
| 47 | + asyncore.file_dispatcher.__init__(self, self.fd, map) | ||
| 48 | + self._q.set_mode(nfqueue.NFQNL_COPY_PACKET) | ||
| 49 | + | ||
| 50 | + def handle_read(self): | ||
| 51 | + print "Processing at most 5 events" | ||
| 52 | + self._q.process_pending(5) | ||
| 53 | + | ||
| 54 | + # We don't need to check for the socket to be ready for writing | ||
| 55 | + def writable(self): | ||
| 56 | + return False | ||
| 57 | + | ||
| 58 | + async_queue = AsyncNfQueue(cb) | ||
| 59 | + asyncore.loop() | ||
| 60 | + | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments