防止ios/android系统被抓包的方法

425 阅读3分钟

怎么防止iOS系统被抓包的呢?

我们知道 iOS 系统, 是可以通过 charles finddler 等等抓包工具来获取 app 请求发送的接口以及参数, 这样的话, 我们可以去抓取别人上线的app, 同理, 别人也能抓取我们的app, 给心有不轨之人有了可乘之机, 那么, 上线之后怎么防止这种情况的呢?

既然知道抓包, 能抓包, 大概都了解抓包的操

  1. 需要手机与抓包工具在同一网段,

  2. 设置代理,

  3. 爽歪歪了, 就可以进行你要抓包的操作了,

既然知道流程, 那么接下来要做的事情 就相对相对简单了,

我们可以检查自己的网络是否处于代理网络之下,

如果这个时候处于代理网络下就拒绝发送任何请求。

那么如何检测是否有代理 以下提供几个检测代理的方法

  1. 对于我们没有做过的, 我们第一印象就是去百度, 也没有让我失望:

pod 'ZXRequestBlock'

我很频繁的尝试了这个库, 发现不符合我的预期, 放弃了, 在我用的哪个版本没效果的

  1. 网上还有说的那个证书放在本地, 放本地的话, 放本地的话, 放本地的话 我都拿到包了, 还愁不能拿到你的文件的吗?

这种虽然可行, 但是我不推荐

  1. 下面的代码,如果 proxy 有值,可以判断当前 wifi 使用了 http proxy

#import <SystemConfiguration/CaptiveNetwork.h>

- (id)fetchHttpProxy {

CFDictionaryRef dicRef = CFNetworkCopySystemProxySettings();

const CFStringRef proxyCFstr = (const CFStringRef)CFDictionaryGetValue(dicRef,

(const void*)kCFNetworkProxiesHTTPProxy);

NSString* proxy = (__bridge NSString *)proxyCFstr;

return proxy;

}

// 如果是有代理的话, 就会出现下面的情况, 如果没有代理, proxynil

image.png

  1. 还有一些WiFi的其他信息也可以通过代码获取到,比如:ssid,广播地址、子网掩码、端口等:

这个也是网上找到的, 但是检测结果不理想

- (id)fetchSSIDInfo {

NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();

NSLog(@"Supported interfaces: %@", ifs);

id info = nil;

for (NSString *ifnam in ifs) {

info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);

NSLog(@"%@ => %@", ifnam, info);

if (info && [info count]) { break; }

}

return info;

}

  1. 不过, 尝试了 上面几个方法, 下面的这个是最方便的

- (BOOL) checkProxySetting {

NSDictionary *proxySettings = (__bridge NSDictionary *)(CFNetworkCopySystemProxySettings());

NSArray *proxies = (__bridge NSArray *)(CFNetworkCopyProxiesForURL((__bridge CFURLRef _Nonnull)([NSURL URLWithString:@"https://www.baidu.com"]), (__bridge CFDictionaryRef _Nonnull)(proxySettings)));

NSDictionary *settings = proxies[0];

if ([[settings objectForKey:(NSString *)kCFProxyTypeKey] isEqualToString:@"kCFProxyTypeNone"])

{

NSLog(@"没设置代理");

return NO;

}

else

{

NSLog(@"设置了代理");

return YES;

}

}

  1. 我们可以在请求配置中清空代理,让请求不走代理, 我们通过hook到sessionWithConfiguration: 方法。然后清空代理

#import "NSURLSession+Extension.h"

#import <objc/runtime.h>

@implementation NSURLSession (EE)

+ (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];

}

@end


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

后话

这样不能禁止禁止所有的抓包情况, 尤其是在真正的高手面前, 这个检测就是一张纸, 但是, 我们这样做能杜绝很大一部分人禁止抓包的行为, 让我们 app 更加 strong


End