鱼C论坛

 找回密码
 立即注册
查看: 2365|回复: 2

python网络嗅探问题

[复制链接]
发表于 2017-7-20 18:00:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
首先先上源代码

  1. import struct
  2. import socket
  3. from ctypes import *

  4. host = "192.168.0.109"

  5. # IP头定义
  6. class IP(Structure):
  7.     _fields_ = [
  8.         ("ihl",             c_ubyte, 4),
  9.         ("version",         c_ubyte, 4),
  10.         ("tos",             c_ubyte),
  11.         ("len",             c_ushort),
  12.         ("id",              c_ushort),
  13.         ("offset",          c_ushort),
  14.         ("ttl",             c_ubyte),
  15.         ("protocol_num",    c_ubyte),
  16.         ("sum",             c_ushort),
  17.         ("src",             c_uint),
  18.         ("dst",             c_uint),
  19.     ]

  20.     def __new__(self, socket_buffer=None):
  21.         return self.from_buffer_copy(socket_buffer)

  22.     def __init__(self, socket_buffer=None):
  23.         self.protocol_map = {1: "ICMP", 6: "TCP", 17: "UDP"}

  24.         # readable ip address
  25.         # print (struct.pack("L", self.src))
  26.         self.src_address = socket.inet_ntoa(struct.pack("L", self.src))
  27.         self.dst_address = socket.inet_ntoa(struct.pack("L", self.dst))
  28.         #socket.inet_ntoa转换32位打包的IPV4地址为IP地址的标准点号分隔字符串表示
  29.         #pack按照给定的格式(fmt),把数据封装成字符串(实际上是类似于c结构体的字节流)

  30.         """
  31. fmt 格式
  32. FORMAT        C TYPE        PYTHON TYPE        STANDARD SIZE        NOTES
  33. x        pad byte        no value                  
  34. c        char        string of length 1        1         
  35. b        signed char        integer        1        (3)
  36. B        unsigned char        integer        1        (3)
  37. ?        _Bool        bool        1        (1)
  38. h        short        integer        2        (3)
  39. H        unsigned short        integer        2        (3)
  40. i        int        integer        4        (3)
  41. I        unsigned int        integer        4        (3)
  42. l        long        integer        4        (3)
  43. L        unsigned long        integer        4        (3)
  44. q        long long        integer        8        (2), (3)
  45. """
  46.         

  47.         # type of protocol
  48.         try:
  49.             self.protocol = self.protocol_map[self.protocol_num]#解析ip头
  50.         except:
  51.             self.protocol = str(self.protocol_num)

  52. socket_protocol = socket.IPPROTO_IP

  53. sniffer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket_protocol)
  54. sniffer.bind((host, 0))
  55. sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

  56. sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)

  57. try:
  58.     while True:
  59.         raw_buffer = sniffer.recvfrom(65565)[0]

  60.         ip_header = IP(raw_buffer[:20])

  61.         print ("Protocol: %s %s -> %s " % (ip_header.protocol, ip_header.src_address, ip_header.dst_address))
  62. except KeyboardInterrupt:
  63.     sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)
复制代码



这段代码没有错,执行正常.,

只是不太明白
    def __new__(self, socket_buffer=None):
        return self.from_buffer_copy(socket_buffer)
self.from_buffer_copy(socket_buffer)的用法


还有sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON
这两句的用法

望大神能详细指点一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-7-20 19:07:30 | 显示全部楼层
  1. help(socket)
复制代码
     |  ioctl(...)
     |      ioctl(cmd, option) -> long
     |      
     |      Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are
     |      SIO_RCVALL:  'option' must be one of the socket.RCVALL_* constants.
     |      SIO_KEEPALIVE_VALS:  'option' is a tuple of (onoff, timeout, interval).
     |  

socket.ioctl(control, option)
平台:        视窗
ioctl()方法是WSAIoctl系统接口的有限接口。有关详细信息,请参阅Win32文档。

在其他平台上,可以使用通用的fcntl.fcntl()和fcntl.ioctl()函数;他们接受一个套接字对象作为他们的第一个参数。


WSAIoctl
https://msdn.microsoft.com/en-us/library/ms741621%28VS.85%29.aspx
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-7-20 20:09:49 | 显示全部楼层

The WSAIoctl function is used to set or retrieve operating parameters associated with the socket, the transport protocol, or the communications subsystem.

该函数用来设置或者检索与socket相关的参数,传输协议,或者联系子系统?还是不知道该函数有啥用...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-26 17:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表