别错过机会,人生比你想象中的要短!

参考:
- App 爬虫神器!Mitmproxy 快速带你入坑!
爬虫抓包原理
爬虫的基本原理就是模拟客户端(可以是浏览器,也有可能是APP)向远程服务器发送 HTTP 请求,我们需要知道目标服务器的 HOST、URI、请求方法、请求参数、请求头、请求体。Web 网站通过 Chrome 浏览器可以找到这些信息。但是对于 APP 爬虫,就显得有些束手无策。这时候必须借用一些抓包工具来辅助我们分析APP背后的秘密。而抓包本质是引入多了一层中间人代理,如下图所示。
抓包和爬虫是两个不同的概念:
抓包是为了查看分析网络请求,是为了了解对方的过程。
爬虫则是模拟网络请求,当了解对方后模拟请求获取数据的工具,两者相辅相成,先抓包分析,再程序模拟,就是爬虫的开发过程,以此来达到你爬虫的目的。
Fiddler 是 Windows 平台最好用的可视化抓包工具,也是大家最熟知的 HTTP 代理工具,功能非常强大,除了可以清晰的了解每个请求与响应之外,还可以进行断点设置,修改请求数据、拦截响应内容。
4-1 fiddler抓包工具介绍,file&&edit功能使用


Result:HTTP状态码
Protocol:请求使用的协议,如HTTP/HTTPS/FTP等
HOST:请求地址的主机名或域名
URL:请求资源的位置
Body:请求大小
Caching:请求的缓存过期时间或者缓存控制值
Content-Type:请求响应的类型
Process:发送此请求的进程ID
Comments:备注
Custom:自定义值
Fiddler右边面板介绍
Statistics页签
通过该页签, 用户可以通过选择多个会话来得来这几个会话的总的信息统计,比如多个请求和传输的字节数。选择第一个请求和最后一个请求, 可获得整个页面加载所消耗的总体时间。从条形图表中还可以分别出哪些请求耗时最多, 从而对页面的访问进行访问速度优化
inspectors页签
它提供headers、textview、hexview,Raw等多种方式查看单条http请求的请求报文的信息,它分为上下两部分:上部分为HTTP Request(请求)展示,下部分为HTTPResponse(响应)展示
AutoResponse页签
Fiddler最实用的功能, 它可以抓取在线页面保存到本地进行调试, 大大减少了在线调试的困难, 可以让我们修改服务器端返回的数据, 例如让返回都是HTTP404或者读取本地文件作为返回内容。
可设置打开某网页显示自己想要的内容,比如抓取百度链接,点击add rule,设置如下所示:
4-2 tooles功能使用
1. Options
证书



端口8889和允许远端连接

其他默认
注意:
无法上网的可能原因;
1.开了VPN;
2.正确安装证书, fiddler设置后要关掉重新打开
2. WinINET Options
代理设置

3. TextWizard编码转换

HOSTS hosts修改
switchyomegal 插件

创建情景模式


4-3 rules功能使用&工具栏详讲
1.Breakpoints

2. Simulate Modem Speeds
4-5 请求前断点设置,响应后断点,命令行请求前后断点设置,网页重定向
1. 断点设置

通过图形界面会拦截到所有的网站,
通过命令行可以拦截到特定域名

2. 基于命令行拦截

# 拦截特定域名
bpu https://www.baidu.com/
# 取消拦截
bpu
# 访问之前拦截特定域名(响应包拦截)
bpafter https://www.baidu.com/
# 取消
bpa
3. 重定向
wd 修改为 python 爬虫

4. 修改百度图片


4-6 手机安装证书,设置fiddler抓取移动端数据包
1. 查看IP
1. win 网络设置查看

