Logos語法學習
它是在Theos開發中允許hook程式碼非常的簡單明瞭,它能夠替換、修改、新增方法或者類。
塊等級
這種等級必須以%end結尾,並且也不存在函式
%group
%group Groupname 複製程式碼
如果為了支援適配不同系統的程式碼,可以通過以下方式
%group iOS8 %hook IOS8_SPECIFIC_CLASS // your code here %end // end hook %end // end group ios8 %group iOS9 %hook IOS9_SPECIFIC_CLASS // your code here %end // end hook %end // end group ios9 %ctor { if (kCFCoreFoundationVersionNumber >1200) { %init(iOS9); } else { %init(iOS8); } } 複製程式碼
%hook
所有的hook都有隱藏了一個“_ungrouped”的隱式分組;它用來修飾 類名
%hook Classname 複製程式碼
hook之間的程式碼是SBApplicationController具體的函式
%hook SBApplicationController -(void)uninstallApplication:(SBApplication *)application { NSLog(@"Hey, we're hooking uninstallApplication:!"); %orig; // Call the original implementation of this method return; } %end 複製程式碼
%new
給hook的類新增新方法;signature是OC型別的新方法名,必須在%hook塊之間
%new// 修飾新的方法 %new(signature)// 修飾新的方法signature 複製程式碼
%hook ClassName %new - (id)someValue { return objc_getAssociatedObject(self, @selector(someValue)); } %end 複製程式碼
%subclass
通過執行時建立的子類,ivars不支援;使用%new修飾新增的方法,如果需要訪問新類,必須通過 %c
操作符獲取,可以在%group塊中
%subclass MyObject : NSObject - (id)init { self = %orig; [self setSomeValue:@"value"]; return self; } //the following two new methods act as `@property (nonatomic, retain) id someValue;` %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 %ctor { MyObject *myObject = [[%c(MyObject) alloc] init]; NSLog(@"myObject: %@", [myObject someValue]); } 複製程式碼
%property
%property (nonatomic|assign|retain|copy|weak|strong|getter|setter) Type name; 複製程式碼
新增新的屬性在類中,必須在 %hook
修飾或者 %subclass
修飾的內部。
%end
%end 複製程式碼
用來修飾 group/hook/subclass
的塊結束
頂部等級
這部分的指令不再 group/hook/subclass
的塊內部。
%config
%config(Key=Value); 複製程式碼

%hookf
%hookf(rtype, symbolName, args...) { … } 複製程式碼
// Given the function prototype FILE *fopen(const char *path, const char *mode); // The hook is thus made %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; // Call the original implementation of this function } 複製程式碼
CFBooleanRef (*orig_MGGetBoolAnswer)(CFStringRef); CFBooleanRef fixed_MGGetBoolAnswer(CFStringRef string) { if (CFEqual(string, CFSTR("StarkCapability"))) { return kCFBooleanTrue; } return orig_MGGetBoolAnswer(string); } %hookf(CFBooleanRef, "_MGGetBoolAnswer", CFStringRef string) { if (CFEqual(string, CFSTR("StarkCapability"))) { return kCFBooleanTrue; } return %orig; } 複製程式碼
%ctor
%ctor { … } 複製程式碼
匿名構造方法
%dtor
%dtor { … } 複製程式碼
匿名銷燬構造方法
函式等級
只在函式block中
%init
%init; %init([<class>=<expr>, …]); %init(Group[, [+|-]<class>=<expr>, …]); 複製程式碼
初始化分組或預設分組
%hook SomeClass -(id)init { return %orig; } %end %ctor { %init(SomeClass=objc_getClass("class with spaces in the name")); } 複製程式碼
%c
%c([+|-]Class) 複製程式碼
等價於例項物件,或者類名
%orig
%orig %orig(arg1, …) 複製程式碼
呼叫原始方法,不能夠在%new裡面,
%log
%log; %log([(<type>)<expr>, …]); 複製程式碼
列印日誌