我最近读了一些信息安全和编程方面的东西,我偶然发现了原始套接字。我对创建原始套接字并正确捕获数据包的方法感到有点困惑。
方法I)使用AF_INET创建套接字
sock=socket.socket(socket.AF_INET,socket.SOCK_RAW,socket.IPPROTO_*)
##IPPROTO_* can be IPPROTO_IP/IPPROTO_TCP/IPPROTO_UDP
raw_data=sock.recvfrom(65535)但是使用这种方法我能够捕获数据包,但当我尝试解码它时,我无法理解它。
方法II)
sock=socket.socket(socket.AF_PACKET,socket.SOCK_RAW,socket.htons(0x0800))
raw_data=socket.recvfrom(65535)通过使用第二种方法,我能够捕获和解码它。
仅供参考,我正在尝试解码来自以太网层的数据包。
所以我的问题是为什么我的方法失败了?有没有其他方法可以在python中创建原始套接字。
在进阶时谢谢。
发布于 2014-06-27 00:50:48
如果你想创建无协议的原始套接字,使用:socket(AF_PACKET, SOCK_RAW);
示例:
#!/usr/bin/env python
from socket import socket, AF_PACKET, SOCK_RAW
s = socket(AF_PACKET, SOCK_RAW)
s.bind(("eth1", 0))
# We're putting together an ethernet frame here,
# but you could have anything you want instead
# Have a look at the 'struct' module for more
# flexible packing/unpacking of binary data
# and 'binascii' for 32 bit CRC
src_addr = "\x01\x02\x03\x04\x05\x06"
dst_addr = "\x01\x02\x03\x04\x05\x06"
payload = ("["*30)+"PAYLOAD"+("]"*30)
checksum = "\x1a\x2b\x3c\x4d"
ethertype = "\x08\x01"
s.send(dst_addr+src_addr+ethertype+payload+checksum)所有积分都归于brice
https://stackoverflow.com/questions/24436047
复制相似问题