python如何利用Mitmproxy抓包
作者:南方的墙 发布时间:2022-06-15 13:18:16
标签:python,Mitmproxy,抓包
一、使用
安装
pip install mitmproxy
mitmproxy 是具有控制台界面的交互式,支持SSL的拦截代理
mitmdump是mitmproxy的命令行版本。想想tcpdump为HTTP
mitmweb 是一个基于web的界面,适用于mitmproxy
mitmproxy(mac)、mitmdump、mitmweb(win) 这三个命令中的任意一个即可
mitmweb -s mitm.py
命令行启动默认端口8080mitmweb -p 8888 -s mitm.py
指定端口8888
ctrl+c退出
启动后设置电脑或手机代理(电脑ip,端口8888),安装证书
打开 cmd,执行 "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --proxy-server=10.12.2.28:8888 --ignore-certificate-errors
二、过滤、修改
"""
flow.request.scheme 请求协议
flow.request.host 请求host
flow.request.url 请求URL链接
flow.request.method 请求方法
flow.request.query 请求URL查询参数
flow.request.path 请求URL https://www.baidu.com/
flow.request.path_components #请求URL不包含域名的元祖 ('project', 'classify', 'list')
flow.request.urlencoded_form 请求POST数据
flow.response.status_code HTTP响应状态码
flow.response.headers HTTP响应头信息
flow.response.get_text HTTP响应内容
"""
class Counter:
def __init__(self):
self.result = {} # 存接口请求和返回信息
# url filter 去掉
self.url_filter = ['baidu.com','qq.com','360']
# url screen 仅访问
self.url_race = ['10.162.16.39:8091']
# http static extension
self.static_ext = ['js', 'css', 'ico', 'jpg', 'png', 'gif', 'jpeg', 'bmp','xml']
# http Content-Type
self.static_files = ['text/css','image/jpeg', 'image/gif','image/png','text/html','application/octet-stream','application/x-protobuf']
# http Content-Type media resource files type
self.media_types = ['image', 'video', 'audio']
def parser_data(self,query,data = {}):
for key, value in query.items():
data[key] = value
return data
def get_extension(self, url_tup):
if not url_tup:
return ''
else:
end_path = url_tup[-1]
split_ext = end_path.split('.') #1148e88a9d97.jpg #list
return '' if not split_ext or len(split_ext) == 1 else split_ext[-1]
# 拒绝连接
def http_connect(self, flow: mitmproxy.http.HTTPFlow):
for i in self.url_filter: #过滤url
if i in flow.request.host:
flow.response = http.HTTPResponse.make(404)
#存在筛选就返回true拦截,Flase通过
def capture_pass(self,request,response):
if self.url_race:
if request.host not in self.url_race: #筛选url
return True
url_tup = request.path_components #获取url的tup
extension = self.get_extension(url_tup)
if extension in self.static_ext: #判断后缀
return True
try:
content_type = response.headers['Content-Type'].split(';')[0]
if not content_type:
return False
elif content_type in self.static_files: #判断Content-Type
return True
else:
http_mime_type = content_type.split('/')[0]
if http_mime_type in self.media_types: #判断Content-Type的files type
return True
else:
return False
except Exception:
return False
def request(self, flow: mitmproxy.http.HTTPFlow):
request = flow.request
# 修改请求头
# request.headers["shuzf"] = "shuzf"
# # 修改get参数
# if "shuzf" in flow.request.query.keys():
# request.query.set_all("shuzf", ["舒志福"])
# # 修改post参数
# if "shuzf" in flow.request.urlencoded_form.keys():
# request.urlencoded_form.set_all('shuzf', '舒志福')
scheme = request.scheme
domain = request.host
self.result['url'] = parse.unquote(request.url) # url解码
self.result['method'] = request.method
self.result['request_headers'] = {}
for item in request.headers:
self.result['request_headers'][item] = request.headers[item]
self.result['get_data'] = self.parser_data(request.query) # 将表单转字典
self.result['post_data'] = self.parser_data(request.urlencoded_form) # 将表单转字典
def response(self, flow: mitmproxy.http.HTTPFlow):
request = flow.request
response = flow.response
# # 修改返回头
# response.headers["shuzf"] = "shuzf"
# # 修改返回体
# text = response.text
# text = text.replace("shuzf", "舒志福")
# flow.response.set_text(text)
if not self.capture_pass(request,response):
print(request.url)
self.result['status_code '] = response.status_code
self.result['response_headers'] = {}
for item in response.headers:
self.result['response_headers'][item] = response.headers[item]
# HTTPResponse内部使用了iso-8859-1编码,先进行解码为Unicode再进行utf-8编码 response.text.encode("iso-8859-1").decode("utf-8")
self.result['response_content'] = response.text
# 添加result至数据库
new_url = Proxy(url=self.result['url'],res=self.result['response_content'], content=json.dumps(self.result))
session.add(new_url)
session.commit()
# 关闭session:
# session.close()
addons = [Counter()] # 实例类
来源:https://www.cnblogs.com/shuzf/p/12157240.html


