一、概述
Logos
語法其實是CydiaSubstruct
框架提供的一組宏定義怀估。便于開發(fā)者使用宏進(jìn)行HOOK
操作骡显。語法簡單,功能強(qiáng)大且穩(wěn)定贬媒,它是跨平臺的。[logos] (http://iphonedevwiki.net/index.php/Logos)
二肘习、logos語法
logos
語法分為3
類际乘。
2.1、Block level
這一類型的指令會開辟一個代碼塊漂佩,以%end
結(jié)束脖含。
%group
用來將代碼分組。開發(fā)中hook
代碼會很多投蝉,這樣方便管理Logos
代碼养葵。所有的group
都必須初始化,否則編譯報錯瘩缆。
#import <UIKit/UIKit.h>
%group group1
%hook RichTextView
- (_Bool)setPrefixContent:(id)arg1 TargetContent:(NSString *)arg2 TargetParserString:(id)arg3 SuffixContent:(id)arg4 {
//hook后要處理的方式1
return %orig;
}
%end
%end
%group group2
%hook RichTextView
- (_Bool)setPrefixContent:(id)arg1 TargetContent:(NSString *)arg2 TargetParserString:(id)arg3 SuffixContent:(id)arg4 {
//hook后要處理的方式2
return %orig;
}
%end
%end
%group group3
%hook RichTextView
- (_Bool)setPrefixContent:(id)arg1 TargetContent:(NSString *)arg2 TargetParserString:(id)arg3 SuffixContent:(id)arg4 {
//hook后要處理的方式3
return %orig;
}
%end
%end
//使用group要配合ctor
%ctor {
//[[UIDevice currentDevice] systemVersion].doubleValue 可以用來判斷版本或其它邏輯港柜。
if ([[UIDevice currentDevice] systemVersion].doubleValue >= 11.0) {
//這里group3會覆蓋group1,不會執(zhí)行g(shù)roup1邏輯咳榜。
%init(group1)%init(group3);
} else {
%init(group2);
}
}
-
group
初始化在%ctor
中夏醉,需要%init
初始化。 - 所有
group
必須初始化涌韩,否則編譯報錯畔柔。 - 在一個邏輯中同時初始化多個
group
,后面的會覆蓋前面的臣樱。 - 在不添加
group
的情況下靶擦,默認(rèn)有個_ungrouped
組腮考,會自動初始化。
Begin a hook group with the name Groupname. Groups cannot be inside another
[%group](https://iphonedev.wiki/index.php/Logos#.25group "Logos")
block. All ungrouped hooks are in the implicit "_ungrouped" group. The _ungrouped group is initialized for you if there are no other groups. You can use the%init
directive to initialize it manually. Other groups must be initialized with the%init(Groupname)
directive
%hook
HOOK
某個類里面的某個方法玄捕。
%hook RichTextView
- (_Bool)setPrefixContent:(id)arg1 TargetContent:(NSString *)arg2 TargetParserString:(id)arg3 SuffixContent:(id)arg4 {
//hook后要處理的方式1
return %orig;
}
%end
%hook
后面需要跟需要hook
的類名踩蔚。
%new
為某個類添加新方法,在%hook
和 %end
中使用枚粘。
%hook RichTextView
%new
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
}
%end
%subclass
%subclass Classname: Superclass <Protocol list>
運(yùn)行時創(chuàng)建子類馅闽,只能包含方法或者關(guān)聯(lián)屬性,不能包含屬性馍迄「R玻可以通過%c
創(chuàng)建類實例。
#import <UIKit/UIKit.h>
@interface MyObject
- (void)setSomeValue:(id)value;
@end
%subclass MyObject : NSObject
- (id)init {
self = %orig;
[self setSomeValue:@"value"];
return self;
}
%new
- (id)someValue {
return objc_getAssociatedObject(self, @selector(someValue));
}
%new
- (void)setSomeValue:(id)value {
objc_setAssociatedObject(self, @selector(someValue), value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
%end
%property
%property (nonatomic|assign|retain|copy|weak|strong|getter|setter) Type name;
為subclass
或者hook
的類添加屬性攀圈。必須在 %subclass
或%hook
中暴凑。
%property(nonatomic,assign) NSInteger age;
%end
與其它命令配對出現(xiàn)。
2.2赘来、Top level
TopLevel
指令不放在BlockLevel
中现喳。
%config
%config(Key=Value);
為logos
設(shè)置標(biāo)記。
Configuration Flags
key | values | notes |
---|---|---|
generator | MobileSubstrate | 生成的代碼使用MobileSubstrate hook
|
generator | internal | 生成的代碼只使用OC runtime 方法hook
|
warnings | none | 忽略所有警告 |
warnings | default | 沒有致命的警告 |
warnings | error | 使所有警告報錯 |
dump | yaml | 以YAML 格式轉(zhuǎn)儲內(nèi)部解析樹 |
%config(generator=internal);
%config(warnings=error);
%config(dump=yaml);
%hookf
hook
函數(shù)犬辰,類似fishhook
拿穴。
語法
%hookf(rtype, symbolName, args...) { … }
-
rtype
:返回值。 -
symbolName
:原函數(shù)地址忧风。 -
args...
:參數(shù)。
示例
FILE *fopen(const char *path, const char *mode);
%hookf(FILE *, fopen, const char *path, const char *mode) {
NSLog(@"Hey, we're hooking fopen to deny relative paths!");
if (path[0] != '/') {
return NULL;
}
return %orig;
}
%ctor
構(gòu)造函數(shù)球凰,用于確定加載那個組狮腿。和%init
結(jié)合用。
%dtor
析構(gòu)呕诉,做一些收尾工作缘厢。比如應(yīng)用掛起的時候。
2.3甩挫、Function level
這一塊的指令就放在方法中
%init
用來初始化某個組贴硫。
%class
%class Class;
已經(jīng)廢棄了,不建議使用伊者。%class
%c
類似getClass
函數(shù)英遭,獲得一個類對象。一般用于調(diào)用類方法亦渗。
//只是為了聲明編譯通過
@interface MainViewController
+ (void)HP_classMethod;
@end
%hook MainViewController
%new
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//方式一
// [self.class HP_classMethod];
//方式二
// [NSClassFromString(@"MainViewController") HP_classMethod];
//方式三
[%c(MainViewController) HP_classMethod];
}
%new
+ (void)HP_classMethod {
NSLog(@"HP_classMethod");
}
%end
-
%c
中沒有引號挖诸。
%orig
保持原有的方法實現(xiàn),如果原來的方法有返回值和參數(shù)法精,那么可以傳遞參數(shù)和接收返回值多律。
%hook RichTextView
- (_Bool)setPrefixContent:(id)arg1 TargetContent:(NSString *)arg2 TargetParserString:(id)arg3 SuffixContent:(id)arg4 {
//傳遞參數(shù)&接收返回值痴突。
BOOL result1 = %orig(arg1,arg2,arg3,arg4);
BOOL result2 = %orig;
return %orig;
}
%end
-
%orig
可以接收返回值。 - 可以傳遞參數(shù)狼荞,不傳就是傳遞該方法的默認(rèn)參數(shù)辽装。
%log
能夠輸出日志,輸出方法調(diào)用的詳細(xì)信息 相味。
%hook RichTextView
- (_Bool)setPrefixContent:(id)arg1 TargetContent:(NSString *)arg2 TargetParserString:(id)arg3 SuffixContent:(id)arg4 {
%log;
return %orig;
}
%end
輸出:
WeChat[11309:6708938] -[<RichTextView: 0x15c4c9720> setPrefixContent:(null) TargetContent:錢已經(jīng)借給你了拾积。 TargetParserString:<contentMD5>0399062cd62208dad884224feae2aa30</contentMD5><fontsize>20.287109</fontsize><fwidth>240.000000</fwidth><parser><type>1</type><range>{0, 8}</range><info><![CDATA[<style><range>{0, 8}</range><rect>{{0, 0}, {135, 21}}</rect></style>]]></info></parser> SuffixContent:(null)]
能夠輸出詳細(xì)的日志信息,包含類攻走、方法殷勘、參數(shù)、以及控件信息等詳細(xì)信息昔搂。
總結(jié)
-
logos
語法其實是CydiaSubstruct
框架提供的一組宏定義玲销。 - 語法
-
%hook
,%end
勾住某個類摘符,在一個代碼塊中直接寫需要勾住的方法贤斜。 -
%group
,%end
用于分組逛裤。- 每一組都需要
%ctor()
函數(shù)構(gòu)造瘩绒。 - 通過
%init(組名稱)
進(jìn)行初始化。
- 每一組都需要
-
%log
輸出方法的詳細(xì)信息(調(diào)用者带族、方法名锁荔、方法參數(shù)) -
%orig
調(diào)用原始方法◎觯可以傳遞參數(shù)阳堕,接收返回值。 -
%c
類似getClass
函數(shù)择克,獲取一個類對象恬总。 -
%new
添加某個方法。
-
-
.xm
文件代表該文件支持OC
肚邢、C/C++
語法壹堰。 - 編譯該文件時需要導(dǎo)入頭文件以便編譯通過。
.xm
文件不參與代碼的執(zhí)行骡湖,編譯后生成的.mm
文件參與代碼的執(zhí)行贱纠。