为什么我不能同时使用 dpkt.http.Response 和 dpkt.http.Request?

问题描述 投票:0回答:0

我正在创建一个工具来捕获 python 中的数据包。 我想一起使用 pkt.http.Response 和 dpkt.http.Request。 目的是检测错误的数据包并从请求到响应的 pacap 中获取信息。

但是,响应不起作用。

请告诉我为什么 如果这是错误的方法,请告诉我正确的方法。 提前致谢

import pcap
import re
import dpkt

nonErrorCode=['200']

sniffer = pcap.pcap(name='cali7f35c4176d5@if3',promisc=True,immediate=True,timeout_ms=50)
sniffer.setfilter('tcp and port 80') # set packet filter

for t, p in sniffer:
    eth = dpkt.ethernet.Ethernet(p)
    ip = eth.data
    tcp = ip.data

    try:
        if len(tcp.data) > 0:
            if 80 == tcp.dport:
                print(f'request : {tcp.data}')
                req = dpkt.http.Request(tcp.data)
                print(f'req : {repr(req)}')

            if 80 == tcp.sport:
                print(f'response : {tcp.data}')
                res = dpkt.http.Response(tcp.data)
                print(f'res : {repr(res)}')
            
    except:
        pass
    

结果

request : b'GET / HTTP/1.1\r\nHost: 10.80.69.40:30003\r\nUser-Agent: curl/7.68.0\r\nAccept: */*\r\n\r\n'
req : Request(version='1.1', method='GET', uri='/', headers=OrderedDict([('host', '10.80.69.40:30003'), ('user-agent', 'curl/7.68.0'), ('accept', '*/*')]), body=b'', data=b'')
response : b'HTTP/1.1 200 OK\r\nServer: nginx/1.23.4\r\nDate: Thu, 18 May 2023 02:04:27 GMT\r\nContent-Type: text/html\r\nContent-Length: 615\r\nLast-Modified: Tue, 28 Mar 2023 15:01:54 GMT\r\nConnection: keep-alive\r\nETag: "64230162-267"\r\nAccept-Ranges: bytes\r\n\r\n'
response : b'<!DOCTYPE html>\n<html>\n<head>\n<title>Welcome to nginx!</title>\n<style>\nhtml { color-scheme: light dark; }\nbody { width: 35em; margin: 0 auto;\nfont-family: Tahoma, Verdana, Arial, sans-serif; }\n</style>\n</head>\n<body>\n<h1>Welcome to nginx!</h1>\n<p>If you see this page, the nginx web server is successfully installed and\nworking. Further configuration is required.</p>\n\n<p>For online documentation and support please refer to\n<a href="http://nginx.org/">nginx.org</a>.<br/>\nCommercial support is available at\n<a href="http://nginx.com/">nginx.com</a>.</p>\n\n<p><em>Thank you for using nginx.</em></p>\n</body>\n</html>\n'

没有'res : ~~ '

python pcap dpkt
© www.soinside.com 2019 - 2024. All rights reserved.