python - 如何将 pyshark 数据包发送到特定的网络接口?
问题描述
我可以.pcap
使用pyshark
. 这是我的代码:
import pyshark
cap = pyshark.FileCapture(pcap_dir) # pcap_dir is the directory of my pcap file
print(cap[0]) # Print a packet
print(cap[0]['IP'].src) # Print some header value
现在,我需要将此数据包发送到某个接口(例如eth0
)。我尝试了以下方法:
from socket import socket, AF_PACKET, SOCK_RAW
sock = socket(AF_PACKET, SOCK_RAW)
sock.bind(('eth0', 0))
sock.send(cap[0])
但我得到了错误:
sock.send(cap[0])
TypeError: a bytes-like object is required, not 'Packet'
任何人都可以帮忙吗?
解决方案
我能够解决我的问题。这是解释:
- 使用两者
use_json=True
并include_raw=True
能够获取原始数据包数据。 - 如果您使用
print(cap[0])
打印第一个数据包,您应该看到一个带有 name 的层FRAME_RAW
。这是包含整个数据包原始数据的层。
编码:
import pyshark
from socket import socket, AF_PACKET, SOCK_RAW
cap = pyshark.FileCapture(
input_file=pcap_dir, # pcap_dir the pcap file directory
use_json=True,
include_raw=True
)._packets_from_tshark_sync()
sock = socket(AF_PACKET, SOCK_RAW)
sock.bind(('YOUR_NETWORK_INTERFACE_NAME', 0))
sock.send(bytearray.fromhex(cap.__next__().frame_raw.value)) # 1st packet in the pcap file
sock.send(bytearray.fromhex(cap.__next__().frame_raw.value)) # 2nd packet in the pcap file
# And so on until the end of the file ...
推荐阅读
- javascript - 如何在 ember 表单字段上禁用自动填充
- php - PHP:以字符串形式插入日期变为-2008
- ios - 更新 NativeScript 插件 nativescript-local-notifications 以支持图像
- apache-spark - 使用 concat_ws 时排除空列
- python - Python从json文件中获取数据
- google-cloud-firestore - 使用 angularfire2 获取单个文档的最简单方法
- c++11 - 迭代差异
- bash - "$(cat file)", "$(
我有一个只包含一行的输入文件:
$ cat input foo bar
我想在我的脚本中使用这一行,我知道有 3 种方法可以得到它:
line=$(ca
- javascript - 为什么一个单一的 key:value 对可能在对象中未定义,而其他对是可访问的?
- docker - 无法使用 JFrog Artifactory 设置 Docker 注册表 - Docker 登录失败