Logos语法学习

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>, …]);
复制代码

打印日志

Logos文件扩展格式

Extension Process order
.x will be processed by Logos, then preprocessed and compiled as objective-c.
.xm will be processed by Logos, then preprocessed and compiled as objective-c++.
.xi will be preprocessed as objective-c first, then Logos will process the result, and then it will be compiled.
.xmi will be preprocessed as objective-c++ first, then Logos will process the result, and then it will be compiled.

xi或者xmi文件可以使用#define 宏

logify.pl

自动动帮你生成对应类的所有hook方法

$THEOS/bin/logify.pl ./SSDownloadAsset.h
复制代码