It's a ruby library to help tcpdump file processing : do some offline analysis on tcpdump files.
Main functionnalities :
- Rebuild tcp streams
- Extract and parse http request
tcpdump -w out.pcap -s 4096 <filter>
- Get the output file out.pcap
Please adjust the 4096 value, to the max packet size to capture.
require 'pcap_tools'
# Load tcpdump file
capture = Pcap::Capture.open_offline('out.pcap')
This function rebuild tcp streams from an array of pcap capture object.
tcp_streams = PcapTools::extract_tcp_streams(captures)
tcp_streams
is an array of hash, each hash has tree keys :
:type
::in
or:out
, if the packet was sent or received:time
: timestamp of packet:data
: payload of packet
Remarks :
- Packets are in the rigth ordere
- Packets are not merged (eg an http response can be splitted on serval consecutive packets,
with the same type
:in
or:out
). To reassemble packet of the same type, please usestream.rebuild_packets
This function extract http calls from a tcp stream, returned from the extract_tcp_streams
function.
http_calls = PcapTools::extract_http_calls(stream)
http_calls
is an array of http_call
.
A http_call
is an array of two objects :
- The http request, an instance of
Net::HTTPRequest
, egNet::HTTPGet
orNet::HTTPPost
. You can use this object like any http request of std libnet/http
req.path
: get the request pathreq['User-Agent']
: get the User-Agentreq.body
: get the request body- ...
- The http response, an instance of
Net::HTTPResponse
, egNet::HTTPOk
orNet::HTTPMovedPermanently
. You can use this object like any http response of std libnet/http
resp.code
: get the http return coderesp['User-Agent']
: get the User-Agentresp.body
: get the request body- ...
The response can be nil
if there is no response in the tcp stream.
The request and response object have some new attributes
req.time
: get the time where the request or response was captured
For the response object body, the following "Content-Encoding" type are honored :
- gzip
The two in one : extract http calls from an array of captures objects
http_calls = PcapTools::extract_http_calls_from_captures(captures)
Load multiple pcap files, in time order. Useful when you use tcpdump -C 5 -W 100000
, to split captured data into pieces of 5M
captures = PcapTools::load_mutliple_files '*pcap*'