2. 命令行查看
$ ipconfig
Windows IP 配置
以太网适配器 以太网:
连接特定的 DNS 后缀 . . . . . . . : DHCP HOST
本地链接 IPv6 地址. . . . . . . . : fe80::183e:9e88:d02b:4977%18
IPv4 地址 . . . . . . . . . . . . : 192.168.0.104
子网掩码 . . . . . . . . . . . . : 255.255.255.0
默认网关. . . . . . . . . . . . . : 192.168.0.1
以太网适配器 VMware Network Adapter VMnet1:
连接特定的 DNS 后缀 . . . . . . . :
本地链接 IPv6 地址. . . . . . . . : fe80::5d44:f68:4a9f:f0da%4
IPv4 地址 . . . . . . . . . . . . : 192.168.255.1
子网掩码 . . . . . . . . . . . . : 255.255.255.0
默认网关. . . . . . . . . . . . . :
以太网适配器 VMware Network Adapter VMnet8:
连接特定的 DNS 后缀 . . . . . . . :
本地链接 IPv6 地址. . . . . . . . : fe80::811a:ad04:79f6:fec5%8
IPv4 地址 . . . . . . . . . . . . : 192.168.57.1
子网掩码 . . . . . . . . . . . . : 255.255.255.0
默认网关. . . . . . . . . . . . . :
以太网适配器 SSTAP 1:
3. IP 网站
2. fiddler 安装手机证书和抓包
1.查看win 本路由下的IP=192.168.0.104


开启只从手机获取


输入网址,获得fiddler提供证书的页面

手机安装fiddler证书,设置 密码为abcd1234


抓取手机百度 请求成功

4-7 mitmproxy软件移动设备安装证书、移动设备抓包
1. mitmproxy介绍


2. Mitmproxy 抓包工具安装
windows安装
在正式安装之前,我们先来看看 Mitmproxy 需要一个怎样的安装环境。
1.基于 Python 环境(我电脑使用的是:Python 3.7)py3.6(pip install mitmproxy=5.0.1)
2.Windows 操作系统中需要安装 Microsoft Visual C++ V14.0 以上(否则会出现保存无法安装)
3.Linux 操作系统则直接基于 Python 安装即可
首先,我们需要打开我们的命令行工具 CMD ,直接在命令行命令输入以下命令:
pip install mitmproxy
pip install mitmdump
# 查看是否安装成功
mitmproxy --version
您可以运行mitmdump或mitmweb代替 所以也就是说 mitmproxy 有两个组件,一个是 mitmdump 另一个是 mitmweb 也就是这三个我们用哪个都可以。
一般我们抓包用的是:mitmproxy ,
在与 Python 交互的时候用哪一个呢?——我们用的是 mitmdump
而在 Windows 里面我们用 mitmweb ,
在 Linux 里面使用 mitmproxy 。
在 Linux 与 Windows 里面都是用 mitmdump 与 Python 交互。
那接下来我们来看一下 mitmdump 的版本号,命令如下:
# windows
mitmdump --version
Mitmproxy: 5.0.1
Python: 3.6.5
OpenSSL: OpenSSL 1.1.0j 20 Nov 2018
Platform: Windows-10-10.0.19041-SP0
# pip install mitmproxy=5.0.0
Linux安装
pip install mitmproxy
(venv) user1@imooc:~$ mitmdump --version
Mitmproxy: 5.3.0
Python: 3.6.9
OpenSSL: OpenSSL 1.1.1h 22 Sep 2020
Platform: Linux-5.4.0-56-generic-x86_64-with-debian-buster-sid
3. Mitmproxy 抓包工具使用
先在Linux启动Mitmproxy ,win中仅能使用mitmdump\mitmweb


设定端口号
# 设定端口号
mitmproxy -p 8889
修改手机代理IP地址

an

安装证书
证书描述:
mitmproxy-ca.pemPEM格式的证书私钥
mitmproxy-ca-cert.pemPEM格式证书,适用于大多数非Windows平台
mitmproxy-ca-cert.p12PKCS12格式的证书,适用于Windows平台
mitmproxy-ca-cert.cer与mitmproxy-ca-cert.pem相同,只是改变了后缀,适用于部分Android
windows证书安装
双击mitmproxy-ca.p12,就会出现导入证书的引导页,一直下一步,选择受信任的根证书颁发机构。
mac 证书安装:
Mac下双击mitmproxy-ca-cert.pem即可弹出钥匙串管理页面,然后找到mitmproxy证书,打开其设置选项,选择“始终信任”即可,如图所示。

安卓手机就选择安卓;

IP地址有变化,就要重新安装证书

