首页 新闻 会员 周边 捐助

有朋友使用过pypcap吗,遇到了问题,希望高人指点

0
[待解决问题]

想实现一个python代码版抓包工具,了解到pypcap可以用于网络请求抓包,
想获取请求体参数,请求响应数据
有点不知道怎么做,希望有经验的朋友看一下

下面是我找到的demo和资料

import pcap
# list all of the Internet devices
devs = pcap.findalldevs()
print(*devs, sep='n')
pc = pcap.pcap(devs[3], promisc=True, immediate=True, timeout_ms=50)
# fiter http pcakets
pc.setfilter('tcp port 80')
for ptime, pdata in pc:
    print(ptime, pdata)
    # TODO 如何解析想要的数据????
import pcap
import dpkt
import socket
pc = pcap.pcap()
for ptime, pdata in pc:
    # print(f'ptime:{ptime}')
    # print(f'pdata:{pdata}')
    eth = dpkt.ethernet.Ethernet(pdata)
    # print(f'eth:{eth}')
    if eth.data.__class__.__name__ == 'IP':
        ip = eth.data
        src = socket.inet_ntoa(ip.src)
        dst = socket.inet_ntoa(ip.dst)
        tcp = ip.data  # tcp.dport  tcp.sport
        try:
            request = dpkt.http.Request(tcp.data)
        except (dpkt.dpkt.NeedData, dpkt.dpkt.UnpackError):
            continue
        if request.headers['host'] == 'www.baidu.com':  # 自行替换需要抓的网站。
            # print(request)
            host = request.headers['host']
            connection = request.headers['connection']
            # accept = request.headers['accept']
            user_agent = request.headers['user-agent']
            accept_encoding = request.headers['accept-encoding']
            accept_language = request.headers['accept-language']
            cookie = request.headers['cookie']
            # break
            print('-----------')
            print('host->%s' % host)
            print('connection->%s' % connection)
            # print('accept->%s' % accept)
            print('user_agent->%s' % user_agent)
            print('accept_encoding->%s' % accept_encoding)
            print('accept_language->%s' % accept_language)
            print('cookie->%s' % cookie)
 

参考资料:

Mr_xiaoH的主页 Mr_xiaoH | 菜鸟二级 | 园豆:202
提问于:2021-07-28 20:05
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册