9

Is there a way to do

tcpdump -i lo -A 

and have it print all urls, any connections made?

I have done:

sudo tcpdump -i lo -A | grep Host: 

which works great. But I was wondering if there are options to do the same in tcpdump

Finally, is there a way to do this in python without using a sys command or Popen/subprocess

Cripto
  • 3,581
  • 7
  • 41
  • 65
  • tcpdump cannot filter using the content of the packages, you could improve your performance by only dumping those packages for `incoming TCP connections to your HTTP port`. – Dennis Guse Jul 26 '13 at 11:16
  • Also, as HTTP doesn't transfer the URL requested in a straightforward manner, it will be slightly harder to pick out of a tcpdump feed - you have to combine the `Host` header and the `GET` or `POST` line to get the full URL... – Stobor Jul 27 '13 at 12:04

3 Answers3

4

tcpdump cannot filter based upon the content of the packets (no deep packet inspection) as it only uses pcacp-filter. You could improve your performance by only dumping those packages for incoming TCP connections to your HTTP port.

tcpdump -i lo -A tcp port 80

TCPDUMP python: use Pcapy

Another option is to use tshark

Community
  • 1
  • 1
Dennis Guse
  • 883
  • 10
  • 34
  • "tcpdump cannot filter based upon the content of the packets (no deep packet inspection)". Using `-vv` gets you pretty close. – The Onin May 22 '17 at 21:01
3

you can use scapy the sniff function and use regex or grep

import scapy
tcpdump = sniff(count=5,filter="host 64.233.167.99",prn=lambda x:x.summary())
print tcpdump

change the filter for your filter text :)

or maybe you want to save the traffic and see it in wireshark

wrpcap("temp.cap",pkts)
raf10x
  • 66
  • 5
  • Is it possible to sniff for a period of time and not a given count.(start sniff) do stuff. (stop sniff) Also, it is important that we record everything on interface lo. – Cripto Jul 22 '13 at 13:09
1

What you want to use is libpcap which is the packet capture library which tcpdump uses. There is a python wrapper for this which can be found here.

You can, in python, then build any filtering that you want on top of the filtering already provided by pcap/tcpdump. Then display this filtered output (or whatever it is you want to do in your python script).

dave
  • 4,812
  • 4
  • 25
  • 38