一、Logos 语法

  • Logos 作为 Theos 开发组件的一部分,通过一组特殊的预处理指令,可以让编写函数钩子(hook)代码变得非常简单和清晰,Logos 是随着 Theos 发布的。
  • %hook 指定需要 hook 的类名,以 %end 结尾。
  • %log 用来打印 log,将信息输入到 syslog 中,如 %log((NSString *)@“ZeluLi”)。
  • %orig 执行被 hook 函数的原始代码,类似于 super.method 功能。
  • %group 该指令用于 %hook 的分组,%group 后边跟的是组名,%group 也是必须以 %end 结尾,其中可以包含多个 %hook。
  • %init 该指令用来初始化某个 %group,一个 group 只有被初始化后才可生效,init 必须在 hook 中进行执行。
  • %ctor tweak 的构造器,用来初始化,如果不显式定义,Theos 就会自动生成一个%ctor,并在其中调用 %init(_ungrouped),如:%ctor { %init(_ungrouped)}。
  • %new 该指令用来给现有的 class 添加一个新的函数,与 Runtime 中的 class_addMethod 相同。
  • %c 该指令用来获取一个类的名称,相当于 objc_getClass。

二、工程目录下文件

  • 新建工程的命令:$THEOS/bin/nic.pl:
devzkndeMacBook-Pro:~ devzkn$ $THEOS/bin/nic.pl
NIC 2.0 - New Instance Creator
------------------------------[1.] iphone/activator_event[2.] iphone/application_modern[3.] iphone/cydget[4.] iphone/flipswitch_switch[5.] iphone/framework[6.] iphone/ios7_notification_center_widget[7.] iphone/library[8.] iphone/notification_center_widget[9.] iphone/preference_bundle_modern[10.] iphone/tool[11.] iphone/tweak[12.] iphone/xpc_service
Choose a Template (required): 11
Project Name (required): testTweakDemo
Package Name [com.yourcompany.testtweakdemo]:
Author/Maintainer Name [devzkn]:
[iphone/tweak] MobileSubstrate Bundle filter [com.apple.springboard]: com.apple.UIKit
[iphone/tweak] List of applications to terminate upon installation (space-separated, '-' for none) [SpringBoard]:
Instantiating iphone/tweak in testtweakdemo/...
Done.
  • 说明:
    • 参数 Package Name:deb包的名字;
    • 参数 Bundle filter:tweak 的目标 app 的 bundle identifier,比如 wechat 的 bundle Id :com.tentcent.xin;
    • iOS 系统的桌面应用的 bundle Id:com.apple.springboard tweak 注入到所有的 app,那么 Bundle filter 应该为 com.apple.UIKit。
  • 新建一个工程之后,会生成四个文件:control、plist、Makefile、Tweak.xm,这几个文件分别有什么用?
$ ls -lrt
total 32
-rw-r--r--  1 devzkn  staff    51 Aug 10 17:07 testTweakDemo.plist
-rw-r--r--  1 devzkn  staff   223 Aug 10 17:07 control
-rw-r--r--  1 devzkn  staff  1045 Aug 10 17:07 Tweak.xm
-rw-r--r--  1 devzkn  staff   189 Aug 10 17:07 Makefile

① control 文件

  • control 文件存储项目有关的信息:名称、版本、开发者:
Package: com.yourcompany.testtweakdemo
Name: testTweakDemo
Depends: mobilesubstrate
Version: 0.0.1
Architecture: iphoneos-arm
Description: An awesome MobileSubstrate tweak!
Maintainer: devzkn
Author: devzkn
Section: Tweaks

② plist文件

  • plist 文件的作用:设置目标 app 的 bundle Id,如果 tweak 要注入多个 APP,就在 Bundles 数组中添加其 bundle Id:
devzkndeMacBook-Pro:testtweakdemo devzkn$ more  testTweakDemo.plist
{ Filter = { Bundles = ( "com.apple.UIKit" ); }; }

③ Makefile 文件

  • Makefile 文件设置需要用到的文件、框架、库:
$ more Makefile
include $(THEOS)/makefiles/common.mkTWEAK_NAME = testTweakDemo
testTweakDemo_FILES = Tweak.xminclude $(THEOS_MAKE_PATH)/tweak.mkafter-install::install.exec "killall -9 SpringBoard"
devzkndeMacBook-Pro:testtweakdemo devzkn$
  • testTweakDemo_FILES 设置工程需要引用的文件,多个文件之间以空格分隔。格式:工程名FILES = 要编译的文件名:
testTweakDemo_FILES = Tweak.xm MyClass1.m MyClass2.m
  • include $(THEOS)/makefiles/common.mk,很多关键的变量都在 common.mk 中。
  • 工程名字_FRAMEWORKS : 设置工程需要引用的第三方库,多个库之间以空格分隔:
工程名字_FRAMEWORKS : 设置工程需要引用的第三方库,多个库之间以空格分隔:
  • after-install:在 tweak 安装之后,执行的一些辅助任务,比如杀掉目标进程,好让 CydiaSubstrate 在进程重启的过程加载对应的 dylib。

④ Tweak.xm

  • 用于编写注入的代码,其中的 %hook,%orig,%log 符合是 theos 对 cydia Substrate 提供的函数的宏封装:
IMP MSHookMessage(Class class, SEL selector, IMP replacement, const char* prefix); // prefix should be NULL.
void MSHookMessageEx(Class class, SEL selector, IMP replacement, IMP *result);
void MSHookFunction(void* function, void* replacement, void** p_original);
  • Tweak.xm 的文件名后缀 x 代表的含义:
    • Tweak.xm 的文件名后缀 x 代表支持 Logos 语法, 如果只有一个 x 代表源文件支持 Logos 和 C 语法;如果是 xm,说明源文件支持 Logos 和 C/C++ 语法;
    • 其中的 %hook,%orig,%log 等都是 Logos 语法支持的内容。
$ more  Tweak.xm
/* How to Hook with Logos
Hooks are written with syntax similar to that of an Objective-C @implementation.
You don't need to #include <substrate.h>, it will be done automatically, as will
the generation of a class list and an automatic constructor.%hook ClassName// Hooking a class method
+ (id)sharedInstance {return %orig;
}// Hooking an instance method with an argument.
- (void)messageName:(int)argument {%log; // Write a message about this call, including its class, name and arguments, to the system log.%orig; // Call through to the original function with its original arguments.%orig(nil); // Call through to the original function with a custom argument.// If you use %orig(), you MUST supply all arguments (except for self and _cmd, the automatically generated ones.)
}// Hooking an instance method with no arguments.
- (id)noArguments {%log;id awesome = %orig;[awesome doSomethingElse];return awesome;
}// Always make sure you clean up after yourself; Not doing so could have grave consequences!
%end
*/

⑤ 编译和安装deb

  • 使用命令 make package 编译,其中 packages 文件夹下保留的是每一次编译成功产生的 deb 安装包文件。theos 编译之后,会生成一个 deb 包,deb 安装到手机上面,有多种方式:
    • 通过 iFile 进行安装:使用 iTools 等工具将这个 deb 包拷贝到手机中,利用 iFile 浏览进行安装;
    • 通过 SSH 进行安装:
    • 需要使用到 openssh 服务,确保手机上已经安装了该服务(cydia 中搜索安装 OpenSSH),这种安装方式需要在 Makefile 文件的首行添加手机设备的 IP 地址 THEOS_DEVICE_IP = 10.200.201.22 (手机本地 IP);
    • 安装的时确保证 iOS 设备和 Mac 在同一局域网的同一网段中,打开终端,先 cd 到工程的目录下,然后输入编译安装命令 make package install;
    • 通过 cydia 源进行安装。

⑥ 免输密码进行 SSH

  • 使用 make package install 命令可以一次性完成编译、打包、安装,这个过程中可能需要两次输入 ssh 的 root 密码(一次是签名打包,一次是安装)。
  • 创建 rsa文件 : ssh-keygen -t rsa -b 2048 按提示输入存放 keygen 存放的目录和文件名。
  • 配置 ssh config:
