C / Python WinPCap 翻译
C / Python WinPCap Translation
抱歉,这对你们大多数人来说都是一个非常简单的问题。
我正在尝试将一些代码从 here 转换为 Python (3.4),因为我不懂 C。这主要是这个问题的基础。虽然我根据 link:
创建了 Python 数据结构
from ctypes import *
u_short = c_ushort
u_char = c_ubyte
u_int = c_int
class ip_address(Structure):
_fields=[("byte1",u_char),
("byte2",u_char),
("byte3",u_char),
("byte4",u_char)]
class ip_header(Structure):
_fields=[("ver_ihl",u_char),
("tos",u_char),
("tlen",u_short),
("identification",u_short),
("flags_fo",u_short),
("ttl",u_char),
("proto",u_char),
("crc",u_short),
("saddr",ip_address),
("daddr",ip_address),
("op_pad",u_int)]
class udp_header(Structure):
_fields=[("sport",u_short),
("dport",u_short),
("len",u_short),
("crc",u_short)]
class data(Structure):
_fields[("data",c_char_p)]
我对 C 的了解还不够,无法在 Python 中转换以下代码:
ip_header *ih;
udp_header *uh;
u_int ip_len;
u_short sport,dport;
/* retrieve the position of the ip header */
ih = (ip_header *) (pkt_data +
14); //length of ethernet header
/* retrieve the position of the udp header */
ip_len = (ih->ver_ihl & 0xf) * 4;
uh = (udp_header *) ((u_char*)ih + ip_len);
/* convert from network byte order to host byte order */
sport = ntohs( uh->sport );
dport = ntohs( uh->dport );
/* print ip addresses and udp ports */
printf("%d.%d.%d.%d.%d -> %d.%d.%d.%d.%d\n",
ih->saddr.byte1,
ih->saddr.byte2,
ih->saddr.byte3,
ih->saddr.byte4,
sport,
ih->daddr.byte1,
ih->daddr.byte2,
ih->daddr.byte3,
ih->daddr.byte4,
dport);
我知道我需要将 "ih->ver_ihl" 更改为 "ih.ver_ihl" 但是我不明白其余部分。如果我正确理解 UDP 数据包,数据包的下一部分将是数据。如果可能的话,我也想从数据包中提取它。
提前感谢您提供的任何帮助
一些注意事项:
- 对于 ctypes 结构,您必须使用
_fields_
而不是 _fields
。
- 根据经验,我倾向于在我的代码中使用 Flake8,即使在代码片段中也是如此。
测试
我在 NTP 数据包上测试了我的代码,因为 NTP 在 UDP 之上,以查看是否一切正确。
- 转到Wireshark wiki: NTP
- 下载第一个示例捕获 (SampleCaptures/NTP_sync.pcap)
- 将 pcap 加载到 wireshark > select 第一个数据包 > 转到 "View " >
确保检查 "Packet details"。
- 单击数据包详细信息 window,第 1 帧,(应显示为 "Frame 1: 75 bytes on wire (600 bits), 75 bytes captured (600 bits)" > 右键单击 > "export selected packet bytes"。
将保存的文件传递给以下脚本。
输出
192.168.50.50:1026 -> 192.168.0.1:53
根据 wireshark,这是正确的。
代码
#!/usr/local/bin/python3
# -*- coding: utf8 -*-
import sys
import ctypes
import socket
from ctypes import *
u_short = c_ushort
u_char = c_ubyte
u_int = c_int
class ip_address(Structure):
_fields_ = [("byte1", u_char),
("byte2", u_char),
("byte3", u_char),
("byte4", u_char)]
class ip_header(BigEndianStructure):
_fields_ = [("ver_ihl", u_char),
("tos", u_char),
("tlen", u_short),
("identification", u_short),
("flags_fo", u_short),
("ttl", u_char),
("proto", u_char),
("crc", u_short),
("saddr", ip_address),
("daddr", ip_address),
("op_pad", u_int)]
class udp_header(BigEndianStructure):
_fields_ = [("sport", u_short),
("dport", u_short),
("len", u_short),
("crc", u_short)]
def packet_handler(pkt_data):
# cast pkt_data to void so we can do some pointer arithmetic
v_pkt_data = ctypes.cast(pkt_data, ctypes.c_void_p)
# retrieve the position of the ip header
v_ip_header = ctypes.c_void_p(v_pkt_data.value + 14)
pih = ctypes.cast(v_ip_header, ctypes.POINTER(ip_header))
ih = pih.contents
# retrieve the position of the udp header
ip_len = (ih.ver_ihl & 0xf) * 4
uh = ctypes.cast(ctypes.cast(pih, ctypes.c_void_p).value + ip_len,
ctypes.POINTER(udp_header)).contents
# convert from network byte order to host byte order
sport = socket.ntohs(uh.sport)
dport = socket.ntohs(uh.dport)
print("{}.{}.{}.{}:{} -> {}.{}.{}.{}:{}".format(
ih.saddr.byte1, ih.saddr.byte2, ih.saddr.byte3, ih.saddr.byte4, sport,
ih.daddr.byte1, ih.daddr.byte2, ih.daddr.byte3, ih.daddr.byte4, dport))
# Extracting data
#
# data offset from ip header start
data_offset = ip_len + ctypes.sizeof(udp_header)
# data length
data_len = ih.tlen - ip_len - ctypes.sizeof(udp_header)
# get data
arr_type = (ctypes.c_uint8 * data_len)
data = arr_type.from_address(v_ip_header.value + data_offset)
print(data[0:data_len])
# note: same thing could be achieved from pkt_data
print(pkt_data[14+data_offset:14+data_offset+data_len])
def main(packet_file):
with open(packet_file, "rb") as fd:
data = fd.read()
# create ctypes.c_char_array
pkt_data = ctypes.c_buffer(data)
packet_handler(pkt_data)
if __name__ == '__main__':
if len(sys.argv) == 2:
main(sys.argv[1])
抱歉,这对你们大多数人来说都是一个非常简单的问题。
我正在尝试将一些代码从 here 转换为 Python (3.4),因为我不懂 C。这主要是这个问题的基础。虽然我根据 link:
创建了 Python 数据结构from ctypes import *
u_short = c_ushort
u_char = c_ubyte
u_int = c_int
class ip_address(Structure):
_fields=[("byte1",u_char),
("byte2",u_char),
("byte3",u_char),
("byte4",u_char)]
class ip_header(Structure):
_fields=[("ver_ihl",u_char),
("tos",u_char),
("tlen",u_short),
("identification",u_short),
("flags_fo",u_short),
("ttl",u_char),
("proto",u_char),
("crc",u_short),
("saddr",ip_address),
("daddr",ip_address),
("op_pad",u_int)]
class udp_header(Structure):
_fields=[("sport",u_short),
("dport",u_short),
("len",u_short),
("crc",u_short)]
class data(Structure):
_fields[("data",c_char_p)]
我对 C 的了解还不够,无法在 Python 中转换以下代码:
ip_header *ih;
udp_header *uh;
u_int ip_len;
u_short sport,dport;
/* retrieve the position of the ip header */
ih = (ip_header *) (pkt_data +
14); //length of ethernet header
/* retrieve the position of the udp header */
ip_len = (ih->ver_ihl & 0xf) * 4;
uh = (udp_header *) ((u_char*)ih + ip_len);
/* convert from network byte order to host byte order */
sport = ntohs( uh->sport );
dport = ntohs( uh->dport );
/* print ip addresses and udp ports */
printf("%d.%d.%d.%d.%d -> %d.%d.%d.%d.%d\n",
ih->saddr.byte1,
ih->saddr.byte2,
ih->saddr.byte3,
ih->saddr.byte4,
sport,
ih->daddr.byte1,
ih->daddr.byte2,
ih->daddr.byte3,
ih->daddr.byte4,
dport);
我知道我需要将 "ih->ver_ihl" 更改为 "ih.ver_ihl" 但是我不明白其余部分。如果我正确理解 UDP 数据包,数据包的下一部分将是数据。如果可能的话,我也想从数据包中提取它。
提前感谢您提供的任何帮助
一些注意事项:
- 对于 ctypes 结构,您必须使用
_fields_
而不是_fields
。 - 根据经验,我倾向于在我的代码中使用 Flake8,即使在代码片段中也是如此。
测试
我在 NTP 数据包上测试了我的代码,因为 NTP 在 UDP 之上,以查看是否一切正确。
- 转到Wireshark wiki: NTP
- 下载第一个示例捕获 (SampleCaptures/NTP_sync.pcap)
- 将 pcap 加载到 wireshark > select 第一个数据包 > 转到 "View " > 确保检查 "Packet details"。
- 单击数据包详细信息 window,第 1 帧,(应显示为 "Frame 1: 75 bytes on wire (600 bits), 75 bytes captured (600 bits)" > 右键单击 > "export selected packet bytes"。
将保存的文件传递给以下脚本。
输出
192.168.50.50:1026 -> 192.168.0.1:53
根据 wireshark,这是正确的。
代码
#!/usr/local/bin/python3
# -*- coding: utf8 -*-
import sys
import ctypes
import socket
from ctypes import *
u_short = c_ushort
u_char = c_ubyte
u_int = c_int
class ip_address(Structure):
_fields_ = [("byte1", u_char),
("byte2", u_char),
("byte3", u_char),
("byte4", u_char)]
class ip_header(BigEndianStructure):
_fields_ = [("ver_ihl", u_char),
("tos", u_char),
("tlen", u_short),
("identification", u_short),
("flags_fo", u_short),
("ttl", u_char),
("proto", u_char),
("crc", u_short),
("saddr", ip_address),
("daddr", ip_address),
("op_pad", u_int)]
class udp_header(BigEndianStructure):
_fields_ = [("sport", u_short),
("dport", u_short),
("len", u_short),
("crc", u_short)]
def packet_handler(pkt_data):
# cast pkt_data to void so we can do some pointer arithmetic
v_pkt_data = ctypes.cast(pkt_data, ctypes.c_void_p)
# retrieve the position of the ip header
v_ip_header = ctypes.c_void_p(v_pkt_data.value + 14)
pih = ctypes.cast(v_ip_header, ctypes.POINTER(ip_header))
ih = pih.contents
# retrieve the position of the udp header
ip_len = (ih.ver_ihl & 0xf) * 4
uh = ctypes.cast(ctypes.cast(pih, ctypes.c_void_p).value + ip_len,
ctypes.POINTER(udp_header)).contents
# convert from network byte order to host byte order
sport = socket.ntohs(uh.sport)
dport = socket.ntohs(uh.dport)
print("{}.{}.{}.{}:{} -> {}.{}.{}.{}:{}".format(
ih.saddr.byte1, ih.saddr.byte2, ih.saddr.byte3, ih.saddr.byte4, sport,
ih.daddr.byte1, ih.daddr.byte2, ih.daddr.byte3, ih.daddr.byte4, dport))
# Extracting data
#
# data offset from ip header start
data_offset = ip_len + ctypes.sizeof(udp_header)
# data length
data_len = ih.tlen - ip_len - ctypes.sizeof(udp_header)
# get data
arr_type = (ctypes.c_uint8 * data_len)
data = arr_type.from_address(v_ip_header.value + data_offset)
print(data[0:data_len])
# note: same thing could be achieved from pkt_data
print(pkt_data[14+data_offset:14+data_offset+data_len])
def main(packet_file):
with open(packet_file, "rb") as fd:
data = fd.read()
# create ctypes.c_char_array
pkt_data = ctypes.c_buffer(data)
packet_handler(pkt_data)
if __name__ == '__main__':
if len(sys.argv) == 2:
main(sys.argv[1])