我的目标是读取我的 HTTP 请求的响应、它们的数据、标头以及它们附带的所有内容。我想获得可读的数据,类似于在 Chrome 开发者工具中打开网络选项卡时可以读取的数据。
我尝试使用此代码:
from scapy.all import *
def process_packet(packet):
if packet.haslayer(TCP):
# Process TCP packets
src_ip = packet[IP].src
src_port = packet[TCP].sport
dst_ip = packet[IP].dst
dst_port = packet[TCP].dport
if packet.haslayer(Raw):
payload = packet[Raw].load
print(f"Source IP: {src_ip}")
print(f"Source Port: {src_port}")
print(f"Destination IP: {dst_ip}")
print(f"Destination Port: {dst_port}")
print(f"Raw Payload: {payload}")
print()
# Start sniffing network packets
sniff(filter="tcp", prn=process_packet)
这是我得到的典型结果:
Source IP: xxx.xxx.xxx.xxx
Source Port: xxxxx
Destination IP: xxx.xxx.xxx.xxx
Destination Port: xxxxx
Raw Payload: b'\x17\x03\x03\x00"\xdc\xfcK\xa5\xb3N\xdfv\xf3$\x06\xeb\n\\V\x8c2\x0cM\xa2^\x02b\x8e\'=\xfc\xe6_%\x1b\x03\t\xe6'
有效负载通常更长,但我选择了这个,这样它就不会占据整个屏幕:)。 我的问题是,我现在如何将我得到的原始有效负载解码为可读的内容?我真的迷失了。
提前致谢!
我尝试使用此代码,并尝试使用各种解码器对其进行解码,但我从未得到任何有用的东西。
编辑:
这是我在 Scapys 文档中找到的内容,我认为这就是我正在寻找的所需结果,但我仍然无法得到它。
当我尝试运行上图中的命令时,我得到的结果与我的代码中的结果相同,而不是图中的结果。
这段代码帮助我获取了HTTP请求体
packets = rdpcap('http_traffic.pcap')
for packet in packets:
if packet.haslayer('Raw') and packet.haslayer('HTTP'):
http_payload = packet['Raw'].load
print(http_payload)