元始天尊 发表于 2021-7-21 23:26:34

越狱开发初级之Theos环境搭建

本帖最后由 元始天尊 于 2021-7-28 17:43 编辑



## 基本常识

* 为什么进行越狱开发,以及越狱开发能做什么?
  实现一些AppStore上的app无法实现的功能,在越狱机器上,可以精细化控制iOS系统和App做批量自动化操作,增删改系统功能等

* 什么是Theos以及Theos用来干什么?
  是目前最流行的跨平台iOS越狱开发工具包,包含越狱开发所有需要的sdk和工具链

* 我该怎样选择开发环境?
  为了达到最快的开发速度和生产速度,建议PC使用环境```MacOS + XCode + Theos + VSCode```,手机端使用```openssh + debugsever + frida ```进行相关开发,逆向,调试,跟踪,测试。本系列教程均基于该环境开发。为此你需要准备的编程技能至少包括```objective-c + c + python + javascript + logos```

* 推荐学习资源?
  论坛: iOSRE, 看雪, 52pojie, ....

## 常见的越狱软件/插件都有哪些文件类型?

* 资源文件
* Daemon, 可执行程序,为常驻于系统的守护进程,用于执行一些需要系统权限的操作或者实现底层服务
* Tool, 可执行程序,用于实现简单功能,比如lsof,wget
* Tweak, 动态库,随App启动自动注入,用作控制进程
* App, 这里开发的是Root权限执行的App,在桌面存在图标


## 准备工作

* 确保系统安装homebrew, xcode

* 安装Theos依赖

```
brew install ldid xz
```

* 下载TheOS

```
mkdir /opt # theos将下载到/opt/theos
git clone --recursive https://github.com/theos/theos.git /opt/theos
```

*** 小技巧: 如果git下载比较慢又不想用vpn,可以用gitee的'导入Github仓库'功能,借助gitee下载***

* 增加iOS私有函数库

```
curl -LO https://github.com/theos/sdks/archive/master.zip
TMP=$(mktemp -d)
unzip master.zip -d $TMP
mv $TMP/sdks-master/*.sdk /opt/theos/sdks
rm -r master.zip $TMP
```

## 简单实例

### 创建工程

执行`/opt/theos/bin/nic.pl`得到如下结果

```
NIC 2.0 - New Instance Creator
------------------------------
iphone/activator_event
iphone/activator_listener
iphone/application_modern
iphone/application_swift
iphone/control_center_module-11up
iphone/cydget
iphone/flipswitch_switch
iphone/framework
iphone/library
iphone/notification_center_widget
iphone/notification_center_widget-7up
iphone/preference_bundle_modern
iphone/theme
iphone/tool
iphone/tool_swift
iphone/tweak
iphone/tweak_with_simple_preferences
iphone/xpc_service
iphone/xpc_service_modern
Choose a Template (required):
```

  这里每行都是一种工程类型,其中用于生成iOS无界面的可执行程序,用于生成注入App的动态库。这两个是最常用的项目。如果打算开发完整越狱App,仍然要借助XCode。这里按如下操作,会产生helloworld目录:

```
Choose a Template (required): 16
Project Name (required): helloworld
Package Name : helloworld
Author/Maintainer Name : helloworld
MobileSubstrate Bundle filter : com.apple.springboard
List of applications to terminate upon installation (space-separated, '-' for none) : SpringBoard
```

### 配置手机环境

#### 安装tcprelay
  tcprelay将usb连接的手机端口映射到本地端口,这样登录手机将非常方便,同时传文件速度非常快。远程ssh登陆到手机,将会面临网络不稳定的问题,操作比较痛苦
* iPhone Cydia商店中安装openssh
* `brew install libusbmuxd`
* 从`https://gitee.com/lich0/jailbroken_programming_lesson/tree/master/tools`获取tcprelay.py和usbmux.py
* 执行`sudo python3 tcprelay.py -t 22:22` (注意因为端口号小于1024所以必须root权限执行)
*

期间可能遇到的问题:

* 如果首次连接手机,会如下提示,按yes即可

```
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
RSA key fingerprint is SHA256:z5N37rPCpeTKcIcm1tx3x3DnkTfeaESagZWuTYnmD3E.
Are you sure you want to continue connecting (yes/no/)?
```

* 如果对多台iPhone采用上述操作,那么会产生knownhost冲突,如下所示

```
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!   @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
SHA256:z5N37rPCpeTKcIcm1tx3x3DnkTfeaESagZWuTYnmD3E.
Please contact your system administrator.
Add correct host key in /Users/apple/.ssh/known_hosts to get rid of this message.
Offending RSA key in /Users/apple/.ssh/known_hosts:2
RSA host key for 127.0.0.1 has changed and you have requested strict checking.
Host key verification failed.
```

此时需要将设备公钥加入到本机,按如下方式操作:

```
ssh-keyscan 127.0.0.1 >> ~/.ssh/known_hosts
```

* 为避免每次登陆手机输入密码,按如下操作

```
ssh-copy-id root@127.0.0.1 # 密码默认为alpine
```

### 部署日志查看工具

```
git clone https://github.com/rpetrich/deviceconsole.git
```
使用XCode编译出deviceconsole并放到$PATH下,这样每次USB连接iPhone后执行deviceconsole就能看到日志(支持iOS12及以下系统)

### 开发hellowolrd

  我们的helloworld是在SpringBoard启动时做个弹窗。SpringBoard可以理解为桌面进程。效果为每次杀死SpringBoard进程时会在桌面弹窗。使用VSCode打开helloworld目录,可以看到存在4个文件:

* control 指定生成的deb的基本信息,包括包名,版本,安装依赖及作者信息
* helloworld.plist 用于指定自动注入的进程,这里可以看到指定为springboard
* Makefile
* Tweak.x 代码

工程在`https://gitee.com/lich0/jailbroken_programming_lesson/tree/master/junior1_helloworld`

执行`make package install`到手机后,得到最终效果如图

![最终效果](https://gitee.com/lich0/jailbroken_programming_lesson/raw/master/junior1_helloworld/screenshot.png)

唐凌 发表于 2021-7-22 16:43:24

你的图裂了

元始天尊 发表于 2021-7-22 19:49:27

tangptr@126.com 发表于 2021-7-22 16:43
你的图裂了

git的图,不用vpn就会显示不出,我还是迁移到gitee吧
页: [1]
查看完整版本: 越狱开发初级之Theos环境搭建