def main(): l = nflog.log() l.set_callback(callback) l.fast_open(123,AF_INET) try: l.try_run() except KeyboardInterrupt, e: print "interrupted, terminating..."
def main(): global l NFLOG_GROUP = 1 # TODO: adapt this; default = 0 l = nflog.log() l.set_callback(callback) l.fast_open(NFLOG_GROUP, socket.AF_INET) l.prepare() # TODO: change this to users with reduced privileges on your system dropPrivileges('nobody', 'nogroup') print "calling loop()" try: l.loop() except KeyboardInterrupt: print "SIGINT caught in main thread" l.stop_loop() pass # normal; sometimes happens in callback handler, sometimes here finally: print "returned from loop(), will now tear down" #l.unbind(socket.AF_INET) # l.unbind() is privileged (requires root) l.close()
# need root privileges import struct import sys from datetime import datetime from socket import AF_INET, AF_INET6, inet_ntoa sys.path.append('python') sys.path.append('build/python') import nflog sys.path.append('dpkt-1.6') from dpkt import ip l = nflog.log() def cb(payload): try: print("Packet received") print("seq: [%d]" % payload.get_seq()) print(" payload len ", payload.get_length()) try: tv = payload.get_timestamp() d = datetime.fromtimestamp(tv.tv_sec + (tv.tv_usec / 1000000.)) print(" timestamp: ", d) except RuntimeError as e: #print(e.args[0]) pass data = payload.get_data()
# need root privileges import struct import sys import time from socket import AF_INET, AF_INET6, inet_ntoa sys.path.append('python') sys.path.append('build/python') import nflog sys.path.append('dpkt-1.6') from dpkt import ip l = nflog.log() def cb(payload): try: print "python callback called !" print "payload len ", payload.get_length() data = payload.get_data() pkt = ip.IP(data) print "proto:", pkt.p print "source: %s" % inet_ntoa(pkt.src) print "dest: %s" % inet_ntoa(pkt.dst) if pkt.p == ip.IP_PROTO_TCP: print " sport: %s" % pkt.tcp.sport print " dport: %s" % pkt.tcp.dport