如何在 Android 上優(yōu)雅的進行 HTTPS 明文抓包
前言
Android系統(tǒng)上抓包HTTPS是不是越來越難了?高版本無法添加CA證書,抓包軟件依賴太多,VPN模式、或HOOK程序時,會被APP檢測到。對抗成本愈加增高。有什么萬能的工具嗎?
是的,eCapture for Android[1]來了。以后在Android上抓HTTPS通訊包,再也不用安裝CA證書了,再也不用下載一堆python依賴環(huán)境了,再也不用重打包ssl類庫了,再也不用改一堆手機參數(shù)了,一鍵啟用,簡單明了。
eCapture簡介
eCapture是一款無需CA證書即可抓獲HTTPS明文的軟件。支持pcapng格式,支持Wireshark直接查看?;趀BPF技術,僅需root權限,即可一鍵抓包。eCapture中文名旁觀者,即 當局者迷,旁觀者清。
2022年年初上海疫情期間,筆者開始編寫并開源[2],至今已經(jīng)半年,GitHub上已經(jīng) 4200個星星。
eCapture是基于eBPF技術實現(xiàn)的抓包軟件,依賴系統(tǒng)內(nèi)核是否支持eBPF。目前支持在操作系統(tǒng)上,支持了X86_64\ARM64的Linux kernel 4.18以上內(nèi)核,支持了ARM64 Android(Linux) kernel 5.4以上版本。最新版是在2022年9月9日發(fā)布的v0.4.3版本。
演示視頻
下載后,一條命令啟動,干凈利索。./ecapture tls -w ecapture.pcapng
先看演示視頻,演示環(huán)境為Ubuntu 21.04、Android 5.4 (Pixel 6)。
模塊功能
eCapture支持tls、bash、mysqld、postgres等模塊的信息提取與捕獲。本文僅討論tls這個HTTPS/TLS明文捕獲模塊。
加密通訊明文捕獲--tls模塊
tls模塊在加密通訊類庫上,支持了openssl、gnutls、nspr/nss、boringssl等類庫。但在Android上,pcapng模式只支持boringssl,文本模式則都支持。
如何使用eCapture
環(huán)境依賴
- 操作系統(tǒng) Linux kernel 4.18以上,Android kernel 5.4以上。
- 支持BPF,可選支持BTF(eCapture版本不同)
- root權限
版本選擇
BPF CO-RE[3]特性為BTF通用的格式,用于做跨內(nèi)核版本兼容。有的Android手機是沒開啟BTF。可以查看系統(tǒng)配置確認,CONFIG_DEBUG_INFO_BTF=y為開啟;CONFIG_DEBUG_INFO_BTF=n為關閉;其他為不支持BPF,無法使用eCapture。
cfc4n@vm-server:~$# cat /boot/config-`uname -r` | grep CONFIG_DEBUG_INFO_BTF
CONFIG_DEBUG_INFO_BTF=y
Android系統(tǒng)上,config是gzip壓縮的,且配置文件目錄也變了??墒褂脄cat /proc/config.gz命令代替。
eCapture默認發(fā)行了支持CO-RE的ELF程序。Android版會發(fā)行一個5.4內(nèi)核不支持BTF(即沒有CO-RE)的版本。下載后,可以通過./ecapture -v確認。非CO-RE版本的version信息中包含編譯時的內(nèi)核版本。
# no CO-RE
eCapture version: linux_aarch64:0.4.2-20220906-fb34467:5.4.0-104-generic
# CO-RE
eCapture version: linux_aarch64:0.4.2-20220906-fb34467:[CORE]
若版本不符合自己需求,可以自行編譯,步驟見文末。
全局參數(shù)介紹
全局參數(shù)重點看如下幾個:
root@vm-server:/home/cfc4n/# ecapture -h
--hex[=false] print byte strings as hex encoded strings
-l, --log-file="" -l save the packets to file
-p, --pid=0 if pid is 0 then we target all pids
-u, --uid=0 if uid is 0 then we target all users
- --hex 用于stdout輸出場景,展示結果的十六進制,用于查看非ASCII字符,在內(nèi)容加密、編碼的場景特別有必要。
- -l, --log-file= 保存結果的文件路徑。
- -p, --pid=0 捕獲的目標進程,默認為0,則捕獲所有進程。
- -u, --uid=0 捕獲的目標用戶,默認為0,則捕獲所有用戶,對Android來說,是很需要的參數(shù)。
模塊參數(shù)
root@vm-server:/home/cfc4n/project/ssldump# bin/ecapture tls -h
OPTIONS:
--curl="" curl or wget file path, use to dectet openssl.so path, default:/usr/bin/curl
--firefox="" firefox file path, default: /usr/lib/firefox/firefox.
--gnutls="" libgnutls.so file path, will automatically find it from curl default.
--gobin="" path to binary built with Go toolchain.
-h, --help[=false] help for tls
-i, --ifname="" (TC Classifier) Interface name on which the probe will be attached.
--libssl="" libssl.so file path, will automatically find it from curl default.
--nspr="" libnspr44.so file path, will automatically find it from curl default.
--port=443 port number to capture, default:443.
--pthread="" libpthread.so file path, use to hook connect to capture socket FD.will automatically find it from curl.
--wget="" wget file path, default: /usr/bin/wget.
-w, --write="" write the raw packets to file as pcapng format.
-i參數(shù)
-i參數(shù)為網(wǎng)卡的名字,Linux上默認為eth0,Android上默認為wlan0,你可以用這個參數(shù)自行指定。
輸出模式
輸出格式支持兩種格式,文本跟pcapng文件。有三個參數(shù),
默認,全局參數(shù),輸出文本結果到stdout
-l 全局參數(shù),保存文本結果的文件路徑
-w 僅tls模塊參數(shù),保存pcapng結果的文件路徑
類庫路徑
Linux上支持多種類庫,不同類庫的路徑也不一樣。
類庫 | 參數(shù)路徑 | 默認值 |
openssl/boringssl | --libssl | Linux自動查找,Android為/apex/com.android.conscrypt/lib64/libssl.so |
gnutls | --gnutls | Linux自動查找,Android pcapng模式暫未支持 |
nspr/nss | --nspr | Linux自動查找,Android pcapng模式暫未支持 |
文本模式
-l 或者不加 -w 參數(shù)將啟用該模式。
支持openssl、boringssl、gnutls、nspr/nss等多種TLS加密類庫。支持DTLS、TLS1.0至TLS1.3等所有版本的加密協(xié)議。支持-p、-u等所有全局過濾參數(shù)。
pcapng模式
-w 參數(shù)啟用該模式,并用-i選擇網(wǎng)卡名,Linux系統(tǒng)默認為eth0,Android系統(tǒng)默認為wlan0,
僅支持openssl、boringssl兩個類庫的數(shù)據(jù)捕獲。暫不支持TLS 1.3協(xié)議。
類庫與參數(shù)支持
在Linux系統(tǒng)上,大部分類庫與參數(shù)都是可以支持的。但在Android系統(tǒng)上,因為內(nèi)核與ARM架構的原因,支持的參數(shù)上,有一定的差異。
不同模式的參數(shù)支持
-p、-u兩個全局參數(shù),支持文本模式,不支持pcapng模式。這是因為pcapng模式是使用eBPF TC技術實現(xiàn)。
模式 | -p | -u | --libssl | --port |
文本 | ? | ? | ? | ? |
pcapng | ? | ? | ? | ? |
不同模式的類庫以協(xié)議支持
模式 | openssl(類庫) | boringssl(類庫) | TLS 1.0/1.1/1.2(協(xié)議) | TLS 1.3(協(xié)議) |
文本 | ? | ? | ? | ? |
pcapng | ? | ? | ? | ? |
pcapng模式暫時不支持TLS 1.3,TLS 1.3密鑰捕獲功能[4]已經(jīng)開發(fā)完成,只是遇到一些BUG,還在解決中。筆者不是openssl的專家,對TLS 協(xié)議也不太熟。需要補充這兩塊的知識,解決起來成本比較高,也歡迎對這塊擅長的朋友一起來解決。
與tcpdump聯(lián)合使用
eCapture基于eBPF TC,實現(xiàn)了流量捕獲,并保存到pcapng文件中?;趀BPF Uprobe實現(xiàn)了TLS Master Secret的捕獲。并基于Wireshark的Decryption Secrets Block (DSB)[5]標準,實現(xiàn)了gopacket的DSB功能[6],合并網(wǎng)絡包與密鑰,保存到pcapng中。
eCapture在網(wǎng)絡包捕獲上,沒有tcpdump強大,不支持豐富的參數(shù)。你可以用eCapture捕獲master secrets,用tcpdump捕獲網(wǎng)絡包,然后使用wiresahrk自定義設置密鑰文件,配合使用。
網(wǎng)絡包捕獲
tcpdump 的常規(guī)用法,不再贅述。
密鑰捕獲
同時啟用ecapture ,模式可以選文本或者pcapng,都會保存TLS的master secrets密鑰數(shù)據(jù)到ecapture_masterkey.log中。
網(wǎng)絡包查看
用Wireshark打開網(wǎng)絡包文件,設置這個master key文件,之后就可以看到TLS解密后的明文了。
配置路徑:Wireshark --> Preferences --> Protocols --> TLS --> (Pre)-Master-Secret log filename
參數(shù)
指定路徑
默認路徑
在Android上,Google使用了boring ssl類庫,也就是C++語言在libssl基礎上的包裝。默認情況下,會使用/apex/com.android.conscrypt/lib64/libssl.so路徑。
APP的類庫確認
你可以使用lsof -p {APP PID}|grep libssl來確認。若不是默認路徑,則可以使用--libssl參數(shù)來指定。
高級用法
如果你需要查看的APP是自定義SSL類庫,那么你可以自助修改eCapture來實現(xiàn)。
自定義函數(shù)名與offset
首先,需要確定HOOK函數(shù)的函數(shù)名或者符號表地址。
沒有源碼
如果你沒有目標類庫的源碼,可以通過IDA等軟件靜態(tài)分析、動態(tài)調(diào)試,確定SSL Write的地址offset。在配置填寫在user/module/probe_openssl.go文件中,對應的probe配置部分。
{
Section: "uprobe/SSL_write",
EbpfFuncName: "probe_entry_SSL_write",
AttachToFuncName: "SSL_write",
UprobeOffset: 0xFFFF00, // TODO
BinaryPath: binaryPath,
},
offset自動計算
如果你有源碼,則可以自行閱讀源碼確定函數(shù)名或者符號表的地址。對于結構體的成員屬性讀取,則可以通過offsetof宏來自動計算。通過偏移量的方式,讀取內(nèi)容。
// g++ -I include/ -I src/ ./src/offset.c -o off
#include <stdio.h>
#include <stddef.h>
#include <ssl/internal.h>
#include <openssl/base.h>
#include <openssl/crypto.h>
#define SSL_STRUCT_OFFSETS \
X(ssl_st, session) \
X(ssl_st, s3) \
X(ssl_session_st, secret) \
X(ssl_session_st, secret_length) \
X(bssl::SSL3_STATE, client_random) \
X(bssl::SSL_HANDSHAKE, new_session) \
X(bssl::SSL_HANDSHAKE, early_session) \
X(bssl::SSL3_STATE, hs) \
X(bssl::SSL3_STATE, established_session) \
X(bssl::SSL_HANDSHAKE, expected_client_finished_)
struct offset_test
{
/* data */
int t1;
bssl::UniquePtr<SSL_SESSION> session;
};
int main() {
printf("typedef struct ssl_offsets { // DEF \n");
#define X(struct_name, field_name) \
printf(" int " #struct_name "_" #field_name "; // DEF\n");
SSL_STRUCT_OFFSETS
#undef X
printf("} ssl_offsets; // DEF\n\n");
printf("/* %s */\nssl_offsets openssl_offset_%d = { \n",
OPENSSL_VERSION_TEXT, OPENSSL_VERSION_NUMBER);
#define X(struct_name, field_name) \
printf(" ." #struct_name "_" #field_name " = %ld,\n", \
offsetof(struct struct_name, field_name));
SSL_STRUCT_OFFSETS
#undef X
printf("};\n");
return 0;
}
參數(shù)提取
對于參數(shù),你需要確認被HOOK函數(shù)的參數(shù)類型,以便確認讀取方式,可以參考kern/openssl_kern.c內(nèi)的SSL_write函數(shù)實現(xiàn)。
編譯
ARM Linux 編譯
公有云廠商大部分都提供了ARM64 CPU服務器,筆者選擇了騰訊云的。在廣州六區(qū)中,名字叫標準型SR1(SR1即ARM 64CPU),最低配的SR1.MEDIUM2 2核2G即滿足編譯環(huán)境??梢园凑瞻戳坑嬞M方式購買,隨時釋放,比較劃算。
操作系統(tǒng)選擇ubuntu 20.04 arm64。
ubuntu@VM-0-5-ubuntu:~$sudo apt-get update
ubuntu@VM-0-5-ubuntu:~$sudo apt-get install --yes wget git golang build-essential pkgconf libelf-dev llvm-12 clang-12 linux-tools-generic linux-tools-common
ubuntu@VM-0-5-ubuntu:~$wget https://golang.google.cn/dl/go1.18.linux-arm64.tar.gz
ubuntu@VM-0-5-ubuntu:~$sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go1.18.linux-arm64.tar.gz
ubuntu@VM-0-5-ubuntu:~$for tool in "clang" "llc" "llvm-strip"
do
sudo rm -f /usr/bin/$tool
sudo ln -s /usr/bin/$tool-12 /usr/bin/$tool
done
ubuntu@VM-0-5-ubuntu:~$export GOPROXY=https://goproxy.cn
ubuntu@VM-0-5-ubuntu:~$export PATH=$PATH:/usr/local/go/bin
編譯方法
- ANDROID=1 make 命令編譯支持core版本的二進制程序。
- ANDROID=1 make nocore命令編譯僅支持當前內(nèi)核版本的二進制程序。