iOS逆向小技能:Cydia Substrate的组成部分、编写Tweak的步骤

6,402 阅读2分钟

「这是我参与2022首次更文挑战的第4天,活动详情查看:2022首次更文挑战」。

前言

Cydia Substrate(以前叫做MobileSubstrate)是一个框架,允许第三方的开发者在系统的方法里打一些运行时补丁,扩展一些方法。

Cydia Substrate由3部分组成:

  • MobileHooker
  • MobileLoader
  • safe mode

I Cydia Substrate]

1.1 MobileHooker

MobileHooker用来替换系统函数,这个过程也叫Hooking。有如下的API可以使用:

IMP MSHookMessage(Class class, SEL selector, IMP replacement, const char* prefix); // prefix should be NULL.
void MSHookMessageEx(Class class, SEL selector, IMP replacement, IMP *result);
void MSHookFunction(void* function, void* replacement, void** p_original);

MSHookMessageEx用来替换Objective-C的函数,MSHookFunction用来替换C/C++函数

1.2 MobileLoader

MobileLoader loads 3rd-party patching code into the running application. MobileLoader will first load itself into the run application using DYLD_INSERT_LIBRARIES environment variable. Then it looks for all dynamic libraries in the directory /Library/MobileSubstrate/DynamicLibraries/, and dlopen them.

控制是否加载到目标程序,是通过一个plist文件来控制的。如果需要被加载的动态库的名称叫做foo.dylib,那么这个plist文件就叫做foo.plist,这个里面有一个字段叫做filter,里面写明需要hook进的目标程序的bundle id。 比如,如果只想要foo.dylib加载进入SpringBoard,那么对应的plist文件中的filter就应该这样写:

Filter = {
  Bundles = (com.apple.springboard);
};

1.3 Safe mode

When a extension crashed the SpringBoard, MobileLoader will catch that and put the device into safe mode. In safe mode all 3rd-party extensions will be disabled.

The following signals will invoke safe mode:


SIGABRT
SIGILL
SIGBUS
SIGSEGV
SIGSYS

II 编写Tweak的步骤

  • 确定目标:在这个App上编写Tweak实现的特定功能,比如拦截某个具体的应用的特定API调用,获得关键信息。

  • 导出头文件:确定目标之后,就可以利用Clutch先破解App,然后利用class-dump-z导出头文件,找到你感兴趣的类,对它进行分析。

  • 获得类的方法:有时候,头文件没有所有方法调用的信息,这个时候你可以利用cycript,使用之前介绍的trick,打印出所需的方法信息。

  • 编写Tweak:这一步你应该拿到需要Hook的类以及对应的方法,编写并安装与测试。

III SpringBoard 相关的API

  • powerDown
+ (void) powerDown {
    id SpringBoard = [UIApplication sharedApplication];//#"<SpringBoard: 0x173d8800>"
    [SpringBoard powerDown];
}

  • relaunchSpringBoard
 @interface SpringBoard : UIApplication
 \t_uiController (SBUIController*): <SBUIController: 0x1809c510>
 - (void)relaunchSpringBoard;  [#0x1617ca00 relaunchSpringBoard]
 - (void)_relaunchSpringBoardNow;
 - (void)powerDown;
 - (void)_powerDownNow;
 - (void)reboot;
 - (void)_rebootNow;
 @end

  • 自动锁屏
    [UIApplication sharedApplication].idleTimerDisabled=YES;//不自动锁屏,放在-(void)viewWillAppear:(BOOL)animated里面的时候,防止失效
  
    [UIApplication sharedApplication].idleTimerDisabled=NO;//自动锁屏

see also

更多内容请关注 #小程序:iOS逆向,只为你呈现有价值的信息,专注于移动端技术研究领域;更多服务和咨询请关注#公众号:iOS逆向。