開篇
搗鼓幾天搞出來的微信修改版,支持自動搶紅包和修改微信運動步數,并且在設置里面可以開關功能。
需求&最終效果
環境要求與即將使用的工具
環境 | 版本 |
---|---|
操作系統 | MacOSX Catalina 10.15.4 版本太新了不太好用很多工具用不了,我后面打算降級 |
手機系統 | Iphone7 IOS11 |
mac上面的 theos | 最新版 |
xcode | 11.5 |
MonkeyDev | - |
工具介紹
MonkeyDev集成在xcode上面,可以快速開發hook的代碼,鏈接到Mach-O文件,支持修改ipa后的免越獄安裝。配合lldb的調試效率高。
實現過程
頁面UI新增控件
新建一個MonkeyDev項目,我的是WeChatDemo。
先把砸殼后的微信ipa拖拽進工程中的TargetApp目錄。
真機調試執行run編譯運行至手機,成功的話會看到手機多出一個微信,并且可以利用xcode來調試微信了!
首先完成設置頁面UI新增“自動搶紅包”和微信步數輸入框的控件功能。
打開微信設置頁面,xcode打開Debug View Hierarychy查看層級。
查找表格布局的數據源,發現Data Source是WCTableViewManager這個類,所以我們的新增控件功能要在這個類注入方法。
打開class-dump好之后的頭文件WCTableViewManager.h
開發過ios的都知道,表格的實現需要實現UITableViewDelegate, UITableViewDataSource協議,用下面三個方法來控制cell
// 表格每一行的遍歷
- (id)tableView:(id)arg1 cellForRowAtIndexPath:(NSIndexPath*)arg2;
// 每組有多少行
- (long long)tableView:(id)arg1 numberOfRowsInSection:(long long)arg2 ;
// 表格有多少組
- (long long)numberOfSectionsInTableView:(id)arg1 ;
注入這三個方法:
我新增了兩個控件,所以numberOfSections加2
// 表格有多少組
- (long long)numberOfSectionsInTableView:(id)arg1 {
NSLog(@"num2===%@", arg1);
if ([[[[arg1 nextResponder] nextResponder] nextResponder] isKindOfClass:%c(MoreViewController)]) {
NSLog(@"這是設置頁面");
return %orig+2;
}
return %orig;
}
我的每組是一行,所以第4個section和第5個section返回一行。
// 每組有多少行
- (long long)tableView:(id)arg1 numberOfRowsInSection:(long long)arg2 {
NSLog(@"num===%lld", arg2);
// 紅包設置的行
if ([[[[arg1 nextResponder] nextResponder] nextResponder] isKindOfClass:%c(MoreViewController)]) {
NSLog(@"紅包section的行數");
if (arg2 == 4) {
return 1;
} else if (arg2 == 5) {
// 微信運動的行數
return 1;
}
}
return %orig;
}
以下是具體每一個cell的實現。
// 表格每一行的遍歷
- (id)tableView:(id)arg1 cellForRowAtIndexPath:(NSIndexPath*)arg2 {
NSLog(@"indexPath===%@", arg2);
if ([[[[arg1 nextResponder] nextResponder] nextResponder] isKindOfClass:%c(MoreViewController)]) {
if (arg2.section == 4) {
NSLog(@"紅包的行cell");
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"mycell"];
cell.textLabel.text=@"自動搶紅包";
cell.backgroundColor =[UIColor whiteColor];
UISwitch *sw = [[UISwitch alloc] init];
sw.on = [Comm confIsRedEnvelopeSwitchEnable];
[sw addTarget:self action:@selector(redEnvelopeSwitchChange:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = sw;
return cell;
} else if (arg2.section == 5) {
[MyMoreViewController setTableViewObject:arg1];
return [MyMoreViewController createWCSportTextField];
}
}
return %orig;
}
其中
sw.on = [Comm confIsRedEnvelopeSwitchEnable];
是我新增的類方法,獲取配置文件中的是否開啟自動紅包的配置。
[MyMoreViewController createWCSportTextField];
是指創建微信步數控件,具體代碼這里我不列出來了。
搶紅包功能實現
網絡上有很多分析拆紅包代碼流程,這里簡述流程。
首先需要找到微信消息接收入口,就是CMessageMgr這個類的onNewSyncAddMessage方法,普通消息、表情、紅包...等等大部分消息都走這個方法。
然后判斷消息類型(m_uiMessageType),為49時確定為微信紅包消息。
調用下面這個方法告訴微信服務器將要拆紅包的請求。[redEnvelopesLogicMgr ReceiverQueryRedEnvelopesRequest:mutableDict];
mutableDict里面的字典數據就是我們要拼裝的數據。
ReceiverQueryRedEnvelopesRequest調用成功后,微信會回調觸發 [WCRedEnvelopesLogicMgr OnWCToHongbaoCommonResponse]這個方法,這個方法能獲取到timingIdentifier這個參數,然后我們再調用[redEnvelopesLogicMgr OpenRedEnvelopesRequest:redParameter.params];
最終實現收獲紅包的調用。redParameter.params就是請求的參數。
下面代碼實現:
%hook CMessageMgr
// 收到微信消息
- (void)onNewSyncAddMessage:(id)arg1{
%orig;
[MyRedEnvelopesProcc onNewSyncAddMessageProcc:arg1];
}
%end
[MyRedEnvelopesProcc onNewSyncAddMessageProcc:arg1];是我寫的類方法。
// 紅包處理類
@implementation MyRedEnvelopesProcc
+(void) onNewSyncAddMessageProcc:(id)arg1 {
//arg1 = (NSObject)arg1;
CMessageWrap * wrap = arg1;
NSLog(@"紅包==========%@\n%@",arg1,[arg1 class]);
NSLog(@"類型==========%d\n",wrap.m_uiMessageType);
if (wrap.m_uiMessageType != 49) {
NSLog(@"不是紅包消息");
return;
}
if (![Comm confIsRedEnvelopeSwitchEnable]) {
NSLog(@"不啟用搶紅包功能");
return;
}
//收到紅包消息
NSString *nsFromUsr = [wrap m_nsFromUsr];
// 只有是紅包消息類型才會有m_oWCPayInfoItem
WCPayInfoItem *payInfoItem = [wrap m_oWCPayInfoItem];
NSLog(@"payInfoItem==========%@\n",payInfoItem);
if (payInfoItem == nil){
return;
}
NSString * m_c2cNativeUrl = [payInfoItem m_c2cNativeUrl];
if (m_c2cNativeUrl == nil){
NSLog(@"m_c2cNativeUrl是nil !!!!!!!!!");
return;
}
NSInteger length = [@"wxpay://c2cbizmessagehandler/hongbao/receivehongbao?" length];
NSString *subString = [m_c2cNativeUrl substringFromIndex: length];
NSDictionary *dict = [objc_getClass("WCBizUtil") dictionaryWithDecodedComponets:subString separator:@"&"];
NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
[mutableDict setObject:@"1" forKey:@"msgType"];
NSString *sendId = dict[@"sendid"];
[mutableDict safeSetObject:sendId forKey:@"sendId"];
NSString *channelId = dict[@"channelid"];
[mutableDict safeSetObject:channelId forKey:@"channelId"];
CContactMgr *service = [[objc_getClass("MMServiceCenter") defaultCenter] getService:[objc_getClass("CContactMgr") class]];
CContact *contact = [service getSelfContact];
NSString *displayName = [contact getContactDisplayName];
[mutableDict safeSetObject:displayName forKey:@"nickName"];
NSString *headerImg = [contact m_nsHeadImgUrl];
[mutableDict safeSetObject:headerImg forKey:@"headImg"];
id nativeUrl = [payInfoItem m_c2cNativeUrl];
[mutableDict safeSetObject:nativeUrl forKey:@"nativeUrl"];
// 之前獲取m_nsUsrName的方式只有在聊天窗口才有能獲得變量,不人性的做法,改
/*MMMsgLogicManager *logicManager = [[objc_getClass("MMServiceCenter") defaultCenter] getService:[objc_getClass("MMMsgLogicManager") class]];
BaseMsgContentLogicController *logicController = [logicManager GetCurrentLogicController];
id m_contact = [logicController m_contact];
id sessionUserName = [m_contact m_nsUsrName];
//wxid_wps5gzsp30an32
*/
NSString *sessionUserName = [wrap m_nsFromUsr];
[mutableDict safeSetObject:sessionUserName forKey:@"sessionUserName"];
if ([nsFromUsr hasSuffix:@"@chatroom"]){
//群紅包
[mutableDict safeSetObject:@"0" forKey:@"inWay"]; //0:群聊,1:單聊
}else {
//個人紅包
[mutableDict safeSetObject:@"1" forKey:@"inWay"]; //0:群聊,1:單聊
}
[mutableDict safeSetObject:@"0" forKey:@"agreeDuty"];
if (sendId.length > 0) {
SPRedParameter *redParameter = [[SPRedParameter alloc] init];
redParameter.params = mutableDict;
[[SPRedManager sharedInstance] addParams:redParameter];
}
NSLog(@"SPRedManager------mutableDict=%@",mutableDict);
WCRedEnvelopesLogicMgr *redEnvelopesLogicMgr = [[objc_getClass("MMServiceCenter") defaultCenter] getService:[objc_getClass("WCRedEnvelopesLogicMgr") class]];
[redEnvelopesLogicMgr ReceiverQueryRedEnvelopesRequest:mutableDict];
}
主要功能是調用下面這個方法告訴微信將要拆紅包的請求:[redEnvelopesLogicMgr ReceiverQueryRedEnvelopesRequest:mutableDict];
然后再注入這個方法
%hook WCRedEnvelopesLogicMgr
// [redEnvelopesLogicMgr ReceiverQueryRedEnvelopesRequest:mutableDict] 請求后的響應,拆紅包請求的回調
- (void)OnWCToHongbaoCommonResponse:(id)hongBaoRes Request:(id)hongBaoReq {
%orig;
[MyRedEnvelopesProcc OnWCToHongbaoCommonResponseProcc:hongBaoRes Request:hongBaoReq];
}
%end
[MyRedEnvelopesProcc OnWCToHongbaoCommonResponseProcc:hongBaoRes Request:hongBaoReq];是我自己寫的方法。最終實現搶紅包的調用。
+(void) OnWCToHongbaoCommonResponseProcc:(id)hongBaoRes Request:(id)hongBaoReq {
HongBaoRes * response = hongBaoRes;
HongBaoReq * request = hongBaoReq;
NSLog(@"request------=%@",request);
NSLog(@"response------=%@",response);
NSError *err;
NSDictionary *bufferDic = [NSJSONSerialization JSONObjectWithData:response.retText.buffer options:NSJSONReadingMutableContainers error:&err];
NSLog(@"bufferDic------=%@",bufferDic);
if (response == nil || bufferDic == nil){
return;
}
if (request == nil){
return;
}
if (request.cgiCmd == 3){
int receiveStatus = [bufferDic[@"receiveStatus"] intValue];
int hbStatus = [bufferDic[@"hbStatus"] intValue];
/*
可搶狀態:cgiCmdid = 3 自己可搶 , cgiCmdid = 5 自己已搶過
紅包狀態:hbStatus = 2 可搶紅包, hbStatus = 4 自己搶過 ,hbStatus=5 過期紅包
是否自己發的:“isSender”:0 別人發的,“isSender”:1 自己發的
是否群紅包:“hbType”:1 群紅包,“hbType”:0 個人紅包
自己是否搶過:“receiveStatus”:0 未搶過 , “receiveStatus”:2 已搶過
*/
if (receiveStatus == 0 && hbStatus == 2){
// 沒有timingIdentifier字段會被判定為使用外掛
NSString *timingIdentifier = bufferDic[@"timingIdentifier"];
NSString *sendId = bufferDic[@"sendId"];
if (sendId.length > 0 && timingIdentifier.length > 0){
SPRedParameter *redParameter = [[SPRedManager sharedInstance] getParams:sendId];
if (redParameter != nil){
redParameter.timingIdentifier = timingIdentifier;
// 搶的太快也會被判定為使用外掛
sleep(1);
WCRedEnvelopesLogicMgr *redEnvelopesLogicMgr = [[objc_getClass("MMServiceCenter") defaultCenter] getService:[objc_getClass("WCRedEnvelopesLogicMgr") class]];
if (nil != redEnvelopesLogicMgr){
// 真正搶紅包的請求
NSLog(@"redParameter------=%@",redParameter.params);
[redEnvelopesLogicMgr OpenRedEnvelopesRequest:redParameter.params];
}
}
}
}
}
}
微信運動步數實現
// 微信步數
%hook WCDeviceStepObject
- (unsigned int)m7StepCount
{
int count = [[Comm confWcWalkNumberString] intValue];
NSLog(@"步數:%d", count);
if (count) {
return count;
}
return %orig;
}
- (unsigned int)hkStepCount
{
int count = [[Comm confWcWalkNumberString] intValue];
NSLog(@"步數:%d", count);
if (count) {
return count;
}
return %orig;
}
%end
[Comm confWcWalkNumberString]是讀取我配置文件中的步數。
具體其他代碼我上傳到github吧。
博文主索引目錄入口
我會把這系列的文章更新到這個入口里面,分享我的心得,大家互相學習。
2020年 IOS 逆向 反編譯 注入修改游戲或APP的調用參數新手系列教程主目錄入口