python解析发往本机的数据包示例 (解析数据包)

 更新时间:2014年01月16日 14:30:16   作者:  
这篇文章主要介绍了使用python解析获取发往本机的数据包,并打印出来, 大家参考使用吧

tcp.py

复制代码 代码如下:

# -*- coding: cp936 -*-
import socket
from struct import *
from time import ctime,sleep
from os import system

system('title tcp sniffer')
system('color 05')

# the public network interface
HOST = socket.gethostbyname(socket.gethostname())

# create a raw socket and bind it to the public interface
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
s.bind((HOST, 0))

# Include IP headers
s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

# receive all packages
#s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)

# receive a package
while 1==1:
    packet = s.recvfrom(65565)
    packet = packet[0]

    ip_header = packet[0:20]
    iph = unpack('!BBHHHBBH4s4s',ip_header)
    version = iph[0] >> 4 #Version
    ihl = iph[0] * 0xF    #IHL
    iph_length = ihl * 4  #Total Length
    ttl = iph[5]
    protocol = iph[6]
    s_addr = socket.inet_ntoa(iph[8])
    d_addr = socket.inet_ntoa(iph[9])
    print ctime()
    print 'Version : ' + str(version) + ' IHL : ' + str(ihl) + ' Total Length: '+str(iph_length) + ' TTL : ' +str(ttl) + ' Protocol : ' + str(protocol) + ' Source Address : ' + str(s_addr) + ' Destination Address : ' + str(d_addr)

    if protocol == 6:
        tcp_header = packet[20:40]
        tcph = unpack('!HHLLBBHHH' , tcp_header)
        source_port = tcph[0]
        dest_port = tcph[1]
        sequence = tcph[2]
        acknowledgement = tcph[3]
        doff_reserved = tcph[4]
        tcph_length = doff_reserved >> 4
        print 'Source Port : ' + str(source_port) + ' Dest Port : ' + str(dest_port) + ' Sequence Number : ' + str(sequence) + ' Acknowledgement : ' + str(acknowledgement) + ' TCP header length : ' + str(tcph_length)

        data = packet[40:len(packet)]
        print 'Data : ' + data


# disabled promiscuous mode
s.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)

udp.py

复制代码 代码如下:

# -*- coding: cp936 -*-
import socket
from struct import *
from time import ctime,sleep
from os import system

system('title udp sniffer')
system('color 05')
# the public network interface
HOST = socket.gethostbyname(socket.gethostname())

# create a raw socket and bind it to the public interface
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
s.bind((HOST, 0))

# Include IP headers
s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

# receive all packages
#s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)

# receive a package
while 1==1:
    packet = s.recvfrom(65565)
    packet = packet[0]

    ip_header = packet[0:20]
    iph = unpack('!BBHHHBBH4s4s',ip_header)
    version = iph[0] >> 4 #Version
    ihl = iph[0] * 0xF    #IHL
    iph_length = ihl * 4  #Total Length
    ttl = iph[5]
    protocol = iph[6]
    s_addr = socket.inet_ntoa(iph[8])
    d_addr = socket.inet_ntoa(iph[9])

    if protocol == 17:
        udp_header = packet[20:28]
        udph = unpack('!HHHH' , udp_header)
        source_port = udph[0]
        dest_port = udph[1]
        length = udph[2]
        checksum = udph[3]
        data = packet[28:len(packet)]

        print ctime()
        print 'Version : ' + str(version) + ' IHL : ' + str(ihl) + ' Total Length: '+str(iph_length) + ' TTL : ' +str(ttl) + ' Protocol : ' + str(protocol) + ' Source Address : ' + str(s_addr) + ' Destination Address : ' + str(d_addr)
        print 'Source Port : ' + str(source_port) + ' Dest Port : ' + str(dest_port) + ' Length : ' + str(length) + ' Checksum : ' + str(checksum)
        print 'Data : ' + data

# disabled promiscuous mode
s.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)

相关文章

  • pip install python-Levenshtein失败的解决

    pip install python-Levenshtein失败的解决

    这篇文章主要介绍了pip install python-Levenshtein失败的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • Pandas数据结构中Series属性详解

    Pandas数据结构中Series属性详解

    本文主要介绍了Pandas数据结构中Series属性详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • Python 爬虫之超链接 url中含有中文出错及解决办法

    Python 爬虫之超链接 url中含有中文出错及解决办法

    这篇文章主要介绍了Python 爬虫之超链接 url中含有中文出错及解决办法的相关资料,出现UnicodeEncodeError: 'ascii' codec can't encode characters,的错误解决办法,需要的朋友可以参考下
    2017-08-08
  • np.newaxis 实现为 numpy.ndarray(多维数组)增加一个轴

    np.newaxis 实现为 numpy.ndarray(多维数组)增加一个轴

    今天小编就为大家分享一篇np.newaxis 实现为 numpy.ndarray(多维数组)增加一个轴,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-11-11
  • Python使用sqlalchemy模块连接数据库操作示例

    Python使用sqlalchemy模块连接数据库操作示例

    这篇文章主要介绍了Python使用sqlalchemy模块连接数据库操作,结合实例形式分析了sqlalchemy模块的安装及连接、调用数据库相关操作技巧,需要的朋友可以参考下
    2019-03-03
  • 基于opencv实现简单画板功能

    基于opencv实现简单画板功能

    这篇文章主要为大家详细介绍了基于opencv实现简单画板功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-08-08
  • Python生成器generator和yield关键字的使用

    Python生成器generator和yield关键字的使用

    生成器是一种特殊的迭代器,可以通过列表推导式的修改或者使用yield关键字来创建,生成器函数能够在迭代时动态产生值,而不是一次性生成所有值,这有助于节省内存,yield关键字使函数执行暂停并保存当前状态,下次调用时从停止处继续执行
    2024-09-09
  • wxPython窗口中文乱码解决方法

    wxPython窗口中文乱码解决方法

    这篇文章主要介绍了wxPython窗口中文乱码解决方法,在Python程序设计中比较常见,是非常实用的技巧,需要的朋友可以参考下
    2014-10-10
  • pytorch + visdom 处理简单分类问题的示例

    pytorch + visdom 处理简单分类问题的示例

    这篇文章主要介绍了pytorch + visdom 处理简单分类问题的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • Pandas时间类型转换与处理的实现示例

    Pandas时间类型转换与处理的实现示例

    本文主要介绍了Pandas时间类型转换与处理的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07

最新评论