iOS逆向0010--HOOK(MethodSwizzle-方法交換)

一、Hook的實(shí)現(xiàn):自己工程中交換 實(shí)例方法、類方法

1.新建一個項(xiàng)目工程
2.創(chuàng)建一個類HookMgr,用來實(shí)現(xiàn)方法交換的

  • HookMgr 類
#import <Foundation/Foundation.h>
@interface HookMgr : NSObject
//Hook方法
+ (void)hookClass:(Class)class oldMethod:(SEL)oldMethod newMethod:(SEL)newMethod;
@end


#import "HookMgr.h"
#import <objc/message.h>
@implementation HookMgr
/**  Hook方法
 SEL                IMP
 字符串(char*) --> 函數(shù)指針
 方法指針交換 --> 方法交換 
 */
+ (void)hookClass:(Class)class oldMethod:(SEL)oldMethod newMethod:(SEL)newMethod {
    
    // 獲得實(shí)例方法
    Method old = class_getInstanceMethod(class, oldMethod);
    Method new = class_getInstanceMethod(class, newMethod);
    // 方法交換:交換IMP
    method_exchangeImplementations(old, new);
}


  • ViewController 類
#import "ViewController.h"
#import "HookMgr.h"
#import <objc/message.h>

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    /** 方法的交換
     */
    //交換實(shí)例方法
    [HookMgr hookClass:self.class oldMethod:@selector(oldMethod) newMethod:@selector(newMethod)];
    [HookMgr hookClass:self.class oldMethod:@selector(btnClick:) newMethod:@selector(newClick:)];
    //交換類方法
    [HookMgr hookClass:object_getClass(self.class) oldMethod:@selector(oldClassMethod) newMethod:@selector(newClassMethod)];
}

- (IBAction)btnClick:(id)sender {
    NSLog(@"btnClick: 按鈕點(diǎn)擊?。?");
}
- (void)newClick:(id)sender {
    NSLog(@"newClick: 按鈕點(diǎn)擊??! ");
}

+ (void)newClassMethod {
    NSLog(@"---newClassMethod被調(diào)用");
}
+ (void)oldClassMethod {
    NSLog(@"---oldClassMethod被調(diào)用");
}

- (void)newMethod {
    NSLog(@"---新:newMethod被調(diào)用");
}
- (void)oldMethod {
    NSLog(@"---舊:oldMethod被調(diào)用");
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self oldMethod];
    [ViewController oldClassMethod];
}
@end


二、Hook微信的注冊方法

1、請先看這篇文章:iOS逆向009--代碼注入、Dylib注入

2、使用注入Framework庫的項(xiàng)目工程,來做這份案例

3、修改shell腳本,將Framework庫添加到MachO文件

# ---------------------------------------------------
# 7. 注入我們編寫的動態(tài)庫
echo "開始注入"
# 需要注入的動態(tài)庫的路徑  這個路徑我就寫死了!
# INJECT_FRAMEWORK_RELATIVE_PATH="Frameworks/libZMHook.dylib"
INJECT_FRAMEWORK_RELATIVE_PATH="Frameworks/ZMHookFramework.framework/ZMHookFramework"
#
## 通過工具實(shí)現(xiàn)注入  MachO文件
yololib "$TARGET_APP_PATH/$APP_BINARY" "$INJECT_FRAMEWORK_RELATIVE_PATH"
echo "注入完成"

4、運(yùn)行工程,查看微信注冊按鈕

所在的類:Target <WCAccountLoginControlLogic: 0x104de6930>
點(diǎn)擊響應(yīng)的按鈕:Action onFirstViewRegester
image.png

5、將微信的頭文件導(dǎo)出,查看WCAccountLoginControlLogic類

image.png

5.1 //連續(xù)的3個命令:導(dǎo)出 Wechat 的所有頭文件

$  cd ZM_破壞微信的注冊.app
$ ls
$ class-dump -H WeChat  -o /Users/zhangmeng/Desktop/WeChatHeardes/ 

5.2 將Headers文件夾拖到Sublime Text工具,
使用快捷鍵:Cmd+Shift+F查找 WCAccountLoginControlLogic,雙擊WCAccountLoginControlLogic.h文件,進(jìn)入此文件

image.png
image.png

6、編輯Hook微信注冊的方法

#import "ZM_Hook.h"
#import <objc/message.h>

@implementation ZM_Hook

+ (void)load
{
    //拿到微信的 注冊方法
    Method oldMethod = class_getInstanceMethod(objc_getClass("WCAccountLoginControlLogic"), @selector(onFirstViewRegester));    
    //自己的方法
    Method newMethod = class_getInstanceMethod(self, @selector(test1));
    method_exchangeImplementations(oldMethod, newMethod);                                     
    NSLog(@"---ZM_破壞微信的注冊:成功了??!??????????????!");
}

- (void)test1 {
    NSLog(@"哥們檢測到異常,不能注冊!!????????????");
}
@end



7、運(yùn)行工程,Hook微信注冊方法

  • 運(yùn)行成功打?。?--ZM_破壞微信的注冊:成功了??!??????????????!
  • 點(diǎn)擊注冊按鈕打?。焊鐐儥z測到異常,不能注冊!!????????????
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容