iOS 防止抓包(SSL Pinning)
1、判断是否有网络代理(不推荐)
当进行网络请求的时候,客户端判断当前是否设置了代理,如果设置了代理,不允许进行访问,附带判断是否设置代理的代码:
+ (BOOL)getProxyStatus {NSDictionary *proxySettings = NSMakeCollectable([(NSDictionary *)CFNetworkCopySystemProxySettings() autorelease]);NSArray *proxies = NSMakeCollectable([(NSArray *)CFNetworkCopyProxiesForURL((CFURLRef)[NSURL URLWithString:@"http://www.baidu.com"], (CFDictionaryRef)proxySettings) autorelease]);NSDictionary *settings = [proxies objectAtIndex:0];NSLog(@"host=%@", [settings objectForKey:(NSString *)kCFProxyHostNameKey]);NSLog(@"port=%@", [settings objectForKey:(NSString *)kCFProxyPortNumberKey]);NSLog(@"type=%@", [settings objectForKey:(NSString *)kCFProxyTypeKey]);if ([[settings objectForKey:(NSString *)kCFProxyTypeKey] isEqualToString:@"kCFProxyTypeNone"]){//没有设置代理return NO;}else{//设置代理了return YES;}
}
结果:
没有代理:
[20383:1874008] type=kCFProxyTypeNone
(lldb) po proxies
<__NSArrayM 0x1c064e5e0>(
{
kCFProxyTypeKey = kCFProxyTypeNone;
}
)
设置了代理
[20391:1874714] type=kCFProxyTypeHTTP
(lldb) po proxies
<__NSArrayM 0x1c005f8c0>(
{
kCFProxyHostNameKey = “127.0.0.1”;
kCFProxyPortNumberKey = 1380;
kCFProxyTypeKey = kCFProxyTypeHTTP;
}
)
2、SSL Pinning(AFN+SSL Pinning)推荐
SSL Pinning,即SSL证书绑定。通过SSL证书绑定来验证服务器身份,防止应用被抓包。
1、取到证书
客户端需要证书(Certification file), .cer格式的文件。可以跟服务器端索取。
如果他们给个.pem文件,要使用命令行转换:
openssl x509 -inform PEM -in name.pem -outform DER -out name.cer
如果给了个.crt文件,请这样转换:
openssl x509 -in name.crt -out name.cer -outform der
如果啥都不给你,你只能自己动手了:
openssl s_client -connect www.website.com:443 </dev/null 2>/dev/null | openssl x509 -outform DER > myWebsite.cer**
2、把证书加进项目中
把生成的.cer证书文件直接拖到你项目的相关文件夹中,记得勾选Copy items if neede和Add to targets。
3、参数名意思
AFSecurityPolicy
SSLPinningMode
AFSecurityPolicy是AFNetworking中网络通信安全策略模块。它提供三种SSL Pinning Mode
/**## SSL Pinning ModesThe following constants are provided by `AFSSLPinningMode` as possible SSL pinning modes.enum {AFSSLPinningModeNone,AFSSLPinningModePublicKey,AFSSLPinningModeCertificate,}`AFSSLPinningModeNone`Do not used pinned certificates to validate servers.`AFSSLPinningModePublicKey`Validate host certificates against public keys of pinned certificates.`AFSSLPinningModeCertificate`Validate host certificates against pinned certificates.
*/
AFSSLPinningModeNone:完全信任服务器证书;
AFSSLPinningModePublicKey:只比对服务器证书和本地证书的Public Key是否一致,如果一致则信任服务器证书;
AFSSLPinningModeCertificate:比对服务器证书和本地证书的所有内容,完全一致则信任服务器证书;
选择那种模式呢?
AFSSLPinningModeCertificate:最安全的比对模式。但是也比较麻烦,因为证书是打包在APP中,如果服务器证书改变或者到期,旧版本无法使用了,我们就需要用户更新APP来使用最新的证书。
AFSSLPinningModePublicKey:只比对证书的Public Key,只要Public Key没有改变,证书的其他变动都不会影响使用。
如果你不能保证你的用户总是使用你的APP的最新版本,所以我们使用AFSSLPinningModePublicKey。
allowInvalidCertificates
/**Whether or not to trust servers with an invalid or expired SSL certificates. Defaults to `NO`.*/
@property (nonatomic, assign) BOOL allowInvalidCertificates;
是否信任非法证书,默认是NO。
validatesDomainName
/**Whether or not to validate the domain name in the certificate's CN field. Defaults to `YES`.*/
@property (nonatomic, assign) BOOL validatesDomainName;
是否校验证书中DomainName字段,它可能是IP,域名如*.google.com,默认为YES,严格保证安全性。
4、使用AFSecurityPolicy设置SLL Pinning
+ (AFHTTPSessionManager *)manager
{static AFHTTPSessionManager *manager = nil;static dispatch_once_t onceToken;dispatch_once(&onceToken, ^{NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:config];AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModePublicKey withPinnedCertificates:[AFSecurityPolicy certificatesInBundle:[NSBundle mainBundle]]];manager.securityPolicy = securityPolicy;});return manager;
}
扩展
Android 防止抓包
1、单个接口访问不带代理的
URL url = new URL(urlStr); urlConnection = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
2、OkHttp框架
OkHttpClient client = new OkHttpClient().newBuilder().proxy(Proxy.NO_PROXY).build();
更多:
https://www.cnblogs.com/zhanggui/p/5754977.html
https://www.jianshu.com/p/23545f8d36d2
iOS 防止抓包(SSL Pinning)相关推荐
- iOS 10.3下解决Fiddler代理抓包ssl证书信任问题
iOS 10.3下解决Fiddler代理抓包ssl证书信任问题 参考文章: (1)iOS 10.3下解决Fiddler代理抓包ssl证书信任问题 (2)https://www.cnblogs.com/ ...
- IOS系统抓包之短链-破解双向证书
前言 上次文章iOS系统抓包入门实践之短链有说道关于常规抓包.路由直连以及单向证书校验的抓包方式,该文章将是在短链抓包中进行补充,主要是针对短链抓包中不太经常用到的双向证书,并进行破解和通过Charl ...
- iOS Charles 抓包
iOS Charles 抓包指南 - 从入门到精通_VictorZhang-CSDN博客_charles ios 下载安装包 Download a Free Trial of Charles • Ch ...
- IOS免费抓包神器——Stream
IOS免费抓包神器--Stream 前言 一.功能介绍 二. 安装步骤 1. App Store商店下载 2. 添加配置VPN 3. 安装CA证书 4. 开始抓包 5. 停止抓包 6. 抓包历史 7. ...
- 用RVI方法,对iOS进行抓包
用RVI方法,对iOS进行抓包 一.创建远程虚拟接口Remote Virtual Interface(RVI) 该方法适用于iOS 5.0以上设备,需要Mac并且安装Xcode. 1.将iOS设备通过 ...
- IOS https抓包及10.3.3版本证书不生效问题解决
Charles安装 HTTP抓包 HTTPS抓包 1. Charles安装 官网下载安装Charles: https://www.charlesproxy.com/download/ 2. HTTP抓 ...
- 【Charles】成功解决使用iOS手机抓包时,iOS手机连接网络失败Charles配置方法
解决方法:原来是因为我换了一台电脑,需要重新安装手机的证书,不能用之前的证书.而且安装完证书以后,Charles没有弹出让我allow的窗口,就直接可以抓包了. 配置方法如下,搬运自同事大佬: 手机和 ...
- ios app抓包分析
1 使用rvictl工具 这是mac下的一条命令.ios usb连mac,然后创建虚拟网络接口. 2 使用wireshark抓包 wireshark可以抓这个虚拟网络接口上的数据包. 转载于:http ...
- fiddler安卓模拟器与ios手机抓包
一.安卓模拟器(雷电模拟器)抓包 1.1fiddler基本配置 1.2导出Fiddler证书 Tools -> Options -> HTTPS -> Actions -> E ...
最新文章
- 「轻松支付,只需几步」使用 LeanCloud 云代码接入支付宝示例
- 2020-10-27(补码10000000为什么可以表示-128?)
- Dynamics 365 on-premises 安装
- VSCode设置命令行终端为Git
- 地图距离算法_ORB-SLAM3 单目地图初始化(终结篇)
- ros的package.xml从format1到format2的修改方法
- 科学解释成功者成功的秘诀
- aic值检验 p值_23. 假设检验的时候为什么常写p lt; 0.05,而不写具体的p值?
- Acoustica 7 for mac(数字音频编辑器)
- matlab gui矩阵计算器,matlab-gui矩阵计算器.doc
- 爬虫项目#4567电影网scrapy数据爬取moviePro/持久化储存handReqPro
- [3D检测系列-PointRCNN]复现PointRCNN代码,并实现PointRCNN3D目标检测可视化,包含预训练权重下载链接(从0开始以及各种报错的解决方法)
- 性能监控——cpu使用率过高,该怎么分析?
- 单片机C语言延时程序
- 2018 银联Java笔试 题,中国银联2018秋招笔试题
- 苹果iOS App上架流程,非iOS开发人员上架教程
- 十六宫格拼图(A*/IDA*)(曼哈顿距离)
- 【链表】leetcode19.删除链表的倒数第N个节点(C/C++/Java/Js)
- 【华为OD机试真题 python】补种未成活胡杨 【2022 Q4 | 100分】
- (arxiv-2018)图像检索的注意力感知广义平均池化
热门文章
- 分享-监控服务器的各种方法
- vscode快捷键记录
- 微博第三方登录,拿捏了
- python水印倾斜_5行Python代码实现批量打水印技巧,值得收藏
- microsoft office professional plus2007在安装过程中出错
- 2、Elasticsearch分词器简介与使用(一)
- 计算机毕业设计ssm校园花卉销售系统ef5ox (1)系统+程序+源码+lw+远程部署
- ipad打开javascript
- BindingNavigator与DataGridView绑定
- 怎么用计算机写出我爱你一生一世,网游之幻世刺客