行動應用 App 安全開發指引 › spaw2 › uploads › files › 1061025_3.pdf ·...
Transcript of 行動應用 App 安全開發指引 › spaw2 › uploads › files › 1061025_3.pdf ·...
iOS 資安現況
3
資料來源:https://www.ithome.com.tw/newstream/110550
資料來源:https://www.bleepingcomputer.com/news/security/76-popular-ios-apps-vulnerable-to-silent-interception-of-tls-encrypted-data/
台灣區App安全問題• 行動App資安聯盟統計排名 檢測編號 檢測項目名稱 不合格率
1 4.1.5.1.2. 應避免資訊安全漏洞 12.90%
2 4.1.5.1.1. 應避免含有惡意程式碼 8.73%
3 4.1.5.4.1. 應針對使用者輸入之字串,進行安全檢查 7.43%
4 4.1.2.3.1. 應於儲存敏感性資料前,取得使用者同意 7.02%
5 4.1.4.2.1. 應避免使用具有規則性之交談識別碼 6.78%
6 4.1.2.3.5.敏感性資料應採用適當且有效之金鑰長度與加密演算法,進行加密處理再儲存
6.45%
7 4.1.2.3.2. 應提供使用者拒絕儲存敏感性資料之權利 6.45%
8 4.1.4.2.2. 應確認伺服器憑證之有效性 5.80%
9 4.1.2.1.2. 應提供使用者拒絕蒐集敏感性資料之權利 5.63%
10 4.1.2.1.1. 應於蒐集敏感性資料前,取得使用者同意 5.47%
App檢測數量:465
台灣區App安全問題• CMAS : iOS App自動化資安檢測
• 分析639支iOS App,以旅遊、社交、美食、財經類型為主
5
47.92% 47.92% 48.41%
4.89%
46.45%
14.67%
3.18% 4.16%7.33%
48.17%
73.84%
0.00%
10.00%
20.00%
30.00%
40.00%
50.00%
60.00%
70.00%
80.00%
4.1.2.3.1 4.1.2.3.2 4.1.2.3.4 4.1.2.3.5 4.1.2.3.7 4.1.2.4.1 4.1.4.2.2 4.1.4.2.3 4.1.4.2.4 4.1.5.1.1 4.1.5.1.2
iOS App資安檢測不通過
不通過率敏感性資料儲存問題 網路傳輸
台灣區App安全問題
• 敏感性資料儲存問題– 檔案保護設定案
– Keychain資料保護
– 敏感性資料加密基準
– 關閉/移除除錯日誌
– 清除暫存資料
• 使用者輸入驗證
• 網路傳輸安全問題– 網路傳輸安全
– 網路憑證綁定
6
資料儲存安全 1.
9
• iOS裝置製作新檔案時,會替檔案指定一個類別,定義資料何時可存取
• 資料保護類別 :
• 安全程式碼範例:
• 不建議使用NSFileProtectionNone
檔案保護設定案
NSDictionary * attributes = [
NSDictionary dictionaryWithObject : NSFileProtectionComplete
forKey : NSFileProtectionKey ] ;
NSFileProtectionComplete 最完備等級的保護,使用者密碼和裝置UID所衍生之密鑰加以保護,鎖屏/booting時無法讀寫
NSFileProtectionCompleteUnlessOpen 保護等級同上,可在鎖屏時創建文件,但不能關閉文件如背景下載,一旦關閉後,在解鎖前無法做任何操作
NSfileProtectionCompleteUntilFirstUserAuthentication 保護等級同上,未啟動設備時不可存取,使用者第一次解鎖後,鎖屏也可操作
NSFileProtectionNone 僅受UID保護,隨時可讀寫
資料儲存安全 2.
10
• 密碼、Token等較簡短且敏感之資料,可儲存於iOS Keychain中,檔
案路徑:/private/var/Keychains/keychain-2.db
• 優點:
• 利用 keychain-access-groups 作資料存取隔離,沒有權限的App
無法讀取他人資料,保證資料的安全
• 全域性統一儲存,即使刪除App,keychain中的資料依然存在,下
次重新安裝app還能存取
• 保存的資料是經過加密的
• 相同keychain-access-groups的app可共享keychain中的資料
• 缺點:
• 刪除App後,不會清除keychain裡的資料 → 越獄後,keychain能
被導出
Keychain資料保護
資料儲存安全 2.
11
• Keychain的安全設定:kSecAttrAccessible屬性,有7種值kSecAttrAccessibleAfterFirstUnlock、kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly、kSecAttrAccessibleAlways、kSecAttrAccessibleWhenUnlocked、kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly、
kSecAttrAccessibleWhenUnlockedThisDeviceOnly
• 不安全設定:
Keychain資料保護
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
NSData *token = [@"secret" dataUsingEncoding:NSUTF8StringEncoding];
// Configure KeyChain Item
[dict setObject:(__bridge id)kSecClassGenericPassword
forKey:(__bridge id) kSecClass];
[dict setObject:token forKey:(__bridge id)kSecValueData];
...
[dict setObject:(__bridge id)kSecAttrAccessibleAlways
forKey:(__bridge id) kSecAttrAccessible];
OSStatus error = SecItemAdd((__bridge CFDictionaryRef)dict, NULL);
資料儲存安全 3.
12
• 加密金鑰長度應為128 bit 以上的AES或Triple DES
• 不建議使用之加密演算法
• DES、CAST、RC4、RC2
• 編碼不算加密機制
• Base64
• ASCII / Unicode
敏感性資料加密基準
iOS系統預設演算法
資料儲存安全 4.
13
• 產品正式發布前,應關閉或移除日誌紀錄功能
• 安全程式碼範例:
• 查看日誌工具:idevicesyslog
關閉/移除除錯日誌
// 於Release版本時,覆寫NSLog函式,以達到不洩漏資訊的目的// NSLog rewrite when publishing
// disable NSLog when Release
#ifndef DEBUG
#define NSLog(...)#endif
資料儲存安全 5.
14
• 開發人員易忽略可以暫存資料之檔案,含日誌/除錯文件、Cookie、網頁歷
史、網頁暫存檔等
• 安全程式碼範例:
清除暫存資料
// 手機端:清除連線cache
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
使用者輸入驗證
16
• 避免行動App損毀、緩衝區溢位、這種注入攻擊發生
• 安全程式碼範例:
字串型別/長度驗證
// 驗證輸入長度、格式Email (regular expression)- (BOOL) validEmail:(NSString*) emailString {
if([emailString length]==0){
return NO; }
NSString *regExPattern = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-
z]{2,4}";
NSRegularExpression *regEx = [[NSRegularExpression alloc]
initWithPattern:regExPattern options:NSRegularExpressionCaseInsensitive
error:nil];
NSUInteger regExMatches = [regEx numberOfMatchesInString:emailString
options:0 range:NSMakeRange(0, [emailString length])];
if (regExMatches == 0) {
return NO; }
else {
return YES; }
}
網路安全開發 1.
19
• iOS 9+ 增加應用程式傳輸安全(ATS : App Transport Security)的新安全
功能,並預設啟用
⇨所有App的資料連結必須啟用HTTPS才能上架(Deafult : TLS v1.2)
• 常見不安全設定範例:
網路傳輸安全
// Info.plist
// 範例一:全面取消 ATS,接受非HTTPS的連線NSAppTransportSecurity
NSAllowsArbitraryLoads = YES
// 範例二:某網域降級TLS至v1.0NSAppTransportSecurity
NSExceptionDomains
"less-secure.example.com"
NSExceptionRequiresForwardSecrecy = NO
NSExceptionMinimumTLSVersion = "TLSv1.0"
網路安全開發 1.
20
• 常見不安全設定範例:
• 參考資料:行動應用App安全開發指引 / 3.3 iOS安全開發實務
網路傳輸安全
// Info.plist
// 範例三:某網域取消 ATSNSAppTransportSecurity
NSAllowsArbitraryLoads = NO // Shown for clarity; this is the default
NSExceptionDomains
"secure-server-i-control.example.com"
NSExceptionAllowsInsecureHTTPLoads = YES
NSExceptionRequiresForwardSecrecy = NO
NSExceptionMinimumTLSVersion = "TLSv1.0"
網路安全開發 2.
21
• 行動應用App沒有驗證x.509的CA(Certificate Authority)憑證
⇨ 中間人攻擊 (MITM, Man-In-The-Middle)
• 解決方案:憑證綁定 (Certificate Pinning)
• 安全程式碼範例:
• 參考資料:行動應用App安全開發指引 / 3.1.13 連線管理機制
網路憑證綁定
1. 將目的地的憑證下載到本地> openssl s_client -connect [domain name]:443 –showcerts
> openssl x509 -inform PEM
-outform DER
-in cert.pem
-out cert.derSave as
aaa.pem
網路安全開發 2.
22
• 安全程式碼範例:
• 參考資料:行動應用App安全開發指引 / 3.1.13 連線管理機制
網路憑證綁定
2. 加入憑證檔至self.trustedCert
// 程式初始化時,可先載入本地端憑證檔
if (!hasReadLocalCerts) {
certsFile = [NSMutableArray arrayWithObjects: @“aaa.com.der", nil];
self.trustedCerts = [NSMutableArray array];
for (NSString *file in certsFile) {
NSString *fpath = [[NSBundle mainBundle] pathForResource:file
ofType:@"der"];
NSData * cerData = [NSData dataWithContentsOfFile:fpath];
SecCertificateRef certificate = SecCertificateCreateWithData(NULL,
(__bridge CFDataRef)(cerData));
[self.trustedCerts addObject:CFBridgingRelease(certificate)];
}
hasReadLocalCerts = YES;
}
網路安全開發 2.
23
• 安全程式碼範例:
• 參考資料:行動應用App安全開發指引 / 3.1.13 連線管理機制
網路憑證綁定
3. 憑證進行驗證- (void)connection:(NSURLConnection *)connection
willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge
*)challenge {
//取得trust
SecTrustRef trust = challenge.protectionSpace.serverTrust;
…
//將上頁載入之憑證設置為Trust Object的anchor certificateSecTrustSetAnchorCertificates(trust, (__bridge
CFArrayRef)self.trustedCerts);
…
//SecTrustEvaluate會查詢前面SecTrustSetAnchorCertificates設定之憑證//或系統默認的憑證,對trust進行確認OSStatus status = SecTrustEvaluate(trust, &result);
網路安全開發 2.
24
• 若使用WebView元件,綁定憑證時,需再啟動連線
• 參考資料:行動應用App安全開發指引 / 3.1.13 連線管理機制
http://www.bkjia.com/IOSjc/1061082.html
網路憑證綁定
4. WebView憑證驗證後,啟動連線
#pragma mark ------ NSURLConnectionDataDelegate
-(void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)pResponse {
authenticated = YES;
[connection cancel];
//驗證通過後,繼續執行之前攔截下來的請求[self.webView loadRequest:failedRequest];
}
• 關閉截圖功能
• 安全程式碼範例:
截圖功能設定
截圖功能安全設定
27
-
(void)applicationDidEnterBackground:(
UIApplication *)application
{
self.window.hidden = YES;
}
• 指定圖片為背景截圖
• 安全程式碼範例:
截圖功能設定
截圖功能安全設定
28
- (void)applicationDidEnterBackground:(UIApplication *)application {
if (!self.backgroundImage){
UIImageView *myBanner = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:@"secure-image.png"]];
self.backgroundImage = myBanner;
}
[self.window addSubview:self.backgroundImage];
// [viewControl.secure_field setHidden:TRUE]; // 隱藏欄位}
- (void)applicationWillEnterForeground:(UIApplication *)application {
if (self.backgroundImage)[self.backgroundImage removeFromSuperview];
// [viewControl.secure_field setHidden:FALSE]; // 回復已隱藏欄位}
總結
• iOS環境相較於Android,有較高的安全性
• iOS App程式開發,應注意
– 敏感性資料皆經過加密再儲存或傳送
– 加密金鑰等建議放在SERVER端或KEYCHAIN裡
– 避免使用非HTTPS的連線,且確實導入憑證並驗證
– 其他功能設定:取消日誌、截圖功能
• 行動應用App安全開發指引https://www.mas.org.tw/spaw2/uploads/images/04_1.pdf
29