转自 http://lsq.me/2014/01/12/cycript-tricks/ 自己留着看看的
引言
在分析iOS应用程序的时候,经常会用到Cycript这个工具。本文将介绍使用Cycript的一些基本命令和高级技巧。
打印Ivar值
很多时候输入*varName就可以:
cy# *controller
{isa:"PrefsRootController",_contentView:">",_navBar:...
cy#
然而有时候却不行:
cy# *UIApp
{message:"hasProperty callback returned true for a property that doesn't exist.",name:"ReferenceError"}
但是你可以这样:
cy# [i for (i in *UIApp)]
["isa","_delegate","_touchMap","_exclusiveTouchWindows","_event",...
为了取得尽可能多的ivar值,你可以用下面这个函数:
function tryPrintIvars(a){ var x={}; for(i in *a){ try{ x[i] = (*a)[i]; } catch(e){} } return x; }
用法是:
cy# *a
{message:"hasProperty callback returned true for a property that doesn't exist.",name:"ReferenceError"}
cy# tryPrintIvars(a)
{isa:"SBWaveView",_layer:"",_tapInfo:null,_gestureInfo:null,_gestureRecognizers:...
打印方法名
可以取得方法名的函数:
function printMethods(className) {
var count = new new Type("I");
var methods = class_copyMethodList(objc_getClass(className), count);
var methodsArray = [];
for(var i = 0; i < *count; i++) {
var method = methods[i];
methodsArray.push({selector:method_getName(method), implementation:method_getImplementation(method)});
}
free(methods);
free(count);
return methodsArray;
}
可以这样用:
cy# printMethods("MailboxPrefsTableCell")
[{selector:@selector(layoutSubviews),implementation:0x302bf2e9},{selector:@selector(setCurrentMailbox:),implementation:0x302bee0d},...
cy#
你也可以只看isa的消息属性,例如用UIApp.keyWindow.rootViewController.isa.messages可以取得rootViewControllers的方法。
用正则表达式取方法名
function methodsMatching(cls, regexp) { return [[new Selector(m).type(cls), m] for (m in cls.messages) if (!regexp || regexp.test(m))]; }
用法:
cy# methodsMatching(NSRunLoop, /forKey:$/)
[["v20@0:4I8@12@16","didChange:valuesAtIndexes:forKey:"],["v20@0:4I8@12@16","willChange:valuesAtIndexes:forKey:"],["v16@0:4@8@12","setValue:forKey:"]]
从地址获取Objective-C对象
用new Instance(0xdeadbabe):
cy# var p = new Instance(0x8614390)
cy# p
[""]
载入框架
function loadFramework(fw) {
var h="/System/Library/",t="Frameworks/"+fw+".framework";
[[NSBundle bundleWithPath:h+t]||[NSBundle bundleWithPath:h+"Private"+t] load];
}
替换Objective-C方法
你可以通过替换messages数组的内容来模拟MSHookMessage:
cy# original_NSRunLoop_description = NSRunLoop.messages['description'];
0x339d94c3
cy# NSRunLoop.messages['description'] = function() { return original_NSRunLoop_description.call(this).toString().substr(0, 80)+", etc."; }
{}
cy# [NSRunLoop currentRunLoop]
"{locked = false, wait port = 0x1303, stopped = , etc."
注意func.call(this)的结构,就是它把原函数的this绑定到用户指定的那个去了。如果需要更多地参数,用function(arg1, arg2, arg3, ...) {...func.call(self, arg1, arg2, arg3, ...);}来代替,例如:
cy# original_SpringBoard_menuButtonDown = SpringBoard.messages['menuButtonDown:']
0x17dbab1
cy# SpringBoard.messages['menuButtonDown:'] = function(arg1) {original_SpringBoard_menuButtonDown.call(this, arg1);}
function (e) {var e;var $cy0=this;original_SpringBoard_menuButtonDown.call($cy0,e);}
注意参数不会被自动映射到相对应的Objective-C类型,所以参数应该用[NSString stringWithString:"foo"]而不是简单的"foo"。
获取类方法
class.messages只包含实例方法。要hook类方法,你需要得到他的metaclass,一个简单地方法是:
cy# NSRunLoop->isa.messages['currentRunLoop'] = ...
引入其他Cycript文件
从0.9.274-1开始,取消了导入native文件的组件。当Cycript需要hook到其他进程时,既然数据都保留在那儿,你可以首先加载那个.cy文件:
localhost:~ mobile$ cycript -p SpringBoard main.cy
0x12345678
localhost:~ mobile$ cycript -p SpringBoard
cy# ...
如果Cycript是独立启动的,使用Cycript编译器和Javascript的eval表达式相结合也可以使引入作伪:
// include other .cy files
function include(fn) {
var t = [new NSTask init]; [t setLaunchPath:@"/usr/bin/cycript"]; [t setArguments:["-c", fn]];
var p = [NSPipe pipe]; [t setStandardOutput:p]; [t launch]; [t waitUntilExit];
var s = [new NSString initWithData:[[p fileHandleForReading] readDataToEndOfFile] encoding:4];
return this.eval(s.toString());
}
使用NSLog
在控制台输入:
NSLog_ = dlsym(RTLD_DEFAULT, "NSLog")
NSLog = function() { var types = 'v', args = [], count = arguments.length; for (var i = 0; i != count; ++i) { types += '@'; args.push(arguments[i]); } new Functor(NSLog_, types).apply(null, args); }
然后就可以像平常一样使用NSLog:
cy# NSLog_ = dlsym(RTLD_DEFAULT, "NSLog")
0x31451329
cy# NSLog = function() { var types = 'v', args = [], count = arguments.length; for (var i = 0; i != count; ++i) { types += '@'; args.push(arguments[i]); } new Functor(NSLog_, types).apply(null, args); }
{}
cy# NSLog("w ivars: %@", tryPrintIvars(w))
如果已经attach到一个进程了,输出就已经写到syslog了:
Nov 17 20:26:01 iPhone3GS Foobar[551]: w ivars: {\n contentView =
使用CGGeometry功能
为了使用CGGeometry类的函数,必须要在Cycript窗体输入:
function CGPointMake(x, y) { return {x:x, y:y}; }
function CGSizeMake(w, h) { return {width:w, height:h}; }
function CGRectMake(x, y, w, h) { return {origin:CGPointMake(x,y), size:CGSizeMake(w, h)}; }
将Cycript输出写入文件
Cycript的输出时NSString,所以调用writeToFile然后把它保存起来时可能的,例如:
[[someObject someFunction] writeToFile:"/var/mobile/cycriptoutput.txt" atomically:NO encoding:4 error:NULL]
你可以这么用,下面是获取SpringBoard view的结构的示例:
iPhone:~$ cycript -p SpringBoard
cy# [[UIApp->_uiController.window recursiveDescription] writeToFile:"/var/mobile/viewdump.txt" atomically:NO encoding:4 error:NULL]
打印View的继承关系
iPhone:~$ cycript -p SpringBoard
cy# ?expand
expand == true
cy# UIApp.keyWindow.recursiveDescription
">
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>"
?expand命令使换行显示的更加正常,而不仅仅是\n。
参考文献