首页
社区
课程
招聘
[原创]Textobot-TB插件进程级API详解&基础篇完结
发表于: 2020-1-7 10:13 10805

[原创]Textobot-TB插件进程级API详解&基础篇完结

2020-1-7 10:13
10805

导读

0x00.交个朋友

0x01.Frida简介

0x02.界面类API

0x03.按键类API

0x04.工具类API

0x05.基础篇完结


0x00.交个朋友

手游从业者模拟器玩家请关注云游模拟器PantaWin;

Android开发者请关注云游模拟器PantaWin/PantaMac/PantaLinux;

iOS越狱开发者请关注晓文框架Textobot;

Android插件开发者请关注飞度框架Fridobot;

移动调试器重度用户请关注利达调试器LidaDbg;

我们的产品推荐使用git下载和更新,在码云搜索geekneo即可。


0x01.Frida简介

我注意到Frida是2015年,那时Frida刚面世一年多,但是功能已经把我震撼到了。多平台支持、脚本化、Native桥接、高级插桩等功能对于逆向工程和动态分析的效率提升那是相当的巨大。
Frida最大的特色就是用JavaScript脚本化了动态插桩这一过程。我们通过编写JavaScript代码就可以达到利用Native+Substrate/Substitution/Detours/FishHook等组合编写的动态分析模块效果。同时,Frida对于Native的桥接工作也是做得非常出色,我们可以用JavaScript非常轻松的调用Native的函数和Objective-C类接口,所以也就可以写出很强大的脚本代码了。
但是有一个很大的局限性就是,Frida需要借助桌面平台的客户端发送JavaScript代码到目标App。如果是通过frida-gatget模块注入又需要修改目标App,所以不管用哪种方式要持久化的执行JavaScript,在官方提供的接口里面都没有现成的。针对这个缺陷,Textobot用TB插件的模式弥补了Frida没有持久化的问题。同时,为了方便编写实用的TB插件,Textobot还导出了一些辅助性的C API。这个在TBROOT/Template/textobot.js中可以看到它的用法,如下:
// ensure our module is loaded
const libtextobot = Module.load('/Library/MobileSubstrate/DynamicLibraries/textobot.dylib');
function TB_api(name, rettype, argstype) {
    return new NativeFunction(libtextobot.getExportByName(name), rettype, argstype);
}
​
// pickup textobot APIs
const textobot = {
    TB_logpath      : TB_api('TB_logpath', 'pointer', []),
    TB_logbuff      : TB_api('TB_logbuff', 'pointer', []),
    TB_logflush     : TB_api('TB_logflush', 'int', []),
    TB_dialog       : TB_api('TB_dialog','int', ['pointer']),
    TB_system       : TB_api('TB_system','int', ['pointer']),
    TB_root_system  : TB_api('TB_root_system', 'int', ['pointer']),
    TB_window       : TB_api('TB_window', 'pointer', []),
    TB_touch        : TB_api('TB_touch', 'int', ['int', 'int']),
    TB_swipe        : TB_api('TB_swipe', 'int', ['int', 'int', 'int', 'int']),
    // API likes TB_*_new should call TB_sptr_free to free the heap memory
    // see free function bellow
    TB_appdir_new   : TB_api('TB_appdir_new', 'pointer', []),
    TB_docdir_new   : TB_api('TB_docdir_new', 'pointer', []),
    TB_tmpdir_new   : TB_api('TB_tmpdir_new', 'pointer', []),
    TB_urlget_new   : TB_api('TB_urlget_new', 'pointer', ['pointer']),
    TB_urlpost_new  : TB_api('TB_urlpost_new', 'pointer', ['pointer', 'pointer']),
    TB_sptr_free    : TB_api('TB_sptr_free', 'void', ['pointer']),
    // ...
    // see https://gitee.com/geekneo/Textobot/blob/master/Doc/api.md for more APIs
};
​
// put the string to textobot's native buffer
function puts(s) {
    // logbuff's size is 4096
    if (s.length >= 4096) {
        console.log('Ignoring long string ' + s.length);
        return;
    }
    textobot.TB_logbuff().writeUtf8String(s);
    return textobot.TB_logbuff();
}
​
// wrap logger util
function logs(s) {
    puts(s);
    textobot.TB_logflush();
}
​
// wrap free util
function free(ptr) {
    var sptr = puts(ptr.toString(16));
    textobot.TB_sptr_free(sptr);
}
​
// slow logger, usually for global log
console.log("The textobot template plugin's log file is at " + 
    textobot.TB_logpath().readUtf8String());
​
// fast log, usually for hooker with better performance
logs('The textobot template plugin is running ...');
​
function demo_impl() {
    const appdir = textobot.TB_appdir_new();
    // show a dialog
    // the first line is TITLE
    // the left lines is CONTENT
    textobot.TB_dialog(puts(
        'Textobot Tips' +
        '\n' +
        'The textobot template plugin is running inside ' +
        appdir.readUtf8String()
    ));
​
    // API ends with _new should call free deallocate the heap memory
    free(appdir);
}

