繁体   English   中英

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

[英]python packet sniffer using raw socket

试图在python中使用原始套接字创建数据包嗅探器,并想使用struct.unpack方法解析完整的TCP头,但是某些字段(例如HLEN(4bits)和tcp头中的偏移量,URG,ACK,PST,RST,SYN,FIN在位上不是Byte。 所以我的问题是如何从标题解析这些值!

您可以使用:

这是一个例子:

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

结果:

>>> 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