mitmproxy常见错误
1.Linux/Win10 系统下要关闭防火墙;否则不会弹出安装证书的状态;
# 关闭运行的防火墙
systemctl stop firewalld.service
# 查看防火墙状态
systemctl status firewalld.service
# 永久禁止防火墙服务,下次重启也不会开启
systemctl disable firewalld.service
Mitmproxy 用法
Z | 清除所有 |
↑↓ | 上下左右移动 |
q | 返回上一级、退出 |

4-8 mitmproxy数据包过滤
? 帮助文档
q 返回/退出程序
b 保存response body
f 输入过滤条件
k 上
j 下
h 左
l 右
space 翻页
enter 进入接口详情
z 清屏
e 编辑
r 重新请求
显示所有不是200的请求
: set view_filter '!(~c 200)'

显示所有域名包含baidu.com的请求
显示所有包含qq.com的请求
set view_filter '(~d qq.com)'
set view_filter '(~d qq.com & ~m get)'
1. 设置断点拦截
set view_filter '(~d qq.com & ~m get)'
flow.set @fous url https://xw.qq.com

4-9 mitmdump详讲
# test.py
# 必须这么写
def request(flow):
print(flow.request.headers)
F:\Projects\imooc\u2_project\mitm
$ mitmdump -p 8889 -s test.py
Loading script test.py
Proxy server listening at http://*:8889
192.168.0.104:7175: clientconnect
Headers[(b'Host', b'httpbin.org'), (b'Proxy-Connection', b'keep-alive'), (b'Cache-Control', b'max-age=0'), (b'Upgrade-Insecure-Requests', b'1'), (b'User-Agent', b'Mozilla/5.0 (Linux; Android 5.1.1; SM-G973N Build/PPR1.190810.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/74.0.3729.136 Mobile Safari/537.36'), (b'Accept', b'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3'), (b'Accept-Encoding', b'gzip, deflate'), (b'Accept-Language', b'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7'), (b'X-Requested-With', b'com.android.browser')]
192.168.0.104:7175: GET http://httpbin.org/get
<< 200 OK 787b

4.10 mitmproxy 运行报错注意事项
有报错信息的时候,可以选择 输出入职监控的内容,也可以 加一些参数
1. 日志监控输出
2个输出模块,选取哪一个呢? 可以用日志监控模块
# test.py
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)) # 红色

其他信息
from mitmproxy import ctx
# 必须这么写
def request(flow):
# print(flow.request.headers)
ctx.log.info(str(flow.request.headers)) # 灰色
ctx.log.info(str(flow.request.url)) # 灰色
ctx.log.info(str(flow.request.host)) # 灰色
ctx.log.info(str(flow.request.method)) # 灰色
ctx.log.info(str(flow.request.path)) # 灰色
# ctx.log.warn(str(flow.request.headers)) # 黄色
# ctx.log.error(str(flow.request.headers)) # 红色
def response(flow):
ctx.log.error(str(flow.response.status_code))
ctx.log.error(str(flow.response.text))
2. 添加正确的参数
这种方法能阻止显示 HTPP2有关的报错
mitmproxy --no-http2 -p8889
4-10 app无法抓包探秘-1-限定系统代理接口
1. app无法抓包的3个原因
技术有浅到深

1.1 APP限定系统代理接口
是APP通过一段JAVA代码判断当前是否设置了系统代理,设置了则断开连接;没有则通过


解决方法1:xposed框架






重新启动模拟器:查看已安装

解决方法2:全局代理 proxydroid
需要root

解决方法3:Packet Capture
不需要root, 无法和Python 交互
先安装apk


加密数据包需要 安装证书

点击第一个箭头,就开始抓包了;



4-11 app无法抓包探秘-2-App启用了ssl-pinning技术防止中间人攻击


SSL Pinning 是一种防止中间人攻击(MITM)的技术,主要的机制是在开发的时候就将服务端的证书打包到客户端中,在客户端发起请求->收到服务器发来的证书这一步之后,对收到的证书进行校验,如果收到的证书不被客户端所信任,就直接断开连接不继续请求。

解决方法Justtrustme插件
Justtrustme(APK)可以屏蔽证书验证的过程

插件安装


安装完成后 就可以屏蔽证书验证
4-12 app无法抓包探秘-3-App采用了双向证书绑定技术
巨难破解
1.逆向破解
- 低版本(客户端证书、)接口可能还是可以用的(概率小)

网友评论