让你的程序支持https以及https的抓包

iOS9推出的时候,苹果但愿你们使用https协议,来提升数据传输之间的安全性。下面我就从最简单的代码介绍,如何在工程中设置,来支持https的请求。api

1、证书准备篇

  • 1.证书转换 在服务器人员,给你发送的crt证书后,进到证书路径,执行下面语句 // openssl x509 -in 你的证书.crt -out 你的证书.cer -outform der 这样你就能够获得cer类型的证书了。双击,导入电脑。安全

  • 2.证书放入工程 一、能够直接把转换好的cer文件拖动到工程中。 二、能够在钥匙串内,找到你导入的证书,单击右键,导出项目,就能够导出.cer文件的证书了服务器

2、代码修改篇

先在info.plist中,增长以下图的配置 网络

QQ20160909-7@2x.png

文本内容以下:session

NSAppTransportSecurityNSAllowsArbitraryLoads

1.使用系统类发送网络请求篇

1.1 NSURLConnection设置支持https。

在2015年iOS9的更新中,NSURLConnection 被废弃 由 NSURLSession 取代,因此自己是不建议你们继续用这个类作网络请求的(一样也有AFNetWorking 2.x版本),可是考虑到一些旧程序,也不能说改就改,说替换就替换的,因此仍是须要普及一下,若是用到了NSURLConnection你须要怎么作。架构

代码以下:模块化

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{    if(challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {        // 告诉服务器,客户端信任证书
        // 建立凭据对象
        NSURLCredential *credntial = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];        // 告诉服务器信任证书
        [challenge.sender useCredential:credntial forAuthenticationChallenge:challenge];
    }
}

你只须要简单的,添加上如上的代理方法,就能够在不影响你原有请求的基础上,增长了https请求的支持了。工具

1.2 NSURLSession设置支持https。

如今推荐使用的就是NSURLSession来处理相关的网络请求了,若是使用系统自带的类,能够参考以下代码:spa

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task  didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * __nullable credential))completionHandler {
    
    // 判断是不是信任服务器证书
    if(challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
        // 告诉服务器,客户端信任证书
        // 建立凭据对象
        NSURLCredential *credntial = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
        // 经过completionHandler告诉服务器信任证书
        completionHandler(NSURLSessionAuthChallengeUseCredential,credntial);
    }
    NSLog(@"protectionSpace = %@",challenge.protectionSpace);
}

2.使用AFNetWorking发送网络请求篇

AFNetworking是一个讨人喜欢的网络库,适用于iOS以及Mac OS X. 它构建于在NSURLConnection, NSOperation, 以及其余熟悉的Foundation技术之上. 它拥有良好的架构,丰富的api,以及模块化构建方式,使得使用起来很是轻松.。代理

2.1 AFNetWorking 2.x版本

考虑到这个版本,咱们还可使用AFHTTPRequestOperationManager这个类来处理网络请求。因此咱们要作的就是给这个类,设置一些参数,让它能够支持https的请求,代码以下:

支持https(校验证书,不能够抓包):

// 1.初始化单例类
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    mgr.securityPolicy.SSLPinningMode = AFSSLPinningModeCertificate;    // 2.设置证书模式
    NSString * cerPath = [[NSBundle mainBundle] pathForResource:@"xxx" ofType:@"cer"];    NSData * cerData = [NSData dataWithContentsOfFile:cerPath];
    mgr.securityPolicy.pinnedCertificates = [[NSArray alloc] initWithObjects:cerData, nil];    // 客户端是否信任非法证书
    mgr.securityPolicy.allowInvalidCertificates = YES;    // 是否在证书域字段中验证域名
    [mgr.securityPolicy setValidatesDomainName:NO];

支持https(不校验证书,能够抓包查看):

// 1.初始化单例类
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    mgr.securityPolicy.SSLPinningMode = AFSSLPinningModeCertificate;    // 2.设置非校验证书模式
    mgr.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    mgr.securityPolicy.allowInvalidCertificates = YES;
    [mgr.securityPolicy setValidatesDomainName:NO];

2.2 AFNetWorking 3.x版本

在Xcode7.0以后,苹果废弃了NSURLConnection方法,数据请求使用NSURLSession,做为网络请求类第三方库使用量最大的AFN也及时的更新的新的版本——AFN 3.0版本。新的版本的里废弃了基于NSURLConnection封装的AFHTTPRequestOperationManager,转而使用基于NSURLSession封装的AFHTTPSessionManager了。

支持https(校验证书,不能够抓包):

// 1.初始化单例类
     AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.securityPolicy.SSLPinningMode = AFSSLPinningModeCertificate;    // 2.设置证书模式
    NSString * cerPath = [[NSBundle mainBundle] pathForResource:@"xxx" ofType:@"cer"];    NSData * cerData = [NSData dataWithContentsOfFile:cerPath];
    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate withPinnedCertificates:[[NSSet alloc] initWithObjects:cerData, nil]];    // 客户端是否信任非法证书
    mgr.securityPolicy.allowInvalidCertificates = YES;    // 是否在证书域字段中验证域名
    [mgr.securityPolicy setValidatesDomainName:NO];

支持https(不校验证书,能够抓包查看):

// 1.初始化单例类
     AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];    // 2.设置非校验证书模式
    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    manager.securityPolicy.allowInvalidCertificates = YES;
    [manager.securityPolicy setValidatesDomainName:NO];

##3、使用Charles抓包https

3.1 软件下载篇

  • 1 工欲善其事必先利其器,须要工具的同窗能够在这里下载,密码: gknp! 下载到V3.9.3版本的Charles软件下载软件后,打开软件,而后command + q退出。 以后将破解文件,放到下面路径 应用程序->Charles->显示包内容->Contents->Resources里面,替换便可。

3.2 程序配置篇

1.进入Charles的配置界面 

QQ20160909-0@2x.png

2.按图上操做 

QQ20160909-1@2x.png

QQ20160909-2@2x.png

这样设置以后理论上就能够抓全部网址443端口的https请求了。可是还没完。咱们还须要安装一个证书。以下图: 

QQ20160909-3@2x.png-224.2kB

3.进入钥匙串,找到这个证书 QQ20160909-4@2x.png-349.8kB单击右键,显示简介。以下图设置为始终信任 

QQ20160909-5@2x.png-92.1kB

以上电脑端的准备就差很少了。

咱们还须要在iPhone手机上,下载一下描述文件。 具体操做,你们就要点开这个网址,手机下载。网址 注意:手机点开这个网址 以下图操做 

QQ20160909-6@2x.png-341.8kB

通过上面些步骤,咱们已经把相应的软件以及证书都布置好了。以后,只须要在手机链接wifi后,设置手机代理。服务器填写电脑ip地址,端口号为8888. 

20160909.jpg

如今你就能够感觉用Charles抓https的请求啦。注意代码要写上面的能够抓包的代码,也就是无证书校验的。若是是有证书校验的,你们就不要妄想抓数据啦。

但愿你们能够关注我耶,我会继续努力分享更多,更好玩的文章的!

文章来自徐不一样的投稿,原文地址:徐不一样的简书

相关文章
相关标签/搜索