iOS逆向— Logos语法解析

Logos 简介(自己翻译的中文,如有不妥之处轻喷!!)

Logos is a component of the Theos development suite that allows
method hooking code to be written easily and clearly,
using a set of special preprocessor directives.

Logos是Theos开发套件的一个组件,它允许使用一组特殊
的预处理器指令轻松而清晰地编写方法Hook代码。

常用Logos 关键字

Logos语法 功能注释 事例
%hook 指定需要hook的class,必须以%end结尾。 %hook Classname
%log 该指令在%hook内部使用,将函数的类名、参数等信息写入syslog,可以%log([(),…..])的格式追加其他打印信息。 %log; %log([(), …]);
%orig 在%hook内部使用,执行被hook的函数的原始代码;也可以用%orig更改原始函数的参数。 %orig
%group 用于将%hook分组,便于代码管理及按条件初始化分组,必须以%end结尾。 一个%group可以包含多个%hook,所有不属于某个自定义group的%hook会被隐式归类到%group_ungrouped中。 %group Groupname
%init 该指令用于初始化某个%group,必须在%hook或%ctor内调用;如果带参数,则初始化指定的group,如果不带参数,则初始化_ungrouped.注: 切记,只有调用了%ini,对应的%group才能起作用! %init([=, …]);
%cto 构造函数 tweak的constructor,完成初始化工作;如果不显示定义,Theos会自动生成一个%ctor,并在其中调用%init(_ungrouped) %ctor { … }
%dtor 析构函数 %dtor { … }
%c %c([+/-]Class);
%new 在%hook内部使用,给一个现有class添加新函数,功能与class_addMethod相同. %new(signature);

1、%hook %end

Open a hook block for the class named Classname.

Can be inside a %group block.

hook 住你需要 hook 的类(Classname),可以放在一个分组 (%group) 内

举个栗子

%hook SBApplicationController
-(void)uninstallApplication:(SBApplication *)application {
    NSLog(@"Hey, we're hooking uninstallApplication:!");
    %orig; // Call the original implementation of this method
    return;
}
%end

2、%group

Begin a hook group (for conditional initialization or code organization) with the name Groupname. All ungrouped hooks are in the implicit “_ungrouped” group.

Cannot be inside another %group block.

分组开始于一个 GroupName ,代码中没有添加到分组中的Hook 代码块全部会自动添加到 _ungrouped 分组中。

举个栗子

%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

3、%new

Add a new method to a hooked class or subclass by adding this directive above the method definition. signature is the Objective-C type encoding for the new method; if it is omitted, one will be generated.

Must be inside a %hook block.

使用%new 方法会添加一个新的方法,签名是新方法的OC 类型编码,代码必须放置于 Hook 代码块内

4、%subClass

Subclass block - the class is created at runtime and populated with methods. ivars are not yet supported (use associated objects). The %new specifier is needed for a method that doesn't exist in the superclass. To instantiate an object of the new class, you can use the %c operator.

Can be inside a %group block.


子类块 - 该类在运行时创建并填充方法。 ivars尚未支持(使用关联的对象)。父类中不存在的方法需要使用%new说明符。要实例化新类的一个对象,可以使用%c运算符。

举个栗子

%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

5、%orig

Call the original hooked method. Doesn't function in a %new'd method. Works in subclasses, strangely enough, because MobileSubstrate will generate a supercall closure at hook time. (If the hooked method doesn't exist in the class we're hooking, it creates a stub that just calls the superclass implementation.) args is passed to the original function - don't include self and _cmd, Logos does this for you.

调用hook 住的函数的原始方法,在 %new 中不起作用 可以放在 %group

大体先说这几个常用的,需要详细了解的直接进官网看就OK 了,英文水平有限翻译的不好轻喷!!

你可能感兴趣的:(iOS非越狱逆向)