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