国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

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

詳解IOS如何防止抓包

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

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

防止抓包

為了數(shù)據(jù)的更安全,那么我們?nèi)绾蝸?lái)防止被抓包。

第一種思路是:如果我們能判斷是否有代理,有代理那么就存在風(fēng)險(xiǎn)。

第二種思路:針對(duì)HTTPS 請(qǐng)求。我們判斷證書(shū)的合法性。

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

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

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']) {//沒(méi)有設(shè)置代理return NO; } else {//設(shè)置代理了return YES; }}二、我們可以在請(qǐng)求配置中清空代理,讓請(qǐng)求不走代理

我們通過(guò)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];}

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

主要是針對(duì)HTTPS 請(qǐng)求,對(duì)證書(shū)的一個(gè)驗(yàn)證。

通過(guò) SecTrustRef 獲取服務(wù)端證書(shū)的內(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];}

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

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

SSL Pinning,即SSL證書(shū)綁定。通過(guò)SSL證書(shū)綁定來(lái)驗(yàn)證服務(wù)器身份,防止應(yīng)用被抓包。

1、取到證書(shū)

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

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

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

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

openssl x509 -in name.crt -out name.cer -outform der

如果啥都不給你,你只能自己動(dòng)手了:

openssl s_client -connect www.website.com:443 </dev/null 2>/dev/null | openssl x509 -outform DER > myWebsite.cer**

2、把證書(shū)加進(jìn)項(xiàng)目中

把生成的.cer證書(shū)文件直接拖到你項(xiàng)目的相關(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ù)器證書(shū);

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

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

選擇那種模式呢?

AFSSLPinningModeCertificate:最安全的比對(duì)模式。但是也比較麻煩,因?yàn)樽C書(shū)是打包在APP中,如果服務(wù)器證書(shū)改變或者到期,舊版本無(wú)法使用了,我們就需要用戶更新APP來(lái)使用最新的證書(shū)。

AFSSLPinningModePublicKey:只比對(duì)證書(shū)的Public Key,只要Public Key沒(méi)有改變,證書(shū)的其他變動(dòng)都不會(huì)影響使用。如果你不能保證你的用戶總是使用你的APP的最新版本,所以我們使用AFSSLPinningModePublicKey。

allowInvalidCertificates

/** Whether or not to trust servers with an invalid or expired SSL certificates. Defaults to `NO`. */@property (nonatomic, assign) BOOL allowInvalidCertificates;

是否信任非法證書(shū),默認(rèn)是NO。

validatesDomainName

/** Whether or not to validate the domain name in the certificate’s CN field. Defaults to `YES`. */@property (nonatomic, assign) BOOL validatesDomainName;

是否校驗(yàn)證書(shū)中DomainName字段,它可能是IP,域名如*.google.com,默認(rèn)為YES,嚴(yán)格保證安全性。

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;}擴(kuò)展

Android 防止抓包

1、單個(gè)接口訪問(wèn)不帶代理的

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如何防止抓包的詳細(xì)內(nèi)容,更多關(guān)于IOS如何防止抓包的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: IOS
相關(guān)文章:
主站蜘蛛池模板: 凤冈县| 甘肃省| 石屏县| 永登县| 丘北县| 安吉县| 商水县| 乌海市| 吴旗县| 板桥市| 桂平市| 长海县| 肇庆市| 遵化市| 四平市| 嘉黎县| 呼和浩特市| 安乡县| 上蔡县| 金华市| 青冈县| 美姑县| 原平市| 金寨县| 高碑店市| 东丽区| 中西区| 皮山县| 鄄城县| 金乡县| 即墨市| 东明县| 迁安市| 郧西县| 芜湖县| 广汉市| 四平市| 南京市| 开封市| 东明县| 平度市|