| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
NetfilterQueue provides access to packets matched by an iptables rule in Linux. Packets so matched can be accepted, dropped, altered, or given a mark.
Libnetfilter_queue (the netfilter library, not this module) is part of the Netfilter project.
The following script prints a short description of each packet before accepting it.
from netfilterqueue import NetfilterQueue
def print_and_accept(pkt):
print pkt
pkt.accept()
nfqueue = NetfilterQueue()
nfqueue.bind(1, print_and_accept)
try:
nfqueue.run()
except KeyboardInterrupt:
print
To send packets destined for your LAN to the script, type something like:
iptables -I INPUT -d 192.168.0.0/24 -j NFQUEUE --queue-num 1
NetfilterQueue is a C extention module that links against libnetfilter_queue. Before installing, ensure you have:
On Debian or Ubuntu, install these files with:
apt-get install build-essential python-dev libnetfilter-queue-dev
To install from PyPI by pip:
pip install NetfilterQueue
To install from source:
wget http://pypi.python.org/packages/source/N/NetfilterQueue/NetfilterQueue-0.3.tar.gz tar -xvzf NetfilterQueue-0.3.tar.gz cd NetfilterQueue-0.3 python setup.py install
If Cython is installed, Distutils will use it to regenerate the .c source from the .pyx. It will then compile the .c into a .so.
NetfilterQueue.COPY_NONE
NetfilterQueue.COPY_META
A NetfilterQueue object represents a single queue. Configure your queue with a call to bind, then start receiving packets with a call to run.
Objects of this type are passed to your callback.
Your callback can be function or a method and must accept one argument, a Packet object. You must call either Packet.accept() or Packet.drop() before returning.
To send packets to the queue:
iptables -I <table or chain> <match specification> -j NFQUEUE --queue-num <queue number>
For example:
iptables -I INPUT -d 192.168.0.0/24 -j NFQUEUE --queue-num 1
The only special part of the rule is the target. Rules can have any match and can be added to any table or chain.
Valid queue numbers are integers from 0 to 65,535 inclusive.
To view libnetfilter_queue stats, refer to /proc/net/netfilter/nfnetlink_queue:
cat /proc/net/netfilter/nfnetlink_queue 1 31621 0 2 4016 0 0 2 1
The fields are:
More details coming soon...
Compiled with a 4096-byte buffer for packets, so it probably won't work on loopback or Ethernet with jumbo packets. If this is a problem, either lower MTU on your loopback, disable jumbo packets, or get Cython, change DEF BufferSize = 4096 in netfilterqueue.pyx, and rebuild.
Full libnetfilter_queue API is not yet implemented:
- Omits packet.set_payload() for altering packet data
- Omits methods for getting information about the interface a packet has arrived on or is leaving on
- Probably other stuff is omitted too
When a packet has been marked, we use nfq_set_verdict_mark rather than nfq_set_verdict2. Apparently nfq_set_verdict_mark is broken, although it works for me.
https://github.com/kti/python-netfilterqueue
Copyright (c) 2011, Kerkhoff Technologies, Inc.
| Back | FazBrowse Home | New Git URL |