File: nfq_dump_pcap.py

package info (click to toggle)
nfqueue-bindings 0.3-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 212 kB
  • ctags: 80
  • sloc: ansic: 260; python: 220; perl: 195; makefile: 70; sh: 25
file content (57 lines) | stat: -rwxr-xr-x 850 bytes parent folder | download
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
#!/usr/bin/python

# 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 nfqueue

outputfile = None
outputfilename = "dump.pcap"

from scapy import Packet, PcapWriter, hexdump

writer = None

def cb(i,payload):
    data = payload.get_data()

    # Add padding before packet
    # src mac + dst mac + 0x0800 (type: IP)
    pad = "\0" * 12 + "\x08\0" + data

    pkt = Packet(_pkt=pad)
    writer.write(pkt)

    return 1

q = nfqueue.queue()

q.open()

q.unbind(AF_INET)
if q.bind(AF_INET) != 0:
    q.close()
    raise RuntimeError("Could not bind to nfqueue")

writer = PcapWriter(outputfilename)

q.set_callback(cb)

q.create_queue(0)

try:
    q.try_run()
except KeyboardInterrupt, e:
    pass


q.unbind(AF_INET)
q.close()