支持https請求以及https請求的抓包


iOS9推出的時候,蘋果希望大家使用https協議,來提高數據傳輸之間的安全性。下面我就從最簡單的代碼介紹,如何在工程中設置,來支持https的請求。

 

一、證書准備篇

 

  • 1.證書轉換

    在服務器人員,給你發送的crt證書后,進到證書路徑,執行下面語句

    // openssl x509 -in 你的證書.crt -out 你的證書.cer -outform der

    這樣你就可以得到cer類型的證書了。雙擊,導入電腦。

  • 2.證書放入工程

    1、可以直接把轉換好的cer文件拖動到工程中。

    2、可以在鑰匙串內,找到你導入的證書,單擊右鍵,導出項目,就可以導出.cer文件的證書了

 

二、代碼修改篇

 

先在info.plist中,增加如下圖的配置

 

 

文本內容如下:

 

<key>NSAppTransportSecurity</key>

    <dict>

        <key>NSAllowsArbitraryLoads</key>

        <true/>

    </dict>

 

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來處理相關的網絡請求了,如果使用系統自帶的類,可以參考如下代碼:

 

- (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];

 

三、使用Charles抓包https

 

3.1 軟件下載篇

 

1 工欲善其事必先利其器,需要工具的同學可以在這里下載,密碼: gknp!

下載到V3.9.3版本的Charles軟件下載軟件后,打開軟件,然后command + q退出。

之后將破解文件,放到下面路徑

應用程序->Charles->顯示包內容->Contents->Resources里面,替換即可。

 

3.2 程序配置篇

 

1.進入Charles的配置界面

 

2.按圖上操作

這樣設置之后理論上就可以抓所有網址443端口的https請求了。但是還沒完。我們還需要安裝一個證書。如下圖:

 

 

3.進入鑰匙串,找到這個證書

單擊右鍵,顯示簡介。如下圖設置為始終信任

 

以上電腦端的准備就差不多了。

 

我們還需要在iPhone手機上,下載一下描述文件。

具體操作,大家就要點開這個網址,手機下載。

https://www.charlesproxy.com/documentation/additional/legacy-ssl-proxying/

 

注意:手機點開這個網址

如下圖操作

 

經過上面些步驟,我們已經把相應的軟件以及證書都布置好了。之后,只需要在手機連接wifi后,設置手機代理。服務器填寫電腦ip地址,端口號為8888.

 

現在你就可以感受用Charles抓https的請求啦。注意代碼要寫上面的可以抓包的代碼,也就是無證書校驗的。如果是有證書校驗的,大家就不要妄想抓數據啦。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM