解析python中的HTTP数据包内容,如wireshark中所示

时间:2011-02-19 19:10:15

标签: python http tcp sniffing

好的,所以我要做的就是拦截一些我知道包含一些JSON数据的数据包。但HTTP数据包不是人类可读的,所以这是我的问题,我需要制作整个数据包(不仅仅是标题,已经是纯文本),人类可读。我根本没有网络经验。

import pcap
from impacket import ImpactDecoder, ImpactPacket

def print_packet(pktlen, data, timestamp):
    if not data:
        return

    decoder = ImpactDecoder.EthDecoder()
    ether = decoder.decode(data)
    iphdr = ether.child()
    tcphdr = iphdr.child()

    if iphdr.get_ip_src() == '*******':
        print tcphdr

p = pcap.pcapObject()
dev = 'wlan0'
p.open_live(dev, 1600, 0, 100)

try:
    p.setfilter('tcp', 0, 0)
    while 1:
        p.loop(1, print_packet)
except KeyboardInterrupt:
    print 'shutting down'

我找到了像libpcap-python,scapy,Impacket pcapy等工具。它们看起来都很好,但我无法弄清楚如何正确解码数据包。

Wireshark有一个名为“基于行的文本数据:text / html”的东西,它基本上显示了我所追求的信息,所以我认为用python获取相同的信息是微不足道的,事实证明它不是

1 个答案:

答案 0 :(得分:2)

HTTP和JSON都是人类可读的。在Wireshark上,选择与您的HTTP事务相关的数据包并右键单击,选择Follow TCP Stream,它应以人类可读的形式显示事务。