2023年6月28日发(作者:)
修改Android源码,解放HTTPS抓包⽂章⽬录为什么HTTPS不能抓包了?Google在Android 7.0以后的版本中,添加了“”的相关配置项。其旨在增强App的安全性,可以避免TargetSDK版本>=N的App内部⽹络请求在⾮测试环境下被恶意抓包。Network Sercurity Configuration对安全性的保证,主要是通过以下⼏个途径:是否允许明⽂HTTP请求(⾮HTTPS)、HTTPS证书(区分系统、⽤户)信任设置、域名以及App的Debug或Release配置,只有符合Manifest中配置的NetworkConfig字段内容下的条件,才可以进⾏正常的HTTP请求,如果需要使⽤Charles、Fiddler等⼯具进⾏抓包,也需要利⽤NetworkConfig来配置可以信任的证书,否则HTTPS请求在CONNECT阶段,就会返回错误,同时LogCat会打印出类似以下的错误信息: Caused by: icateException: xxx. at Chain(:661) at rustedRecursive(:539) at rustedRecursive(:605) at rusted(:495) at erverTrusted(:321) at erverTrusted(:113) at erverTrusted(:87) at erverTrusted(:116) at erverTrusted(:105) at erverTrusted(:212) at CertificateChain(:404) at _do_handshake(Native Method) at shake(:375)通常,Android开发者会将NetworkConfig按照以下XML⽂件进⾏配置:
* The default configuration has the following properties: *
- *
- If the application targets API level 27 (Android O MR1) or lower then cleartext traffic * is allowed by default. *
- Cleartext traffic is not permitted for ephemeral apps. *
- HSTS is not enforced. *
- No certificate pinning is used. *
- The system certificate store is trusted for connections. *
- If the application targets API level 23 (Android M) or lower then the user certificate * store is trusted by default as well for non-privileged applications. *
- Privileged applications do not trust the user certificate store on Android P and higher. * *
parseNetworkSecurityConfig⽅法会⾸先利⽤NetworkSecurityConfig类的getDefaultBuilder⽅法获取⼀个通⽤的Builder,构建⼀个默认配置,再根据XML中的实际情况进⾏补充修改相关源码了解了⽹络安全配置加载的流程,那么从理论上来说,我们可以在任⼀环境进⾏修改,即可去掉HTTPS的抓包限制,其中,修改的⼏个点如下:1. ⾸先修改NetworkSecurityConfig的默认Builder,让它能够和Android 6.0以下的配置⼀样,默认添加对⽤户配置的证书信任,具体修改如下: //
//
修改前 public static Builder getDefaultBuilder(ApplicationInfo info) { Builder builder = new Builder() .setHstsEnforced(DEFAULT_HSTS_ENFORCED) // System certificate store, does not bypass static pins. .addCertificatesEntryRef( new CertificatesEntryRef(tance(), false)); final boolean cleartextTrafficPermitted = SdkVersion < N_CODES.P && SandboxVersion < 2; artextTrafficPermitted(cleartextTrafficPermitted); // Applications targeting N and above must opt in into trusting the user added certificate // store. if (SdkVersion <= N_CODES.M && !ilegedApp()) { // User certificate store, does not bypass static pins. tificatesEntryRef( new CertificatesEntryRef(tance(), false)); } return builder; } //
//
修改后 public static final Builder getDefaultBuilder(ApplicationInfo info) { Builder builder = new Builder() .setHstsEnforced(DEFAULT_HSTS_ENFORCED) // System certificate store, does not bypass static pins. .addCertificatesEntryRef( new CertificatesEntryRef(tance(), false)); final boolean cleartextTrafficPermitted = SdkVersion < N_CODES.P && SandboxVersion < 2; artextTrafficPermitted(true); //
修改点1:忽略targetSdkVersion的判断,直接添加UserCertificateSource tificatesEntryRef(new CertificatesEntryRef(tance(), false)); return builder; }2. 其次我们需要忽略App内置的配置,直接忽略XmlConfigSource中的解析结果,getDefaultConfig⽅法直接返回我们改过的DefaultConfig: // //
修改前 public NetworkSecurityConfig getDefaultConfig() { ensureInitialized(); return mDefaultConfig; } // //
修改后 public NetworkSecurityConfig getDefaultConfig() { //
防⽌出现其他异常,其他初始化操作不进⾏修改(保留ensureInitialized⽅法的调⽤) ensureInitialized(); //
修改点2:不使⽤解析后的mDefaultConfig,重新获取⼀个DefaultConfig r builder = aultBuilder(null); return (); }刷机验证确认没有基本的Java语法问题以后,lunch、make⼀波⾛起!!稍等⽚刻,刷机试验……注意:刷完机后,先不要着急连上WiFi挂上代理抓包,很多⼿机刷完以后时间会还原,所以先去设置中把时间调成正确的,否则⽆论如何证书都是过期的……调整好时间后,连上Charles或者Fiddler的代理,装上它们的证书,即可对任意的App进⾏HTTPS抓包了!终于不是满屏红叉了!!
发布者:admin,转转请注明出处:http://www.yc00.com/news/1687954499a60525.html
评论列表(0条)