(iOS) App防抓包

2019-01-09  本文已影响0人  終于

大致有两种做法,一种是检测到有代理服务器,就不发送网络请求;一种是不发送给代理服务器,而是正常发给目标服务器。

第一种,提供一个检测当前手机是否有开启代理,剩下的工作根据业务去完成即可。

CFDictionaryRef dicRef = CFNetworkCopySystemProxySettings();

const CFStringRef proxyCFstr = CFDictionaryGetValue(dicRef, (const void*)kCFNetworkProxiesHTTPProxy);

NSString* proxy = (__bridgeNSString*)(proxyCFstr);

 if(proxy)returnYES;

 else return NO;    

第二种,因为NSURLSession实例化需要传入NSURLSessionConfiguration对象,config中有个属性是connectionProxyDictionary,保存的是网络会话连接中的代理服务器。所以,不走代理服务器只需要Hook系统方法,将此属性置为空字典即可。

Method method1 = class_getClassMethod([NSURLSession class],@selector(sessionWithConfiguration:));

Method method2 = class_getClassMethod([NSURLSession class],@selector(hhz_sessionWithConfiguration:));

method_exchangeImplementations(method1, method2);

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

Method method4 = class_getClassMethod([NSURLSession class],@selector(hhz_sessionWithConfiguration:delegate:delegateQueue:));

method_exchangeImplementations(method3, method4);

+ (NSURLSession*)hhz_sessionWithConfiguration:(NSURLSessionConfiguration*)configuration

                                     delegate:(nullableid)delegate

                                delegateQueue:(nullableNSOperationQueue*)queue

{

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

    return [self hhz_sessionWithConfiguration:configuration delegate:delegate delegateQueue:queue];

}

+ (NSURLSession*)hhz_sessionWithConfiguration:(NSURLSessionConfiguration*)configuration

{

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

    return [self hhz_sessionWithConfiguration:configuration];

}

其中method替换可以抽离出方法,此处为了方便复制粘贴测试就没处理。然后根据项目需求,提供相应的open,close类方法即可实现实时打开关闭抓包设置。

上一篇 下一篇

猜你喜欢

热点阅读