Python写的嗅探器——Pyside,Scapy
使用Python的Pyside和Scapy写的嗅探器原型,拥有基本框架,但是功能并不十分完善,供参考。
- import sys
- import time
- import binascii
- from PySide.QtCore import *
- from PySide.QtGui import *
- from scapy.all import *
- # Every Qt application must have one and only one QApplication object;
- # it receives the command line arguments passed to the script, as they
- # can be used to customize the application's appearance and behavior
- qt_app = QApplication(sys.argv)
- global_pkt_list = []
- # Try to redirect hexdump()'s output, but failed!Why? T_T
- class redirect_output:
- def __init__(self):
- self.str = ''
- def write(self, s):
- self.str += s
- def show(self):
- return self.str
- class Sniffer(QThread):
- pkt_arrive = Signal(str)
- bGo = True
- filter = None
- iface = 'eth0'
- def __init__(self, parent=None):
- QThread.__init__(self, parent)
- # self.pkt_arrive.connect(OnPktArrive)
- def run(self):
- # self.emit(SIGNAL("pkt_arrive(str)"), "pkt")
- while (self.bGo):
- p = sniff(count=1, filter = self.filter)
- global_pkt_list.append(p[0])
- self.pkt_arrive.emit((p[0].summary()))
- def go(self):
- self.bGo = True
- self.start()
- def stop(self):
- print 'Sniffer got exit message'
- self.bGo = False
- class PktListItem(QListWidgetItem):
- def __init__(self, pkt=None, num=None):
- QListWidgetItem.__init__(self)
- self.pkt = pkt
- self.num = num
- class MainWindow(QWidget):
- ''' An example of PySide absolute positioning; the main window
- inherits from QWidget, a convenient widget for an empty window. '''
- number = 0
- def __init__(self):
- QWidget.__init__(self)
- self.setWindowTitle('J_Sniffer')
- self.setMinimumSize(800, 500)
- # set layout
- self.main_layout = QVBoxLayout()
- # edit and btn
- self.layout1 = QHBoxLayout()
- self.Label_Iface = QLabel("Iface", self)
- self.layout1.addWidget(self.Label_Iface)
- self.TextBox_Iface = QLineEdit(self)
- self.TextBox_Iface.setPlaceholderText("Choose network interface")
- self.layout1.addWidget(self.TextBox_Iface)
- self.Label_Fliter = QLabel("Filter", self)
- self.layout1.addWidget(self.Label_Fliter)
- self.TextBox_Filter = QLineEdit(self)
- self.layout1.addWidget(self.TextBox_Filter)
- self.layout1.addStretch(1)
- self.Btn_Start = QPushButton("&Start", self)
- self.layout1.addWidget(self.Btn_Start)
- self.main_layout.addLayout(self.layout1)
- # List to show packets
- self.List_Pkt = QListWidget(self)
- self.main_layout.addWidget(self.List_Pkt)
- # Tree to see pkt's detail
- self.Tree = QTreeWidget(self)
- self.main_layout.addWidget(self.Tree)
- self.Tree.setColumnCount(2)
- self.Tree.setHeaderLabels(['Key', 'Value'])
- self.setLayout(self.main_layout)
- # create signal and sniff thread
- self.thread = Sniffer()
- self.connect(self.Btn_Start, SIGNAL("clicked()"), self.Sniff)
- # self.connect(self.thread, SIGNAL("pkt_arrive(str)"), self.OnPktArrive) Connot work!
- self.thread.pkt_arrive.connect(self.OnPktArrive)
- self.List_Pkt.currentItemChanged.connect(self.On_ItemChanged)
- @Slot(str)
- def OnPktArrive(self, pkt):
- print 'received pkt arrive signal'
- #p = Ether(pkt) #only Ethernet now, 802.11 may be crash!
- item = PktListItem(num = self.number)
- item.setText(str(self.number) + '\t' + pkt)
- self.List_Pkt.addItem(item)
- self.number += 1
- @Slot()
- def Sniff(self):
- print self.Btn_Start.text()
- if self.Btn_Start.text() == '&Start':
- self.Btn_Start.setText("&Stop")
- self.thread.filter = self.TextBox_Filter.text()
- self.thread.iface = self.TextBox_Iface.text()
- self.thread.go()
- else:
- self.Btn_Start.setText("&Start")
- self.thread.stop()
- def On_ItemChanged(self, curr, prev):
- print curr.num
- self.Tree.clear()
- p = global_pkt_list[curr.num]
- root1 = QTreeWidgetItem(self.Tree)
- if (p.haslayer(Ether)):
- root1.setText(0, 'Ethernet:')
- child1_1 = QTreeWidgetItem(root1)
- child1_1.setText(0, 'dst')
- child1_1.setText(1, p.dst)
- child1_2 = QTreeWidgetItem(root1)
- child1_2.setText(0, 'src')
- child1_2.setText(1, p.src)
- child1_3 = QTreeWidgetItem(root1)
- child1_3.setText(0, 'type')
- child1_3.setText(1, hex(p.type))
- p = p.getlayer(1)
- if (p.haslayer(IP)):
- self._SetIPTree(p)
- p = p.getlayer(1)
- if (p.haslayer(ICMP)):
- self._SetICMPTree(p)
- elif (p.haslayer(TCP)):
- pass
- else:
- pass
- elif (p.haslayer(IPv6)):
- pass
- else:
- root1.setText(0, 'Not Ethernet')
- root1.setText(1, hexdump(p))
- def _SetIPTree(self, p):
- root2 = QTreeWidgetItem(self.Tree)
- root2.setText(0, 'IPv4')
- child2_1 = QTreeWidgetItem(root2)
- child2_1.setText(0, 'Version')
- child2_1.setText(1, str(p.version))
- child2_2 = QTreeWidgetItem(root2)
- child2_2.setText(0, 'ihl(Header Length)')
- child2_2.setText(1, str(p.ihl))
- child2_3 = QTreeWidgetItem(root2)
- child2_3.setText(0, 'tos')
- child2_3.setText(1, str(p.tos))
- child2_4 = QTreeWidgetItem(root2)
- child2_4.setText(0, 'len')
- child2_4.setText(1, str(p.len))
- child2_5 = QTreeWidgetItem(root2)
- child2_5.setText(0, 'id')
- child2_5.setText(1, str(p.id))
- child2_6 = QTreeWidgetItem(root2)
- child2_6.setText(0, 'flags')
- child2_6.setText(1, str(p.flags))
- child2_7 = QTreeWidgetItem(root2)
- child2_7.setText(0, 'frag')
- child2_7.setText(1, str(p.frag))
- child2_8 = QTreeWidgetItem(root2)
- child2_8.setText(0, 'TTL')
- child2_8.setText(1, str(p.ttl))
- child2_9 = QTreeWidgetItem(root2)
- child2_9.setText(0, 'protocol')
- child2_9.setText(1, str(p.proto))
- child2_10 = QTreeWidgetItem(root2)
- child2_10.setText(0, 'checksum')
- child2_10.setText(1, str(p.chksum))
- child2_11 = QTreeWidgetItem(root2)
- child2_11.setText(0, 'src')
- child2_11.setText(1, str(p.src))
- child2_12 = QTreeWidgetItem(root2)
- child2_12.setText(0, 'dst')
- child2_12.setText(1, str(p.dst))
- def _SetICMPTree(self, p):
- root3 = QTreeWidgetItem(self.Tree)
- root3.setText(0, 'ICMP')
- child3_1 = QTreeWidgetItem(root3)
- child3_1.setText(0, 'Type')
- if (p.type == 8):
- child3_1.setText(1, 'echo request')
- elif (p.type == 0):
- child3_1.setText(1, 'echo reply')
- else:
- child3_1.setText(1, str(p.type))
- child3_2 = QTreeWidgetItem(root3)
- child3_2.setText(0, 'Code')
- child3_2.setText(1, str(p.code))
- child3_3 = QTreeWidgetItem(root3)
- child3_3.setText(0, 'Checksum')
- child3_3.setText(1, str(p.chksum))
- child3_4 = QTreeWidgetItem(root3)
- child3_4.setText(0, 'ID')
- child3_4.setText(1, str(p.id))
- child3_5 = QTreeWidgetItem(root3)
- child3_5.setText(0, 'Sequence number')
- child3_5.setText(1, str(p.seq))
- child3_6 = QTreeWidgetItem(root3)
- child3_6.setText(0, 'Data')
- child3_6.setText(1, binascii.b2a_hex(str(p.load)))
- def run(self):
- self.show()
- if __name__ == '__main__':
- # Create an instance of the application window and run it
- win = MainWindow()
- win.run()
- qt_app.exec_()
Python写的嗅探器——Pyside,Scapy的更多相关文章
- 用python写网路爬虫 PDF高清完整版免费下载 Python基础教程免费电子书 python入门书籍免费下载
<用python写网路爬虫PDF免费下载>PDF书籍下载 内容简介 作为一种便捷地收集网上信息并从中抽取出可用信息的方式,网络爬虫技术变得越来越有用.使用Python这样的简单编程语言,你 ...
- Python写各大聊天系统的屏蔽脏话功能原理
Python写各大聊天系统的屏蔽脏话功能原理 突然想到一个视频里面弹幕被和谐的一满屏的*号觉得很有趣,然后就想用python来试试写写看,结果还真玩出了点效果,思路是首先你得有一个脏话存放的仓库好到时 ...
- python写红包的原理流程包含random,lambda其中的使用和见简单介绍
Python写红包的原理流程 首先来说说要用到的知识点,第一个要说的是扩展包random,random模块一般用来生成一个随机数 今天要用到ramdom中unifrom的方法用于生成一个指定范围的随机 ...
- Python写地铁的到站的原理简易版
Python地铁的到站流程及原理(个人理解) 今天坐地铁看着站牌就莫名的想如果用Python写其工作原理 是不是很简单就小试牛刀了下大佬们勿喷纯属小弟个人理解 首先来看看地铁上显示的站牌如下: 就想这 ...
- 用Python写一个简单的Web框架
一.概述 二.从demo_app开始 三.WSGI中的application 四.区分URL 五.重构 1.正则匹配URL 2.DRY 3.抽象出框架 六.参考 一.概述 在Python中,WSGI( ...
- 读书笔记汇总 --- 用Python写网络爬虫
本系列记录并分享:学习利用Python写网络爬虫的过程. 书目信息 Link 书名: 用Python写网络爬虫 作者: [澳]理查德 劳森(Richard Lawson) 原版名称: web scra ...
- Python写UTF8文件,UE、记事本打开依然乱码的问题
Python写UTF8文件,UE.记事本打开依然乱码的问题 Leave a reply 现象:使用codecs打开文件,写入UTF-8文本,正常无错误.用vim打开正常,但记事本.UE等打开乱码. 原 ...
- python 写的http后台弱口令爆破工具
今天来弄一个后台破解的Python小程序,哈哈,直接上代码吧,都有注释~~ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 ...
- python写xml文件
为了便于后续的读取处理,这里就将信息保存在xml文件中,想到得到的文件如下: 1 <?xml version="1.0" encoding="utf-8" ...
随机推荐
- 超级简单的Android Studio jni 实现(无需命令行)
1.配置Anroid Studio(这步是关键) 使用[command+,] 打开Preferences,选择External Tools,点击加号框如下图: Paste_Image.png 点击+号 ...
- AE属性表操作
转自chanyinhelv原文AE属性表操作 实现的操作包括:1.打开属性表:2.编辑属性表:3.增加属性列:4.数据排序:5.字段计算…… 嗯,实现的功能目前就这些吧,后续还会继续跟进,还望大家多多 ...
- Eclipse Che安装入门和使用(一)
Eclipse Che序列博文如下: 安装和调试篇:Eclipse Che安装入门和使用(一) Web进阶篇:Eclipse Che开发Spring Web应用(入门) (二) 本文摘要: Eclip ...
- reduce 阶段遍历对象添加到ArrayList中的问题
起初遍历values时直接把对象添加到集合中,后来输出结果和预期不符,debug时发现添加到集合中的对象的值全部是最后一个对象的值,网上百度了下,发现是reduce阶段对象重用的问题,reduce阶段 ...
- C++请求web service与xml解析
1. C++解析XML的开源库 在项目中XML的解析使用的是开源的第三方库,TinyXML:这个解析库的模型通过XML文件,然后再内存中生成DOM模型,从而让我们能够非常方便的遍历这颗XML树. DO ...
- 分布式系统和CAP
帽子理论(CAP): C:Consistency,一致性, 数据一致更新,所有数据变动都是同步的 A:Availability,可用性, 好的响应性能,完全的可用性指的是在任何故障模型下,服务都会在有 ...
- Cython 的学习
开发效率极高的 Python 一直因执行效率过低为人所诟病,Cython 由此诞生,特性介于 Python 和 C 语言之间. Cython 学习 1. Cython 是什么? 它是一个用来快速生成 ...
- C#步骤控件
C#开发step步骤条控件 现在很多的javascript控件,非常的不错,其中step就是一个,如下图所示: 那么如何用C#来实现一个step控件呢? 先定义一个StepEntity类来存储步骤 ...
- HDU 1671 (字典树统计是否有前缀)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 Problem Description Given a list of phone number ...
- “warning C4996: 'fopen': This function or variable may be unsafe”和“LINK : fatal error LNK1104”的解决办法
程序有时编译出现警告C4996,报错: warning C4996: 'fopen': This function or variable may be unsafe. Consider using ...