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
0
投稿
猜你喜欢
- 一、题目描述本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。小明刚刚找到工作,老板人很好,只是老板夫人很爱购物。老板
- 知道python有这几个内置方法,但一直以来用的都不多,最近重新看了一下,重新记录一下。map()会根据提供的函数对指定序列进行映射,pyt
- 本文主要分享基于python的数据分析三方库pandas,numpy的一次爬坑经历,发现并分析了python语言对于浮点数精度处理不准确的问
- -crop参数是从一个图片截取一个指定区域的子图片.格式如下:convert -crop widthxheight{+-
- 前言SciPy 是一个开源的 Python 算法库和数学工具包, Scipy 是基于 Numpy 的科学计算库,用于数学、科学、工程学等领域
- AES加密方式有五种 : ECB, CBC, CTR, CFB, OFB从安全性角度推荐cbc算法windows 下安装 : pip ins
- 有一个优秀的库可以使用————demjson示范链接http
- 概述在绝大部分的开发语言中与实际开发过程中,Dictionary扮演着举足轻重的角色。从我们的数据模型到服务器返回的参数到数据库的应用等等,
- rem ---表单提示函数 Being-----------------------------CODE Copy ... Fu
- 本文实例讲述了python获取指定网页上所有超链接的方法。分享给大家供大家参考。具体如下:这段python代码通过urllib2抓取网页,然
- 看到sam关于max-height的文章,觉得按捺不住了。sam注重于样式表的写法,过多的要求div+css的布局,sam可是追求艺术的人哦
- 前言:今天我来分享几个好用到爆的Pycharm插件,在安装上之后,你的编程效率、工作效率都能够得到极大地提升。一、安装方法插件的安装方法一点
- 模块:xmllibxmllib 是一个非验证的低级语法分析器。应用程序员使用的 xmllib 可以覆盖 XMLParser 类,并提供处理文
- 一. 安装 Beautiful Soup首先,您需要安装 Beautiful Soup。在终端或命令提示符中运行以下命令:pip insta
- 本文实例讲述了python单向链表的基本实现与使用方法。分享给大家供大家参考,具体如下:# -*- coding:utf-8 -*-#! p
- 情感短文本分类TextRNN是一种循环神经网络(RNN)结构,特别适用于处理序列数据。它通过将上一个时刻的隐状态与当前时刻的输入进行结合,来
- 一、高斯滤波 高斯滤波是一种线性平滑滤波,适用于消除高斯噪声,广泛应用于图像处理的减噪过程。 [1] 通俗的讲,高斯滤波就是对整幅图像进
- 一、介绍在做YOLOv3项目时,会需要将文本文件中的某部分内容进行批量替换和修改,所以编写了python程序批量替换所有文本文件 * 定部分的
- 动态变量名赋值在使用 tkinter 时需要动态生成变量,如动态生成 var1...var10 变量。使用 exec 动态赋值exec 在
- 在网页设计初期,我们会先收集大家对设计方向的期许,我们经常收集到这样的建议:产品经理:要易用,要简洁设计总监:要特色,要亮点部门经理:要大气