国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

詳解IOS如何防止抓包

瀏覽:11日期:2022-09-16 16:02:12
目錄抓包原理防止抓包一、發(fā)起請求之前判斷是否存在代理,存在代理就直接返回,請求失敗。二、我們可以在請求配置中清空代理,讓請求不走代理SSL Pinning(AFN+SSL Pinning)推薦擴展抓包原理

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

防止抓包

為了數(shù)據(jù)的更安全,那么我們?nèi)绾蝸矸乐贡蛔グ?/p>

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

第二種思路:針對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']) {//沒有設(shè)置代理return NO; } else {//設(shè)置代理了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 獲取服務(wù)端證書的內(nèi)容

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

然后讀取本地證書的內(nèi)容進行對比

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

SSL Pinning,即SSL證書綁定。通過SSL證書綁定來驗證服務(wù)器身份,防止應(yīng)用被抓包。

1、取到證書

客戶端需要證書(Certification file), .cer格式的文件。可以跟服務(wù)器端索取。

如果他們給個.pem文件,要使用命令行轉(zhuǎn)換:

openssl x509 -inform PEM -in name.pem -outform DER -out name.cer

如果給了個.crt文件,請這樣轉(zhuǎn)換:

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證書文件直接拖到你項目的相關(guān)文件夾中,記得勾選Copy items if neede和Add to targets。

3、參數(shù)名意思

AFSecurityPolicy

SSLPinningMode

AFSecurityPolicy是AFNetworking中網(wǎng)絡(luò)通信安全策略模塊。它提供三種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:完全信任服務(wù)器證書;

AFSSLPinningModePublicKey:只比對服務(wù)器證書和本地證書的Public Key是否一致,如果一致則信任服務(wù)器證書;

AFSSLPinningModeCertificate:比對服務(wù)器證書和本地證書的所有內(nèi)容,完全一致則信任服務(wù)器證書;

選擇那種模式呢?

AFSSLPinningModeCertificate:最安全的比對模式。但是也比較麻煩,因為證書是打包在APP中,如果服務(wù)器證書改變或者到期,舊版本無法使用了,我們就需要用戶更新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設(shè)置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如何防止抓包的詳細內(nèi)容,更多關(guān)于IOS如何防止抓包的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標簽: IOS
相關(guān)文章:
主站蜘蛛池模板: 久久只有这才是精品99 | 亚洲国产欧美目韩成人综合 | 久久精品国产99久久6动漫欧 | 欧美理论片在线观看一区二区 | 成熟性xxxxx| 日韩不卡毛片 | 男女配种猛烈免费视频 | 国产精品一区二区三区久久 | 1024香蕉国产在线视频 | 黄性色 | 免费国产成人 | 成年毛片 | 久免费视频 | 亚洲精品视频免费观看 | 日本一道免费一区二区三区 | 国产精品久久久久久一区二区三区 | 中文字幕在亚洲第一在线 | 成年女人毛片免费播放人 | 亚洲在线免费 | 国内精品一区二区 | 免费黄色网址在线播放 | 伊人五月天婷婷琪琪综合 | 我看毛片| 国产三级毛片 | 国产欧美日韩一区二区三区 | 欧美67194| 99久久免费看国产精品 | 在线观看日本亚洲一区 | 激情一区二区三区成人 | 亚洲一区二区三区四区在线 | 97在线视频网站 | 91精品视频在线播放 | 中国美女一级片 | 真人毛片免费全部播放完整 | 亚洲国产精久久久久久久春色 | 亚洲激情视频网 | 亚洲一区二区中文 | 极品的亚洲 | 国产第一页久久亚洲欧美国产 | 亚洲国产精品欧美日韩一区二区 | 怡红院老首页主页入口 |