参考链接:https://juejin.cn/post/7325678062581760015
在Android14上,会发现依然存在证书错误的问题,这是因为Android14上面,存放证书的地址变了。归根究底,就是原先的路径 /system/etc/security/cacerts 在android14上无法使用了,android14开始使用 /apex/com.android.conscrypt/cacerts/

必须要做的:下载证书
1、手机必须root
2、先按照这个操作一下,不要出错,目的就是为了下载下来一个证书文件

方案:证书挂载
下面就介绍如何不修改代码进行网络数据抓包。
由于文件夹/apex/com.android.conscrypt/cacerts/的安全问题,无法通过命令直接push一个文件进去,所以需要把/apex/com.android.conscrypt/cacerts/文件夹挂载到一个临时文件夹,把证书放到这个临时文件夹。思路是这样,按照下面几个步骤严格执行就行了,不需要管什么文件夹
第1步:
把保存的.pem证书随便拷贝到一个Linux系统里面, 然后使用如下命令生成证书hash, "charles.pem"是我保存的证书的名字, "3af8aaba"是生成的hash, 然后重命名该证书为"3af8aaba.0", 就是在末尾加个".0"的意思
openssl x509 -inform PEM -subject_hash -in charles.pem

把上面下载的证书3af8aaba.0和脚本android14-root-ca-inject.sh放在目录/data/local/tmp/

