首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C/ Python WinPCap翻译

C/ Python WinPCap翻译
EN

Stack Overflow用户
提问于 2015-03-01 15:22:43
回答 1查看 872关注 0票数 0

对不起,对你们大多数人来说,这将是一个非常简单的问题。

我正在尝试将这里中的一些代码转换为Python3.4,因为我不知道C。这主要是这个问题的基础。虽然我已经根据链接创建了Python数据结构:

代码语言:javascript
复制
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转换以下代码:

代码语言:javascript
复制
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数据包,数据包的下一部分将是数据。如果可能的话,我也想从包裹中提取出来。

谢谢您能提供的任何帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-02 04:20:26

一些注意事项:

  • 对于ctype结构,必须使用_fields_而不是_fields
  • 根据经验,我倾向于在代码中使用Flake8,甚至在代码片段上也是如此。

测试

我在一个NTP包上测试了我的代码,因为NTP已经通过了UDP,以确定是否一切都是正确的。

  • 转到Wireshark wiki: NTP
  • 下载第一个示例捕获( sync.pcap )
  • 加载>选择第一个数据包>转到“查看”>确保“数据包详细信息”是摩擦。
  • 单击“数据包详细信息”窗口,框架1,(应阅读“Frame1: 75字节在线路上(600位),75字节捕获(600位)”>>右击>“导出选定的数据包字节”。

将保存的文件传递给以下脚本。

输出

192.168.50.50:1026 -> 192.168.0.1:53

根据wireshark的说法,这是正确的。

代码语言:javascript
复制
#!/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])
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28795254

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档