編輯:關於 Android 程式設計
Android 軟體通常透過 Wi-Fi 網路與伺服器通訊,然而 Wi-Fi 並非總是可靠的。在開放式或弱加密網路中,接入者可能監聽網路流量;攻擊者甚至可能自建釣魚 Wi-Fi 網路。此外,在取得 root 權限後,也能於 Android 系統中監聽網路資料。
最危險的情況是直接以 HTTP 協議登入帳戶或交換資料。例如攻擊者在自建的釣魚網路中配置 DNS 伺服器,將軟體要連接的伺服器網域名稱解析至攻擊者的另一台伺服器,該伺服器便可取得使用者登入資訊,或充當客戶端與原伺服器之間的中間人轉發資料。
早期國外一些知名社交網站的 Android 客戶端登入工作階段並未加密,後來出現了名為 FaceNiff 的駭客工具,專門嗅探並劫持這些工作階段(甚至支援在 WEP、WPA、WPA2 加密的 Wi-Fi 上發動攻擊)。這類問題的解法很明確——對敏感資料採用基於 SSL/TLS 的 HTTPS 傳輸。
在 SSL/TLS 通信中,客戶端透過數字憑證判斷伺服器是否可信,並採用憑證的公鑰與伺服器加密通信。然而,開發者在開發過程中為了解決自簽憑證導致的 CertificateException 等異常,常會在客戶端採用「信任所有憑證」的方式,並覆蓋 Google 預設的憑證檢查機制(X509TrustManager),且不做任何有效性校驗:
public class MySSLSocketFactory extends SSLSocketFactory {
SSLContext sslContext = SSLContext.getInstance("TLS");
public MySSLSocketFactory(KeyStore truststore) throws Exception {
super(truststore);
TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}
// 未對 SSL 憑證有效性進行校驗,並覆蓋了 Android 自帶的檢查
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sslContext.init(null, new TrustManager[]{tm}, null);
}
}
一旦使用者手機中安裝了惡意憑證,攻擊者便能以中間人攻擊的方式竊聽通信,甚至竄改 request 或 response 中的資料。
1. 客戶端啟動、傳輸資料前,需與伺服端進行握手以確立加密密碼資訊;
2. 中間人攔截握手資訊後,模擬客戶端向伺服器請求,伺服器選出加密與 HASH 演算法,並將身分憑證回傳;
3. 中間人攔截伺服器回傳的憑證,替換成自己的憑證;
4. 客戶端以中間人的憑證加密傳輸;
5. 中間人以自己的憑證解密取得明文;
6. 竊聽或竄改後,再模擬客戶端加密轉發給真正的伺服器,完成整個攻擊。
使用 CA 機構頒發憑證雖可行,但時間與成本太高,實務上較少採用。由於手機應用的伺服器與憑證其實是固定的,因此可採用「憑證或公鑰鎖定」(Certificate / Public Key Pinning)來防護。
1. 公鑰鎖定:將憑證公鑰寫入客戶端 APK,HTTPS 通信時檢查伺服器傳輸的憑證公鑰是否與 APK 中一致:
public final class PubKeyManager implements X509TrustManager {
private static String PUB_KEY = "30820122300d06092a864886f70d01010105...";
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
if (chain == null) {
throw new IllegalArgumentException("X509Certificate array is null");
}
if (!(chain.length > 0)) {
throw new IllegalArgumentException("X509Certificate is empty");
}
if (!(null != authType && authType.equalsIgnoreCase("RSA"))) {
throw new CertificateException("AuthType is not RSA");
}
// 執行慣例的 SSL/TLS 檢查
try {
TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
tmf.init((KeyStore) null);
for (TrustManager tm : tmf.getTrustManagers()) {
((X509TrustManager) tm).checkServerTrusted(chain, authType);
}
} catch (Exception e) {
throw new CertificateException(e);
}
// 取出憑證公鑰並比對
RSAPublicKey pubkey = (RSAPublicKey) chain[0].getPublicKey();
String encoded = new BigInteger(1, pubkey.getEncoded()).toString(16);
final boolean expected = PUB_KEY.equalsIgnoreCase(encoded);
if (!expected) {
throw new CertificateException("Expected public key: " + PUB_KEY
+ ", got public key: " + encoded);
}
}
}
2. 憑證鎖定:為客戶端頒發公鑰憑證並存放於手機(使用 keystore),HTTPS 通信時固定在客戶端讀取憑證資訊,而非從伺服器取得。
Android TabLayout 搭配 ViewPager 實作頁籤切換
TabLayout 是 Android Design Support Library 提供的頁籤元件,官方已經幫我們做好了「頁籤與 ViewPager 連動」這件事,只要
Android RatingBar 與屬性動畫製作男女比例圖
RatingBar 本身是打分元件,但透過 isIndicator 屬性可把它變成只讀的指示器。再搭配自訂 progressDrawable 與屬性動畫,就能做出類似 Q
Eclipse 導入 Android 專案時出現「Remove @Override annotation」錯誤的解法
問題現象 把既有專案匯入 Eclipse 之後,一堆方法上標了紅色錯誤,提示訊息是要求移除 @Override 註解,但那些方法明明覆寫了父類的實作,看起來完全正常。
Android Sensor詳解(1):簡介與系統架構
前言 近來正在學習如何porting sensor,藉此機會寫部落格來督促自己進步。若有人對文中的理解提出不同看法,那是最好不過的,大家可以一起交流成長。本系列將從底層到