# Private 192.168.2.125
Host iPhone
HostName  192.168.2.125
User root
IdentityFile ~/.ssh/id_rsa_Theos125
  • 添加对应的公钥到对应的远程服务器 :ssh-copy-i d千万不要把私钥泄漏,只是把公钥(*.pub 文件)复制给远程服务器,ssh-copy-id root@:
ssh-copy-id 指定认证文件$ ssh-copy-id -i id_rsa_Theos125 root@192.168.2.131
$ ssh-copy-id root@192.168.2.212
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/Users/devzkn/.ssh/id_rsa_Theos.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.2.212's password: Number of key(s) added:        1Now try logging into the machine, with:   "ssh 'root@192.168.2.212'"
and check to make sure that only the key(s) you wanted were added.

三、常见问题

① 压缩方式不被支持

  • 原因是 dpkg 有两个不同的版本,最新版本不支持 lzma:
$ make package
> Making all for tweak testTweakDemo…
==> Preprocessing Tweak.xm…
==> Compiling Tweak.xm (armv7)…
==> Linking tweak testTweakDemo (armv7)…
clang: warning: libstdc++ is deprecated; move to libc++ with a minimum deployment target of iOS 7 [-Wdeprecated]
==> Preprocessing Tweak.xm…
==> Compiling Tweak.xm (arm64)…
==> Linking tweak testTweakDemo (arm64)…
clang: warning: libstdc++ is deprecated; move to libc++ with a minimum deployment target of iOS 7 [-Wdeprecated]
==> Merging tweak testTweakDemo…
==> Signing testTweakDemo…
> Making stage for tweak testTweakDemo…
dpkg-deb: error: obsolete compression type 'lzma'; use xz insteadType dpkg-deb --help for help about manipulating *.deb files;
Type dpkg --help for help about installing and deinstalling packages.
make: *** [internal-package] Error 2
  • 解决方案:修改 lzma 为 xz:
$ find /opt/theos -type f -name "*.mk" | xargs grep "lzma"
/opt/theos/makefiles/package/deb.mk:_THEOS_PLATFORM_DPKG_DEB_COMPRESSION ?= lzma
  • 修改 /opt/theos/makefiles/package/deb.mk 第六行为_THEOS_PLATFORM_DPKG_DEB_COMPRESSION ?= xz。
  • 另外一个办法是直接使用旧版本,命令如下:
Debian 'dpkg' package management program version 1.18.24 (darwin-amd64)
brew switch dpkg 1.18.10
brew link dpkg
brew pin dpkg 1.18.10
  • 验证是否修复:
$ make package
> Making all for tweak testTweakDemo…
make[2]: Nothing to be done for `internal-library-compile'.
> Making stage for tweak testTweakDemo…
dpkg-deb: building package 'com.yourcompany.testtweakdemo' in './packages/com.yourcompany.testtweakdemo_0.0.1-2+debug_iphoneos-arm.deb'.
  • 另如果只修改为 XZ 的话,届时安装的时候仍会报错:
root@192.168.2.212's password:
Selecting previously unselected package com.yourcompany.testtweakdemo.
(Reading database ... 1848 files and directories currently installed.)
Preparing to unpack /tmp/_theos_install.deb ...
Unpacking com.yourcompany.testtweakdemo (0.0.1-8+debug) ...
dpkg-deb (subprocess): unable to execute decompressing archive member (xz): No such file or directory
dpkg-deb (subprocess): subprocess decompressing archive member returned error exit status 2
dpkg-deb: error: subprocess <decompress> returned error exit status 2
dpkg: error processing archive /tmp/_theos_install.deb (--install):subprocess dpkg-deb --fsys-tarfile returned error exit status 2
Errors were encountered while processing:/tmp/_theos_install.deb
make: *** [internal-install] Error 1
  • 如果在出现上面错误时是将 lzma->xz 的话,后面可能会出现此错误,解决方法是将 lzma->gzip 即可:
_THEOS_PLATFORM_DPKG_DEB_COMPRESSION ?=gzip
  • 验证是否成功:
$ make package install
> Making all for tweak testTweakDemo…
make[2]: Nothing to be done for `internal-library-compile'.
> Making stage for tweak testTweakDemo…
dpkg-deb: building package 'com.yourcompany.testtweakdemo' in './packages/com.yourcompany.testtweakdemo_0.0.1-9+debug_iphoneos-arm.deb'.
==> Installing…
root@192.168.2.212's password:
(Reading database ... 1848 files and directories currently installed.)
Preparing to unpack /tmp/_theos_install.deb ...
Unpacking com.yourcompany.testtweakdemo (0.0.1-9+debug) ...
Setting up com.yourcompany.testtweakdemo (0.0.1-9+debug) ...
install.exec "killall -9 SpringBoard"
root@192.168.2.212's password:

