简体   繁体   English

使用原始套接字的python数据包嗅探器

[英]python packet sniffer using raw socket

trying to create packet sniffer using raw socket in python, and want to parse the full TCP header using struct.unpack method, but some fields like HLEN(4bits) and offset, URG,ACK,PST,RST,SYN,FIN in tcp header are on bits not Byte . 试图在python中使用原始套接字创建数据包嗅探器,并想使用struct.unpack方法解析完整的TCP头,但是某些字段(例如HLEN(4bits)和tcp头中的偏移量,URG,ACK,PST,RST,SYN,FIN在位上不是Byte。 so my question is how to parse these values from header ! 所以我的问题是如何从标题解析这些值!

You could use: 您可以使用:

Here is an example: 这是一个例子:

from ctypes import c_int32, c_uint32, Structure, Union

class _bits(Structure):
    _fields_ = [
        ("odd", c_uint32, 1),
        ("half", c_uint32, 31),
    ]

class Int(Union):
    _fields_ = [
        ("bits", _bits),
        ("number", c_uint32),
    ]


a = Int(number=12345)
a.bits.odd, a.bits.half

the result: 结果:

>>> a = Int(number=12345)
>>> a.bits.odd, a.bits.half
(1L, 6172L)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM