Links
Comment on page

03 iOS-网络安全之HTTPS

一: 中间人攻击

概念

  • 经常会提到的就是中间人攻击
  • Man-in-the-middle attack(MITM)
  • 攻击者插入到原本直接通信的双方之间,双方已为还在直接跟对方通信,但实际是通过了攻击者这个中间人,信息会被它获取或篡改

时机

  • HTTPS在建立了TCP链接后,会进行SSL握手来校验证书,写上加密密钥.
  • 所以一般中间人攻击分为
    • SSL链接建立前的攻击
    • HTTPS传输中的攻击

常见攻击

a. SSL证书欺骗攻击

较为常见的攻击 通过ARP欺骗,DNS劫持甚至网管劫持,将客户端访问重定向到攻击者的服务器.让客户端与攻击者的机器建立HTTPS(伪造的证书)链接. 浏览器会提示证书不可信,用户不点击继续浏览就能避免被劫持.
  • 防范措施
    • iOS原生
      • 使用系统API建立HTTPS链接就没问题的,系统默认是没有信任相关伪造证书的不会SSL握手成功
    • WebView
      • 如果忽略证书校验的话可能会被劫持
b. SSL剥离攻击(SSLSTrip)
将HTTPS链接降级为HTTP链接 客户端直接访问HTTPS链接,攻击者是无法进行降级的 攻击的主要方式是利用用户不会每次都会输入HTTPS://xxxx.xxx.com这样的链接来访问网站
  • 防范措施
    • 原生
      • 无法劫持App内部HTTPS会话
    • WebView
      • 如果在全网HTTPS的情况下,可以对打开的链接做检查
c. 针对SSL算法进行攻击
上述两种攻击一般只能影响到WebApp,很难攻击到NativeApp. 牛逼的Hacker会对SSL算法相关的漏洞进行攻击,密码学相关了...
  • 防范措施
    • 最好对服务端的SSL/TLS配置进行升级
      • 只支持尽量高版本的TLS(最低TLSv1)
      • 禁用一些已经爆出安全隐患的加密方法
      • 使用2048位的数字证书

二: 打保证书校验

本地打保证书进App,使用SecTrustSetAnchorCertificates(SecTrustRef trust, CFArrayRef anchorCertificates)来设置Anchor Certificate进行校验
文档说明
Calling this function without also calling SecTrustSetAnchorCertificatesOnly disables the trusting of any anchors other than the ones specified by this function call.
https://developer.apple.com/library/mac/documentation/Security/Reference/certifkeytrustservices/#//apple_ref/c/func/SecTrustCopyAnchorCertificates
也就是说,单纯调用SecTrustSetAnchorCertificates方法后不调用SecTrustSetAnchorCertificatesOnly来验证证书,则只会相信SecTrustSetAnchorCertificates传入的证书,而不会信任其他锚点证书。关于这一点,SecTrustSetAnchorCertificatesOnly方法参数讲解中也有说明:
anchorCertificatesOnly:
If true, disables trusting any anchors other than the ones passed in with the SecTrustSetAnchorCertificates function. If false, the built-in anchor certificates are also trusted. If SecTrustSetAnchorCertificates is called and SecTrustSetAnchorCertificatesOnly is not called, only the anchors explicitly passed in are trusted.
只相信传入的锚点证书,也就只会验证通过由这些锚点证书签发的证书。这样就算被验证的证书是由系统其他信任的锚点证书签发的,也无法验证通过。
选择哪一级的CA证书就需要自行判断了 如果选择末端的叶子证书,可能导致域名变化后无法访问,甚至证书过期的情况

参考