1.安装环境:
- 基于python
- windows操作系统需要安装Microsoft Visual C++ V14.0以上
- linux操作系统则直接基于python安装即可
2.安装mitmproxy
pip install mitmproxy
3.查看版本号
mitmproxy --version # linux mitmdump --version # windows
4.启动
mitmproxy -p 8080 # linux中启动 mitmdump -w text.txt -p 8080 # windows mitmweb -p 8080 # 图像化界面 -w 输出文件 -p 指定端口
5.设置代理
ip:8080 # 默认监听8080端口
6.安装证书
mitm.it # 访问这个网站
7.过滤请求
f
view_filter=!(-c 200) # 请求状态码不是200的
view_filter=-d baidu.com # 显示url包含百度的请求
view_filter=-d baidu.com & -m post # 显示url包含百度的请求和请求方式是post的
8.断点拦截
i
intercept= -d baidu.com & -m get # 域名包含 baidu.com和请求方式是get的
9.和python的交互
from mitmproxy import ctx # 日志模块
def request(flow):
print(flow.request.headers) # 请求头信息
ctx.log.info(str(flow.request.headers)) # 输出颜色:白色 输入日志 必须是文本格式
ctx.log.warn(str(flow.request.headers)) # 输出颜色:黄色
ctx.log.error(str(flow.request.headers)) # 输出颜色:红色
ctx.log.error(str(flow.request.url)) # url
ctx.log.error(str(flow.request.host)) # 主机头
ctx.log.error(str(flow.request.method)) # 请求方式
ctx.log.error(str(flow.request.path)) # 请求路径
def response(flow):
ctx.log.error(str(flow.response.status_code)) # 相应状态码
ctx.log.error(str(flow.response.text)) # 返回内容
from mitmproxy import ctx
# 所有发出的请求数据包都会被这个方法所处理
# 所谓的处理,我们这里只是打印一下一些项;当然可以修改这些项的值直接给这些项赋值即可
def request(flow):
# 获取请求对象
request = flow.request
# 实例化输出类
info = ctx.log.info
# 打印请求的url
info(request.url)
info(request.get_url()) # 请求的url
info(request.get_query()) # 得到请求的url的参数,被存放成了字典
# 打印请求方法
info(request.method)
# 打印host头
info(request.host)
# 打印请求端口
info(str(request.port))
# 打印所有请求头部
info(str(request.headers))
# 打印cookie头
info(str(request.cookies))
# 请求的内容,如果请求时post,那么content就是指代post的参数。
info(str(request.content))
# 所有服务器响应的数据包都会被这个方法处理
# 所谓的处理,我们这里只是打印一下一些项
def response(flow):
# 获取响应对象
response = flow.response
# 实例化输出类
info = ctx.log.info
# # 打印响应码
# info(str(response.status_code))
# # 打印所有头部
# info(str(response.headers))
# # 打印cookie头部
# info(str(response.cookies))
# 打印响应报文内容
info(str(response.text))
print(info(str(response.text)))
mitmdump.exe -s 03-a.py 03-a.py 为python文件名称 默认监听端口为8080
10.示例
from mitmproxy import ctx
import re, json, pymysql
parent_id = 0
# 所有发出的请求数据包都会被这个方法所处理
# 所谓的处理,我们这里只是打印一下一些项;当然可以修改这些项的值直接给这些项赋值即可
def request(flow):
# 获取请求对象
request = flow.request
if 'https://api.yst.vodjk.com/v1/course?keyid=1004' in request.url:
f = str(request.content)
global parent_id
# parentid=1236
parent_id = re.findall(r'parentid=(\d+)', str(request.content))[0]
print('******{}'.format(parent_id))
def response(flow):
# 获取响应对象
response = flow.response
data_json = json.loads(response.text)
db = pymysql.connect(host='localhost', port=3306, database='cfda', user='root', password='root',
charset='utf8')
cursor = db.cursor()
for data in data_json['data']['childcat']['items']:
item = {}
item['parent_id'] = parent_id
item['ids'] = data['id']
item['name'] = data['name']
item['cattype'] = data['cattype']
item['image'] = data['image']
item['haschild'] = data['haschild']
num = cursor.execute(
'select id from yaoshitong_class_1 where parent_id="{}" and ids={}'.format(item['parent_id'],
item['ids']))
if not num:
list_key = []
list_lalues = []
for key, lalues in item.items():
list_key.append(key)
list_lalues.append("'" + str(lalues).replace("'", "‘") + "'")
# 拼接sql语句
insert_sql = 'insert into yaoshitong_class_1({}) values({})'.format(', '.join(list_key),
', '.join(list_lalues))
print('insert_sql:', insert_sql)
cursor.execute(insert_sql)
db.commit()
mitmdump.exe -s 03-a.py 03-a.py 为python文件名称