使用 Scapy 过滤 HTTP 数据包
Using Scapy to fitler HTTP packets
我正在尝试为包含 HTTP 数据的数据包创建过滤器,但我不知道如何操作。
即有没有办法使用 Scapy 过滤仅 HTTP 的数据包?
是的,你可以。您可以通过 TCP 端口 80 进行过滤(检查每个数据包或使用 BPF),然后检查 TCP 负载以确保存在 HTTP header.
是的,有 .haslayer 函数和一些解析:
methods=['GET','POST','HEAD','PUT','DELETE','CONNECT','OPTIONS','TRACE']#Define http methods
s=sniff(1)#sniff one packet to parse you can put this in a loop
a=[]
a.append(s[0])
if a[0].haslayer(TCP):#Checks for TCP protocol
if a[0].dport == 80:#Checks for http port 80
if a[0].haslayer(Raw):#Checks if packet has payload
r=a[0][0][Raw].load
for i in methods:#Checks if any of the http methods are present in load, if there are it prints to screen
if i in r:
print r
其他答案为您提供了一个非常准确的解决方案,因为您可以在 80 以外的其他端口使用 HTTP,和 对于版本 2.4.3 scapy 团队已经发布了一个新的 HTTP layer,所以我们不必再依赖那些假设了:
>>> import scapy.all as S
>>> S.load_layer("http")
>>> HTTPRequest
<class 'scapy.layers.http.HTTPRequest'>
>>> def filter_get_requests(pkg):
return pkg.haslayer(HTTPRequest) and pkg[HTTPRequest].Method==b'GET'
>>> s = S.sniff(lfilter=filter_get_requests)
然后向您最喜欢的 HTTP 站点发出 GET 请求,就可以了 :)
您可以阅读整个 HTTP 层文档 in here.
我正在尝试为包含 HTTP 数据的数据包创建过滤器,但我不知道如何操作。
即有没有办法使用 Scapy 过滤仅 HTTP 的数据包?
是的,你可以。您可以通过 TCP 端口 80 进行过滤(检查每个数据包或使用 BPF),然后检查 TCP 负载以确保存在 HTTP header.
是的,有 .haslayer 函数和一些解析:
methods=['GET','POST','HEAD','PUT','DELETE','CONNECT','OPTIONS','TRACE']#Define http methods
s=sniff(1)#sniff one packet to parse you can put this in a loop
a=[]
a.append(s[0])
if a[0].haslayer(TCP):#Checks for TCP protocol
if a[0].dport == 80:#Checks for http port 80
if a[0].haslayer(Raw):#Checks if packet has payload
r=a[0][0][Raw].load
for i in methods:#Checks if any of the http methods are present in load, if there are it prints to screen
if i in r:
print r
其他答案为您提供了一个非常准确的解决方案,因为您可以在 80 以外的其他端口使用 HTTP,和 对于版本 2.4.3 scapy 团队已经发布了一个新的 HTTP layer,所以我们不必再依赖那些假设了:
>>> import scapy.all as S
>>> S.load_layer("http")
>>> HTTPRequest
<class 'scapy.layers.http.HTTPRequest'>
>>> def filter_get_requests(pkg):
return pkg.haslayer(HTTPRequest) and pkg[HTTPRequest].Method==b'GET'
>>> s = S.sniff(lfilter=filter_get_requests)
然后向您最喜欢的 HTTP 站点发出 GET 请求,就可以了 :) 您可以阅读整个 HTTP 层文档 in here.