欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解IOS如何防止抓包

 更新時間:2021年06月07日 10:32:05   作者:為童沉淪  
為了防止被抓包那么就要了解抓包的原理。本文將詳細介紹IOS如何防止抓包,感興趣的同學,可以參考下。

抓包原理

其實原理很是簡單:一般抓包都是通過代理服務來冒充你的服務器,客戶端真正交互的是這個假冒的代理服務,這個假冒的服務再和我們真正的服務交互,這個代理就是一個中間者 ,我們所有的數(shù)據(jù)都會通過這個中間者,所以我們的數(shù)據(jù)就會被抓取。HTTPS 也同樣會被這個中間者偽造的證書來獲取我們加密的數(shù)據(jù)。

防止抓包

為了數(shù)據(jù)的更安全,那么我們如何來防止被抓包。

第一種思路是:如果我們能判斷是否有代理,有代理那么就存在風險。

第二種思路:針對HTTPS 請求。我們判斷證書的合法性。

第一種方式的實現(xiàn):

一、發(fā)起請求之前判斷是否存在代理,存在代理就直接返回,請求失敗。

CFDictionaryRef dicRef = CFNetworkCopySystemProxySettings();
CFStringRef proxyStr = CFDictionaryGetValue(dicRef, kCFNetworkProxiesHTTPProxy);
NSString *proxy = (__bridge NSString *)(proxyStr);
+ (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;
    }
}

二、我們可以在請求配置中清空代理,讓請求不走代理

我們通過hook到sessionWithConfiguration: 方法。然后清空代理

+ (void)load{
  Method method1 = class_getClassMethod([NSURLSession class],@selector(sessionWithConfiguration:));
  Method method2 = class_getClassMethod([NSURLSession class],@selector(px_sessionWithConfiguration:));
  method_exchangeImplementations(method1, method2);

  Method method3 = class_getClassMethod([NSURLSession class],@selector(sessionWithConfiguration:delegate:delegateQueue:));
  Method method4 = class_getClassMethod([NSURLSession class],@selector(px_sessionWithConfiguration:delegate:delegateQueue:));
  method_exchangeImplementations(method3, method4);
}

+ (NSURLSession*)px_sessionWithConfiguration:(NSURLSessionConfiguration*)configuration delegate:(nullable id)delegate delegateQueue:(nullable NSOperationQueue*)queue
{
      if(configuration) configuration.connectionProxyDictionary = @{};

  return [self px_sessionWithConfiguration:configuration delegate:delegate delegateQueue:queue];
}

+ (NSURLSession*)px_sessionWithConfiguration:(NSURLSessionConfiguration*)configuration
{

      if(configuration) configuration.connectionProxyDictionary = @{};

  return [self px_sessionWithConfiguration:configuration];
}

​ 第二種思路的實現(xiàn):

主要是針對HTTPS 請求,對證書的一個驗證。

通過 SecTrustRef 獲取服務端證書的內容

static NSArray * AFCertificateTrustChainForServerTrust(SecTrustRef serverTrust) {
   CFIndex certificateCount = SecTrustGetCertificateCount(serverTrust);
   NSMutableArray *trustChain = [NSMutableArray arrayWithCapacity:(NSUInteger)certificateCount];

   for (CFIndex i = 0; i < certificateCount; i++) {
       SecCertificateRef certificate = SecTrustGetCertificateAtIndex(serverTrust, i);
       [trustChain addObject:(__bridge_transfer NSData *)SecCertificateCopyData(certificate)];
   }

   return [NSArray arrayWithArray:trustChain];
}

然后讀取本地證書的內容進行對比

NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"certificate" ofType:@"cer"];//證書的路徑
NSData *certData = [NSData dataWithContentsOfFile:cerPath];
SSet<NSData*> * set = [[NSSet alloc]initWithObjects:certData  , nil];  // 本地證書內容
// 服務端證書內容
NSArray *serverCertificates = AFCertificateTrustChainForServerTrust(serverTrust);
for (NSData *trustChainCertificate in [serverCertificates reverseObjectEnumerator]) {
    if ([set containsObject:trustChainCertificate]) {
        // 證書驗證通過
    }
}

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、參數(shù)名意思

