Python scapy无法在非标准端口上获取HTTP请求
我正在尝试获取所有HTTP get/POST传入请求 我发现这段代码似乎很有前途,但我注意到它只适用于标准HTTP端口。如果我使用另一个端口(比如8080),我可能找不到HTTP层(packet.haslayer(HTTP.HTTPRequest)==False) 代码如下:Python scapy无法在非标准端口上获取HTTP请求,python,scapy,packet-sniffers,Python,Scapy,Packet Sniffers,我正在尝试获取所有HTTP get/POST传入请求 我发现这段代码似乎很有前途,但我注意到它只适用于标准HTTP端口。如果我使用另一个端口(比如8080),我可能找不到HTTP层(packet.haslayer(HTTP.HTTPRequest)==False) 代码如下: from scapy.all import IP, sniff from scapy.layers import http def process_tcp_packet(packet): ''' Pro
from scapy.all import IP, sniff
from scapy.layers import http
def process_tcp_packet(packet):
'''
Processes a TCP packet, and if it contains an HTTP request, it prints it.
'''
if not packet.haslayer(http.HTTPRequest):
# This packet doesn't contain an HTTP request so we skip it
return
http_layer = packet.getlayer(http.HTTPRequest)
ip_layer = packet.getlayer(IP)
print '\n{0[src]} just requested a {1[Method]} {1[Host]}{1[Path]}'.format(ip_layer.fields, http_layer.fields)
# Start sniffing the network.
sniff(filter='tcp', prn=process_tcp_packet)
知道我做错了什么吗
****更新****
我去掉了scapy_http,只是查看了原始数据。
我在这里发布了我正在使用的代码——它对我来说很好,因为我正在调试Apache Solr上遇到的一个奇怪问题——但是您的里程可能会有所不同
def process_tcp_packet(packet):
msg = list()
try:
if packet.dport == 8983 and packet.haslayer(Raw):
lines = packet.getlayer(Raw).load.split(os.linesep)
# GET or POST requests ?
if lines[0].lower().startswith('get /') or lines[0].lower().startswith('post /'):
# request forwarded by a proxy ?
_ = [line.split()[1] for line in lines if line.startswith('X-Forwarded-For:')]
s_ip = (_[0] if _ else packet.getlayer(IP).src)
# collect info
d_port = packet.getlayer(IP).dport
now = datetime.datetime.now()
msg.append('%s: %s > :%i -> %s' % (now, s_ip, d_port, lines[0]))
except Exception, e:
msg.append('%s: ERROR! %s' % (datetime.datetime.now(), str(e)))
pass
with open('http.log', 'a') as out:
for m in msg:
out.write(m + os.linesep)
您使用的是哪个
scapy
http实现http
不是标准scapy
library的一部分谢谢您的回复。我使用“pip install scapy_http”安装了它,但最后我使用了不同的方法(我更新了问题)。您使用的是哪个scapy
http实现http
不是标准scapy
library的一部分谢谢您的回复。我使用“pip install scapy_http”安装了它,但是,最后我使用了不同的方法(我更新了问题)。