iOS逆向 11:代碼注入(下)

iOS 底層原理 + 逆向 文章匯總

本文主要是以WeChat為例,講解如何破壞WeChat注冊、以及如何獲取登錄密碼

引子

在進(jìn)行WeChat實踐操作時,首先需要了解一個概念:Method Swizzing(即方法交換)

Method Swizzing(即方法交換)是利用OC的Runtime特性,動態(tài)改變SEL(方法編號)和IMP(方法實現(xiàn))的對應(yīng)關(guān)系,達(dá)到OC方法調(diào)用流程改變的目的,主要用于OC方法。

在OC中,SEL和IMP之間的關(guān)系,類似與一本書的目錄,是一一對應(yīng)的

  • SEL:方法編號,類似于目錄中的標(biāo)題

  • IMP:方法實現(xiàn)的真實地址指針,類似于目錄中的頁碼

同時,Runtime中也提供了用于交換兩個SEL和IMP的方法,method_exchangeIMP,我們可以通過這個函數(shù)交換兩個SEL和IMP的對應(yīng)關(guān)系

更為具體的講解請參考這篇文章:iOS-底層原理 21:Method-Swizzling 方法交換

破壞微信注冊

準(zhǔn)備工作:需要新建一個工程,并重簽名,且采用Framework注入的方式

這里破壞的微信的注冊,主要是通過runtime方法進(jìn)行注冊方法的hook

1、獲取注冊的相關(guān)信息

  • 1、通過lldb調(diào)試獲取WeChat的注冊

    獲取注冊的相關(guān)信息-01

  • 2、獲取注冊的target、action


    獲取注冊的相關(guān)信息-02
    • target:WCAccountLoginControlLogic

    • action:onFirstViewRegister

2、簡單hook演示

  • 1、通過class_getInstanceMethod +method_exchangeImplementations方法,hook注冊的點擊事件
@implementation inject

+ (void)load{
//    改變微信的注冊
    Method oldMethod = class_getInstanceMethod(objc_getClass("WCAccountLoginControlLogic"), @selector(onFirstViewRegister));
    Method newMethod = class_getInstanceMethod(self, @selector(my_method));
    
    method_exchangeImplementations(oldMethod, newMethod);
    
}

- (void)my_method{
    NSLog(@"CJLHook --- 注冊不了了!");
}

@end
  • 2、重新運(yùn)行,點擊注冊按鈕,發(fā)現(xiàn)執(zhí)行的是我們自己的方法


    簡單hook演示

3、點擊登錄時,獲取用戶的密碼

準(zhǔn)備工作

  • 1、點擊登錄,輸入密碼

  • 2、點擊Debug View Hierarchy動態(tài)調(diào)試登錄界面

  • 3、獲取登錄按鈕信息


    準(zhǔn)備工作-01
    • target:WCAccountMainLoginViewController
    • action:onNext
  • 4、獲取密碼的類:WCUITextField


    準(zhǔn)備工作-02

方式1:通過lldb獲取密碼

  • llfb獲取密碼的調(diào)試如下


    通過lldb獲取密碼-01

此時問題來了,如果我們想通過hook登錄方法,在點擊登錄按鈕時,如何獲取密碼呢?有以下幾種方式

  • 1、通過響應(yīng)鏈,一層一層查找,缺點是很繁瑣

    響應(yīng)鏈方式

  • 2、靜態(tài)分析:可以通過class-dump獲取類、方法的列表,其本質(zhì)也是從Mach-O文件讀取出來的

hook登錄按鈕 - 動態(tài)調(diào)試獲取

  • 1、- 在CJLHook的inject類中hook登錄按鈕的方法
@implementation inject

+ (void)load{
//    改變微信的注冊
    Method oldMethod = class_getInstanceMethod(objc_getClass("WCAccountMainLoginViewController"), @selector(onNext));
    Method newMethod = class_getInstanceMethod(self, @selector(my_onNext));
    
    method_exchangeImplementations(oldMethod, newMethod);
    
}

- (void)my_onNext{
    
}

@end
  • 2、通過class_dump工具會dump出OC中類列表(包括成員變量)、方法列表,具體操作如下:

    • 1)將class-dump、wechat可執(zhí)行文件拷貝至同一個文件夾


      動態(tài)調(diào)試獲取-01
    • 2)在終端執(zhí)行以下命令:./class-dump -H WeChat -o ./headers/,其本質(zhì)是通過讀取Mach-O文件獲取
      動態(tài)調(diào)試獲取-02
  • 3、通過sublime Text打開headers文件夾,在其中查找WCAccountMainLoginViewController類,找到密碼的成員變量_textFieldUserPwdItem

    動態(tài)調(diào)試獲取-03

    • 查找類文件順序為:WCAccountTextFieldItem -> WCBaseTextFieldItem -> WCUITextField
      動態(tài)調(diào)試獲取-04
  • 4、通過獲取的成員變量,利用lldb動態(tài)調(diào)試獲取密碼


    動態(tài)調(diào)試獲取-05
    • po [(WCAccountMainLoginViewController *)0x10a84e800 valueForKey:@"_textFieldUserPwdItem"]

    • po [(WCAccountTextFieldItem *)0x281768360 valueForKey:@"m_textField"]

    • po ((WCUITextField *)0x10a1566e0).text

hook登錄按鈕 - hook代碼注入方式獲取

  • 1、修改 my_onNext 方法
@implementation inject