AFSecurityPolicy

SSLPinningMode

AFSecurityPolicy是AFNetworking中網(wǎng)絡通信安全策略模塊。它提供三種SSL Pinning Mode

/**

 ## SSL Pinning Modes

 The 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(); 

以上就是詳解IOS如何防止抓包的詳細內容,更多關于IOS如何防止抓包的資料請關注腳本之家其它相關文章!

相關文章

  • iOS實現(xiàn)去除html標簽的方法匯總

    iOS實現(xiàn)去除html標簽的方法匯總

    相信大家在做網(wǎng)站的時候,經(jīng)常會遇到去除html標簽的問題,下面這篇文章主要給大家總結介紹了關于iOS如何實現(xiàn)去除html標簽的一些方法,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-10-10
  • 分享一個關于Storyboard 跳轉與傳值

    分享一個關于Storyboard 跳轉與傳值

    近日不忙,給大家分享一個關于storyboard跳轉傳值的相關知識,感興趣的朋友一起看看吧
    2015-12-12
  • iOS App設計模式開發(fā)中對interpreter解釋器模式的運用

    iOS App設計模式開發(fā)中對interpreter解釋器模式的運用

    這篇文章主要介紹了iOS App設計模式開發(fā)中對interpreter解釋器模式的運用,示例為傳統(tǒng)的Objective-C寫成,需要的朋友可以參考下
    2016-04-04
  • iOS頁面跳轉及數(shù)據(jù)傳遞(三種)

    iOS頁面跳轉及數(shù)據(jù)傳遞(三種)

    本文主要介紹了iOS頁面跳轉的三種方法及數(shù)據(jù)傳遞的方法。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-03-03
  • iOS實現(xiàn)列表與網(wǎng)格兩種視圖的相互切換

    iOS實現(xiàn)列表與網(wǎng)格兩種視圖的相互切換

    相信大家應該也都發(fā)現(xiàn)了,在現(xiàn)在很多的電商app中,都會有列表視圖和網(wǎng)格兩種視圖的相互切換。例如京東和淘寶。這樣更利于提高用戶的體驗度,所以這篇文章小編就是大家分享下利用iOS實現(xiàn)列表與網(wǎng)格兩種視圖相互切換的方法,文中介紹的很詳細,感興趣的下面來一起看看吧。
    2016-10-10
  • iOS獲取到用戶當前位置

    iOS獲取到用戶當前位置

    這篇文章主要為大家詳細介紹了iOS獲取到用戶當前位置,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • 理解iOS多線程應用的開發(fā)以及線程的創(chuàng)建方法

    理解iOS多線程應用的開發(fā)以及線程的創(chuàng)建方法

    這篇文章主要介紹了理解iOS多線程應用的開發(fā)以及線程的創(chuàng)建方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2015-11-11
  • unix 編程進程控制詳細介紹

    unix 編程進程控制詳細介紹

    這篇文章主要介紹了unix 編程進程控制詳細介紹的相關資料,需要的朋友可以參考下
    2017-01-01
  • 關于適配iOS11和iPhoneX的一些事

    關于適配iOS11和iPhoneX的一些事

    隨著iOS11和xcode9剛開始正式發(fā)布,小編也迫不及待的更新了xcode9,手機也順利更新到iOS,也終于見到iPhone X 的真面,怎么適配是個問題,下面這篇文章主要給大家介紹了關于適配iOS11和iPhoneX的一些事,需要的朋友可以參考下。
    2017-10-10
  • iOS模仿微信長按識別二維碼的多種方式

    iOS模仿微信長按識別二維碼的多種方式

    這篇文章主要介紹了iOS模仿微信長按識別二維碼的兩種方式,文章第二種方式是識別網(wǎng)頁中的二維碼,具體思路詳解大家參考下本文
    2017-07-07

最新評論