python crawling phone app data

Today we show app crawling.
@

A: the main process outlined crawling

Most of the data crawling easier than Web crawling, anti-reptile is not so strong, and the 1.APP is transmitted in the form of JSON parsing simple.

2. To view the request and response (similar to a browser developer tools to monitor each network requests and responses) in APP, we need to help capture software.

3. Before capture, the agent will need to set the phone to listen in the next packet capture software, you can listen with the same network, and network access to all the requests.

4. If there is a rule, you only need to analyze; if not the law, you can deal directly with another tool mitmdump Response docking Python script.

5. crawl certainly not done by human hand, but also to achieve automation, but also on the App automated control, you can use the library Appium.


Two: Charles packet capture tool

1.Charles use

   Charles is a network packet capture tool, more powerful than Fiddler function can be analyzed mobile App packets get all network requests and web content

2. Install

(1) install link

   Official website: https: //www.charlesproxy.com

(2) Notice

   charles is billing software, but you can try for free for 30 days. After a trial period, you can try, but not more than 30 minutes each trial, starting a 10-second delay, but most can also be used.

(3) after installation

Here Insert Picture Description

3. certificate configuration

(1) Certificate Configuration Description

   Now many pages are in the HTTPS (an encrypted version of the Hypertext Transfer Protocol, HTTP added SSL layer), more secure through SSL encryption, true, most of the agencies issued by the CA Security Seal (not CA 12306 issued by the agency, but no Being trusted). Now the application App HTTPS protocol communication data will be encrypted, the conventional method is unable to identify the cut package interior of the requested data.
   To grab the end of the APP data to the PC and mobile terminal are installed certificate.

(2) windows Certificate Installation Configuration

   Open Charles, click Help-> SSL Proxying-> Install Charles Root Certificate, enter the certificate installation page:
Here Insert Picture Description
   Click Install Certificate, Certificate Installation Wizard opens, click Next, then need to select the storage area of the certificate, select the second option "Place all certificates in the following store":
Here Insert Picture Description
   then click Browse, select the certificate store location "Authorities trusted root certificate" is, click OK and the next step:
Here Insert Picture Description

(3) Android phone to install the certificate configuration

Phone and computer connected to the same WiFi.
Set as follows:
you can use the operating system command ipconfig to view the PC ip:

Here Insert Picture Description
   Is 10.61.131.172, default proxy port number is 8888. After the proxy server IP address of the computer. Port 8888. Set as follows:
Here Insert Picture Description
'll find 360 phone sign
Here Insert Picture Description
next to the phone to download the certificate:
Access mobile access chls.pro/ssl, download the certificate:
Here Insert Picture Description
   this time to download a .pem format. .Crt format needs to be changed manually, and finally just put a folder to:
Here Insert Picture Description
cell phone (I use the 360 phone) is set in the security, storage devices found to install from crt to find the configuration file:
Here Insert Picture Description
need to set the lock screen:
Here Insert Picture Description
to complete the installation of the certificate.

4. Turn SSL listener

   点击Proxy->SSLProxying Settings,在弹出的窗口中点击Add按钮,添加需要监听的地址和端口号。需要监听所有的HTTPS请求,可以直接将地址和端口设置为,即添加:*设置,就可以抓取所有的HTTPS请求包;如果不配置,抓取的HTTPS请求包状态可能是unknown。
Here Insert Picture Description
Here Insert Picture Description
这样就指定只监听我们需要的ip和端口的请求和响应。

5.原理

   Charles运行时会在PC端的8888开启一个代理服务,实际上是一个HTTP/HTTPS的代理。
   可以是用手机通过相同的无线网络连接(这里用的是校园网),设置手机代理为Charles的代理地址,这样手机访问互联网的数据就会经过Charles抓包工具,Charles转发这些数据到真实的服务器,再转发到手机中。这样抓包工具(Charles)就起到了中间人的作用,还有权对请求和响应进行修改。

6.抓包

   我们可以看到我们已经请求了很多数据了,点击左上角的扫帚按钮即可清空捕获到的所有请求,然后点击第二个监听按钮,表明Charles正在监听App的网络数据流。如下:
Here Insert Picture Description
抓包后可以看到音乐:
Here Insert Picture Description
   可以通过分析不同的URL请求,获得数据,也可以通过修改数据,得到那些参数是需要的。
Here Insert Picture Description
   这是有规则的,无规则的如果没有规律的url,就可以用另一个工具mitmdump对接Python脚本直接处理Response。



三:抓包工具mitmproxy(免费的)

1.简介

   mitmproxy是一个支持HTTP和HTTPS的抓包程序,有类似Fiddler和Charles的功能,但它是一个控制台的形式操作。

2.关联组件

   mirmproxy还有两个关联组件。一个是mitmdump,是命令行接口,可以对接Python脚本,用Python处理数据;另一个是mitmweb,是一个web程序,可以清楚地查看mitmproxy捕获的请求。

3.安装和证书配置

(1)用pip安装

   这个是安装python后就有的安装方法,也是最简单的安装方法,在控制台输入
   Pip install mitmproxy即可,会自动安装mitmdump和mitmweb组件:
Here Insert Picture Description
但是到最后安装会失败:
Here Insert Picture Description
   是因为安装这个包的 window 系统需要首先安装 Microsoft Visual C++ V14.0以上 才行。
   可以在https://visualstudio.microsoft.com/downloads/
直接下载即可,安装之后需要c++的库之类的东西都安装了,然后再在命令行进行安装 mitmproxy即可:
Here Insert Picture Description
然后进入安装目录,我们选择第三个:
Here Insert Picture Description
在“安装 Visual Studio”屏幕中找到所需的工作负载,选择"使用的桌面开发C++"工作负荷:
注:这里的文件太大,我直接安装到E盘。
Here Insert Picture Description
之后再安装就可以安装成功了:
Here Insert Picture Description

(2)在GitHub或官网上安装

GitHub:https://github.com/mitmproxy/mitmproxy
官网:https://mitmproxy.org
下载地址:https://github.com/mitmproxy/mitmproxy/releases

3.证书配置

(1) 产生CA证书

   对于mitmproxy来说,如果想要截获HTTPS请求,也需要设置证书。它在安装后会提供一套CA证书,只要客户端信任了mitmproxy的证书,就可以通过mitmproxy获取HTTPS请求的具体内容,否则无法解析HTTPS请求。
   在cmd中输入命令行接口命令mitmdump,产生CA证书,在用户目录找到CA证书:
Here Insert Picture Description
Here Insert Picture Description
这里的目录可以自行指定,但必须是在第一次运行mimdump时。
(2)PC端安装
   电脑是Window系统,因此用mitmproxy-ca.p12,双击后出现导入证书的引导页:
Here Insert Picture Description
点击下一步,不需要设置密码:
Here Insert Picture Description
接下来点击下一步,需要选择证书的存储区域,和配置Charles一样:
Here Insert Picture Description
最后会有警告,选择是即可,证书就安装到我们的电脑上了:
Here Insert Picture Description
Here Insert Picture Description
(3)手机端安装
   我的手机是Android系统,直接把mitmproxy-ca.pem文件发送到手机上即可。
   配置方法和Charles类似。HTTPS请求。
   手动更改为.crt 格式,最后随便放进某个文件夹即可,用手机(我用的360手机)设置里的安全,找到从存储设备安装找到该crt文件进行配置:
Here Insert Picture Description
Here Insert Picture Description

4.抓包原理

   在mitmproxy中,会在PC端的8080端口运行,然后开启一个代理服务,就是一个HTTP/HTTPS代理,类似ip代理。
   手机和PC在同一局域网内,设置代理为mitmproxy的代理地址,抓包工具mitmproxy就相当于中间人的作用,数据就会流经抓包工具。这个过程还可以对接mitmdump,抓取到的请求和响应都可以直接用Python来处理,然后分析,存到本地,或存到数据库。

5.设置代理

   在抓包前需要先设手机局域网置代理为抓包工具mitmproxy代理。

(1)启动代理服务

   在cmd中输入mitmproxy,会在8080端口运行一个代理服务,由于该命令不支持Windows系统,因此在Windows系统中用mitmdump命令:
Here Insert Picture Description
用mitmdump启动代理服务,监听8080端口:
Here Insert Picture Description

(2)设置当前代理

   将手机和电脑连到同一局域网下,先用操作系统命令ipconfig查看PC端当前局域网的ip:
Here Insert Picture Description
为10.61.131.172,在手机端设置代理如下:
Here Insert Picture Description
这样就配置好了mitmproxy的代理。

6.抓包

   运行mitmweb(由于mitmproxy不能用),就可以看到手机上的所有请求:
Here Insert Picture Description
网页呈现如下:
Here Insert Picture Description
   随便打开一条网络请求,发现和在网页的开发者工具下,监听到的一样:
Here Insert Picture Description
Here Insert Picture Description

7.mitmproxy的高级功能mitmdump

   上面的功能与方法在Fiddler和Charles中也有,那么mitmproxy的优势何在?
   在mitmproxy中它的强大体现到mitmdump工具,可以对接Python对数据请求进行处理。
   它是mitmproxy的命令行接口,可以对接python程序对请求进行处理,不需要手动截取和分析HTTP请求和响应,进行数据存储和解析都可以通过Python来完成。

(1)简单获取数据(随便一个app)