+ (void)load{
//    改變微信的注冊
    Method oldMethod = class_getInstanceMethod(objc_getClass("WCAccountMainLoginViewController"), @selector(onNext));
    Method newMethod = class_getInstanceMethod(self, @selector(my_onNext));
    
    method_exchangeImplementations(oldMethod, newMethod);
    
}

- (void)my_onNext{
    UITextField *pwd = (UITextField *)[[self valueForKey:@"_textFieldUserPwdItem"] valueForKey:@"m_textField"];
    NSLog(@"密碼是:%@", pwd.text);
}

@end

運(yùn)行結(jié)果如下所示


hook代碼注入方式獲取-01
  • 2、然后此時需要在my_onNext中調(diào)用原來的方法,走回原來的登錄流程,此時的my_onNext方法修改如下
- (void)my_onNext{
    UITextField *pwd = (UITextField *)[[self valueForKey:@"_textFieldUserPwdItem"] valueForKey:@"m_textField"];
    NSLog(@"密碼是:%@", pwd.text);
    
    //調(diào)回原來的方法objc_msgSend(WCAccountMainLoginViewController,onNext的IMP)
    [self my_onNext];
}
  • 3、在[self my_onNext];處加斷點,驗證此時的my_onNext中的self、_cmd

    hook代碼注入方式獲取-02

  • 4、然后繼續(xù)執(zhí)行,發(fā)現(xiàn)程序會崩潰,即在執(zhí)行objc_msgSend后會直接崩潰,原因是找不到my_onNext

    hook代碼注入方式獲取-03

解決崩潰的方案:添加一個method

  • 修改inject中的代碼,此時是通過class_addMethodWCAccountMainLoginViewController中新增一個方法,然后在和原來的onNext交換新增后的方法
@implementation inject

+ (void)load{
    //原始的method
    Method oldMethod = class_getInstanceMethod(objc_getClass("WCAccountMainLoginViewController"), @selector(onNext));
    //給WCAccountMainLoginViewController添加新方法
    class_addMethod(objc_getClass("WCAccountMainLoginViewController"), @selector(new_onNext), new_onNext, "v@:");
    //獲取添加后的方法
    Method newMethod = class_getInstanceMethod(objc_getClass("WCAccountMainLoginViewController"), @selector(new_onNext));
    //交換
    method_exchangeImplementations(oldMethod, newMethod);
    
}

//新的IMP
void new_onNext(id self, SEL _cmd){
    UITextField *pwd = (UITextField *)[[self valueForKey:@"_textFieldUserPwdItem"] valueForKey:@"m_textField"];
    NSLog(@"密碼是:%@", pwd.text);
    
    //調(diào)回原來的方法
objc_msgSend(WCAccountMainLoginViewController,onNext的IMP)
    [self performSelector:@selector(new_onNext)];
}
@end

重新運(yùn)行后查看可以走到原來的登錄流程,且同時可以獲取用戶密碼

代碼注入優(yōu)化:通過替換的方式

但是上面的代碼看上不并不簡潔,所以我們來對其一些優(yōu)化,采用class_replaceMethod函數(shù)進(jìn)行替換原來的onNext方法,修改后的代碼如下

+ (void)load{
    //原始的method
    Method oldMethod = class_getInstanceMethod(objc_getClass("WCAccountMainLoginViewController"), @selector(onNext));
    //替換
    old_onNext = class_replaceMethod(objc_getClass("WCAccountMainLoginViewController"), @selector(onNext), new_onNext, "v@:");
    
}

//原來的IMP
IMP (*old_onNext)(id self, SEL _cmd);

//新的IMP
void new_onNext(id self, SEL _cmd){
    UITextField *pwd = (UITextField *)[[self valueForKey:@"_textFieldUserPwdItem"] valueForKey:@"m_textField"];
    NSLog(@"密碼是:%@", pwd.text);
    
    //調(diào)回原來的方法
objc_msgSend(WCAccountMainLoginViewController,onNext的IMP)
    old_onNext(self, _cmd);
}

更好的方式

  • 通過method_getImplementation、method_setImplementation方法進(jìn)行覆蓋原來onNext方法的IMP
+ (void)load{
    //原始的method
    old_onNext = method_getImplementation(class_getInstanceMethod(objc_getClass("WCAccountMainLoginViewController"), @selector(onNext)));
    //通過set覆蓋原始的IMP
    method_setImplementation(class_getInstanceMethod(objc_getClass("WCAccountMainLoginViewController"), @selector(onNext)), new_onNext);
    
}

//原來的IMP
IMP (*old_onNext)(id self, SEL _cmd);

//新的IMP
void new_onNext(id self, SEL _cmd){
    UITextField *pwd = (UITextField *)[[self valueForKey:@"_textFieldUserPwdItem"] valueForKey:@"m_textField"];
    NSLog(@"密碼是:%@", pwd.text);
    
    //調(diào)回原來的方法objc_msgSend(WCAccountMainLoginViewController,onNext的IMP)
    old_onNext(self, _cmd);
}

總結(jié)

  • Method Swizzing(即方法交換):是利用OC的Runtime特性,動態(tài)改變SEL(方法編號)和IMP(方法實現(xiàn))的對應(yīng)關(guān)系,達(dá)到OC方法調(diào)用流程改變的目的

  • 多種hook方式:

    • 1、 class_addMethod方式: 利用AddMethod方式,讓原始方法可以被調(diào)用,不至于因為找不到SEL而崩潰

    • 2、class_replaceMethod方式:利用class_replaceMethod,直接給原始的方法替換IMP

    • 3、method_setImplementation方式:利用method_setImplementation,直接重新賦值原始的新的IMP

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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