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

著眼總是浮游 觀化頗領(lǐng)幻趣