一、绪论

最近一直想弄一个代理,并且对数据包进行解读,从而完成来往流量的嗅探。于是今天学习了一下如何使用Python抓包并进行解包。

首先要用到两个模块

dpkt(我这边ubuntu16.04 LTS)Python2.7中默认安装的

pcap安装

 pip install pypcap

然后来说一下,pypcap主要用来抓包或者说是sniffer的,dpkt用来解包的,我对dpkt的认知是解包传输层以及传输层一下的数据比较不错,但是对于应用层数据的解读就是渣渣。尤其是HTTP,所以HTTP部分解包,是我自己重写的,并没有使用dpkt.http.Request和dpkt.http.Response。(总他妈报错).

二、目前做到:

(1)正常解码请求和响应数据包。

(2)对于长连接传输数据的数据包解读失败。

三、先来讲抓包

 import pcap
import dpkt sniffer = pcap.pcap(name="eth1") #name parameter => interface name
sniffer.setfilter("tcp") #filter sentence
for packet_time packet_data in sniifer:
pass # packet_time => packet receive time
# packet_data => ethernet level data

四、解包:

 packet = dpkt.ethernet.Ethernet(pdata)#二层数据报文嘛
print "SRC IP:%d.%d.%d.%d"%tuple(map(ord,list(packet.data.src)))
print "DST IP:%d.%d.%d.%d"%tuple(map(ord,list(packet.data.dst)))
print "SRC PORT:%s"%packet.data.data.sport
print "DST PORT:%s"%packet.data.data.dport

五、HTTP部分是我自己解的包:

 def http_request_analyst(string):
string = string[1:-1]
method = string.split(" ")[0]
print "Method:",method
path = string.split(" ")[1]
print "Path:",urllib.unquote(path)
protover = string.split(" ")[2].split("\\r\\n")[0]
print "Protocol Version:",protover
headers = string.split("\\r\\n\\r\\n")[0].split("\\r\\n")[1:]
print "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
print "Header:"
for header in headers:
header = header.split(":")
try:
hstr = "%s:%s"%(str(header[0]),str(header[1])) if header[0] not in ["Referer"] else "%s:%s:%s"%(str(header[0]),str(header[1]),str(header[2]))
except Exception,ex:
print "[*]",ex
print header
raw_input()
print hstr
print "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
print "Data:",string.split("\\r\\n")[-1]
 def http_response_analyst(string):
string = string[1:-1]
protover = string.split(" ")[0]
print "Protocol Version:",protover
status_code = string.split(" ")[1]
print "Response Code:",status_code
status_string = string.split(" ")[2].split("\\r\\n")[0]
print "Reposne String:",status_string
headers = string.split("\\r\\n\\r\\n")[0].split("\\r\\n")[1:]
print repr(headers)
print repr(string)
print "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
print "Header:"
for header in headers:
header = header.split(":")
try:
hstr = "%s:%s"%(str(header[0]),str(header[1])) if header[0] not in ["Referer"] else "%s:%s:%s"%(str(header[0]),str(header[1]),str(header[2]))
except Exception,ex:
print "[*]",ex
print header
raw_input()
print hstr
print "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
print "Data:",string.split("\\r\\n")[-1]

六、效果如图了:

python 进行抓包嗅探的更多相关文章

  1. 小白日记22:kali渗透测试之提权(二)--抓包嗅探

    抓包嗅探 通过抓包嗅探目标机器的流量,发现账号密码. Windows系统 1.Wirehshark 2.Omnipeek 3.commview 4.Sniffpass 只会抓取识别传输密码的明文协议, ...

  2. Python—网络抓包与解包(pcap、dpkt)

    pcap安装 [root@localhost ~]# pip install pypcap 抓包与解包 # -*- coding:utf-8 -*- import pcap, dpkt import ...

  3. Python + winpcap抓包和发包

    winpcapy Python的winpcapy库可以简单地实现收发Layer2层(数据链路层,以太网)数据. winpcapy主页:https://github.com/orweis/winpcap ...

  4. python抓包截取http记录日志

    #!/usr/bin/python import pcap import dpkt import re   def main(): pc=pcap.pcap(name="eth1" ...

  5. python编写网络抓包分析脚本

    python编写网络抓包分析脚本 写网络抓包分析脚本,一个称手的sniffer工具是必不可少的,我习惯用Ethereal,简单,易用,基于winpcap的一个开源的软件 Ethereal自带许多协议的 ...

  6. python socket+tcp三次握手四次撒手学习+wireshark抓包

    Python代码: server: #!/usr/bin/python # -*- coding: UTF-8 -*- # 文件名:server.py import socket # 导入 socke ...

  7. Python+Requests接口测试教程(1):Fiddler抓包工具

    本书涵盖内容:fiddler.http协议.json.requests+unittest+报告.bs4.数据相关(mysql/oracle/logging)等内容.刚买须知:本书是针对零基础入门接口测 ...

  8. Python 爬虫——抖音App视频抓包

    APP抓包 前面我们了解了一些关于 Python 爬虫的知识,不过都是基于 PC 端浏览器网页中的内容进行爬取.现在手机 App 用的越来越多,而且很多也没有网页端,比如抖音就没有网页版,那么上面的视 ...

  9. jmeter压测数据库,抓包工具,python基础

    jmeter压力测试 前提场景的设置:单场景(单个接口进行压力测试一个请求)或混合场景(有业务流程的场景进行压力测试多个请求),压测时间一般在5--1515分组具体看需求. 数据准备:数据量少和数据量 ...

随机推荐

  1. (转)windows环境vue+webpack项目搭建

    首先,vue.js是一种前端框架,一般利用vue创建项目是要搭配webpack项目构建工具的,而webpack在执行打包压缩的时候是依赖node.js的环境的,所以,要进行vue项目的开发,我们首先要 ...

  2. LintCode - Copy List with Random Pointer

    LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Descripti ...

  3. ip辅助和别名的区别

    更流畅 IP 别名和辅助 IP 地址 2017-01-25 12:05             838人阅读             评论(0)             收藏              ...

  4. 【安装Python环境】之安装Selenium2时报UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 12: invalid continuation byte问题

    问题描述: windows8.1系统,Python3环境安装Selenium2时报错,错误如下: ..... ..... File "F:\软件\python3.6.1\lib\site-p ...

  5. windows宿主机ping不通Docker容器的解决办法

      网卡上有       docker is not a virtual machine, and you don't get access to the docker host via IP add ...

  6. 联合主键用hibernate注解映射方式主要有三种:

    将联合主键的字段单独放在一个类中,该类需要实现java.io.Serializable接口并重写equals和hascode 第一.将该类注解为@Embeddable,最后在主类中(该类不包含联合主键 ...

  7. seqtk 的安装和使用

    seqtk 是一款针对fasta/fastq 文件进行处理的小程序,有很多的功能,速度很快,很方便: 源代码:https://github.com/lh3/seqtk 安装: git clone ht ...

  8. 【Java面试题】41 两个对象值相同(x.equals(y) == true),但却可有不同的hash code,这句话对不对?

    对. 如果对象要保存在HashSet或HashMap中,它们的equals相等,那么,它们的hashcode值就必须相等. 如果不是要保存在HashSet或HashMap,则与hashcode没有什么 ...

  9. js使用正则表达式从url中获取参数值

    //从url中获取参数值 function getvl(name) { var reg = new RegExp("(^|\\?|&)"+ name +"=([^ ...

  10. logging.xml file setfile(null,true) call failed

    定义目录三个方法:一:${catalina.base}或${catalina.home}相对路径配置方法.catalina.home是你配置服务器时自动在环境变量中加的路径,默认是指向tomcat服务 ...