Scrape https requests with Charles

  To sum up, use Charles for simple interface stress testing and modifying interface requests. Charles is a very useful packet capture tool. It is usually used for APP development packet capture and debugging, especially HTTPS requests.

1. Install Charles

  Go to the official website ( https://www.charlesproxy.com/ ) to download the software and install it.

  After the installation is complete, turn on the system proxy (this test is carried out in the windows environment), check the menu->Proxy->Windows Proxy, then the windows system proxy is turned on, and you can see the http requests of the system.

  Windows detects whether the system proxy is turned on, IE->Tools->Internet Options->Connection->LAN Settings->Use Proxy Server for LAN->Advanced, as shown in Figure 1.1.

Figure 1.1 View windows system agent

  If the chrome and firefox requests are not captured, you only need to set them as the system proxy, or manually set the proxy address: 127.0.0.1:8888.  

2. Request filtering

  When only the target request data needs to be observed, other request data can be filtered.

1. Setting method 1: Proxy->Recording Settings->Include, click Add to add the address to be observed, as shown in Figure 2.1.

Figure 2.1 Filter network request setting method 1

2. Setting method 2: Enter the Filter address in the Sequence interface, as shown in Figure 2.2.

3. Setting method 3: Temporary filtering, right-click the request to be observed, select Focus, and then check the Focused radio button on the right side of the Sequence interface to temporarily filter the observation request, as shown in Figure 2.2.

Figure 2.2 Filter network request setting method two and three

3. Capture APP request data

  To capture the APP request data, the APP must use the same network segment as the pc where Charles is located, and let the APP request go through the charles proxy.

1. Open and set the APP proxy

  操作步骤:

  1) 开启charles代理,设置:Proxy->Proxy Sesstings->Proxied,设置Port=8888,并选择"Enable transparent HTTP proxying"单选框,开启透明代理。

  2) 设置APP代理,在对应wifi中设置代理,地址为charles所在pc的ip地址,端口为8888。

  开启后,即可抓取APP的http请求。

2、抓取APP的https请求

  在文章《HTTPS到底是个什么鬼?》中,我们已经分析了https的相关资料,https存在的目的就是为了保障通讯数据的隐私性。那么需要抓取https数据,必须安装charles的CA证书,让charles在https交互中起到中间人监听的目的。

  操作步骤:选择Help->SSL Proxying->Install Charles Root Certificate on a Mobile Device or Remote Browser,看到弹出如图3.1对话框。 

图3.1 安装APP之ca证书

  根据提示好像需要设置APP的代理为192.168.155.1:8888,然实际经笔者多次尝试,发现此步骤不需要修改APP的代理(仍为之前的APP代理地址),根据提示修改代理后反而无法打开页面。然后APP浏览器访问地址:chls.pro/ssl,根据提示下载安装ca证书即可,如图3.2所示。 

图3.2 app端安装ssl证书

  安装完毕后,charles默认并不截取https内容,在需要截取的https请求上右击,选择SSL Proxy菜单,即可截取https请求

3、抓取pc的https请求

  选择Help->SSL Proxying->Install Charles Root Certificate,下载安装证书即可。     

四、进行网络调试

1、模拟慢速网络环境

  有时候需要测试APP在慢网速情况下的表现,刚好charles提供了此功能。点击Proxy->Throttle Sessing选项,勾选上Enable Throttling选项,下方可以选择需要模拟的网速,包含常见的3G和4G,如图4.1所示。

图4.1 设置慢速网络环境

2、修改请求参数

  chales提供了调试工具,可以修改请求参数,在Sequence视图下,选择需要修改的请求,点击Edit即可,如图4.2所示。

图4.2 修改请求参数

3、简单对服务器接口进行压力测试 

  charles提供了多线程并发发起请求,可以简单测试服务器接口的并发表现。开启方法,右击需要发送的请求,选择Repeat Advanced,设置并发线程数以及每个线程执行的次数即可,如图4.3所示。

图4.3 设置并发请求

4、修改服务器响应内容

  charles提供了Map、Rewrite和Beakpoints来调试修改服务器响应内容,这三个功能都可以修改服务器响应的内容,他们区别是:① Map功能适合长期调试修改某一接口到某地址(网络文件或本地文件),可以直接按要求事先指定文件内容;② Rewrite功能可以对网络请求进行一些正则替换,适合相同正则处理多个同类请求;③ Breakpoints功能可以进行临时断点调试,允许求改请求和响应内容。

1) Map功能   

  右键请求,可以看到有Map Local和Map Remote,Map Local可以将请求指定到本地文件,Map Remote可以将请求指定到远程文件地址。可以使用右键->Save Response来迅速保存请求响应,然后可以对响应内容进行修改,其中Map Local设置如图4.4所示。

图4.4 Map Local功能设置

2) Rewrite功能

  可以实现设置Rewrite规则,然后再对需要的请求开启Rewrite替换。设置Rewrite规则步骤,Tools->Rewrite Settings,勾选Enable Rewrite选项,具体设置界面如图4.5所示。

图4.5 Rewrite规则配置界面

  然后对需要进行Rewrite的请求,只需开启就行,开启步骤如图4.6所示。

图4.6 对某个请求开启Rewrite功能

3) Breakpoints功能

  使用Breakpoints可以临时调试请求,允许修改请求和响应参数,对于需要开启断点调试的请求,右键勾选Breakpoints选项即可,使用界面如图4.7所示。

图4.7 开启Breakpoints功能 

五、常见问题

1) APP已安装charles证书,抓取https请求时,显示"unknown",如图5.1所示

图5.1 APP抓取https显示"unknown"错误

  解决APP未设置证书信任,“通用->关于本机->证书信任设置->CA勾选”即可

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324783820&siteId=291194637