Search code examples
httptcpscapypcaptcpdump

Extract TCP payload from pcap file


Using tcpdump, I am capturing network traffic. I am interested in extracting the actual TCP payload data, i.e. HTTP traffic in my particular case.

I tried to achieve that using scapy, but I only found function remove_payload(). Is there a corresponding counterpart? Or do you know of any other tools that provide such functionality?

Unfortunately, I did not find a satisfactory scapy documentation.


Solution

  • In case other users might have similar questions: I ended up using the following script:

    infile=infile.pcap
    outfile=outfile
    ext=txt
    
    rm -f ${outfile}_all.${ext}
    
    for stream in $(tshark -nlr $infile -Y tcp.flags.syn==1 -T fields -e tcp.stream | sort -n | uniq | sed 's/\r//')
    do
        echo "Processing stream $stream: ${outfile}_${stream}.${ext}"
        tshark -nlr $infile -qz "follow,tcp,raw,$stream" | tail -n +7 | sed 's/^\s\+//g' | xxd -r -p | tee ${outfile}_${stream}.${ext} >> ${outfile}_all.${ext}
    done