使用Python的Pyside和Scapy写的嗅探器原型,拥有基本框架,但是功能并不十分完善,供参考。

  1. import sys
  2. import time
  3. import binascii
  4. from PySide.QtCore import *
  5. from PySide.QtGui import *
  6. from scapy.all import *
  7.  
  8. # Every Qt application must have one and only one QApplication object;
  9. # it receives the command line arguments passed to the script, as they
  10. # can be used to customize the application's appearance and behavior
  11. qt_app = QApplication(sys.argv)
  12. global_pkt_list = []
  13.  
  14. # Try to redirect hexdump()'s output, but failed!Why? T_T
  15. class redirect_output:
  16. def __init__(self):
  17. self.str = ''
  18. def write(self, s):
  19. self.str += s
  20. def show(self):
  21. return self.str
  22.  
  23. class Sniffer(QThread):
  24. pkt_arrive = Signal(str)
  25. bGo = True
  26. filter = None
  27. iface = 'eth0'
  28.  
  29. def __init__(self, parent=None):
  30. QThread.__init__(self, parent)
  31. # self.pkt_arrive.connect(OnPktArrive)
  32.  
  33. def run(self):
  34. # self.emit(SIGNAL("pkt_arrive(str)"), "pkt")
  35. while (self.bGo):
  36. p = sniff(count=1, filter = self.filter)
  37. global_pkt_list.append(p[0])
  38. self.pkt_arrive.emit((p[0].summary()))
  39.  
  40. def go(self):
  41. self.bGo = True
  42. self.start()
  43.  
  44. def stop(self):
  45. print 'Sniffer got exit message'
  46. self.bGo = False
  47.  
  48. class PktListItem(QListWidgetItem):
  49. def __init__(self, pkt=None, num=None):
  50. QListWidgetItem.__init__(self)
  51. self.pkt = pkt
  52. self.num = num
  53.  
  54. class MainWindow(QWidget):
  55. ''' An example of PySide absolute positioning; the main window
  56. inherits from QWidget, a convenient widget for an empty window. '''
  57. number = 0
  58. def __init__(self):
  59. QWidget.__init__(self)
  60. self.setWindowTitle('J_Sniffer')
  61. self.setMinimumSize(800, 500)
  62.  
  63. # set layout
  64. self.main_layout = QVBoxLayout()
  65. # edit and btn
  66. self.layout1 = QHBoxLayout()
  67.  
  68. self.Label_Iface = QLabel("Iface", self)
  69. self.layout1.addWidget(self.Label_Iface)
  70. self.TextBox_Iface = QLineEdit(self)
  71. self.TextBox_Iface.setPlaceholderText("Choose network interface")
  72. self.layout1.addWidget(self.TextBox_Iface)
  73.  
  74. self.Label_Fliter = QLabel("Filter", self)
  75. self.layout1.addWidget(self.Label_Fliter)
  76. self.TextBox_Filter = QLineEdit(self)
  77. self.layout1.addWidget(self.TextBox_Filter)
  78.  
  79. self.layout1.addStretch(1)
  80. self.Btn_Start = QPushButton("&Start", self)
  81. self.layout1.addWidget(self.Btn_Start)
  82.  
  83. self.main_layout.addLayout(self.layout1)
  84.  
  85. # List to show packets
  86. self.List_Pkt = QListWidget(self)
  87. self.main_layout.addWidget(self.List_Pkt)
  88.  
  89. # Tree to see pkt's detail
  90. self.Tree = QTreeWidget(self)
  91. self.main_layout.addWidget(self.Tree)
  92. self.Tree.setColumnCount(2)
  93. self.Tree.setHeaderLabels(['Key', 'Value'])
  94.  
  95. self.setLayout(self.main_layout)
  96.  
  97. # create signal and sniff thread
  98. self.thread = Sniffer()
  99. self.connect(self.Btn_Start, SIGNAL("clicked()"), self.Sniff)
  100. # self.connect(self.thread, SIGNAL("pkt_arrive(str)"), self.OnPktArrive) Connot work!
  101. self.thread.pkt_arrive.connect(self.OnPktArrive)
  102. self.List_Pkt.currentItemChanged.connect(self.On_ItemChanged)
  103.  
  104. @Slot(str)
  105. def OnPktArrive(self, pkt):
  106. print 'received pkt arrive signal'
  107.  
  108. #p = Ether(pkt) #only Ethernet now, 802.11 may be crash!
  109. item = PktListItem(num = self.number)
  110. item.setText(str(self.number) + '\t' + pkt)
  111. self.List_Pkt.addItem(item)
  112. self.number += 1
  113.  
  114. @Slot()
  115. def Sniff(self):
  116. print self.Btn_Start.text()
  117. if self.Btn_Start.text() == '&Start':
  118. self.Btn_Start.setText("&Stop")
  119. self.thread.filter = self.TextBox_Filter.text()
  120. self.thread.iface = self.TextBox_Iface.text()
  121. self.thread.go()
  122. else:
  123. self.Btn_Start.setText("&Start")
  124. self.thread.stop()
  125.  
  126. def On_ItemChanged(self, curr, prev):
  127. print curr.num
  128. self.Tree.clear()
  129. p = global_pkt_list[curr.num]
  130. root1 = QTreeWidgetItem(self.Tree)
  131. if (p.haslayer(Ether)):
  132. root1.setText(0, 'Ethernet:')
  133. child1_1 = QTreeWidgetItem(root1)
  134. child1_1.setText(0, 'dst')
  135. child1_1.setText(1, p.dst)
  136. child1_2 = QTreeWidgetItem(root1)
  137. child1_2.setText(0, 'src')
  138. child1_2.setText(1, p.src)
  139. child1_3 = QTreeWidgetItem(root1)
  140. child1_3.setText(0, 'type')
  141. child1_3.setText(1, hex(p.type))
  142. p = p.getlayer(1)
  143. if (p.haslayer(IP)):
  144. self._SetIPTree(p)
  145. p = p.getlayer(1)
  146. if (p.haslayer(ICMP)):
  147. self._SetICMPTree(p)
  148. elif (p.haslayer(TCP)):
  149. pass
  150. else:
  151. pass
  152. elif (p.haslayer(IPv6)):
  153. pass
  154. else:
  155. root1.setText(0, 'Not Ethernet')
  156. root1.setText(1, hexdump(p))
  157.  
  158. def _SetIPTree(self, p):
  159. root2 = QTreeWidgetItem(self.Tree)
  160. root2.setText(0, 'IPv4')
  161. child2_1 = QTreeWidgetItem(root2)
  162. child2_1.setText(0, 'Version')
  163. child2_1.setText(1, str(p.version))
  164. child2_2 = QTreeWidgetItem(root2)
  165. child2_2.setText(0, 'ihl(Header Length)')
  166. child2_2.setText(1, str(p.ihl))
  167. child2_3 = QTreeWidgetItem(root2)
  168. child2_3.setText(0, 'tos')
  169. child2_3.setText(1, str(p.tos))
  170. child2_4 = QTreeWidgetItem(root2)
  171. child2_4.setText(0, 'len')
  172. child2_4.setText(1, str(p.len))
  173. child2_5 = QTreeWidgetItem(root2)
  174. child2_5.setText(0, 'id')
  175. child2_5.setText(1, str(p.id))
  176. child2_6 = QTreeWidgetItem(root2)
  177. child2_6.setText(0, 'flags')
  178. child2_6.setText(1, str(p.flags))
  179. child2_7 = QTreeWidgetItem(root2)
  180. child2_7.setText(0, 'frag')
  181. child2_7.setText(1, str(p.frag))
  182. child2_8 = QTreeWidgetItem(root2)
  183. child2_8.setText(0, 'TTL')
  184. child2_8.setText(1, str(p.ttl))
  185. child2_9 = QTreeWidgetItem(root2)
  186. child2_9.setText(0, 'protocol')
  187. child2_9.setText(1, str(p.proto))
  188. child2_10 = QTreeWidgetItem(root2)
  189. child2_10.setText(0, 'checksum')
  190. child2_10.setText(1, str(p.chksum))
  191. child2_11 = QTreeWidgetItem(root2)
  192. child2_11.setText(0, 'src')
  193. child2_11.setText(1, str(p.src))
  194. child2_12 = QTreeWidgetItem(root2)
  195. child2_12.setText(0, 'dst')
  196. child2_12.setText(1, str(p.dst))
  197.  
  198. def _SetICMPTree(self, p):
  199. root3 = QTreeWidgetItem(self.Tree)
  200. root3.setText(0, 'ICMP')
  201. child3_1 = QTreeWidgetItem(root3)
  202. child3_1.setText(0, 'Type')
  203. if (p.type == 8):
  204. child3_1.setText(1, 'echo request')
  205. elif (p.type == 0):
  206. child3_1.setText(1, 'echo reply')
  207. else:
  208. child3_1.setText(1, str(p.type))
  209. child3_2 = QTreeWidgetItem(root3)
  210. child3_2.setText(0, 'Code')
  211. child3_2.setText(1, str(p.code))
  212. child3_3 = QTreeWidgetItem(root3)
  213. child3_3.setText(0, 'Checksum')
  214. child3_3.setText(1, str(p.chksum))
  215. child3_4 = QTreeWidgetItem(root3)
  216. child3_4.setText(0, 'ID')
  217. child3_4.setText(1, str(p.id))
  218. child3_5 = QTreeWidgetItem(root3)
  219. child3_5.setText(0, 'Sequence number')
  220. child3_5.setText(1, str(p.seq))
  221. child3_6 = QTreeWidgetItem(root3)
  222. child3_6.setText(0, 'Data')
  223. child3_6.setText(1, binascii.b2a_hex(str(p.load)))
  224.  
  225. def run(self):
  226. self.show()
  227.  
  228. if __name__ == '__main__':
  229. # Create an instance of the application window and run it
  230. win = MainWindow()
  231. win.run()
  232. qt_app.exec_()

