想实现一个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)
参考资料: