一、目標
要抓App的包,首先需要App信任抓包軟件的證書。
不過從Android 從 7.0 開始,系統不再信任用戶 CA 證書,所以你需要把 CA 證書安裝到系統 CA 證書目錄。
如果你是用Magisk越獄的話,這個工作就比較簡單了,只需要安裝一個模塊 Move Certificates。
不過今天的故事從我刷了一個新rom開始,這個rom比較奇怪,刷完之后 adb連上 直接就是root狀態,但是App卻沒法獲取root狀態。
我去,這不就是誤打誤撞刷了一個隱藏root的rom嗎?這下我可舍不得裝Magisk了。
那現在的問題就是 如何把證書安裝到系統目錄?
二、步驟
霸王硬上弓
計算證書名
openssl x509 -subject_hash_old -in charles-ssl-proxying-certificate_saved.pem
算出數值,比如3a1074b3
然后把原Charles證書charles-ssl-proxying-certificate_saved.pem改名為 3a1074b3.0
最后把 3a1074b3.0 文件拷貝到 /system/etc/security/cacerts/ 目錄下面。
搞定~~
理想很豐滿,現實很骨感,/system 大概率是不可寫的,即使你有root權限,也寫不進去。
問了下谷哥,哥說,可以把 /system 重新 mount 成可讀寫的,不過我沒有成功。
之前有兩種方式成功過。
1、安裝RootExplorer.apk,把/system 加載成可讀寫。
2、adb reboot recovery 進入之前刷的 twrp ,在twrp下去寫入 /system
不過這次翻車了,RootExplorer沒法加載可讀。 twrp寫完 /system 之后這個rom發瘋了,設置 進不去了,老報崩潰。
師夷長技
想起了 Http Toolkit這個抓包軟件,它有個 Android Device via ADB 模式,居然可以順利抓包。
說明它可以利用ADB把 證書寫入到 /system , 畢竟我的ADB是有Root權限的。
太神奇了,它是怎么實現的呢?
這下又開始了漫長的谷歌之旅,最后在他們官網找到一篇文章,詳細講述了 通過有root權限的adb 來寫入系統證書的神奇方案。
1、通過 ADB 將 HTTP Toolkit CA 證書推送到設備上。
2、從 /system/etc/security/cacerts/ 中復制所有系統證書到臨時目錄。
3、在 /system/etc/security/cacerts/ 上面掛載一個 tmpfs 內存文件系統。這實際上將一個可寫的全新空文件系統放在了 /system 的一小部分上面。 將復制的系統證書移回到該掛載點。
4、將 HTTP Toolkit CA 證書也移動到該掛載點。
5、更新臨時掛載點中所有文件的權限為 644,并將系統文件的 SELinux 標簽設置為 system_file,以使其看起來像是合法的 Android 系統文件。
關鍵點就是掛載一個 內存文件系統,太有才了。
Show me the Code
# htk-inject-system-cert.sh
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
cp /data/local/tmp/c88f7ed0.0 /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"
內存文件系統嘛,重啟之后肯定就失效了,所以保存成腳本,抓包之前跑一下,也不是很麻煩的。
三、總結
有時候神奇的技術就是一層窗戶紙,捅破了,你會驚嘆,原來這么簡單。
掌握了新的方案之后,未來就可以舉一反三了。
參考 https://httptoolkit.com/blog/intercepting-android-https/
著眼總是浮游 觀化頗領幻趣