iOS逆向工程(Cycript脚本语言使用与实战)

一、Cycript的介绍与安装
Cycript是由Cydia创始人Saurik推出的一款脚本语言,Cycript 混合了Objective-C与javascript语法的解释器,这意味着我们能够在一个命令中用Objective-C或者javascript,甚至两者兼用。
它能够挂钩正在运行的进程,能够在运行时修改应用的很多东西。
Cycript的安装简单,可在越狱设备上从Cydia自带源Cydia/Telesphoreo下载,直接打开设备上的Cydia然后搜索Cycript后安装即可。
二、Cycript脚本命令的简单使用
1,new Instance根据地址获取对象,和直接使用#号获取对象
#0x8614390
var p = new Instance(0x8614390)

2,choose传递一个类,可以在内存中找出属于这个类的对象
choose(UITextField)

4,打印视图层次
UIApp.keyWindow.recursiveDescription().toString()

5,Load frameworks
function loadFramework(fw) {
var h="/System/Library/",t="Frameworks/"+fw+".framework";
[[NSBundle bundleWithPath:h+t]||
[NSBundle bundleWithPath:h+"Private"+t] load];
}
6,使用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);
}
7,Using CGGeometry functions
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)}; }
8,输出对象的属性
方法1: 简单基本获取方法。
*controller(直接在对象前面加个*)
方法2:方法一无法获取,就使用方法2
[i for (i in *UIApp)]
方法3:建议方法三,方法三能获取到更多
function tryPrintIvars(a){ var x={}; for(i in *a){ try{ x[i] = (*a)[i]; } catch(e){} } return x; }

9,根据类获取方法
1,获取所有方法:
function printMethods(className, isa) {
var count = new new Type("I");
var classObj = (isa != undefined) ? objc_getClass(className)->isa :
objc_getClass(className);
var methods = class_copyMethodList(classObj, 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);
return methodsArray;
}

10,获取当前控制器
function currentVC() {
var app = [UIApplication sharedApplication]
var keyWindow = app.keyWindow
var rootController = keyWindow.rootViewController
var visibleController = rootController.visibleViewController
if (!visibleController){
return rootController
}
return visibleController.childViewControllers[0]
}
var vc = currentVC()

10,增加分类

11,创建Block

三、Cycript 命令演示
挂钩进程:

我这边已经创建了一个APP用来演示实战,修改按钮的enable属性,和修改文本框内容。
![]()
1,首先SSH到设备上。
ssh root@10.10.245.208
2,使用ps命令查看所需要修改的APP的进程ID
ps ax | grep Cycript
ps命令是查看进程的命令,如果不懂,大家可以私下去查下,有很多相关的资料。
3,挂钩指定的ID的进程
cycript -p 1354
(1354为进程ID)
4,思路整理
想要修改按钮的状态,我们可以有很多方法,现在整理下:
第一种:打印当前视图层次结构找出按钮
第二种:通过dump Class 找出相应的类和头文件,然后再找出按钮。
第三种:通过choose UIButton类,找出按钮。
等等,其实有很多方法都可以找到,大家可以自己研究。
这里我就随便用一种方法,来演示下了:

在这里其实是快速演示了,在演示中我搜索通过直接搜索按钮文字直接找到了按钮,但是大家看到VIP后面的汉字是UNICODE编码的,如果全是中文,我们可以编码转换为UNICODE后,直接搜索UNICODE编码。
推荐一个转换编码的网址:http://tool.chinaz.com/Tools/Unicode.aspx
上面的演示操作很简单,其实演示到这一步我感觉已经不用再演示修改文本框了,因为原理都一样,但是使用方法千变万化,在这里我也是简单的整理下我的学习和理解,其它要靠大家自己发掘了。感谢观看

更多详细资料可参阅 官网 和 iphonedevwiki 。
官网 :http://www.cycript.org/
Tricks :http://iphonedevwiki.net/index.php/Cycript_Tricks