美文网首页iOS审核上架逆向开发
iOS逆向工程(三):利用Cycript调试App

iOS逆向工程(三):利用Cycript调试App

作者: 冰雨9527 | 来源:发表于2020-10-21 14:41 被阅读0次

利用Cycript调试App

一、Cycript是什么?
    1. Cycript是一种脚本语言,是Objective-C、JavaScript、Java等语法的混合物,这就意味着我们可以在一条命令中使用这些语言的语法,甚至可以混合使用。(作者是Jay Freeman,没错,就是Cydia的作者,Jay Freeman出品必属精品,放心使用吧)
    1. Cycript可以用来调试、探索、修改正在运行的Mac/iOS App,只需要通过Cydia就可以安装Cycript了,然后就可以尽情的在iPhone上调试运行中的App了。
二、Cycript的用法
    1. 使用Cycript的前提是:Mac通过SSH远程登录到了iPhone,并且iPhone安装了Cycript脚本 (iPhone越狱后通过Cydia搜索安装)
    1. 想要调试某个App就需要知道此App的进程名称,在手机上打开想要调试的App,使用ps -A命令,列出来所有进程后,找到想要调试的APP的进程名即可。(例如:我们要调试皮皮虾App,在手机上打开皮皮虾,输入ps -A命令,找到皮皮虾的进程名称是Super,如下图所示)

      利用ps -A命令,列出来当前手机所有进程
    1. 选择某个进程进行挂载,挂载某个进程cycript -p 进程名称或者进程ID
    • 特别 特别 特别 要注意,这种挂载进程的方法只适用于iOS12之前的系统,iOS12及其以后的系统,要用另一种方法挂载,详情请看下面的第五章
例如:想挂载皮皮虾App,先让皮皮虾App在手机里跑起来,找到进程名后,输入以下命令
cycript -p Super
    1. 利用以上三步就可以挂载某个进程了,挂载之后,出现cy#,如下图所示,就意味着进入了Cycript脚本环境了,就可以输入Cycript语法来调试这个进程了

    1. cycript环境下的快捷键:
    • 退出:Ctrl + D
    • 清屏:Command + R
    • 取消输入:Ctrl + C
    1. 总结一下,其实非常简单
1>.使用前提:MacSSH登录了iPhone,iPhone安装了Cycript
2>.ps -A , 找出所有进程
3>.cycript -p Super,挂载皮皮虾,出现cy#,说明进入到了Cycript环境
三、Cycript的常用语法
    1. UIApp,就相当于OC中的[UIApplication sharedApplication]

    1. 定义变量var 变量名 = 变量值

    1. 用变量地址获取对象#内存地址,例如:#0x109703340,就拿到了这个地址的对象

    1. 查看对象的所有成员变量*对象,例如:*UIApp,就是拿到了UIApplication对象的所有变量

    1. 查看已经加载的所有OC类,ObjectiveC.classes
    1. 递归打印所有View的子控件,view.recursiveDescription().toString()
    1. 筛选出某种类型的对象choose(类型名称),例如choose(UIButton),找出所有UIButton类型的对象

    1. Cycript官方手册请点击这里
四、封装并导入Cycript库
    1. 大家是不是觉得上面的语法比较繁琐,用起来很不方便,我们可以把常用的语法封装进一个.cy文件中,以后直接用就可以了
    1. 例如把下面的代码,放到common.cy文件中 (大家仿照下面的代码写就可以了,exports参数是固定的,不需要改)
(function(exports){
    // app id
    AppId = [NSBundle mainBundle].bundleIdentifier;

    // mainBundlePath
    BundlePath = [NSBundle mainBundle].bundlePath;

    // keyWindow
    KeyWin = function() {
        return UIApp.keyWindow;
    };

    // rootView
    RootVc =  function() {
        return UIApp.keyWindow.rootViewController;
    };
})(exports);
    1. 然后把封装好的.cy文件放到手机的/usr/lib/cycript0.9目录下
    1. 以后进入cycript环境后,首先导入封装好的cy文件,就可以使用封装好的方法了,命令是:@import xxx.cy
    1. MJ大佬封装好了一个.cy文件mjcript,大家可以去下载,放到手机的/usr/lib/cycript0.9目录下,以后再进入cycript环境后,只需要@import mjcript,就可以使用里面的方法了。如下图所示:

