前提 : 有时会遇到这样的需求:将一些别人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

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

@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的网络请求就完成了, 如果有不明白或者工具不会使用的留言,看到就会回答. 感谢大家!!!

android拦截请求数据库,拦截所有App的网络请求并将需要的数据上传到属于你自己的服务器...相关推荐

  1. android网络拦截,拦截所有app的网络请求

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

  2. android开发收藏功能实现,Android使用Realm数据库如何实现App中的收藏功能

    Android使用Realm数据库如何实现App中的收藏功能 发布时间:2021-05-07 11:20:34 来源:亿速云 阅读:63 作者:小新 这篇文章主要介绍了Android使用Realm数据 ...

  3. Android 微博实时热点获取(GET)网络请求

    Android 微博实时热点获取(GET)网络请求 首先在xml文件上界面布局 这边采用LinearLayour布局,首先添加TextView控件来显示标题,第二采用RecyclerView列表控件来 ...

  4. 微信小程序/网页/app/android等各种bar图标导航图标文章图标标题图标下拉/检索收藏上传客服等图标整理

    在做微信小程序的时候,需要给底部放置图标.但是找了好久都没有找到自己想要的,适合的.后来我就把所有的小程序的图标都看了下.后来想想还是整理出来,方便自己以后用,也方便大家一下.我敢保证有你想要的,适合 ...

  5. C#工业物联网和集成系统解决方案的技术路线(数据源、数据采集、数据上传与接收、ActiveMQ、Mongodb、WebApi、手机App)

    2000年以后,互联网在中国的大地上如火如荼的发展,在这个行业竞争中比的是加速度.我清晰的记得<世界是平的>中有这样一段话:在非洲,羚羊每天早上醒来时,它知道自己必须跑得比最快的狮子还快, ...

  6. C#工业物联网和集成系统解决方案的技术路线(数据源、数据采集、数据上传与接收、ActiveMQ、Mongodb、WebApi、手机App)...

    目       录 工业物联网和集成系统解决方案的技术路线... 1 前言... 1 第一章           系统架构... 3 1.1           硬件构架图... 3 1.2      ...

  7. 树莓派数据上传数据库_树莓派内部数据向domoticz的上传

    树莓派的数据上传->Domoticz 树莓派的温度检测到的数据可以通过API发送到domoticz 示例代码:#!/usr/bin/env python # -*- coding:utf-8 - ...

  8. Excel数据上传并且导入数据库

    Excel数据上传并导入数据库 Excel表中的数据示例样板 相关依赖(本博客编写环境springboot) <!--poi实现excel导入导出--><dependency> ...

  9. 新大陆物联网-Android实现网关功能-连接云平台并上传传感器数据-获取执行器指令并执行-Android网关开发-通信-数据上传云平台-JAVA原理讲解-免费云平台使用-竞赛2022国赛真题

    目录 一.任务要求 二.开发环境 三.网关上线 四.数据上传与命令下发 五.JSON命令解析思路 六.总结 一.任务要求 我们将要实现的效果是:Android开发平板与Lora板进行有线串口通信,解析 ...

最新文章

  1. 四位先行进位电路逻辑表达式_如何用基本的逻辑门设计32bit的超前进位加法器?...
  2. python语言入门z-python基础语法_8循环语句
  3. python有趣代码-wtfPython―Python中一组有趣微妙的代码【收藏】
  4. Vue中正确使用jQuery的方法
  5. get assigned pageset and my pages
  6. 其中一个页签慢_Word中如何快速定位到页、行、表格、公式,查找与替换方法...
  7. And Then There Was One POJ - 3517(变形约瑟夫环+规律)
  8. Facebook与Google的互联网霸主争夺战
  9. rdlc报表的制作步骤
  10. Unity 使物体朝向某个方位
  11. A*算法、导航网格、路径点寻路对比(A-Star VS NavMesh VS WayPoint)
  12. 【全开源商城小程序源码】ThinkPHP 5.1+带后台商城源码程序+带详细安装使用文档
  13. 计算机犀牛建人体模型步骤,Clayoo加Rhino如何建模卡通人物2
  14. java.lang.UnsatisfiedLinkError: Couldn't load XXX
  15. Flutter Navigator基础使用
  16. 【2020牛客多校】第九场 K The Flee Plan of Groundhog——BFS
  17. 回忆录——一份曾经面试“网易AI产品经理”的作品
  18. 模型小常识,C4D扫描的使用
  19. win10蓝牙功能无法打开的解决办法
  20. webpack安装使用教程

热门文章

  1. 泄密事件不断 内网安全该如何保障?
  2. RecyclerView 联系人排序
  3. java反射的原理_java反射机制的实现原理
  4. cocos2dx-lua 之 ProgressTimer 条形进度条 简单血条的实现
  5. 简易聊天室(C语言)
  6. 阿里巴巴开源项目集锦
  7. input输入正则校验
  8. java 实现AES CCM模式
  9. Vue项目:路由跳转时中文传参被URL编码,怎么解决?用js封装Base64编码解码加密解密
  10. c语言winsock 实现简单的域名解析功能(DNS. v 1.0)