“我正在参加「启航计划」”
前言
动手实践:写一个tweak ,修正恳求的HTTPHeaderField
NSURLProtocol 只能阻拦 UIURLConnection、NSURLSession 和 UIWebView 中的恳求;
对于 WKWebView 中宣布的网络恳求也无能为力
,如果真的要阻拦来自 WKWebView 中的恳求,还是需求实现 WKWebView 对应的 WKNavigationDelegate,并在署理办法中获取恳求。
使用场景:
1、 自定义恳求头的HTTPHeaderField
2、针对NSURLSessionConfiguration设置署理和端口,让一些特别的恳求走自定义的地道IP和端口
I NSURLProtocol 阻拦 HTTP 恳求
1.1 NSURLProtocol 阻拦 HTTP 恳求的原理
An NSURLProtocol object handles the loading of protocol-specific URL data. The NSURLProtocol class itself is an abstract class that provides the infrastructure for processing URLs with a specific URL scheme. You create subclasses for any custom protocols or URL schemes that your app supports.
HTTP 恳求开始时,URL 加载体系创立一个合适的 NSURLProtocol 目标处理对应的 URL 恳求,因此咱们只需写一个承继自 NSURLProtocol 的类,并通过 – registerClass: 办法注册咱们的协议类,然后 URL 加载体系就会在恳求宣布时使用咱们创立的协议目标对该恳求进行处理。
1.2 使用 NSURLProtocol 阻拦 HTTP 恳求
从CSDN下载Demo:https://download.csdn.net/download/u011018979/16753164
1、文章:kunnan.blog.csdn.net/article/det… 2、原理:利用NSURLProtocol 阻拦 HTTP 恳求 3、使用场景:地道APP恳求咱们自己接口的都不走地道、修正恳求的HTTPHeaderField,设置署理IP和端口、防抓包(使Thor,Charles,Burp等署理抓包方式悉数失效) CustomHTTPProtocol
- 决议恳求是否需求当时协议目标处理的办法
/**
决议恳求是否需求当时协议目标处理
*/
+(BOOL)canInitWithRequest:(NSURLRequest *)request{
if ([NSURLProtocol propertyForKey:protocolKey inRequest:request]) {
return NO;
}
NSString * url = request.URL.absoluteString;
return [self isUrl:url];
}
- 对当时的恳求目标需求进行哪些处理
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request;
修正NSURLRequest 目标:在CanonicalRequestForRequest 办法中,能够修正 request headers
- NSURLProtocol 如何实例化?
- (instancetype)initWithRequest:(NSURLRequest *)request cachedResponse:(nullable NSCachedURLResponse *)cachedResponse client:(nullable id <NSURLProtocolClient>)client NS_DESIGNATED_INITIALIZER;
- app发动的时分进行注入
/*! Call this to start the module. Prior to this the module is just dormant, and
* all HTTP requests proceed as normal. After this all HTTP and HTTPS requests
* go through this module.
*/
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[CustomHTTPProtocol setDelegate:self];
if (YES) {
[CustomHTTPProtocol start];
}
}
II 动手实践
源码获取请关注【公号:iOS逆向】: 写一个tweak ,修正恳求的HTTPHeaderField
2.1 hook
%hook AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
%log();
BOOL rect = %orig;
[CustomHTTPProtocol setDelegate:self];
if (YES) {
[CustomHTTPProtocol start];
}
return rect;
}
%end
2.2 针对NSURLSessionConfiguration设置署理和端口
+ (QNSURLSessionDemux *)sharedDemux
{
static dispatch_once_t sOnceToken;
static QNSURLSessionDemux * sDemux;
dispatch_once(&sOnceToken, ^{
NSURLSessionConfiguration * config;
config = [NSURLSessionConfiguration defaultSessionConfiguration];
// You have to explicitly configure the session to use your own protocol subclass here
// otherwise you don't see redirects <rdar://problem/17384498>.
#pragma mark - ******** 针对NSURLSessionConfiguration设置署理和端口
NSString* proxyHost = @"socks-";
// NSString* proxyHost = @"192.168.2.166";
NSNumber* proxyPort = [NSNumber numberWithInt: 8830 ];
// NSNumber* proxyPort = [NSNumber numberWithInt: 8888 ];
// Create an NSURLSessionConfiguration that uses the proxy
NSDictionary *proxyDict = @{
@"HTTPEnable" : @1,
(NSString *)kCFStreamPropertyHTTPProxyHost : proxyHost,
(NSString *)kCFStreamPropertyHTTPProxyPort : proxyPort,
@"HTTPSEnable" : @(1),
(NSString *)kCFStreamPropertyHTTPSProxyHost : proxyHost,
(NSString *)kCFStreamPropertyHTTPSProxyPort : proxyPort,
};
// NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
config.connectionProxyDictionary = proxyDict;
[config setHTTPAdditionalHeaders:@{
Authorizationkey:Authorizationvalue,
}
];
config.protocolClasses = @[ self ];
sDemux = [[QNSURLSessionDemux alloc] initWithConfiguration:config];
});
return sDemux;
}
2.3 测试
- 查看恳求头信息
%hook NSURLProtocol
+ (void)setProperty:(id)value
forKey:(NSString *)key
inRequest:(NSMutableURLRequest *)request{
%log();
%orig;
}
%end
see also
- iOS APP 不走大局proxy的计划【 例如:地道APP的恳求接口,一些自己特别接口不走地道】(利用NSURLProtocol 进行恳求的阻拦)
kunnan.blog.csdn.net/article/det…
- 防署理 configuration.connectionProxyDictionary
//APP恳求咱们自己接口的都不走地道
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
configuration.connectionProxyDictionary = @{};
// 2/ AFHTTPSessionManager 创立NSURLSessionDataTask
AFHTTPSessionManager *mgr = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:configuration];