五、在iOS12及以后的系统,挂载进程的方法
    1. 在iOS12以后,使用cycript -p 进程名的方法挂载进程,就会报下面的错误:
assert($mach_task_self != NULL)
*** _assert(status == 0):../Inject.cpp(143):InjectLibrary
    1. iOS12以后要使用cyrun挂载进程,进入cycript环境后,所有操作还跟以前一样
    1. cyrun安装方法:
    • (1). 通过Cydia安装New Curses、readline、adv-cmds、wgetwget主要为了在iPhone上可以下载deb文件)
    • (2). 以root账户登录到iPhone上,输入以下命令,下载以下deb文件
    wget http://apt.saurik.com/debs/cycript_0.9.594_iphoneos-arm.deb
    wget http://www.tateu.net/repo/files/net.tateu.cycriptlistenertweak_1.0.0_iphoneos-arm.deb
    wget http://www.tateu.net/repo/files/net.tateu.cyrun_1.0.5_iphoneos-arm.deb
    
    • (3). 输入以下命令,安装下载好的deb
    dpkg -i cycript_0.9.594_iphoneos-arm.deb
    dpkg -i net.tateu.cycriptlistenertweak_1.0.0_iphoneos-arm.deb   net.tateu.cyrun_1.0.5_iphoneos-arm.deb
    
    1. 安装成功之后,以后想进入cycript环境,直接输入cyrun -n 进程名 -e -d -f或者cyrun -b bundleID -e命令就可以了,此命令会重启相应的进程,然后自动进入cycript环境
    1. 想退出的时候,按Control+D就可以退出cycript环境时,退出后会自动杀掉该进程
    1. 以皮皮虾APP为例,使用cyrun命令进入cycript环境,导入写好的mjcript库,查看皮皮虾APP的当前控制器和根控制器,如下图所示:

