MonkeyDev安装与使用

作者: yahibo | 来源:发表于2019-11-06 23:57 被阅读0次

    一、前言
    二、MonkeyDev插件的安装
    三、MonkeyApp的简单使用
    四、微信步数修改
    五、自动抢红包
    六、修改钉钉打卡位置

    一、前言

    前面介绍了APP应用重签名及方法函数的hook,有这些准备工作,就能够对其他应用做一些小改动,修改微信步数、自动抢红包、钉钉打卡定位等等。以上需要我们配置脚本,创建动态库,步骤不复杂但Facebook也给出了更简单的方法,提供了逆向开发的插件 《MonkeyDev》,安装后,我们可以在xcode上直接创建逆向开发工程。

    相关链接

    安装方法:https://github.com/AloneMonkey/MonkeyDev/wiki/%E5%AE%89%E8%A3%85
    使用方法:https://iphonedevwiki.net/index.php/Logos

    二、MonkeyDev插件的安装

    官方给出的步骤:

    环境要求

    1、安装最新的theos

    sudo git clone --recursive https://github.com/theos/theos.git /opt/theos
    

    2、安装ldid

    brew install ldid
    

    安装

    1、选择指定的Xcode进行安装

    sudo xcode-select -s /Applications/Xcode-beta.app
    

    2、默认安装的Xcode

    xcode-select -p
    

    3、执行安装命令

    sudo /bin/sh -c "$(curl -fsSL https://raw.githubusercontent.com/AloneMonkey/MonkeyDev/master/bin/md-install)"
    

    4、卸载命令

    sudo /bin/sh -c "$(curl -fsSL https://raw.githubusercontent.com/AloneMonkey/MonkeyDev/master/bin/md-uninstall)"
    

    5、更新命令

    sudo /bin/sh -c "$(curl -fsSL https://raw.githubusercontent.com/AloneMonkey/MonkeyDev/master/bin/md-update)"
    

    安装完成后重启Xcode即可。如下:

    monkey.jpg

    至此就可以开始逆向开发了。

    注意:安装过程可能出现安装失败问题(raw.githubusercontent.com不能访问)。可直接下载源码安装。

    问题:
    执行sudo ./md-install安装命令报错如下:

    Failed to download https://raw.githubusercontent.com/AloneMonkey/frida-ios-dump/3.x/dump.py to /opt/MonkeyDev/bin/dump.py
    

    解决:

    • https://github.com/AloneMonkey/frida-ios-dump地址下载firda-ios-dump
    • dump.pydump.js复制到/opt/MonkeyDev/bin
    • 给执行权限sudo chmod +x dump.pysudo chmod +x dump.js
    • Xcodesublime或其他编辑器编辑md-install可执行文件,将下载报错地方注释掉即可。重新执行安装sudo ./md-install
    #下载一些基础文件和模板文件
    # downloadGithubTarball "https://codeload.github.com/AloneMonkey/MonkeyDev/tar.gz/$branch" "$MonkeyDevPath" "MonkeyDev base"
    # downloadGithubTarball "https://codeload.github.com/AloneMonkey/MonkeyDev-Xcode-Templates/tar.gz/$branch" "$MonkeyDevPath/templates" "Xcode templates"
    
    # #下载frida-ios-dump
    # echo "Downloading frida-ios-dump from Github..."
    # downloadFile "https://raw.githubusercontent.com/AloneMonkey/frida-ios-dump/3.x/dump.py" "$MonkeyDevPath/bin/dump.py"
    # downloadFile "https://raw.githubusercontent.com/AloneMonkey/frida-ios-dump/3.x/dump.js" "$MonkeyDevPath/bin/dump.js"
    

    报错:File /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Xcode/Specifications/MacOSX Package Types.xcspec not found

    解决:

    macosxSDKSpecificationsPath=$macosSdkPlatformPath/Developer/Library/Xcode/Specifications
    

    修改为:

    macosxSDKSpecificationsPath=$macosSdkPlatformPath/Developer/Library/Xcode/PrivatePlugIns/IDEOSXSupportCore.ideplugin/Contents/Resources
    

    三、MonkeyApp的简单使用

    MonkeyApp原理和 《动态库注入》 的原理一毛一样,这里逆向一个应用,只需要将相应的ipa包加入到TargetApp文件中即可,此处的TargetApp既是 《动态库注入》 中的app文件夹。

    MonkeyApp工程中,使用的是Logos语法,使用比较简单,可以参考相关用法:《Logos语法》

    下面使用MonkeyApp完成一个简单的功能。

    破壳ipa获取:
    1、通过越狱手机获取破壳应用;
    2、通过PP助手获取越狱应用。

    微信.ipa下载

    创建MonkeyApp工程

    创建工程,选择MonkeyApp工程,点击create会自动生成逆向相关的文件:

    create.jpg
    • TargetApp:放目标ipa的文件,将需要逆向的破壳ipa放在此处
    • Logos:编写相关hook的文件,所有hook操作在此处
    • fishhook:用来hook系统函数的库

    配置MonkeyApp工程

    1、设置TARGETS->MonkeyDemo->Signing & Capabilities->Signing

    target.jpg

    2、设置TARGETS->MonkeyDemoDylib->Build Settings->Signing->Development Team

    dylib.jpg

    以上Development Team处保持一致。

    常用logos语法

    选择Logos下的MonkeyDemoDylib.xm文件,这里有说明

    // See http://iphonedevwiki.net/index.php/Logos
    
    #import <UIKit/UIKit.h>
    
    @interface CustomViewController
    
    @property (nonatomic, copy) NSString* newProperty;
    
    + (void)classMethod;
    
    - (NSString*)getMyName;
    
    - (void)newMethod:(NSString*) output;
    
    @end
    
    %hook CustomViewController
    
    + (void)classMethod
    {
        %log;
    
        %orig;
    }
    
    %new
    -(void)newMethod:(NSString*) output{
        NSLog(@"This is a new method : %@", output);
    }
    
    %new
    - (id)newProperty {
        return objc_getAssociatedObject(self, @selector(newProperty));
    }
    
    %new
    - (void)setNewProperty:(id)value {
        objc_setAssociatedObject(self, @selector(newProperty), value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    - (NSString*)getMyName
    {
        %log;
        
        NSString* password = MSHookIvar<NSString*>(self,"_password");
        
        NSLog(@"password:%@", password);
        
        [%c(CustomViewController) classMethod];
        
        [self newMethod:@"output"];
        
        self.newProperty = @"newProperty";
        
        NSLog(@"newProperty : %@", self.newProperty);
    
        return %orig();
    }
    
    %end
    

    iOS应用开发类似,不同之处,逆向在于修改其他应用代码的执行流程,因此需要hook方法、添加方法。这里以%开头的都是逆向插件所特有的一些功能。

    1、%hook指定需要hook的类名,以%end结尾。

    %hook CustomViewController
    
    + (void)classMethod {
        //编写hook代码
        %orig;//执行原始方法
    }
    
    %end
    

    classMethod为要hook的应用的方法,在内部编写自己的代码。

    2、% orig调用原有方法,根据原有方法有无参数,可以选择添加参数%orig(arge1,arge2)

    %orig;
    或
    %orig(1,2);
    

    3、%log用来打印log的,将信息输入到syslog中,格式%log([(<type><expr>,…)])

    + (void)classMethod {
        %orig;
        %log((NSString *)@"iOSRE",(NSString *)@"Debug");
    }
    

    4、%new该指令用来给指定的class添加一个新的函数。与Runtime中的class_addMethod相同。

    %new
    -(void)newMethod:(NSString*) output{
        NSLog(@"This is a new method : %@", output);
    }
    

    如果方法没找到,在%hook上方加入interface,如下:

    @interface UIViewController
    -(void)newMethod:(NSString*) output;
    @end
    

    5、%c该指令用来获取一个类的名称,类似于objc_getClass

    [%c(CustomViewController) classMethod];
    

    其他标签

    6、%group该指令用于给%hook分组,%group后边跟的是组名,以%end结尾,可以包含多个%hook

    %group1
    %hook class
    -(void)function {
        nslog();
    }
    %end
    %end
    
    %group2
    %hook class
    -(void)function {
        nslog();
    }
    %end
    %end
    
    //构造函数,初始化那一组使用哪一组
    %ctor{
        NSString *v = [UIDevice currentDevice].systemVersion;
        CGFloat version = [v floatValue];
        if(version >= 12.0){
            %init(group1);
        }else {
            %init(group2);
        }
    }
    

    7、%init指令用来初始化某个%groupgroup被初始化后生效。

    %init(group1);
    

    8、%ctor为构造器,用来做初始化操作。

    %ctor{
        NSString *v = [UIDevice currentDevice].systemVersion;
        CGFloat version = [v floatValue];
        if(version >= 12.0){
            %init(group1);
        }else {
            %init(group2);
        }
    }
    

    更多参考官方 >>>

    编写logos代码

    1、准备工具

    Class-dump:能够将存储在Mach-O文件中的@interface、@protocal信息提取并生产对应的.h文件,通过该文件可查看被逆向应用所有的类,及类的属性、成员变量、方法。

    sublime:编辑器,帮助开始定位代码位置。

    安装后找到微信ipa包中的可执行二进制,执行如下命令:

    class-dump -H WeChat -o wxh
    

    头文件便全部导出,根据头文件信息,通过sublime搜索,快速定位类和方法的位置。

    2、使用monkey工程运行被逆向的应用

    将砸壳后的ipa包放入到TargetApp文件中,并运行,通过图层调试(Debug > View Debugging > Capture View Hierarchy或快捷方式),如下:

    wx_layer.png

    在图层属性中很快定位到了登录按钮的方法Action onFirstViewLogin,和所在的控制器,也可以使用sublime打开头文件,全局搜索该方法,能够定位到方法所在的类WCAccountLoginControlLogic

    3、监听登录按钮

    基于以上信息,编写相关代码,来监听登录按钮。代码如下:

    #import <UIKit/UIKit.h>
    
    %hook WCAccountLoginControlLogic
    
    - (void)onFirstViewLogin {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@" 这个按钮是我的!"   preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil]];
        [[[UIApplication sharedApplication] keyWindow].rootViewController presentViewController:alert animated:YES completion:nil];
    }
    
    %end
    

    运行代码,点击登录按钮,显示如下:

    result.png

    该工程帮助我们来hook相关类的方法,这里我们只需要插入要修改的内容即可,除了Logos限定的规则,具体修改内容还是使用OC相关的语法,使用很简单。

    四、微信步数修改

    这里只上一张图作为参考,具体查看 《动态库注入》

    通过class-dump工具获取头文件信息,结合视图属性,查找对应的方法及方法所在类。

    modify.png

    使用MonkeyDev插件来逆向应用就很简单了,只需要几行代码就能修改原有业务。步数修改代码如下:

    #import <UIKit/UIKit.h>
    
    %hook WCDeviceStepObject
    - (int)m7StepCount {
        return 54321;
    }
    %end
    

    代码就这些,不能再多了,看一下微信排行:

    result.jpeg

    五、自动抢红包

    redEnvelope(更新:2022.1.2)

    注意:
    新版本微信包,会出现报错Unable to install "",根据报错描述,删除.app包中的com.apple.WatchPlaceholdercmd+k清除再运行即可。其他报错,根据错误提示修改。

    六、修改钉钉打卡位置

    ……

    七、常见问题

    1、库缺失报错

    ld: file not found: /usr/lib/libstdc++.dylib
    

    Xcode10之后废弃了libstdc++库。相关的库文件:

    libstdc++.6.0.9.dylib
    libstdc++.6.dylib
    libstdc++.dylib
    libstdc++.6.0.9.tbd
    libstdc++.6.tbd
    libstdc++.tbd 
    

    下载地址:https://github.com/devdawei/libstdc-
    目录:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/

    2、插件导致错误

    The xxxKit app's Info.plist must have a WKCompanionAppBundleIdentifier key set to the bundle identifier of the companion app.
    Domain: com.apple.dt.MobileDeviceErrorDomain

    根据提示信息,进入.app文件,将相应的插件文件删除即可。

    3、CydiaSubstrate.tbd文件报错

    Ignoring file /opt/theos/vendor/lib/CydiaSubstrate.framework/CydiaSubstrate.tbd, missing required architecture x86_64 in file /opt/theos/vendor/lib/CydiaSubstrate.framework/CydiaSubstrate.tbd (4 slices)

    找到该文件,编辑器打开,将i386x86_64架构符号删除。

    4、Logos Tweak工程编译报错

    Showing All Messages An empty identity is not valid when signing a binary for the product type 'Dynamic Library'.

    TARGETS - Build Settings中点击加号添加 CODE_SIGNING_ALLOWED设置为NO,重新Build报错解决

    5、安装报错

    Failed to download https://raw.githubusercontent.com/AloneMonkey/frida-ios-dump/3.x/dump.pyto /opt/MonkeyDev/bin/dump.py

    终端上 ping github.com 获取IP,打开hosts:vim /etc/hosts,在最后添加:
    20.205.243.166 raw.githubusercontent.com
    添加后重新安装即可。

    相关文章

      网友评论

        本文标题:MonkeyDev安装与使用

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