② THEOS_DEVICE_IP 设置错误

$ make package install
> Making all for tweak testTweakDemo…
make[2]: Nothing to be done for `internal-library-compile'.
> Making stage for tweak testTweakDemo…
dpkg-deb: building package 'com.yourcompany.testtweakdemo' in './packages/com.yourcompany.testtweakdemo_0.0.1-3+debug_iphoneos-arm.deb'.
==> Error: /Applications/Xcode.app/Contents/Developer/usr/bin/make install requires that you set THEOS_DEVICE_IP in your environment.
==> Notice: It is also recommended that you have public-key authentication set up for root over SSH, or you will be entering your password a lot.
  • 解决:THEOS_DEVICE_IP = 10.200.201.22 (手机本地 IP),首先得保证 iOS 设备和 Mac 在同一局域网的同一网段中,打开终端,输入 ssh root@192.168.xxx.xxx 输入 iOS 设备密码,默认 alpine:
$ ping 192.168.2.212
PING 192.168.2.212 (192.168.2.212): 56 data bytes
64 bytes from 192.168.2.212: icmp_seq=0 ttl=64 time=34.303 ms
64 bytes from 192.168.2.212: icmp_seq=1 ttl=64 time=28.949 ms
64 bytes from 192.168.2.212: icmp_seq=2 ttl=64 time=25.753 ms
64 bytes from 192.168.2.212: icmp_seq=3 ttl=64 time=5.306 ms
64 bytes from 192.168.2.212: icmp_seq=4 ttl=64 time=106.028 ms
64 bytes from 192.168.2.212: icmp_seq=5 ttl=64 time=84.643 ms
64 bytes from 192.168.2.212: icmp_seq=6 ttl=64 time=44.845 ms
^C
--- 192.168.2.212 ping statistics ---
7 packets transmitted, 7 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 5.306/47.118/106.028/32.913 ms
devzkndeMacBook-Pro:testtweakdemo devzkn$  ssh root@192.168.2.212
The authenticity of host '192.168.2.212 (192.168.2.212)' can't be established.
RSA key fingerprint is SHA256:/.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.2.212' (RSA) to the list of known hosts.
root@192.168.2.212's password:
Permission denied, please try again.
root@192.168.2.212's password:
iPhone:~ root#

iOS逆向之深入解析如何使用Theos开发插件相关推荐

  1. iOS逆向小知识:使用Theos开发插件

    文章目录 前言 I 工程目录文件介绍 1.1 control文件 1.2 plist文件 1.3 Makefile文件 1.4 Tweak.xm 1.5 编译和安装deb 1.6 免输密码进行SSH ...

  2. iOS逆向之深入解析App签名的双向验证机制和原理

    一.非对称加密 通常说的签名就是数字签名,它是基于非对称加密算法实现的. 对称加密是通过同一份密钥加密和解密数据,而非对称加密则有两份密钥,分别是公钥和私钥,用公钥加密的数据,要用私钥才能解密,用私钥 ...

  3. iOS逆向之深入解析如何Hook所有+load方法及Category的处理

    一.类方法 +load iOS 有四种方法可方便的在 premain 阶段执行代码: Objective C 类的 +load 方法: C++ static initializer: C/C++ at ...

  4. iOS逆向之深入解析MachO文件

    MachO文件简介 一.什么是MachO文件? Mach-O其实是Mach Object文件格式的缩写,它是Mac以及iOS上一种用于可执行文件.目标代码.动态库的文件格式,类似于Windows上面的 ...

  5. iOS逆向之深入解析如何计算+load方法的耗时

    一.类方法 +load 在 pre-main 时期,objc 会向 dyld 注册一个 init 回调: void _objc_init(void) {static bool initialized ...

  6. iOS逆向工程之Theos

    iOS逆向工程之Theos 如果你对iOS逆向工程有所了解,那么你对Tweak并不陌生.那么由Tweak我们又会引出Theos, 那么什么是Theos呢,简单一句话,Theos是一个越狱开发工具包,T ...

  7. 2020年 IOS 逆向 反编译 注入修改游戏或APP的调用参数新手系列教程——使用theos tweak 注入hook修改游戏执行代码上传动态头像

    2020年 IOS 逆向 反编译 注入修改游戏或APP的调用参数新手系列教程--使用theos tweak 注入hook修改游戏执行代码上传动态头像 开篇 需求&最终效果 环境要求与即将使用的 ...

  8. android ios 逆向工程,iOS逆向工程(七):使用Theos逆向项目

    使用Theos逆向项目 一.Theos是什么? Theos是一套跨平台的开发工具,用于在不使用Xcode的情况下开发.部署iOS插件,大多数插件开发人员都使用Theos. Theos工具套件包含一些重 ...

  9. IOS逆向笔记之HOOK实现(非越狱)

    HOOK是越狱的最终目标,目的是给应用添加功能如插件或者是更改应用的某个功能来满足我们的需求,如微信中添加抢红包插件.本文将以最近比较火的"快看"漫画为例子去除付费漫画中的收费弹窗 ...

最新文章

  1. 十个经典的学生上课插嘴ZT
  2. iOS中 支付宝钱包详解/第三方支付 韩俊强的博客
  3. VC++ 剪贴板编程
  4. 3.QT中的debug相关的函数,以及文件锁的使用
  5. ui设计中的版式设计_设计中的版式-第3部分
  6. 降低winnt Apache服务的权限,让你的虚拟主机更安全
  7. 流线动态图python_Node.js Stream(流)
  8. HashMap 和 Hashtable 的同和不同
  9. 一种新的人机交流方式——sound ware 声件
  10. 数据库事务的四大特性以及事务的隔离级别-与-Spring事务传播机制隔离级别
  11. 牛客(3)从尾到头打印链表
  12. atitit.研发管理--标准化流程总结---java开发环境与项目部署环境的搭建工具包总结...
  13. java-word模板导出
  14. 线粒体和叶绿体的基因组特点_线粒体和叶绿体基因组的组织及表达解析.ppt
  15. 一将成,万骨枯,趣店上市背后的残酷游戏
  16. 场景编程集锦 - BMI指数与健身达人
  17. 如何运行PHP文件 /创建PHP项目【基于VScode、XAMPP】超级详细,亲测有效,这一篇就够了
  18. Adobe_Acrobat_Pro_DC_2022.003.20314 下载安装
  19. 软考下午题第1题——数据流,题目分析与案例解析:
  20. 从 Go log 库到 Zap,怎么打造出好用又实用的 Logger

热门文章

  1. tomcat 相关以及安装时遇到的一些问题整理
  2. CString 操作函数
  3. 游戏设计、原型与开发:基于Unity与C#从构思到实现pdf
  4. HDU 4787 GRE Words Revenge
  5. 51nod 1277 KMP 前缀出现次数
  6. cocos2d_android 瞬间动作
  7. (原创).Net将EF运用于Oralce一 准备工作
  8. 58同城买二手电脑的感想
  9. hadoop搭建_hadoop分布式搭建之虚拟机克隆
  10. windows时间同步软件_有没有好用的windows时间管理软件?这款便签软件帮你