手动制作是其中一种方法(还有一种方法是使用iosopendev,在其它文章介绍)。
dump-class后发现xx应用有使用类xxViewController,这个类估计是继承于UIViewController,对
这个类的viewDidLoad作一个hook。
dylib代码:
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
@implementation UIViewController (hookxx)
- (void)new_viewDidLoad {
NSLog(@"-------------in new_viewDidLoad----------");
}
@end
// 加载时这个函数先被执行
static void __attribute__((constructor)) initialize(void)
{
NSLog(@"=======in initialize=================");
Class class = objc_getClass("xxViewController");
Method ori_method = class_getInstanceMethod(class, @selector(viewDidLoad));
Method new_method = class_getInstanceMethod(class, @selector(new_viewDidLoad));
method_exchangeImplementations(ori_method, new_method);
}
可以直接使用xcode来编译出来.o。
也可以用clang来编译出来.o。我的是iphone4手机。
clang -c hookxx.m -arch armv7 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.1.sdk
再使用ld来链接成dylib:
(/applications/xcode.app/contents/developer/toolchains/xcodedefault.xctoolchain/usr/bin)
ld -dylib -lsystem -lobjc -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.1.sdk/ -o hookxx.dylib hookxx.o -framework Foundation -framework UIKit -ios_version_min 6.0
scp hookxx.dylib root@192.168.1.100:/Library/MobileSubstrate/DynamicLibraries/
每个app启动都会载入/Library/MobileSubstrate/DynamicLibraries/ 目录下所有的动态库,除非动态库有用plist来指定只给哪些应用加载,否则所有应用都会加载。
可以写个简单例子,测试下这个dylib能否被加载:
#import <stdio.h>
int main() {
void *handle = (void*)dlopen("/Library/MobileSubstrate/DynamicLibraries/hookkg.dylib", 0x2);
if (handle) {
printf("load ok\n");
}
else {
printf("load fail\n");
}
return 0;
}
编译成执行文件:
clang -o testhookxx -arch armv7 testhookxx.m -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.1.sdk
拷贝到手机:
scp testhookxx root@192.168.1.100:testhookxx
ssh到手机执行:
./testhookxx
(会看到进入了dylib的initialize,然后输出load ok)
启动xx,观察log。
看应用输出的log,使用socat:
apt-get install socat
socat – UNIX-CONNECT:/var/run/lockdown/syslog.sock #连接系统日志
>watch
根据进程id来过滤(ps aux | grep xx):
* PID 951
根据bundleid来过滤:
* Facility com.xx.xx
会看以下的log输出,说明已经加载了hookxx动态库,并且已经执行到库里面的new_viewDidLoad函数了。
Dec 2 11:22:05 810 xx[974] <Warning>: =======in initialize=================
...
Dec 2 11:24:48 810 xx[974] <Warning>: -------------in new_viewDidLoad----------
dump-class后发现xx应用有使用类xxViewController,这个类估计是继承于UIViewController,对
这个类的viewDidLoad作一个hook。
dylib代码:
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
@implementation UIViewController (hookxx)
- (void)new_viewDidLoad {
NSLog(@"-------------in new_viewDidLoad----------");
}
@end
// 加载时这个函数先被执行
static void __attribute__((constructor)) initialize(void)
{
NSLog(@"=======in initialize=================");
Class class = objc_getClass("xxViewController");
Method ori_method = class_getInstanceMethod(class, @selector(viewDidLoad));
Method new_method = class_getInstanceMethod(class, @selector(new_viewDidLoad));
method_exchangeImplementations(ori_method, new_method);
}
可以直接使用xcode来编译出来.o。
也可以用clang来编译出来.o。我的是iphone4手机。
clang -c hookxx.m -arch armv7 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.1.sdk
再使用ld来链接成dylib:
(/applications/xcode.app/contents/developer/toolchains/xcodedefault.xctoolchain/usr/bin)
ld -dylib -lsystem -lobjc -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.1.sdk/ -o hookxx.dylib hookxx.o -framework Foundation -framework UIKit -ios_version_min 6.0
scp hookxx.dylib root@192.168.1.100:/Library/MobileSubstrate/DynamicLibraries/
每个app启动都会载入/Library/MobileSubstrate/DynamicLibraries/ 目录下所有的动态库,除非动态库有用plist来指定只给哪些应用加载,否则所有应用都会加载。
可以写个简单例子,测试下这个dylib能否被加载:
#import <stdio.h>
int main() {
void *handle = (void*)dlopen("/Library/MobileSubstrate/DynamicLibraries/hookkg.dylib", 0x2);
if (handle) {
printf("load ok\n");
}
else {
printf("load fail\n");
}
return 0;
}
编译成执行文件:
clang -o testhookxx -arch armv7 testhookxx.m -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.1.sdk
拷贝到手机:
scp testhookxx root@192.168.1.100:testhookxx
ssh到手机执行:
./testhookxx
(会看到进入了dylib的initialize,然后输出load ok)
启动xx,观察log。
看应用输出的log,使用socat:
apt-get install socat
socat – UNIX-CONNECT:/var/run/lockdown/syslog.sock #连接系统日志
>watch
根据进程id来过滤(ps aux | grep xx):
* PID 951
根据bundleid来过滤:
* Facility com.xx.xx
会看以下的log输出,说明已经加载了hookxx动态库,并且已经执行到库里面的new_viewDidLoad函数了。
Dec 2 11:22:05 810 xx[974] <Warning>: =======in initialize=================
...
Dec 2 11:24:48 810 xx[974] <Warning>: -------------in new_viewDidLoad----------