python嗅探器_Python中的简单原始数据包嗅探器

首先,我是python的初学者.我利用在第2层运行的PF_PACKET接口开发了一个简单的原始数据包嗅探器.

嗅探器只是找出以下内容…

-以太网头(源-目标-协议)

-IP标头(源IP-目标IP)

-TCP标头(源端口-目标端口)

这是我到目前为止编写的代码…

#!/usr/bin/env python

import struct

import socket

import binascii

rawSocket=socket.socket(socket.PF_PACKET,socket.SOCK_RAW,socket.htons(0x0800))

#ifconfig eth0 promisc up

receivedPacket=rawSocket.recv(2048)

#Ethernet Header...

ethernetHeader=receivedPacket[0][0:14]

ethrheader=struct.unpack("!6s6s2s",ethernetHeader)

destinationIP= binascii.hexlify(ethrheader[0])

sourceIP= binascii.hexlify(ethrheader[1])

protocol= binascii.hexlify(ethrheader[2])

print "Destinatiom: " + destinationIP

print "Souce: " + sourceIP

print "Protocol: "+ protocol

#IP Header...

ipHeader=receivedPacket[0][14:34]

ipHdr=struct.unpack("!12s4s4s",ipHeader)

destinationIP=socket.inet_ntoa(ipHdr[2])

print "Source IP: " +sourceIP

print "Destination IP: "+destinationIP

#TCP Header...

tcpHeader=receivedPacket[0][34:54]

tcpHdr=struct.unpack("!2s2s16s",tcpHeader)

sourcePort=socket.inet_ntoa(tcpHdr[0])

destinationPort=socket.inet_ntoa(tcpHdr[1])

print "Source Port: " + sourcePort

print "Destination Port: " + destinationPort

我似乎在以太网头部分和无法弄清的解压缩方法中遇到了问题.提前致谢 :)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值