当前的Textobot是以独立模块的方式集成Frida,所以只能提供C API,然后通过Frida的NativeFunction桥接一下才可以使用。这样的模式使用起来稍微麻烦一些,直接扩展Frida的JavaScript运行时当然是最佳方案了,这个留着后面更新时再说了吧,我们现阶段的目标是稳定、够用。Frida的使用需要iOS SDK开发经验,门槛略高。关于它的详细使用方法请参考官方文档,在这里我们着重指出对于Textobot而言最核心的一点,Objective-C桥接。
const NSURL = ObjC.classes.NSURL;
Interceptor.attach(NSURL['- initWithString:'].implementation, {
    onEnter: function (args) {
        var inst = new ObjC.Object(args[2]);
        logs(inst.UTF8String() + '\ninitWithString\n' + backtrace(this.context) + '\n');
    }
});

我们通过ObjC.classes就可以导入Objective-C类到JavaScript,这个和Pyobjus的autoclass是一样的效果。然后就可以调用类的方法了,非常方便。


0x02.界面类API

int TB_dialog(const char *lines);
在运行的App内弹出一个提示框;
lines[0] = title:提示框标题;
lines[1...] = content:提示框内容;

0x03.按键类API

int TB_touch(int x, int y);
x:点击x坐标;
y:点击y坐标;
​
int TB_swipe(int x1, int y1, int x2, int y2);
x1,y1:滑动起始坐标;
x2,y2:滑动终止坐标;
Note:iOS版本小于10.0的系统需要安装TBROOT/iOS/Util/SimulateTouch-iOS8_9.deb插件;

0x04.工具类API

const char *TB_logpath();
获取日志文件路径;
​
char *TB_logbuff();
获取日志文件可写入的内存地址;
​
int TB_logflush();
如果对TB_logbuff返回的地址写入了日志字符串,使用此函数刷新至文件;
如果创建日志文件失败,则返回errno指向的值;
​
样例代码:
// put the string to textobot's native buffer
function puts(s) {
    // logbuff's size is 4096
    if (s.length >= 4096) {
        console.log('Ignoring long string ' + s.length);
        return;
    }
    textobot.TB_logbuff().writeUtf8String(s);
    return textobot.TB_logbuff();
}
​
// wrap logger util
function logs(s) {
    // 写入缓存
    puts(s);
    // 写入文件
    textobot.TB_logflush();
}
​
​
UIWindow *TB_window();
获取当前显示的UIWindow对象;
​
void TB_system(const char *cmds);
以当前App所在的用户组同步执行cmds命令;
​
void TB_root_system(const char *cmds);
以root用户异步执行cmds命令;
​
char *TB_appdir_new();
获取当前运行的App根目录,需要调用TB_sptr_free释放返回内存;
​
样例代码:
// wrap free util
function free(ptr) {
    // 将NativePointer对象转为字符串并写入内部缓存
    var sptr = puts(ptr.toString(16));
    // 释放字符串内容指向的内存
    textobot.TB_sptr_free(sptr);
}
​
const appdir = textobot.TB_appdir_new();
...
free(appdir);
​
​
char *TB_docdir_new();
获取当前运行的App文档目录,需要调用TB_sptr_free释放返回内存;
​
char *TB_tmpdir_new();
获取当前运行的App临时目录,需要调用TB_sptr_free释放返回内存;
​
char *TB_urlget_new(const char *url);
执行HTTP Get请求,需要调用TB_sptr_free释放返回内存;
​
char *TB_urlpost_new(const char *url, const char *body);
执行HTTP Post请求,需要调用TB_sptr_free释放返回内存;
​
void TB_sptr_free(const char *sptr);
释放字符串内容指向的内存地址,比如"0x188880000";

在这里我们单独封装了一组高性能的log函数,这个用于频繁写日志时使用,比如需要打印很多App运行时信息时。函数TB_logpath返回值也是对应到TB编辑器Log配置参数的,用于通过VSCode Textobot Editor - AppLog命令返回这一组log函数的输出。之所以单独提供这样的函数,是因为Frida自带的console.log需要通过IPC传递到调度器交给Python的print函数输出,对于频繁的输出,这个性能是不具有任何实用性的,巨慢无比。


0x05.基础篇完结