我们先创建一个文件夹:
Here Insert Picture Description
然后打开转到该文件夹下:
Here Insert Picture Description
   输入mitmdump -w outfile(outfile名称和扩展名可以自定义,文件放到当前cmd打开目录下,也可以用绝对相对路径,但比较麻烦):
截取广播页面:
Here Insert Picture Description
Here Insert Picture Description
之后把截获的数据保存到文件夹下:
Here Insert Picture Description
打开文件,发现好多乱码:
Here Insert Picture Description
下面会对其进行处理。

(2)可以指定一个脚本来处理截获的数据:

   Mitmdump -s script.py(脚本名字可以自定义),需要放在cmd当前目录下,也可以用绝对相对路径,但比较麻烦。
写入如下代码:
Here Insert Picture Description
   就是定义了一个request()方法,参数为url,其实是一个HTTPFlow对象,通过request属性即可获取到当前请求对象。然后打印出请求头,将User-Agent(用户代理,简称 UA,它是一个特殊字符串头,使得服务器能够识别客户使用的操作系统及版本、CPU 类型、浏览器及版本、浏览器渲染引擎、浏览器语言浏览器插件等)修改为MitmProxy。
运行之后手机端访问http://httpbin.org/get,可以看到请求头:
Here Insert Picture Description
PC端如下:
Here Insert Picture Description
   手机端请求的Headers实际上就是请求的Headers,User-Agent被修改成了mitmproxy(防反爬),PC端显示修改后的Headers内容,其User-Agent正是MitProxy。
   这样就可以让服务器识别不出我们的是爬虫程序了。

(3)日志输出

   mitmdump提供了专门的日志输出功能,就是设定输出到控制台的颜色,可以分别为不同级别配置不同的颜色,更加直观:
Here Insert Picture Description
效果如下:
Here Insert Picture Description

(4)输出请求数据

   可以输出一些请求信息,比如请求连接(url),请求头(headers),请求Cookies(cookies),请求Host(host),请求方法(method),请求端口(port),请求协议(scheme)等。
   还可以把这些请求的信息进行修改后,再发送到服务器中,这样,服务器返回的可能不是app想请求的网页,这就是为什么一些app打开后却访问到了其他网址的原因。
   也可以通过该用法,通过修改Cookies,添加代理等方式来尽可能避免反爬。
这里就先进行略过。

(5)获得响应

   响应就是请求后服务器返回的信息,也就是我们需要的,大部分是json格式文件。
   对于json格式文件,mitmdump也提供了对应的处理接口,就是response()方法。
   我们可以打印各个http/https请求的响应内容,不过text才是我们想要的json格式文件,也可以用content,不过返回的是二进制文件。
代码如下:
Here Insert Picture Description
我们先试一下:
Here Insert Picture Description
内容有些乱,接下来我们就进行分析得到我们需要的数据。



四:利用mitmproxy进行app数据获取

1.获取请求url

获取http/https请求url,也可以认为是接口:
修改python文件,获得请求的url:
Here Insert Picture Description
然后停留在该页面:
Here Insert Picture Description
会发现出现了好多url:
Here Insert Picture Description
我们一一分析一下,发现接口信息分别是:
这个接口
https://images.sanmaoyou.com/img/new_images/5ac83890033ec.jpg
是GET请求获取文件:
Here Insert Picture Description
这个接口的为json文件,为GET获取方式:
Here Insert Picture Description
Here Insert Picture Description
这个接口
https://images.sanmaoyou.com/audio/library/2018-04/5ac83887c5263.mp3
是下载音乐,为GET获取方式:
Here Insert Picture Description
这个接口
http://sdk-open-phone.getui.com/api.php?format=json&t=1&d=76060FDC1EFFF74C1956225F1DBEE6AF006578C33658C4AF90D802512BF321C8&k=2是POST方式,是传文件。
我们在向右点击下一个广播,进行验证:
Here Insert Picture Description
   发现基本上,新打开一个广播后,会加载mp3文件,图片文件,比较长的接口就返回json文件:
Here Insert Picture Description

2.接口url文件

把接口保存到txt文件中:
Here Insert Picture Description
发现只有很长的接口才返回json文件。因此下面就需要对这些接口进行筛选。

3.爬取json

代码在pa_qu_json.py文件中,需要先创建一个json爬取数据文件夹,爬取后的结果如下:
Here Insert Picture Description
Here Insert Picture Description

Next, the processing can be used Python, here simply crawling. In practice, not human, and is analyzed by each request and response codes automated crawling. Here is a cross-platform mobile end automated testing tool that can easily be automated crawling, reptile next blog will be mentioned in detail, we hope to put forward valuable suggestions to learn together and progress.

Guess you like

Origin www.cnblogs.com/ITXiaoAng/p/11777060.html