from scapy.all import * import re def packet_callback(packet): # 过滤MySQL流量 if packet.haslayer(TCP) and packet.haslayer(Raw) and packet[TCP].dport == 3306: # 解码MySQL协议 payload = str(packet[TCP].payload, 'utf-8', errors='ignore') if re.search(r'(\bUNION\b|\bSELECT\b.*\bFROM\b)', payload, re.IGNORECASE): # 检测到SQL注入攻击 print('[!] SQL Injection detected from {}:{}'.format(packet[IP].src, packet[TCP].sport)) # 开始抓包 sniff(filter='tcp port 3306', prn=packet_callback)代码中没有引用TCP和IP
时间: 2023-12-26 19:06:22 浏览: 36
这是因为 Scapy 库中的 Packet 类已经自动解析了 TCP 和 IP 层,可以在 packet[TCP] 和 packet[IP] 中直接访问它们的字段。因此,我们可以直接使用 packet.haslayer(TCP) 和 packet.haslayer(IP) 来检查是否存在 TCP 和 IP 层。同时,payload 字段也可以通过 packet[TCP].payload 直接访问。
相关问题
from scapy.all import * # 定义过滤器,只抓取TCP端口为80(HTTP端口)的包 filter_str = "tcp port 80" # 开始抓包 sniff(filter=filter_str, prn=lambda x: x.summary()) 获取的数据打印出来
使用Scapy抓取到的数据包可以通过回调函数进行处理,也可以存储到文件中进行后续分析。下面是一个简单的示例,将抓取到的数据包的源IP地址、目的IP地址和协议类型存储到文件中。
```python
from scapy.all import *
# 定义过滤器,只抓取TCP端口为80(HTTP端口)的包
filter_str = "tcp port 80"
# 定义回调函数,将抓取到的数据包的源IP地址、目的IP地址和协议类型存储到文件中
def packet_callback(packet):
src_ip = packet[IP].src
dst_ip = packet[IP].dst
proto = packet[IP].proto
with open('packet_info.txt', 'a') as f:
f.write(f'src_ip: {src_ip}, dst_ip: {dst_ip}, proto: {proto}\n')
# 开始抓包
sniff(filter=filter_str, prn=packet_callback)
```
在这个程序中,我们定义了一个回调函数packet_callback,它接收一个数据包作为参数,提取数据包的源IP地址、目的IP地址和协议类型,并将这些信息存储到文件中。程序运行时,所有抓取到的数据包都会被传递给这个回调函数进行处理。
import sys import os from scapy.all import * from scapy.layers.dot11 import Dot11 # 设置网络接口为监视模式的函数 def set_monitor_mode(iface): try: comm = subprocess.Popen(['sudo','ifconfig',iface,'down'],stdout=subprocess.PIPE,stderr=subprocess.PIPE) output,error = comm.communicate() comm = subprocess.Popen(['sudo','iwconfig',iface,'mode', 'monitor'], stdout=subprocess.PIPE,stderr=subprocess.PIPE) output, error = comm.communicate() comm = subprocess.Popen(['sudo', 'ifconfig',iface,'up'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) output, error = comm.communicate() except Exception as e: print("Error: {0}".format(e)) sys.exit(1) # 定义处理数据包的回调函数 def packet_callback(packet, q): # 过滤数据包中的MAC地址 if packet.haslayer(Dot11): if packet.addr2 and (packet.addr2.lower() != 'ff:ff:ff:ff:ff:ff'): # 将连接的MAC地址存入队列 q.put(packet.addr2) # 处理数据包的线程 def process_packets(q): while True: if not q.empty(): # 输出连接的MAC地址 print("Device connected: %s" % q.get()) # 主程序 if name == 'main': # 设置网络接口为监视模式 set_monitor_mode('wlan0') # 创建队列对象 q = Queue() # 创建并启动处理数据包的线程 t = Thread(target=process_packets, args=(q,)) t.daemon = True t.start() # 开始抓取数据包 sniff(prn=lambda packet: packet_callback(packet, q), iface='wlan0', filter='arp')如何在window系统下实现
这段代码需要在 Linux 操作系统下运行,因为它使用了一些 Linux 下的命令和库。在 Windows 系统下,可以考虑使用 Python 的 winpcapy 库来实现类似的功能。以下是修改后的代码:
```python
import sys
import os
from queue import Queue
from threading import Thread
from scapy.all import *
from scapy.layers.dot11 import Dot11
# 设置网络接口为监视模式的函数
def set_monitor_mode(iface):
try:
comm = subprocess.Popen(['netsh', 'interface', 'set', 'interface', iface, 'admin=disable'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = comm.communicate()
comm = subprocess.Popen(['netsh', 'interface', 'set', 'interface', iface, 'admin=enable'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = comm.communicate()
except Exception as e:
print("Error: {0}".format(e))
sys.exit(1)
# 定义处理数据包的回调函数
def packet_callback(packet, q):
# 过滤数据包中的MAC地址
if packet.haslayer(Dot11):
if packet.addr2 and (packet.addr2.lower() != 'ff:ff:ff:ff:ff:ff'):
# 将连接的MAC地址存入队列
q.put(packet.addr2)
# 处理数据包的线程
def process_packets(q):
while True:
if not q.empty():
# 输出连接的MAC地址
print("Device connected: %s" % q.get())
# 主程序
if __name__ == '__main__':
# 设置网络接口为监视模式
set_monitor_mode('Wi-Fi')
# 创建队列对象
q = Queue()
# 创建并启动处理数据包的线程
t = Thread(target=process_packets, args=(q,))
t.daemon = True
t.start()
# 开始抓取数据包
sniff(prn=lambda packet: packet_callback(packet, q), iface='Wi-Fi', filter='arp')
```
需要注意的是,这段代码需要在 Windows 操作系统下运行,并且需要使用管理员权限来执行。另外,需要安装 winpcapy 库来支持嗅探网络数据包。