(iOS) App防抓包

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

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

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类方法即可实现实时打开关闭抓包设置。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 我们先看一下AFNetworking.h文件都给了我们什么方法 #import <Foundation/Found...
    潇岩阅读 3,919评论 0 1
  • 大部分人缺乏一种主动发掘信息的意识。读着别人写好的文字,思考着别人提出的问题,做着别人交代的事情。 https:/...
    士梦阅读 4,898评论 0 4
  • 建议去看原文 AFURLSessionManager _AFURLSessionTaskSwizzling 当时看...
    泥孩儿0107阅读 3,690评论 0 0
  • 平时在项目中一直是用网络请求的第三方框架 AFNetwork和 ASIHTTPRequest比较多一些。最近还是想...
    明若晴空阅读 6,521评论 0 1
  • 1.NSTimer //暂停if ([timer isValid]) {[timer setFireDate:[N...
    俊月阅读 5,190评论 0 0