普通视图
iOS小技能:给debugserver添加task_for_pid权限,以便调试从AppStore中获取的App。
iOS小技能:__attribute__的应用
iOS小技能:消息发送的步骤(利用类型编码加快消息分发)
iOS小技能:动态地给类添加新的方法、实例变量、属性。
iOS动态库的注入原理
iOS逆向小技能:Cydia Substrate的组成部分、编写Tweak的步骤
iOS设备日志查看工具:syslog、socat
iOS逆向小技能:Theos的安装
「这是我参与2022首次更文挑战的第7天,活动详情查看:2022首次更文挑战」。
前言
Theos是越狱开发工具包。logos语法简单。,它给我们准备好了一些代码模板、预置一些基本的Makefile脚本,这样我们开发一个tweak就会变得方便的多,
(整合在Xcode的iOSopendev )由于逆向工程很多东西无法自动化,因此推荐Theos
I 安装Thoes
1.1 安装Xcode
安装Xcode,以及command line tools
1.2 下载Thoes
下载Thoes:https://github.com/theos/theos
查看环境变量: 终端中输入命令env
建立环境变量:
export THEOS=/opt/theos
#在当前终端中起作用了,关闭终端后又得重新设置。
为了避免每次都建立这个环境变量,建立一个永久的环境变量 : 编辑~/.profile文件,在其中添加export THEOS=/opt/theos/,这个环境变量就是永久的了.
记得source
source .profile
或者使用.bash_profile
open -e ~/.bash_profile
export THEOS=/opt/theos
source .bash_profile
devzkndeMacBook-Pro:opt devzkn$ git clone --recursive https://github.com/theos/theos.git $THEOS
fatal: could not create work tree dir '/opt/theos': Permission denied
devzkndeMacBook-Pro:opt devzkn$ sudo git clone --recursive https://github.com/theos/theos.git $THEOS
Password:
Cloning into '/opt/theos'...
新版的theos 已经自带CydiaSubstrate.framework(基本上,tweak都依赖于一个名叫cydia Substrate (以前名字也叫mobile Substrate)的动态库,Mobile Substrate是Cydia的作者Jay Freeman (@saurik)的作品,也叫Cydia Substrate,它的主要功能是hook某个App,修改代码比如替换其中方法的实现,Cydia上的tweak都是基于Mobile Substrate实现的.)
devzkndeMacBook-Pro:lib devzkn$ pwd
/opt/theos/vendor/lib
devzkndeMacBook-Pro:lib devzkn$ ls -lrt
total 72
drwxr-xr-x 3 root wheel 102 Aug 10 15:19 libswift
lrwxr-xr-x 1 root wheel 43 Aug 10 15:19 libsubstrate.tbd -> CydiaSubstrate.framework/CydiaSubstrate.tbd
-rw-r--r-- 1 root wheel 635 Aug 10 15:19 librocketbootstrap.tbd
-rw-r--r-- 1 root wheel 392 Aug 10 15:19 libprefs.tbd
-rw-r--r-- 1 root wheel 588 Aug 10 15:19 libflipswitch.tbd
-rw-r--r-- 1 root wheel 1111 Aug 10 15:19 libapplist.tbd
-rw-r--r-- 1 root wheel 5646 Aug 10 15:19 libactivator.tbd
drwxr-xr-x 4 root wheel 136 Aug 10 15:19 TechSupport.framework
-rw-r--r-- 1 root wheel 432 Aug 10 15:19 README.md
drwxr-xr-x 5 root wheel 170 Aug 10 15:19 Opener.framework
-rw-r--r-- 1 root wheel 3342 Aug 10 15:19 LICENSE.md
drwxr-xr-x 5 root wheel 170 Aug 10 15:19 CydiaSubstrate.framework
drwxr-xr-x 4 root wheel 136 Aug 10 15:19 Cycript.framework
drwxr-xr-x 5 root wheel 170 Aug 10 15:19 CepheiPrefs.framework
drwxr-xr-x 4 root wheel 136 Aug 10 15:19 Cephei.framework
1.3 配置ldid
用来专门签名iOS可执行文件的工具,用以在越狱iOS中取代Xcode自带的codesign. 安装这个ldid,推荐的方式是采用brew来安装--
brew install ldid
1.4 dpkg-deb
deb是越狱开发包的标准格式,dpkg-deb是个用于操作deb文件的工具,有了这个工具,Theos才能正确的把工程打包成deb文件.
brew install dpkg
see also
更多内容请关注 #小程序:iOS逆向,只为你呈现有价值的信息,专注于移动端技术研究领域;更多服务和咨询请关注#公众号:iOS逆向。
iOS逆向小技能:使用substrate及runtime进行hook(定时检测app是否开启)
「这是我参与2022首次更文挑战的第1天,活动详情查看:2022首次更文挑战」。
前言
- 利用runtime API进行hook
method_exchangeImplementations
可以直接是一个函数地址,不管是OC还是C所有的OC函数都是IMP类型。IMP就是个c函数指针。
- 使用
substrate.h
进行hook - 定时检测app是否处于前台运行状态
I 利用runtime API进行hook
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface KNHook : NSObject
/**
替换对象方法
@param originalClass 原始类
@param originalSelector 原始类的方法
@param swizzledClass 替换类
@param swizzledSelector 替换类的方法
*/
void kn_hookMethod(Class originalClass, SEL originalSelector, Class swizzledClass, SEL swizzledSelector);
/**
替换类方法
@param originalClass 原始类
@param originalSelector 原始类的类方法
@param swizzledClass 替换类
@param swizzledSelector 替换类的类方法
*/
void kn_hookClassMethod(Class originalClass, SEL originalSelector, Class swizzledClass, SEL swizzledSelector);
1.1 替换对象方法
/**
替换对象方法
@param originalClass 原始类
@param originalSelector 原始类的方法
@param swizzledClass 替换类
@param swizzledSelector 替换类的方法
*/
void kn_hookMethod(Class originalClass, SEL originalSelector, Class swizzledClass, SEL swizzledSelector){
Method originalMethod = class_getInstanceMethod(originalClass, originalSelector);
Method swizzledMethod = class_getInstanceMethod(swizzledClass, swizzledSelector);
if(originalMethod && swizzledMethod) {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
1.2 替换类方法
/**
替换类方法
@param originalClass 原始类
@param originalSelector 原始类的类方法
@param swizzledClass 替换类
@param swizzledSelector 替换类的类方法
*/
void kn_hookClassMethod(Class originalClass, SEL originalSelector, Class swizzledClass, SEL swizzledSelector){
Method originalMethod = class_getClassMethod(originalClass, originalSelector);
Method swizzledMethod = class_getClassMethod(swizzledClass, swizzledSelector);
if(originalMethod && swizzledMethod) {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
1.3 runtime的使用例子
- hook OnSyncBatchAddMsgs
static void __attribute__((constructor)) initialize(void) {
MSHookMessageEx(objc_getClass("MessageService"), @selector(OnSyncBatchAddMsgs:isFirstSync:), (IMP)&new_MessageService_OnSyncBatchAddMsgs_isFirstSync, (IMP*)&origin_new_MessageService_OnSyncBatchAddMsgs_isFirstSync);
[NSObject hookWeChat];
}
- hook CUtility
#import "NSObject+WeChatHook.h"
@implementation NSObject (WeChatHook)
+ (void)hookWeChat {
kn_hookClassMethod(objc_getClass("CUtility"), @selector(HasWechatInstance), [self class], @selector(hook_HasWechatInstance));
}
#pragma mark - hook 方法
/**
hook 是否已启动
*/
+ (BOOL)hook_HasWechatInstance {
NSLog(@"kn hook_HasWechatInstance");
return NO;
}
@end
1.4 定时检测app是否开启
应用场景:长期保证app一只处于运行中
NSTimer *timer ;
%hook SpringBoard
//applicationDidFinishLaunching
-(void)applicationDidFinishLaunching: (id)application
{
%orig;
timer = [NSTimer scheduledTimerWithTimeInterval:60*2 target:self selector:@selector(checkHeart) userInfo:nil repeats:YES];
}
%new
- (void)checkHeart
{
//定时检测微信是否开启
[[UIApplication sharedApplication] launchApplicationWithIdentifier:@"com.tencent.xin" suspended:0];
}
%end
//qutolock
%hook SBLockScreenViewController
-(void)activate{
%orig;
[[%c(SBLockScreenManager) sharedInstance] unlockUIFromSource:0 withOptions:nil];
}
%end
II 使用substrate.h
进行hook
static void (*origin_new_MessageService_OnSyncBatchAddMsgs_isFirstSync)(MessageService*,SEL,NSArray *,BOOL);
static void new_MessageService_OnSyncBatchAddMsgs_isFirstSync(MessageService* self,SEL _cmd,NSArray * msgs,BOOL isFirstSync){
origin_new_MessageService_OnSyncBatchAddMsgs_isFirstSync(self,_cmd,msgs,isFirstSync);
}
see also
更多内容请关注 #小程序:iOS逆向,只为你呈现有价值的信息,专注于移动端技术研究领域;更多服务和咨询请关注#公众号:iOS逆向。