iOS之cycript

简介

Cycript是由Cydia创始人Saurik推出的一款脚本语言,Cycript混合了OC、JavaScript语法的解释器,我们能够在一个命令中使用Oc或者JavaScript,甚至两者并用。它能够挂钩正在运行的进程,能够在运行时修改很多东西。

官方参考手册

使用方法

1.越狱手机安装Cycript插件,ssh登录到手机中

1
cycript -p pid

2.使用MonkeyDev新建MonkeyApp工程,运行需要动态调试的App,Monkey默认开启了Cycript服务:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
CHConstructor{
NSLog(INSERT_SUCCESS_WELCOME);

[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidFinishLaunchingNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {

#ifndef __OPTIMIZE__
CYListenServer(6666);

MDCycriptManager* manager = [MDCycriptManager sharedInstance];
[manager loadCycript:NO];

NSError* error;
NSString* result = [manager evaluateCycript:@"UIApp" error:&error];
NSLog(@"result: %@", result);
if(error.code != 0){
NSLog(@"error: %@", error.localizedDescription);
}
#endif

}];
}

我们只需要Cycript链接到目标应用:

1
cycript -r IP:6666

常用命令

  • UIApp
  • NSHomeDirectory()
  • [[NSBundle mainBundle] bundleIdentifier]
  • UIApp.delegate
  • UIApp.keyWindow
  • UIApp.keyWindow.recursiveDescription().toString()
  • UIApp.keyWindow.rootViewController
  • #address :获取该对象
  • *#address:打印该对象成员变量
  • ?exit

弹窗

1
alert = [[UIAlertView alloc]initWithTitle:"@@" message:"hahaha" delegate:nil cancelButtonTitle:"OK" otherButtonTitles:nil]

调用

1
2
3
[alert show]
[#0x1046bcbc0 show]
alert.show()

案例

  1. 将手机语言设置未英文(方便定位),进入设置-关于

  2. 打印当前页面的所有信息

    1
    2
    3
    4
    UIApp.keyWindow.recursiveDescription().toString()

    //只显示一部分
    <PSTableCell: 0x10392e000; baseClass = UITableViewCell; frame = (0 290; 375 44); text = 'Network'; autoresize = W; tag = 4; layer = <CALayer: 0x2811e46a0>>\n

    可以看见Network的PSTableCell以及对象地址

  3. 隐藏/显示

    1
    2
    3
    4
    //隐藏
    [#0x10392e000 setHidden:YES]
    //显示
    [#0x10392e000 setHidden:NO]
  4. 通过nextResponder找到cell对应的controller

    通过三次nextResponder最终找到了controller->PSGAboutController

  5. 修改controller的标题

    1
    [#0x104137400 setTitle:"Cola"]