iOS 安全:UIWebView访问Https站点防止中间人攻击
尽管Https协议能够提供数据的加密、身份的认证等安全服务,但并不是没有漏洞。HTTPS协议安全隐患的存在可能使用户受到各种极具破坏力的网络攻击。其中中间人攻击(Man In The Middle, MITM)就是非常危险的一种攻击方式。
场景分析:假设用户手机接入了不安全的wifi,这时发生了dns被篡改的情况。如用户访问了Https://www.taobao.com, 被跳转到了类似淘宝的钓鱼站点,且该站点用的是真实ca证书,这时用户的账户安全将受到巨大威胁。
这里介绍一个ios中能大大降低此类风险的做法,即公钥证书匹配。
1. 在终端执行,openssl s_client -connect www.taobao.com:443 -showcerts ,获取公钥内容。
0 s:/C=CN/ST=ZheJiang/L=HangZhou/O=Alibaba (China) Technology Co., Ltd./CN=*.tmall.com i:/C=BE/O=GlobalSign nv-sa/CN=GlobalSign Organization Validation CA - G2 -----BEGIN CERTIFICATE----- MIIGqzCCBZOgAwIBAgISESHmhyz9S+Hphge+SQIlaA0ZMA0GCSqGSIb3DQEBBQUA MF0xCzAJBgNVBAYTAkJFMRkwFwYDVQQKExBHbG9iYWxTaWduIG52LXNhMTMwMQYD VQQDEypHbG9iYWxTaWduIE9yZ2FuaXphdGlvbiBWYWxpZGF0aW9uIENBIC0gRzIw HhcNMTUwNjI2MDY1NzA0WhcNMTUxMjI2MTU1OTU5WjB4MQswCQYDVQQGEwJDTjER MA8GA1UECBMIWmhlSmlhbmcxETAPBgNVBAcTCEhhbmdaaG91MS0wKwYDVQQKEyRB bGliYWJhIChDaGluYSkgVGVjaG5vbG9neSBDby4sIEx0ZC4xFDASBgNVBAMMCyou dG1hbGwuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAp1c2CDm6 I6V8W7h1NnvnZz02NzRnMd/rqimdeqERgsZV192GRo5gwnyCgulZePcXFJSdpv82 uZHnr1Ya83SX8vhxNzEyFkk3KCLPfkSbNbOOkWfQGwKCNPmDlPbM+rxBN8sdaCGV /oLptOanTzabS3oj0eFW9ePq7uOcuM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S5nbG9iYWxzaWduLmNvbS9jYWNlcnQvZ3Nvcmdhbml6 YXRpb252YWxnMi5jcnQwOwYIKwYBBQUHMAGGL2h0dHA6Ly9vY3NwMi5nbG9iYWxz aWduLmNvbS9nc29yZ2FuaXphdGlvbnZhbGcyMB0GA1UdDgQWBBQHw4CCRIGhjNjl 3r4fH4troxWe7zAfBgNVHSMEGDAWgBRdRrKNxEt0HLvt9XO2Orc4j3WefjANBgkq hkiG9w0BAQUFAAOCAQEALXJXHszy4jjNs5KbovH5wv6trqdXz02fEo5hpV0/UCXI Eyk3Glm+4376kCTordj64YEIe05G+URVG3MlBI/webk6P6lrVTdHv+ihwLcvJah5 A3u7xXcay53KV/j2tkRc5CaP/ifCndoGL+Z22qQdLeckY/ArS3xrP76bBOQF8k6U gM1+iZWbL59wmRPwYBVrGqIhn+Gcb7PGJmyS/PaxL+hu/w3EKH9Yos10Xd+L367c 8sdNQhRFBxCuiIrvW7IW6GAoZRd0dU+e6gGc/QUfoE8RQ3m8XISZwb9g0mKbt0aG idQ75IPLfSgMiRy6o9PG4l3Y+d9G1ch+SFIidvmlrA== -----END CERTIFICATE-----
2. 将公钥内容(-----BEGIN CERTIFICATE----- 至-----END CERTIFICATE-----)保存成.PEM格式文件。
3. 执行命令 openssl x509 -inform PEM -outform DER -in cert.pem -out cert.der , 将.pem证书转换成.der格式。
4. 在app工程中,导入生成好的.der证书。
5. 在实现UIWebViewDelegate 的View Controller.h中添加
@interface TargetViewController : UIViewController<UIWebViewDelegate, NSURLConnectionDelegate, NSURLConnectionDataDelegate>
6. 在init方法中添加读取证书的逻辑,并存到self.trustedCerts数组中
//读取ssl证书信息
if (!hasReadLocalCerts) {
certsFile = [NSMutableArray arrayWithObjects: @"taobao.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;
}
7. 在
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
方法中添加https站点的处理逻辑:
NSURL *url = [request URL];
NSString *schema = [[url scheme] lowercaseString];
//未有过证书验证,将失败的请求纪录下来
if ([schema isEqualToString:@"https"]){
if (!authenticated) {
NSLog(@"Authenticated failed!");
[self.webView stopLoading];
failedRequest = request;
[[[NSURLConnection alloc] initWithRequest:request delegate:self] start] ;
return NO;
}
return YES;
}
8. 实现证书验证
#pragma mark ------ NSURLConnectionDelegate
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
NSLog(@"Check ssl cert.");
//获取trust object
SecTrustRef trust = challenge.protectionSpace.serverTrust;
SecTrustResultType result;
//这里将之前导入的证书设置成下面验证的Trust Object的anchor certificate
SecTrustSetAnchorCertificates(trust, (__bridge CFArrayRef)self.trustedCerts);
//SecTrustEvaluate会查找前面SecTrustSetAnchorCertificates设置的证书或者系统默认提供的证书,对trust进行验证
OSStatus status = SecTrustEvaluate(trust, &result);
if (status == errSecSuccess &&
(result == kSecTrustResultProceed ||
result == kSecTrustResultUnspecified)) {
//验证成功,生成NSURLCredential凭证cred,告知challenge的sender使用这个凭证来继续连接
NSURLCredential *cred = [NSURLCredential credentialForTrust:trust];
[challenge.sender useCredential:cred forAuthenticationChallenge:challenge];
NSLog(@"SSL cert match!");
}
else {
//验证失败,取消这次验证流程
[challenge.sender cancelAuthenticationChallenge:challenge];
NSLog(@"SSL cert missmatch!");
}
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
#pragma mark ------ NSURLConnectionDataDelegate
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)pResponse {
authenticated = YES;
[connection cancel];
//验证通过,继续执行之前被拦截下来的请求
[self.webView loadRequest:failedRequest];
}
转载请注明: