【问题标题】:python arp header unpacking with struct modulepython arp头解包与struct模块
【发布时间】:2019-03-31 21:01:52
【问题描述】:

我要制作自己的 python 嗅探器,但我在解压 arp 协议头时遇到问题。

这是我的代码:

def Sniffer():
    try:
        # AF_PACKET, That's basically packet level.
        # 0X0003, That's every packet. (We can find it here: /usr/include/linux/if_ether.h) 
        SK = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0003))
    except socket.error as MSG:
        print "Socket creation error:\n", MSG

    try:
        while True:
            Receive = SK.recvfrom(65565)
            Packet = Receive[0]
            Ethernet(Packet)
    except socket.error as MSG:
        print "Receive error:\n", MSG



# Ethernet Decapsulation (We need EtherType field value)
def Ethernet(Packet):
    ETHERNET_LENGTH = 14
    ETHERNET_HEADER = Packet[:ETHERNET_LENGTH]
    ETHERNET_HEADER_UNPACK = struct.unpack("!6s6sH", ETHERNET_HEADER)

    EtherType = ETHERNET_HEADER_UNPACK[2]
    print EtherType

    if EtherType == 2054:
        ARP(ETHERNET_LENGTH, Packet)
    if EtherType == 2048:
        IPV4(Packet)



# ARP Decapsulation (We need OPCODE field value)
def ARP(ETHERNET_LENGTH, Packet):
    ARP_LENGTH = 42
    ARP_HEADER = Packet[ETHERNET_LENGTH:ARP_LENGTH]
    ARP_HEADER_UNPACK = struct.unpack("!2s2s1s1s2s6s4s6s4s", ARP_HEADER)

    OPCODE = ARP_HEADER_UNPACK[4]

    if OPCODE == 1:
        print "ARP Request (Some one scann your network)"    

这是我的错误:

Traceback (most recent call last):
  File "HoneySniffer.py", line 130, in <module>
    Sniffer()
  File "HoneySniffer.py", line 22, in Sniffer
    Ethernet(Packet)
  File "HoneySniffer.py", line 38, in Ethernet
    ARP(ETHERNET_LENGTH, Packet)
  File "HoneySniffer.py", line 48, in ARP
    ARP_HEADER_UNPACK = struct.unpack("!2s2s1s1s2s6s4s6s4s", ARP_HEADER)
struct.error: unpack requires a string argument of length 28

为什么会这样?
我该如何解决?

我在这里找到它:Python arp sniffing raw socket no reply packets

【问题讨论】:

    标签: python struct arp unpack sniffer


    【解决方案1】:

    我有完全相同的问题,我使用您的参考链接Python arp sniffing raw socket no reply packets 和我自己的少量研究解决了它。


    conn=socket.socket(socket.AF_PACKET,socket.SOCK_RAW,socket.ntohs(0x0003))
    
    def arp_header(packet):
    
    (a ,b ,c ,d ,e ,f ,g ,h ,i ) = struct.unpack('2s2s1s1s2s6s4s6s4s',packet[14:42])
    
    hw_type=(binascii.hexlify(a)).decode('utf-8')
    proto_type=(binascii.hexlify(b)).decode('utf-8')
    hw_size=(binascii.hexlify(c)).decode('utf-8')
    proto_size=(binascii.hexlify(d)).decode('utf-8')
    opcode=(binascii.hexlify(e)).decode('utf-8')
    
    return (hw_type,proto_type,hw_size,proto_size,opcode,socket.inet_ntoa(g),socket.inet_ntoa(i))
    

    使用struct.unpack('2s2s1s1s2s6s4s6s4s',packet[14:42]) 不是struct.unpack('!2s2s1s1s2s6s4s6s4s',packet[14:42]) 这解决了我的struct.error: unpack requires a string argument of length 28 错误

    由于我的项目要求,我也使用了单个变量,

    现在,为了调试,我使用了 print(packet[14:42]) - 它给了我字节十六进制文字(如 b'\x00\x01\x08\x00\x06\x04\x00\x01\xe4\x11[I&amp;\xbe\n\x00..... 等)

    所以我必须在使用 hexlify 后以 utf-8 解码,因为 hexlify 再次将字节对象返回给我。

    我使用的Python版本:3.6.5

    日期:2019 年 4 月 3 日

    结果:- Arp Packet: - H/W Type: 0001, Protocol Type: 0800, H/W Size: 06 ,Protocol Size: 04 - Opcode: 0001, Source IP: 10.0.15.141, Destination IP: 10.0.10.2

    结论:这个方法对我有用,我希望它也对你有用:)

    【讨论】:

      猜你喜欢
      • 2021-07-03
      • 1970-01-01
      • 2011-08-27
      • 1970-01-01
      • 1970-01-01
      • 2011-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多