android14-root-ca-inject.sh 的内容如下
# Create a separate temp directory, to hold the current certificates
# Otherwise, when we add the mount we can't read the current certs anymore.
mkdir -p -m 700 /data/local/tmp/tmp-ca-copy
# Copy out the existing certificates
cp /apex/com.android.conscrypt/cacerts/* /data/local/tmp/tmp-ca-copy/
# Create the in-memory mount on top of the system certs folder
mount -t tmpfs tmpfs /system/etc/security/cacerts
# Copy the existing certs back into the tmpfs, so we keep trusting them
mv /data/local/tmp/tmp-ca-copy/* /system/etc/security/cacerts/
# Copy our new cert in, so we trust that too
mv /data/local/tmp/3af8aaba.0 /system/etc/security/cacerts/
# Update the perms & selinux context labels
chown root:root /system/etc/security/cacerts/*
chmod 644 /system/etc/security/cacerts/*
chcon u:object_r:system_file:s0 /system/etc/security/cacerts/*
# Deal with the APEX overrides, which need injecting into each namespace:
# First we get the Zygote process(es), which launch each app
ZYGOTE_PID=$(pidof zygote || true)
ZYGOTE64_PID=$(pidof zygote64 || true)
# N.b. some devices appear to have both!
# Apps inherit the Zygote's mounts at startup, so we inject here to ensure
# all newly started apps will see these certs straight away:
for Z_PID in "$ZYGOTE_PID" "$ZYGOTE64_PID"; do
if [ -n "$Z_PID" ]; then
nsenter --mount=/proc/$Z_PID/ns/mnt -- \
/bin/mount --bind /system/etc/security/cacerts /apex/com.android.conscrypt/cacerts
fi
done
# Then we inject the mount into all already running apps, so they
# too see these CA certs immediately:
# Get the PID of every process whose parent is one of the Zygotes:
APP_PIDS=$(
echo "$ZYGOTE_PID $ZYGOTE64_PID" | \
xargs -n1 ps -o 'PID' -P | \
grep -v PID
)
# Inject into the mount namespace of each of those apps:
for PID in $APP_PIDS; do
nsenter --mount=/proc/$PID/ns/mnt -- \
/bin/mount --bind /system/etc/security/cacerts /apex/com.android.conscrypt/cacerts &
done
wait # Launched in parallel - wait for completion here
echo "System certificate injected"
第2步:
使用git(有的adb窗口也可以)打开操作窗口,运行脚本android14-root-ca-inject.sh
(1) adb shell
(2) chmod +x /data/local/tmp/android14-root-ca-inject.sh
(3)./data/local/tmp/android14-root-ca-inject.sh

(1)如果你使用的证书是自己的,那这里要在脚本里面改下证书名称,再保存
(2)需要安装python运行环境 (小米14实测能直接运行)
结果:
这时候就可以使用Charles抓包了

说明:有部分Android14系统可能采用这个方式依然不行,换个手机吧
博主是小米14, 实测可抓包, 额外步骤是需要先在下图中, 点右边的开关, 先关闭 再打开, 就可以抓包了, 不做此步骤抓不了包

后续:
2024/03/12
小米14好像只能抓edge浏览器的包, 大多数app都不行, 寻找新方案中
2024/03/13
找到另一篇文章, 尝试了可行, 小米14 hyperos 1.0.34, Android 14系统
所需文件: https://github.com/ViRb3/TrustMeAlready/releases
手机需要root,安装好 Magisk 和 Xposed 框架(比如 LSPosed);
下载安装 【MT管理器】 (或其他类似文件管理器),并授予 root 权限。点这里 查看链接 下载里面3个文件,安装 HttpCanary_3.3.5.apk,其他两个待用。
说明:HttpCanary_3.3.5.apk:HttpCanary 安装包,原版无修改;HttpCanary根证书.zip:Magisk 模块,将HttpCanary根证书添加至系统CA目录,证书文件需自己添加;TrustMeAlready_1.11.apk:Xposed模块,解决“目标app使用固定证书,无法抓包”。
安装证书
1.安装 HttpCanary 后,打开软件,若“安装根证书”无法安装点“跳过”即可,进入软件后,即可退出软件;
2.MT管理器打开 /data/user/0/com.guoshi.httpcanary/cache,新建名为 HttpCanary.jks 的空文件;然后把 HttpCanary.p12 和 HttpCanary.pem 两个文件复制到其他文件夹里(比如 Download 文件夹),后面要用到,如下图。

3.打开 设置→安全→加密与凭据→安装证书,不同机型可能不同,也可以在设置搜索”安装证书”打开。这里需要安装三个证书(本人测试安装“CA 证书”即可,推荐全部安装):
- 点击 CA 证书,它会提示您的数据将不再是私密的,点仍然安装。找到刚才放证书的文件夹,选择 HttpCanary.pem 。 显示已安装CA证书。
- 点击 xxx和应用用户证书,选择 HttpCanary.p12 ,密码为 HttpCanary(推荐复制,避免输错),证书名称为 xxx and Apps(xxx看图)。
- 点击WLAN证书,选择 HttpCanary.p12 ,密码为 HttpCanary,证书名称为 WiFi。
至此,用户证书安装完毕,点开用户凭据会看到有三个证书,如下图。
添加根证书至系统
由于从Android 7.0版本开始,系统不再信任安装的用户证书,可能会导致无法抓取SSL/TLS加密数据包或者无网络等问题,如下图。因此需要将HttpCanary根证书添加至系统CA目录,此操作需要在Root设备上进行。
1.直接移动根证书
打开 HttpCanary→设置→HttpCanary根证书→添加根证书至系统(Root)→移动。若成功,则可以跳过此部分;若显示下图,则说明 system 分区没有解锁,可以通过 Magisk 模块移动根证书,接着看下面内容。

2.通过 Magisk 模块移动根证书
(1) MT管理器打开 /data/misc/user/0/cacerts-added/ ,安装完用户证书后,这个路径下面会生成一个 .0格式证书文件。在MT管理器另一侧窗口打开之前下载好的 HttpCanary根证书.zip ,打开到 HttpCanary根证书.zip/system/etc/security/cacerts/ 路径。
然后长按 .0格式文件,添加到 HttpCanary根证书.zip/system/etc/security/cacerts/ 里,如下图。

(2) 打开 Magisk→模块→从本地安装→选择“HttpCanary根证书.zip”→确定安装,安装完成后重启手机。
如下图,若打开 /system/etc/security/cacerts/ 路径,可以找到和 /data/misc/user/0/cacerts-added/ 路径下相同的 .0文件,说明已成功把 HttpCanary根证书添加至系统CA目录。
解决“目标App使用固定证书”问题
如下图,在将HttpCanary根证书添加至系统CA目录后,对部分APP抓包仍会报错:目标App使用固定证书。
解决办法是:安装前面下载好的“TrustMeAlready_1.11.apk”,安装完成在 LSPosed 中启用模块,勾选系统框架和需要抓包的APP,如下图。重启手机,一定要重启手机,否则不会生效。
在完成上面设置后就可以愉快地抓包了。