六、知识点总结
1>.使用Cycript调试APP的前提:用Mac以SSH的方式登录了iPhone,iPhone安装了Cycript
2>.ps -A , 找出所有进程,找到要调试的APP
3>.以皮皮虾APP为例,挂载皮皮虾APP的进程:
- iOS12系统以前的挂载 :cycript -p Super
- iOS12系统以后的挂载 :cyrun -n Super -e -d -f 或者 cyrun  -b  bundleID -e
4>. 挂载后,会进入cycript环境,导入封装好的库,使用其中的方法进行调试: @import mjcript
5>. 找到皮皮虾APP的当前控制器: MJFrontVc()
6>. 退出cycript环境:Control + D
七、修改皮皮虾APP界面
    1. 利用上述所学,来实际改一下皮皮虾APP的界面,将登录界面中的"登录",改成"逆向调试"
    1. 流程如下:
    • 首先,SSH到iPhone:sh login.sh
    • 然后,挂载皮皮虾APP:cyrun -n Super -e -d -f
    • 然后,导入mjcipt库:@import mjcript
    • 然后,用手机打开皮皮虾的登录界面
    • 然后,查看登录界面的控制器:MJFrontVc()
    • 然后,查看登录界面的View的层级结构:MJVcSubviews(#0x107b0f400)
    • 然后,输入"登录",查看”登录“的unicode编码
    • 然后,搜索”登录“的unicode编码,找到登录所属的UILabel对象
    • 然后,修改此UILabel的text属性:#0x117917e10.text = '逆向调试'
    • 然后, 修改此UILabel的size属性:#0x117917e10.size = MJSizeMake(200,30)
    • 最后,Control+D退出cycript环境
    1. 所有代码如下:
songpengdeMBP:~ songpeng$ sh login.sh
iPhone7ceshiji:~ root# cyrun -n Super -e -d -f
applicationName: Super is not running (-1)
    executableName: Super
    bundleIdentifier: com.bd.iphone.super
    Cycript is inactive:
    Device is not passcode locked
    Tweak Mode
Waiting for Cycript to become active...
Successfully enabled, you may now run
    cycript -r 127.0.0.1:8556
cy# @import mjcript
{}
cy# MJFrontVc()
#"<BDSLoginViewController: 0x107b0f400>"
cy# MJVcSubviews(#0x107b0f400)
`<UIView: 0x11719b2b0; frame = (0 0; 375 667); autoresize = W+H; gestureRecognizers = <NSArray: 0x2836757d0>; layer = <CALayer: 0x283b2dea0>>
   | <BDSNavigationBar: 0x117194a60; baseClass = UIControl; frame = (0 0; 375 64); layer = <CALayer: 0x283b2e060>>
   |    | <UIView: 0x106dc3600; frame = (0 20; 375 44); layer = <CALayer: 0x283b2aec0>>
   |    |    | <BDSNavigationBarItem: 0x1179baa00; baseClass = UIControl; frame = (0 0; 48 44); layer = <CALayer: 0x283b29d80>>
   |    |    |    | <UIImageView: 0x1179b2870; frame = (12 10; 24 24); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x283b2ac60>>
   |    |    | <BDSNavigationBarItem: 0x1179babf0; baseClass = UIControl; frame = (314 8; 48 36); hidden = YES; layer = <CALayer: 0x283b2ae40>>
   |    |    |    | <UILabel: 0x1179b2bc0; frame = (0 0; 48 36); text = '\u8df3\u8fc7'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x281a15e00>>
   |    |    | <UILabel: 0x117196d00; frame = (187.5 0; 0 44); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x281a137f0>>
   |    |    |    | <_UILabelContentLayer: 0x283b34c60> (layer)
   | <UILabel: 0x117917e10; frame = (24 81.4815; 80 56); text = '\u767b\u5f55'; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x281a15a40>>
   |    | <_UILabelContentLayer: 0x283b34b60> (layer)
   | <UITextField: 0x117136290; frame = (26 186.37; 336 42); text = ''; opaque = NO; gestureRecognizers = <NSArray: 0x28349c5a0>; layer = <CALayer: 0x283b2dce0>>
   |    | <UITextFieldLabel: 0x11719a4c0; frame = (0 0; 309 42); text = '\u8f93\u5165\u624b\u673a\u53f7'; opaque = NO; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x281a11590>>
   |    |    | <_UILabelContentLayer: 0x283b34a80> (layer)
   |    | <UIFieldEditor: 0x1073b4c00; frame = (0 0; 309 42); text = ''; clipsToBounds = YES; opaque = NO; gestureRecognizers = <NSArray: 0x283652c70>; layer = <CALayer: 0x283b2dd00>; contentOffset: {0, 0}; contentSize: {309, 42}; adjustedContentInset: {0, 0, 0, 0}>
   |    |    | <_UITextFieldCanvasView: 0x117169530; frame = (0 0; 309 42); opaque = NO; userInteractionEnabled = NO; layer = <_UITextTiledLayer: 0x28085d8c0>>
   |    |    |    | <UITextSelectionView: 0x1171a6d20; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <CALayer: 0x283b3a260>>
   |    |    |    |    | <UIView: 0x1179a4810; frame = (0 0; 2 42); userInteractionEnabled = NO; layer = <CALayer: 0x283b3d9c0>>
   | <UIView: 0x11712f6f0; frame = (24 233.556; 327 1); layer = <CALayer: 0x283b2e7e0>>
   | <UIButton: 0x1171bbd80; frame = (0 401; 375 50); opaque = NO; layer = <CALayer: 0x283b2e880>>
   |    | <UIImageView: 0x117194e00; frame = (0 0; 375 50); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x283b34f20>>
   |    | <UIButtonLabel: 0x106cd0180; frame = (145 13; 85 24); text = '\u83b7\u53d6\u9a8c\u8bc1\u7801'; opaque = NO; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x281a1f840>>
   |    |    | <_UILabelContentLayer: 0x283b35200> (layer)
   | <UILabel: 0x1171b5b40; frame = (24 246.5; 327 20); text = '\u4e3a\u4e86\u60a8\u7684\u8d26\u53f7\u5b89\u5168\uff0c\u8bf7\u7ed1\u5b9a\u624b\u673a\u53f7'; hidden = YES; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x281a1f7f0>>
   |    | <_UILabelContentLayer: 0x283b34ec0> (layer)
   | <UILabel: 0x11717c3d0; frame = (24 372.5; 327 18); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x281a1dc70>>
   |    | <_UILabelContentLayer: 0x283b35140> (layer)
   | <BDSVerifyCodeInputView: 0x117147b90; frame = (24 168.889; 327 54); hidden = YES; layer = <CALayer: 0x283b2eb00>>
   |    | <UITextField: 0x11718d1b0; frame = (0 8; 2 38); text = ''; opaque = NO; tintColor = UIExtendedGrayColorSpace 0.25098 1; gestureRecognizers = <NSArray: 0x283648720>; layer = <CALayer: 0x283b2eb20>>
   |    |    | <_UITextFieldCanvasView: 0x1171bd3d0; frame = (0 0; 2 38); opaque = NO; userInteractionEnabled = NO; layer = <_UITextTiledLayer: 0x28085da70>>
   |    | <BDSVerifyCodeInputLabel: 0x1179b3330; baseClass = UILabel; frame = (0 0; 327 54); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x281a15860>>
   |    |    | <_UILabelContentLayer: 0x283b35820> (layer)
   | <UILabel: 0x11792be30; frame = (0 231.778; 375 20); text = '\u9a8c\u8bc1\u7801\u9519\u8bef'; hidden = YES; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x281a15770>>
   |    | <_UILabelContentLayer: 0x283b35080> (layer)
   | <BDSRegisterProtocolView: 0x117958e20; frame = (39 361; 297 38); layer = <CALayer: 0x283b32c20>>
   |    | <UIButton: 0x1179f8ec0; frame = (10 10; 18 18); opaque = NO; layer = <CALayer: 0x283b32b60>>
   |    |    | <UIImageView: 0x11717e320; frame = (0 0; 18 18); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x283b359e0>>
   |    | <TTTAttributedLabel: 0x117920400; baseClass = UILabel; frame = (32 9; 255 20); text = '\u6211\u5df2\u9605\u8bfb\u5e76\u540c\u610f\u201c\u7528\u6237\u534f\u8bae\u201d\u548c\u201c\u9690\u79c1\u653f\u7b56\u201d'; gestureRecognizers = <NSArray: 0x28366c2a0>; layer = <_UILabelLayer: 0x281a145a0>>
   |    |    | <_UILabelContentLayer: 0x283b35300> (layer)
   | <UIButton: 0x1179dfa80; frame = (145 140.481; 17 17); hidden = YES; opaque = NO; layer = <CALayer: 0x283b32b20>>
   |    | <UIImageView: 0x11715ed00; frame = (0 0; 17 17); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x283b35a00>>
   | <UIView: 0x1179bf5e0; frame = (24 249.5; 128 24); layer = <CALayer: 0x283b32500>>
   |    | <ArticlePlatformLoginView: 0x11791bb60; frame = (0 0; 128 24); layer = <CALayer: 0x283b32f60>>
   |    |    | <BDSAlphaThemedButton: 0x117159ab0; baseClass = UIButton; frame = (104 0; 24 24); opaque = NO; layer = <CALayer: 0x283b2f300>>
   |    |    |    | <UIImageView: 0x11717a9b0; frame = (0 0; 24 24); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x283b35860>>
   |    |    | <UILabel: 0x11710ec00; frame = (0 2; 84 20); text = '\u5176\u5b83\u65b9\u5f0f\u767b\u5f55'; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x281a1f570>>
   |    |    |    | <_UILabelContentLayer: 0x283b357a0> (layer)`
cy# 登录
throw new ReferenceError("Can't find variable: \u767b\u5f55")
cy# #0x117917e10.text = '逆向调试'
"\u9006\u5411\u8c03\u8bd5"
cy# #0x117917e10.size = MJSizeMake(200,30)
{0:200,1:30}
    1. 效果图如下:

      皮皮虾APP,逆向调试

相关文章

网友评论

    本文标题:iOS逆向工程(三):利用Cycript调试App

    本文链接:https://www.haomeiwen.com/subject/urxemktx.html