好了,到此为止,我们通过8篇文章把Textobot所有的基础内容完整的介绍了一遍。如果每一篇你都有看完,我想你应该就具备了通过Textobot编写一些实用TB插件的能力了。以下,我们总结一下Textobot到底为何物。
Textobot是一款基于文本的越狱插件框架,可扩展脚本基于Python实现。目标App动态修改框架,基于Frida JavaScript API实现。Textobot的设计目标是替换现有的Cydia deb插件系统。你可以仅仅使用Python+JavaScript就可以拥有deb/dylib插件的全部能力,并且维护、更新、分发十分容易,开发效率比Native也提高很多。
在Textobot插件的生命周期中,Python运行在系统级别,JavaScript运行在进程级别。代码编辑器使用VSCode,textobot-editor插件提供脚本代码编写、运行、打包等功能。在装有textobot-editor插件的VSCode中,可以直接编辑Python或者JS代码发送到手机端执行。
TBROOT/Sample/AppleIDLogin,这个用于演示如何使用Textobot编写自动化的插件。
TBROOT/Sample/SimpleGUI,这个用于演示如何使用Textobot编写GUI交互式的插件。
TBROOT/Sample/URLCapture,这个用于演示如何使用Textobot编写综合性的插件,同时使用Python系统模式和JavaScript进程模式。
TBROOT/Sample/Httpeek、TBROOT/Sample/Httpeek.src,这个用于演示如何使用Textobot编写更具现实意义的插件,同时使用Python系统模式、JavaScript进程模式、复用Native插件代码。

[培训]《安卓高级研修班(网课)》月薪三万计划,掌握调试、分析还原ollvm、vmp的方法,定制art虚拟机自动化脱壳的方法

最后于 2020-1-7 10:15 被GeekNeo编辑 ,原因:
收藏
免费 1
支持
分享
最新回复 (3)
雪    币: 211
活跃值: (511)
能力值: ( LV9,RANK:172 )
在线值:
发帖
回帖
粉丝
2
FBIDEMac 没运行起来, 码云上也提了

yedeMacBook-Pro-2:~ ye$ /Users/ye/work/source/FBIDEMac.git/Fridobot.app/Contents/MacOS/fridobot

Warning: translation file 'qt_zh_CN'could not be loaded.

Using default.

Warning: translation file 'eric6_zh_CN'could not be loaded.

Using default.

Warning: translation file 'qscintilla_zh_CN'could not be loaded.

Using default.

BackgroundService listening on: 58200

/Users/ye/work/source/FBIDEMac.git/Fridobot.app/Contents/MacOS/2.7/Resources/Python.app/Contents/MacOS/Python: can't open file '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/eric6/Utilities/BackgroundClient.py': [Errno 2] No such file or directory

An unhandled exception occurred. Please report the problem

using the error reporting dialog or via email to <eric-bugs@eric-ide.python-projects.org>.

A log has been written to "/Users/ye/.eric6/eric6_error.log".


Error information:

--------------------------------------------------------------------------------

2020-03-06, 17:55:54

--------------------------------------------------------------------------------

<class 'PluginManager.PluginExceptions.PluginPathError'>:

The internal plugin directory <b>/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/eric6/Plugins</b> does not exits.

--------------------------------------------------------------------------------

  File "/Users/ye/work/source/FBIDEMac.git/Fridobot.app/Contents/MacOS/2.7/lib/python2.7/site-packages/eric6/eric6.py", line 359, in <module>

    main()

  File "/Users/ye/work/source/FBIDEMac.git/Fridobot.app/Contents/MacOS/2.7/lib/python2.7/site-packages/eric6/eric6.py", line 340, in main

    restartArgs)

  File "/Users/ye/work/source/FBIDEMac.git/Fridobot.app/Contents/MacOS/2.7/lib/python2.7/site-packages/eric6/UI/UserInterface.py", line 226, in __init__

    self.pluginManager = PluginManager(self, develPlugin=plugin)

  File "/Users/ye/work/source/FBIDEMac.git/Fridobot.app/Contents/MacOS/2.7/lib/python2.7/site-packages/eric6/PluginManager/PluginManager.py", line 123, in __init__

    raise PluginPathError(msg)


--------------------------------------------------------------------------------

Version Numbers:

  Python 2.7.13

  Qt 4.8.7

  PyQt 4.11.4

  sip 4.18.1

  QScintilla 2.9.3

  WebKit 534.34

  eric6 6.1.8 (rev. a8a747768561)


Platform: darwin

2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 12:39:47)

[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]


yedeMacBook-Pro-2:~ ye$ ls -l /Library/Frameworks/Python.framework

ls: /Library/Frameworks/Python.framework: No such file or directory

2020-3-6 18:12
0
雪    币: 211
活跃值: (511)
能力值: ( LV9,RANK:172 )
在线值:
发帖
回帖
粉丝
3
.
最后于 2020-3-9 10:45 被vmtest编辑 ,原因:
2020-3-6 18:12
0
雪    币: 1662
活跃值: (3569)
能力值: ( LV7,RANK:100 )
在线值:
发帖
回帖
粉丝
4
vmtest FBIDEMac 没运行起来, 码云上也提了yedeMacBook-Pro-2:~ ye$ /Users/ye/work/source/FBIDEMac.git/Fridobot.app/Conten ...
修复了,你再试试。
2020-3-8 08:50
0
游客
登录 | 注册 方可回帖
返回
//