1. 程式人生 > >iOS逆向之Logos語法

iOS逆向之Logos語法

指定 以及 如果 priority prior bug 多個 upn spring

Logos語法
http://iphonedevwiki.net/index.php/Logos

  • 新建Monkey工程時,MonkeyDev已經將libsubstrate.dylib庫和RevealServer.framework庫註入進去了,有了libsubstrate.dylib庫就能寫Logos語法

  • Logos語法:

Logos語法 功能解釋 事例
%hook 需要hook哪個類 %hook Classname
%end 代碼塊結束標記
%group 分組 %group Groupname
%new 添加新方法 %new(signature)
%ctor 構造函數 %ctor { … }
%dtor 析構函數 %dtor { … }
%log 輸出打印 %log; %log([(
%orig 保持原有方法 %orig;%orig(arg1, …);
%c %c([+/-]Class);

1.%hook %end

指定需要hook的class,必須以%end結尾。

// hook SpringBoard類裏面的_menuButtonDown函數,先打印一句話,再之子那個函數原始的操作
%hook SpringBorad
- (void)_menuButtonDown:(id)down
{
    NSLog(@"111111");
   %orig; // 調用原始的_menuButtonDown函數
}
%end

2.%group

該指令用於將%hook分組,便於代碼管理及按條件初始化分組,必須以%end結尾。
一個%group可以包含多個%hook,所有不屬於某個自定義group的%hook會被隱式歸類到%group_ungrouped中。

/*
在%group iOS7Hook中鉤住iOS7Class的iOS7Method,在%group iOS8Class中鉤住iOS8Method函數,然後在%group _ungroup中鉤住SpringBoard類的powerDown函數.
*/
%group iOS7Hook
%hook iOS7Class
- (id)ios7Method
{
    id result = %orig;
    NSLog(@"這個方法只有iOS7適用");
    return result;
}
%end
%end // iOS7Method

%group iOS8Hook
%hook iOS8Class
- (id)ios8Method
{
    id result = %orig;
    NSLog(@"這個方法只有iOS7適用");
    return result;
}
%end
%end // iOS8Method

%hook SpringBoard
- (void)powerDown
{
     %orig;
}
%end

3.%new

在%hook內部使用,給一個現有class添加新函數,功能與class_addMethod相同.
註:
Objective-C的category與class_addMethod的區別:
前者是靜態的而後者是動態的。使用%new添加,而不需要向.h文件中添加函數聲明,如果使用category,可能與遇到這樣那樣的錯誤.

%hook SpringBoard
%new
- (void)addNewMethod
{
    NSLog(@"動態添加一個方法到SpringBoard");
}
%end

4.%ctor

tweak的constructor,完成初始化工作;如果不顯示定義,Theos會自動生成一個%ctor,並在其中調用%init(_ungrouped)。%ctor一般可以用來初始化%group,以及進行MSHookFunction等操作,如下:

#ifndef KCFCoreFoundationVersionNumber_iOS_8_0
#define KCFCoreFoundationVersionNumber_iOS_8_0      1140.10
#endif

%ctor
{
    %init;

    if (KCFCoreFoundationVersionNumber >= KCFCoreFoundationVersionNumber_iOS_7_0 && KCFCoreFoundationVersionNumber > KCFCoreFoundationVersionNumber_iOS_8_0)
    %init(iOS7Hook);
 if (KCFCoreFoundationVersionNumber >= KCFCoreFoundationVersionNumber_iOS_8_0)
    %init(iOS8Hook);
MSHookFunction((void *)&AudioServicesPlaySystemSound,(void *)&replaced_AudioServerPlaySystemSound,(void **)&orginal_AudioServicesPlaySystemSound);
}

5.%dtor

Generate an anonymous deconstructor (of default priority).

%dtor { … }

6.%log

該指令在%hook內部使用,將函數的類名、參數等信息寫入syslog,可以%log([(),…..])的格式追加其他打印信息。

%hook SpringBorad
- (void)_menuButtonDown:(id)down
{
    %log((NSString *)@"iosre",(NSString *)@"Debug");
    %orig; // 調用原始的_menuButtonDown方法
}
%end

6.%orig

該指令在%hook內部使用,執行被hook的函數的原始代碼;也可以用%orig更改原始函數的參數。

%hook SpringBorad
- (void)setCustomSubtitleText:(id)arg1 withColor:   (id)arg2
{
    %orig(@"change arg2",arg2);// 將arg2的參數修 改為"change arg2"
}
%end

7.%init

該指令用於初始化某個%group,必須在%hook或%ctor內調用;如果帶參數,則初始化指定的group,如果不帶參數,則初始化_ungrouped.
註: 切記,只有調用了%init,對應的%group才能起作用!

8.%c

該指令的作用等同於objc_getClass或NSClassFromString,即動態獲取一個類的定義,在%hook或%ctor內使用 。

iOS逆向之Logos語法