猜你喜欢
- 今天出于需要,要将爬虫爬取的一些数据整理成二维数组,再编码成json字符串传入数据库那么问题就来了,在php中这个过程很简便 ,类似这样:
- mysql数据库自不必说,现在很多程序开发应用及站长们常用到,jdbc可能一般会比较陌生一些,jdbc是什么意思?这里也会提到,最主要的是为
- 主备同步,也叫主从复制,是MySQL提供的一种高可用的解决方案,保证主备数据一致性的解决方案。在生产环境中,会有很多不可控因素,例如数据库服
- <script language="javascript"> /* &nb
- 1线性回归1.1简单线性回归在简单线性回归中,通过调整a和b的参数值,来拟合从x到y的线性关系。下图为进行拟合所需要优化的目标,也即是MES
- pytorch retain_graph==True的作用说明总的来说进行一次backward之后,各个节点的值会清除,这样进行第二次bac
- 密码与通信密码技术是一门历史悠久的技术。信息传播离不开加密与解密。密码技术的用途主要源于两个方面,加密/解密和签名/验签在信息传播中,通常有
- --BEGIN DISTRIBUTED TRANSACTION [transactionname]--标志一个由分布式事务处理协调器MSDT
- 如下所示:#!/usr/bin/env python#-*- coding: utf-8 -*-"""[0,
- 起因:有一批数据需要每个月进行分析,数据存储在excel中,行标题一致,需要横向合并进行分析。数据示意:具有多个代码:# -*- codin
- 不同类型的语言支持不同的数据类型,比如 Go 有 int32、int64、uint32、uint64 等不同的数据类型,这些类型占用的字节大
- 首先要说明的是,这个标题有点标题党的意思,这个 bug 也存在于 IE8 下,在 IE6 和 IE7 下正常。之前写过两篇关于 I
- 如果内部修改外部变量需要nonlocal,globaldef f1(): print("in f1..") num=11
- With语句是什么?有一些任务,可能事先需要设置,事后做清理工作。对于这种场景,Python的with语句提供了一种非常方便的处理方式。一个
- window运行:regedit然后找到HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Contro
- 废话不多说,直接开干!抖音字符视频在今年火过一段时间。反正我是始终忘不了那段刘耕宏老师本草纲目的音乐…这一次自己也来实
- 按住Ctrl,点击函数名称,即可跳转到该函数的代码文件中选中后,函数显示有下划线:点击函数名称,跳转到该函数所在的文件里:补充:pychar
- 本文实例讲述了Codeigniter发送邮件的方法。分享给大家供大家参考。具体分析如下:Codeigniter的邮件发送支持一下特性:Mul
- 前言:在上篇博文中使用了matplotlib绘制了3D小红花,本篇博客主要介绍一下3D小红花的绘制原理。1. 极坐标系对于极坐标
- 前言最近很多新入职的同事问这个问题,特别是通过 homebrew 自动安装的 mysql ,其版本默认已经是 8.0 了,由于增加了一些安全