lost and found ( for me ? )

Showing posts with label dpkt. Show all posts
Showing posts with label dpkt. Show all posts

Mint 12 : use iptables , nfqueue-bindings-python , python-dpkt


Here's an explanation of how to use nfqueue , nfqueue-bindings and python-dpkt

I’m not familiar with nfqueue , so I referred to the following URLs.

Many thanks!!
# uname –ri
3.0.0-20-generic x86_64

# tail -1 /etc/lsb-release
DISTRIB_DESCRIPTION="Linux Mint 12 Lisa"


install nfqueu-bindings-python and required packages via apt-get
# apt-get install -y nfqueue-bindings-python
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
 libnetfilter-queue1 libpython2.6 python-nfqueue python2.6 python2.6-minimal
Suggested packages:
 python2.6-doc
The following NEW packages will be installed:
 libnetfilter-queue1 libpython2.6 nfqueue-bindings-python python-nfqueue
 python2.6 python2.6-minimal


load nfnetlink_queue module.
# modprobe nfnetlink_queue

# lsmod | grep -i nfnetlink_queue
nfnetlink_queue        17743  0
nfnetlink              14327  1 nfnetlink_queue


jump to NFQUEUE when tcp dst port is 8080/
# iptables –F

#  iptables -I OUTPUT -p tcp -o eth0 --dport 8080 -j NFQUEUE --queue-num 0

# iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
NFQUEUE    tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:8080 NFQUEUE num 0


use the following sample script.

Please note that you need to specify the same queue number both iptables and example.py.
In this case , queue number needs to be 0.

--queue-num 0 (iptables )

q.fast_open(0, AF_INET) ( example.py )

Before using that script , install dpkt python module which is required for example.py
# apt-get install -y python-dpkt --force-yes


access to a remote host with TCP 8080
# telnet 192.168.10.15 8080
Trying 192.168.10.15...
Connected to 192.168.10.15.
Escape character is '^]'.
GET / HTTP/1.0

HTTP/1.1 200 OK
Date: Wed, 06 Jun 2012 23:31:51 GMT
Server: Apache/2.2.20 (Ubuntu)
Last-Modified: Fri, 09 Dec 2011 06:23:48 GMT
ETag: "1062407-b1-4b3a2d3eb1200"
Accept-Ranges: bytes
Content-Length: 177
Vary: Accept-Encoding
Connection: close
Content-Type: text/html

<html><body><h1>It works!</h1>
<p>This is the default web page for this server.</p>
<p>The web server software is running but no content has been added, yet.</p>
</body></html>
Connection closed by foreign host.


OK , I could capture 8080 data connection.
# ./example.py
setting callback
open
trying to run
python callback called !
 len 60 proto 6 src: 192.168.122.124:48921    dst 192.168.10.15:8080
python callback called !
 len 52 proto 6 src: 192.168.122.124:48921    dst 192.168.10.15:8080
python callback called !
 len 64 proto 6 src: 192.168.122.124:48921    dst 192.168.10.15:8080
python callback called !
 len 68 proto 6 src: 192.168.122.124:48921    dst 192.168.10.15:8080
python callback called !
 len 54 proto 6 src: 192.168.122.124:48921    dst 192.168.10.15:8080
python callback called !
 len 52 proto 6 src: 192.168.122.124:48921    dst 192.168.10.15:8080
python callback called !
 len 52 proto 6 src: 192.168.122.124:48921    dst 192.168.10.15:8080


flush iptables setting.
# iptables -F

# iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Python dpkt : counts # of IPv4 UDP and TCP packets from a pcap file


Here’s a description of how to counts # of UDP , TCP packets from a pcap with python dpkt.

The logics are:
1. counts total # of packets
2. picks up IPv4 packets ( ethernet type is 2048 )
3. picks up UDP packets ( IP protcol number is 17 ) and counts
4. picks up TCP packets ( IP protcol number is 6 ) and counts
5. counts # of packets except for thertnet type 2048

# http://www.iana.org/assignments/ethernet-numbers
# http://www.iana.org/assignments/protocol-numbers/protocol-numbers.xml

python script
root@hat1:~/python_works# less -N count_IPv4_UDP_TCP_packets.py
     1 #!/usr/bin/env python
     2
     3 import dpkt, sys
     4
     5 if len(sys.argv) < 2 or len(sys.argv) > 2:
     6         print "Usage:", sys.argv[0], "filename.pcap"
     7         sys.exit()
     8
     9 f = open(sys.argv[1])
    10 pcap = dpkt.pcap.Reader(f)
    11
    12 udp_packet = 0
    13 tcp_packet = 0
    14 other = 0
    15 total_packet = 0
    16
    17 for ts, buf in pcap:
    18         eth = dpkt.ethernet.Ethernet(buf)
    19 # counts total number of packets
    20         total_packet += 1
    21
    22 # picks up IPv4 packets
    23 # http://www.iana.org/assignments/ethernet-numbers
    24         if eth.type == 2048:
    25                 ip = eth.data
    26                 
    27 # picks up UDP packets
    28 # http://www.iana.org/assignments/protocol-numbers/protocol-numbers.xml
    29                 if ip.p == 17:
    30                         udp_packet += 1
    31
    32 # picks up TCP packets
    33                 if ip.p == 6:
    34                         tcp_packet += 1
    35
    36 # picks up ether type except for 2048
    37         else:
    38                 other += 1
    39
    40 print "total # of packets: %s"  % (total_packet)
    41 print "# of UDP packets: %s" % (udp_packet)
    42 print "# of TCP packets: %s" % (tcp_packet)
    43 print "# of other packets except for TCP or UDP: %s"  % (other)

[ example usage ]

I prepared a pcap file containing 12 packets , 8 TCP packets and 4 UDP packets.
root@hat1:~/python_works# tshark -r zzz.pcap | wc -l
12

root@hat1:~/python_works# tshark -r zzz.pcap tcp | wc -l
8

root@hat1:~/python_works# tshark -r zzz.pcap udp | wc -l
4

root@hat1:~/python_works# ./count_IPv4_UDP_TCP_packets.py zzz.pcap
total # of packets: 12
# of UDP packets: 4
# of TCP packets: 8
# of other packets except for TCP or UDP: 0