通过ADB安装用户证书

问题描述 投票:0回答:8

有没有办法通过ADB在

.crt
下安装CA证书(
Security -> Trusted Credential -> User tab
文件)?或任何其他“可编写脚本”的方式。

android certificate adb x509certificate
8个回答
24
投票

我找到了一种方法来做到这一点,因此我能够信任查尔斯代理证书。它将被添加为受信任的 SSL 根证书。

首先你需要获取证书哈希

openssl x509 -inform PEM -subject_hash_old -in charles-proxy-ssl-proxying-certificate.pem | head -1>hashedCertFile

我使用Windows,将其存储在var中以自动化该过程

set /p certHash=<hashedCertFile
    

set certHash=%certHash%.0 && DEL toto
cat charles-proxy-ssl-proxying-certificate.pem > %certHash%

openssl x509 -inform PEM -text -in charles-proxy-ssl-proxying-certificate.pem -out nul >> %certHash%

adb shell mount -o rw,remount,rw /system

adb push %certHash% /system/etc/security/cacerts/

adb shell mount -o ro,remount,ro /system

adb reboot

这是从这个答案复制的unix版本:

PEM_FILE_NAME=logger-charles-cert.pem
hash=$(openssl x509 -inform PEM -subject_hash_old -in $PEM_FILE_NAME | head -1)
OUT_FILE_NAME="$hash.0"

cp $PEM_FILE_NAME $OUT_FILE_NAME
openssl x509 -inform PEM -text -in $PEM_FILE_NAME -out /dev/null >> $OUT_FILE_NAME

echo "Saved to $OUT_FILE_NAME"
adb shell mount -o rw,remount,rw /system
adb push $OUT_FILE_NAME /system/etc/security/cacerts/
adb shell mount -o ro,remount,ro /system
adb reboot

22
投票

感谢这个答案通过 ADB 安装用户证书我能够改编一个在 bash shell 上运行的脚本:

PEM_FILE_NAME=logger-charles-cert.pem
hash=$(openssl x509 -inform PEM -subject_hash_old -in $PEM_FILE_NAME | head -1)
OUT_FILE_NAME="$hash.0"

cp $PEM_FILE_NAME $OUT_FILE_NAME
openssl x509 -inform PEM -text -in $PEM_FILE_NAME -out /dev/null >> $OUT_FILE_NAME

echo "Saved to $OUT_FILE_NAME"
adb shell mount -o rw,remount,rw /system
adb push $OUT_FILE_NAME /system/etc/security/cacerts/
adb shell mount -o ro,remount,ro /system
adb reboot

(是的,我知道这可能应该是一条评论,但我还没有足够的声誉将其发布为评论)


12
投票

我能够通过以下步骤获得服务器证书显示在

Trusted Credential -> User
选项卡(而不是系统选项卡,其他答案显示)下:

#!/bin/bash
subjectHash=`openssl x509 -inform PEM -subject_hash_old -in server.crt | head -n 1`
openssl x509 -in server.crt -inform PEM -outform DER -out $subjectHash.0
adb root
adb push ./$subjectHash.0 /data/misc/user/0/cacerts-added/$subjectHash.0
adb shell "su 0 chmod 644 /data/misc/user/0/cacerts-added/$subjectHash.0"
adb reboot

7
投票

2022:httptoolkit 有一个很好的解决方案,可以将自定义证书无需重新启动注入有根设备/模拟器

详细信息在这里:https://httptoolkit.tech/blog/intercepting-android-https/#injecting-ca-certificates-into-rooted-devices

    set -e # Fail on error
    # Create a separate temp directory, to hold the current certificates
    # Without this, when we add the mount we can't read the current certs anymore.

    mkdir -m 700 /data/local/tmp/htk-ca-copy
    # Copy out the existing certificates

    cp /system/etc/security/cacerts/* /data/local/tmp/htk-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 mount, so we keep trusting them

    mv /data/local/tmp/htk-ca-copy/* /system/etc/security/cacerts/
    # Copy our new cert in, so we trust that too

    mv ${certificatePath} /system/etc/security/cacerts/
    # Update the perms & selinux context labels, so everything is as readable as before

    chown root:root /system/etc/security/cacerts/*
    chmod 644 /system/etc/security/cacerts/*
    chcon u:object_r:system_file:s0 /system/etc/security/cacerts/*
    # Delete the temp cert directory & this script itself

    rm -r /data/local/tmp/htk-ca-copy
    rm ${injectionScriptPath}
    echo "System cert successfully injected"

来源


7
投票

将文件推送到设备

adb push "C:\path\cacert.cer" "/data/local"

启动证书安装程序

adb shell am start -n com.android.certinstaller/.CertInstallerMain -a android.intent.action.VIEW -t application/x-x509-ca-cert -d file:///data/local/cacert.cer

现在根据设备上出现的提示完成安装。


1
投票

这只会在未root的android上启动“你想信任这个证书窗口吗”。这是 @hoghart45 的答案,除了一行确保您有权将证书粘贴到

/data/local/..
目录中:

certificateName=ca.crt
ca_dir_in_phone="/data/local/tmp/try3"
ca_path_in_phone="$ca_dir_in_phone/$certificateName"

adb shell mkdir -m 700 "$ca_dir_in_phone"
adb push "$certificateName" "$ca_path_in_phone"

adb shell am start -n com.android.certinstaller/.CertInstallerMain -a android.intent.action.VIEW -t application/x-x509-ca-cert -d file://"$ca_path_in_phone"

enter image description here

为了完整起见,here 是一个 WIP Python 项目 WIP,它还使用

uiautomator
以受控方式自动单击“确定”。 (它在单击之前验证它是“确定”按钮,它不只是发送盲输入,如
send keyevent 20
命令)。免责声明,我参与了该项目。


0
投票

就我而言,我首先需要将模拟器启动为可写:

adb start-server
emulator -writable-system -avd Pixel_2_API_24

然后就可以安装证书了:

adb root
adb remount
adb push c8750f0d.0 /system/etc/security/cacerts

https://docs.mitmproxy.org/stable/howto-install-system-trusted-ca-android


0
投票

由于这是“安装 CA CERT adb oculus / meta quest 2”的第一篇文章,我将在此处添加 2 美分以帮助下一篇文章:

在 Meta Quest 2 VR 耳机上,您可以使用 ADB 打开 Android 设置(不是 oculus 设置应用程序,真正的 Android 设置应用程序!)来安装 CA 证书

您的设备必须激活开发者模式。 只需输入:

./adb shell am start -n com.android.settings/.Settings\$NetworkDashboardActivity

从那里向下滚动到“安全 -> 加密和凭证 -> 安装证书

享受:)

© www.soinside.com 2019 - 2024. All rights reserved.