Giter VIP home page Giter VIP logo

pcapdj's Introduction

pcapdj

pcapdj - dispatch pcap files

Network captures often result in very large files. Therefore, tools like tcpdump or dumpcap offer features of file rotation either after a fixed size or a fixed amount of time.

When these files are analyzed focusing on stateful protocols such as TCP, TCP sessions could have been established in one pcap file and continue in the next pcap files. When these TCP sessions have to be properly reassembled, then either the TCP reassembly tool has to support multiple pcap files as input or the pcap files have to merged in a single file using for instance a tool such as editcap. However, in this case, very large files are the results, that were tried to be avoided with the file rotation.

PCAPDJ processes a list of pcap files and write each individual packet in a named pipe. A second process reads these individual packets and does some processing. A third process, does some cleanup operations and controls pcapdj.

Each pcap file that is processed by pcapdj must be authorized by a third process. When a pcap file is not acknowledged the file descriptor to the named pipe is not closed and hence the other program processing pcap data does not end and keeps its internal states. Before a next file is authorized, other tasks could be done, such as removing duplicated files or already processed pcap files in order to free disk space.

Building PCAPDJ

Dependencies

In an Ubuntu 16.04 Operating system the following packages must be installed.

apt-get install libwsutil-dev libwiretap-dev libhiredis-dev libglib2.0-dev

Compiling

unzip pcapdj-master.zip

cd pcapdj-master

make

Use case with Suricata

The Suricata IDS can be configured to extract all HTTP payloads [1]. This feature is used in the following example in conjunction with pcapdj feeding suricata with multiple pcap files.

Import the pcap files that should be processed by pcapdj

#!/usr/bin/python
import redis
import os
root="mypcapfiledir"
red = redis.Redis()
files = os.listdir(root)
files.sort()
for rf in files:
    f = root + "/"+rf
    if f.endswith('pcap') == True:
    # Compressed files are supported too
    # if f.endswith('pcap.gz') == True:
        red.rpush("PCAPDJ_IN_QUEUE",f)

Create a name pipe that is shared between pcapdj and suricata

mkfifo /tmp/pcapbuffer

Launch pcapdj

 ./pcapdj -b /tmp/pcapbuffer 
redis_server = 127.0.0.1
redis_port = 6379
named pipe = /tmp/pcapbuffer
Waiting for other peer (IDS, tcp-reassembly engine, etc)...
PCAPDJ waits for the consumer of the fifo bufer. In this case suricata.

Launch suricata

suricata -r /tmp/pcapbuffer 

Until now no packets are put in the buffer because pcapdj needs an authorization. PCAPDJ says that it is ready to process the pcapfile 1.pcap and that it waits for this authorization. For doing so, pcapdj puts the next file it wants to process in a queue called PCAPDJ_NEXT and it searches for the given filename in the PCAPDJ_AUTH set. This way several pcadj processes can be managed by the same authorization script.

[INFO] Next file to process /tmp/testpcaps/1.pcap
[INFO] Waiting authorization to process file /tmp/testpcaps/2.pcap

Launch the controller script that authorizes each pcap file that is put in the named pipe.

while True:
    #Check if some data is ready to be process
    pcapname = red.lpop("PCAPDJ_NEXT")
    if pcapname != None:
        print "Authorized file ",pcapname
        red.sadd("PCAPDJ_AUTH", pcapname)

Wait until pcapdj and suricata are done

Additional Features

On the experimental branch two new features were implemented based on a signal handler.

Suspending PCAPDJ

If PCAPDJ is processing a large file and the resources are at the point of being exhausted, the command kill -SIGUSR1 can be executed. PCAPDJ stops feeding the fifo buffer and resources can be manually freed without terminating the consumer program.

Once, the machine is cleaned up, PCAPDJ can be resumed by sending a second time the SIGUSR1 signal.

Display Accounting Data

When PCAPDJ is running for a while, it might be interesting to determine what is happening. The signal SIGUSR2 can be sent to PCAPDJ. PCAPDJ shows then following information on standard output.

  • A timestamp when PCAPDJ started
  • The number of seconds elapsed since PCAPDJ started
  • The internal state of PCAPDJ
  • The number of times PCAPDJ has been suspended
  • The number of files PCAPDJ processed
  • The number of packets PCAPDJ processed
  • The sum of the cap length fields
  • The sum of the length fields. If the sum of lengths is different from the sum of cap lengths then the capture is incomplete.

An example is shown below:

[STATS] Start time:2013-06-09 09:17:50
[STATS] Uptime:322 (seconds)
[STATS] Internal state:Waiting for authorization
[STATS] Number of suspensions:0
[STATS] Number of files:1
[STATS] Number of packets:2968
[STATS] Number of cap_lengths:330581
[STATS] Number of lengths:330581

References

[1] http://blog.inliniac.net/2011/11/29/file-extraction-in-suricata/

pcapdj's People

Contributors

gallypette avatar haegardev avatar steveclement avatar wllm-rbnt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pcapdj's Issues

Send raw packets to a redis

Add a command line option to send the packets to a redis set. Hence, multiple pcapdjs producers can be run in parallel. The pcapdj consumer can consume the set and send the data to a fifo buffer, zmq feed or other redis server. This feature might be useful in conjunction with the feature described in issue #3.

Add BPF filter support

Add a command line for specifying a BPF filter. Hence a stack of pcap files can be processed and the matched packets can either put to fifo buffer or to zmq socket.

