如何使用tcpdump捕获所有HTTP数据包

时间:2012-02-11 15:32:20

标签: bash http network-programming tcpdump

我想使用一些参数运行tcpdump(仍然不知道要使用什么),然后加载stackoverflow.com页面。

输出应该是HTTP通信。后来,我想将它用作shell脚本,所以每当我想检查站点site.com的HTTP通信时,我就可以运行script.sh site.com

HTTP通信应该足够简单。像这样:

GET /questions/9241391/how-to-capture-all-the-http-communication-data-using-tcp-dump
Host: stackoverflow.com
... 
...

HTTP/1.1 200 OK
Cache-Control: public, max-age=60
Content-Length: 35061
Content-Type: text/html; charset=utf-8
Expires: Sat, 11 Feb 2012 15:36:46 GMT
Last-Modified: Sat, 11 Feb 2012 15:35:46 GMT
Vary: *
Date: Sat, 11 Feb 2012 15:35:45 GMT


....
decoded deflated data
....

现在,我应该使用tcpdump哪些选项来捕获它?

3 个答案:

答案 0 :(得分:93)

可以通过ngrep

完成
ngrep -q -d eth1 -W byline host stackoverflow.com and port 80 
       ^  ^       ^         ^        
       |  |       |         |
       |  |       |         |
       |  |       |         v
       |  |       |         filter expression
       |  |       |         
       |  |       +-->  -W  is set the dump format ("normal", "byline", "single", "none")
       |  |
       |  +---------->  -d  is use specified device instead of the pcap default
       |
       +------------->  -q  is be quiet ("don't print packet reception hash marks")

答案 1 :(得分:19)

根据您提到的内容,ngrep(在Unix上)和Fiddler(Windows)可能是更好/更简单的解决方案。

如果您绝对想使用tcpdump,请尝试以下选项

tcpdump -A -vvv host destination_hostname

-A (ascii)
-vvv (verbose output)

答案 2 :(得分:0)

tcpdump -i eth0 -w dump3.pcap -v  'tcp and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)'

请参阅http://www.tcpdump.org/manpages/tcpdump.1.html