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

41

我想用一些参数(还不知道要使用哪些)运行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一起捕获它?


2
看起来你需要的是 "curl -v site.com"。 :) - Mingjiang Shi
3个回答

99
可以通过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
注意:对于在OS X 10.9.x / Mavericks上通过brew安装的ngrep 1.45的用户,它可能会导致“分段错误:11”。 jfarcand在这里找到了一个解决方法:https://gist.github.com/jfarcand/8302675,对我有效:`ngrep -q -W byline -d en0'' 'host some_hostname and port 80'`。 - Gary S. Weaver
11
ASCII 艺术点赞。 - neevek
主机在HTTP中不是主机,而在TCP中只是IP。 - Donghua Liu
有没有生成这种ASCII文档的工具呢?看起来很酷! - Somebody

20
根据你提到的内容,ngrep (Unix系统)和Fiddler (Windows系统)可能是更好/更容易的解决方案。如果您坚持要使用tcpdump,请尝试以下选项:
tcpdump -A -vvv host 目标主机名

-A (ascii)
-vvv (详细输出)

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接