Python写的嗅探器——Pyside,Scapy的更多相关文章

  1. 用python写网路爬虫 PDF高清完整版免费下载 Python基础教程免费电子书 python入门书籍免费下载

    <用python写网路爬虫PDF免费下载>PDF书籍下载 内容简介 作为一种便捷地收集网上信息并从中抽取出可用信息的方式,网络爬虫技术变得越来越有用.使用Python这样的简单编程语言,你 ...

  2. Python写各大聊天系统的屏蔽脏话功能原理

    Python写各大聊天系统的屏蔽脏话功能原理 突然想到一个视频里面弹幕被和谐的一满屏的*号觉得很有趣,然后就想用python来试试写写看,结果还真玩出了点效果,思路是首先你得有一个脏话存放的仓库好到时 ...

  3. python写红包的原理流程包含random,lambda其中的使用和见简单介绍

    Python写红包的原理流程 首先来说说要用到的知识点,第一个要说的是扩展包random,random模块一般用来生成一个随机数 今天要用到ramdom中unifrom的方法用于生成一个指定范围的随机 ...

  4. Python写地铁的到站的原理简易版

    Python地铁的到站流程及原理(个人理解) 今天坐地铁看着站牌就莫名的想如果用Python写其工作原理 是不是很简单就小试牛刀了下大佬们勿喷纯属小弟个人理解 首先来看看地铁上显示的站牌如下: 就想这 ...

  5. 用Python写一个简单的Web框架

    一.概述 二.从demo_app开始 三.WSGI中的application 四.区分URL 五.重构 1.正则匹配URL 2.DRY 3.抽象出框架 六.参考 一.概述 在Python中,WSGI( ...

  6. 读书笔记汇总 --- 用Python写网络爬虫

    本系列记录并分享:学习利用Python写网络爬虫的过程. 书目信息 Link 书名: 用Python写网络爬虫 作者: [澳]理查德 劳森(Richard Lawson) 原版名称: web scra ...

  7. Python写UTF8文件,UE、记事本打开依然乱码的问题

    Python写UTF8文件,UE.记事本打开依然乱码的问题 Leave a reply 现象:使用codecs打开文件,写入UTF-8文本,正常无错误.用vim打开正常,但记事本.UE等打开乱码. 原 ...

  8. 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 ...

  9. python写xml文件

    为了便于后续的读取处理,这里就将信息保存在xml文件中,想到得到的文件如下: 1 <?xml version="1.0" encoding="utf-8" ...

随机推荐

  1. 超级简单的Android Studio jni 实现(无需命令行)

    1.配置Anroid Studio(这步是关键) 使用[command+,] 打开Preferences,选择External Tools,点击加号框如下图: Paste_Image.png 点击+号 ...

  2. AE属性表操作

    转自chanyinhelv原文AE属性表操作 实现的操作包括:1.打开属性表:2.编辑属性表:3.增加属性列:4.数据排序:5.字段计算…… 嗯,实现的功能目前就这些吧,后续还会继续跟进,还望大家多多 ...

  3. Eclipse Che安装入门和使用(一)

    Eclipse Che序列博文如下: 安装和调试篇:Eclipse Che安装入门和使用(一) Web进阶篇:Eclipse Che开发Spring Web应用(入门) (二) 本文摘要: Eclip ...

  4. reduce 阶段遍历对象添加到ArrayList中的问题

    起初遍历values时直接把对象添加到集合中,后来输出结果和预期不符,debug时发现添加到集合中的对象的值全部是最后一个对象的值,网上百度了下,发现是reduce阶段对象重用的问题,reduce阶段 ...

  5. C++请求web service与xml解析

    1. C++解析XML的开源库 在项目中XML的解析使用的是开源的第三方库,TinyXML:这个解析库的模型通过XML文件,然后再内存中生成DOM模型,从而让我们能够非常方便的遍历这颗XML树. DO ...

  6. 分布式系统和CAP

    帽子理论(CAP): C:Consistency,一致性, 数据一致更新,所有数据变动都是同步的 A:Availability,可用性, 好的响应性能,完全的可用性指的是在任何故障模型下,服务都会在有 ...

  7. Cython 的学习

    开发效率极高的 Python 一直因执行效率过低为人所诟病,Cython 由此诞生,特性介于 Python 和 C 语言之间. Cython 学习 1. Cython 是什么? 它是一个用来快速生成 ...

  8. C#步骤控件

    C#开发step步骤条控件   现在很多的javascript控件,非常的不错,其中step就是一个,如下图所示: 那么如何用C#来实现一个step控件呢? 先定义一个StepEntity类来存储步骤 ...

  9. HDU 1671 (字典树统计是否有前缀)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 Problem Description Given a list of phone number ...

  10. “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 ...