add pcapdj header

Add a header containing the following information.

  • A reference to the pcapfile
  • A counter for each packet

The pcapdj consumer can explore this header and create lists of missed packets or just compute the packet loss rate.

Compilation Broken with modern versions of libwtap

# make
gcc -Wall -c pcapdj.c `pkg-config --cflags glib-2.0` -I /usr/include/wireshark/wiretap -I /usr/include/wireshark/wsutil -I /usr/include/wireshark `pkg-config --libs glib-2.0` -I /usr/local/include/hiredis -ggdb
pcapdj.c: In function ‘process_file’:
pcapdj.c:240:20: warning: implicit declaration of function ‘wtap_phdr’; did you mean ‘wtap_dump’? [-Wimplicit-function-declaration]
             phdr = wtap_phdr(wth);
                    ^~~~~~~~~
                    wtap_dump
pcapdj.c:240:18: warning: assignment to ‘const struct wtap_pkthdr *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
             phdr = wtap_phdr(wth);
                  ^
pcapdj.c:241:19: warning: implicit declaration of function ‘wtap_buf_ptr’; did you mean ‘wtap_get_buf_ptr’? [-Wimplicit-function-declaration]
             buf = wtap_buf_ptr(wth);
                   ^~~~~~~~~~~~
                   wtap_get_buf_ptr
pcapdj.c:241:17: warning: assignment to ‘guint8 *’ {aka ‘unsigned char *’} from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
             buf = wtap_buf_ptr(wth);
                 ^
pcapdj.c:242:32: error: dereferencing pointer to incomplete type ‘const struct wtap_pkthdr’
             pchdr.caplen = phdr->caplen;
                                ^~
pcapdj.c: In function ‘init’:
pcapdj.c:313:5: error: too few arguments to function ‘wtap_init’
     wtap_init();
     ^~~~~~~~~
In file included from pcapdj.c:27:
/usr/include/wireshark/wiretap/wtap.h:1589:6: note: declared here
 void wtap_init(gboolean load_wiretap_plugins);
      ^~~~~~~~~
make: *** [Makefile:4: pcapdj.o] Error 1```

Version of libwsutil and libwiretap is 

pcapdj-master# dpkg -l | grep libwire
ii libwiretap-dev 2.6.8-1.1 amd64 network packet capture library -- development files
ii libwiretap8:amd64 2.6.8-1.1 amd64 network packet capture library -- shared library

Compiling against libwiretap-dev 1.12 fails

Greetings! First thank you for this awesome tool which is filling a need for myself very elegantly.

I have found that pcapdj will not compile on debian systems with newer libwiretap headers. If I install the headers associated with libwiretap3
(version 1.10 from http://lmde-mirror.gwendallebihan.net/latest//pool/main/w/wireshark/libwiretap-dev_1.10.3-1~bpo70+1_amd64.deb )

And add '-I /usr/include/wireshark' to the Makefile I am then able to compile.

The version currently being distributed with debian is
ii libwiretap-dev 1.12.1+g01b65bf-4+deb8u9 amd64 network packet capture library -- development files

When I try to make against that I have the following result;

make
gcc -Wall -c pcapdj.c pkg-config --cflags glib-2.0 -I /usr/include/wireshark/wiretap pkg-config --libs glib-2.0 -I /usr/local/include/hiredis -ggdb
In file included from pcapdj.c:29:0:
/usr/include/wireshark/wiretap/wtap.h:30:27: fatal error: wsutil/buffer.h: No such file or directory
#include <wsutil/buffer.h>
^
compilation terminated.
Makefile:4: recipe for target 'pcapdj.o' failed
make: *** [pcapdj.o] Error 1

If I extend the include paths to include wsutil and wireshark header directories i get

gcc -Wall -c pcapdj.c pkg-config --cflags glib-2.0 -I /usr/include/wireshark/wiretap pkg-config --libs glib-2.0 -I /usr/local/include/hiredis -ggdb -I /usr/include/wireshark/wsutil -I /usr/include/wireshark

pcapdj.c: In function ‘process_file’:
pcapdj.c:430:41: warning: passing argument 2 of ‘wtap_open_offline’ makes integer from pointer without a cast
wth = wtap_open_offline ( filename, (int*)&err, (char**)&errinfo, FALSE);
^
In file included from pcapdj.c:29:0:
/usr/include/wireshark/wiretap/wtap.h:1377:14: note: expected ‘unsigned int’ but argument is of type ‘int
struct wtap
wtap_open_offline(const char filename, unsigned int type, int err,
^
pcapdj.c:430:53: warning: passing argument 3 of ‘wtap_open_offline’ from incompatible pointer type
wth = wtap_open_offline ( filename, (int
)&err, (char
*)&errinfo, FALSE);
^
In file included from pcapdj.c:29:0:
/usr/include/wireshark/wiretap/wtap.h:1377:14: note: expected ‘int ’ but argument is of type ‘char
struct wtap
wtap_open_offline(const char filename, unsigned int type, int err,
^
pcapdj.c:430:11: error: too few arguments to function ‘wtap_open_offline’
wth = wtap_open_offline ( filename, (int
)&err, (char
)&errinfo, FALSE);
^
In file included from pcapdj.c:29:0:
/usr/include/wireshark/wiretap/wtap.h:1377:14: note: declared here
struct wtap
wtap_open_offline(const char *filename, unsigned int type, int *err,

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.