【iOS】iOS逆向系列六 - theos

要想修改应用的代码,并且稳定的运行(不受页面重启的影响),就需要编写代码并安装到可执行文件中。theos工具就可以帮我们做好这系列复杂的操作,我们只需要编写hook代码。

以喜马拉雅App为例,我们尝试播放音频的详情界面禁止广告弹出。

注意:仅供学习交流使用,禁止使用技术手段非法获益。

一、theos

1.1. 安装签名⼯具ldid

  • 先确保安装了brew
1
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  • 利用brew安装ldid
1
$ brew install ldid

1.2. 修改环境变量

  • 编辑⽤户的配置文件
1
$ vim ~/.bash_profile
  • .bash_profie文件后面加入以下2⾏
1
2
export THEOS=~/theos
export PATH=$THEOS/bin:$PATH
  • .bash_profiel配置的环境变量立即⽣效(或者重新打开终端)
1
$ source ~/.bash_profile

1.3. 下载theos

建议在$THEOS⽬录下载代码(也就是刚才配置的~/theos⽬录)。

1
$ git clone --recursive https://github.com/theos/theos.git $THEOS

1.4. 新建tweak项⽬

  • cd到一个存放项目代码的⽂件夹(⽐如桌面),然后执行nic.pl
1
2
$ cd ~/Desktop 
$ nic.pl
  • 选择[11.] iphone/tweak

  • 填写项目信息
    • Project Name
      • 项⽬名称
    • Package Name
      • 项⽬ID(随便写)
    • Author/Maintainer Name
      • 作者
      • 直接敲回车按照默认做法就行(默认是Mac上的用户名)
    • [iphone/tweak] MobileSubstrate Bundle filter
      • 需要修改的APP的Bundle Identifier(喜⻢拉雅FM的是com.gemd.iting)
      • 可以通过Cycript查看APP的Bundle Identifier
    • [iphone/tweak] List of applications to terminate upon installation
      • 直接敲回车按照默认做法就行
1
2
3
4
5
6
7
Project Name (required): ting_tweak
Package Name [com.yourcompany.ting_tweak]: com.daben.ting
Author/Maintainer Name [daben]:
[iphone/tweak] MobileSubstrate Bundle filter [com.apple.springboard]: com.gemd.iting
[iphone/tweak] List of applications to terminate upon installation (space- separated, '-' for none) [SpringBoard]:
Instantiating iphone/tweak in ting_tweak/...
Done.

1.5. 编辑Makefile

  • 在前⾯加⼊环境变量,写清楚通过哪个IP和端口访问⼿机
    • THEOS_DEVICE_IP
    • THEOS_DEVICE_PORT
1
2
3
4
5
6
7
8
9
10
11
12
export THEOS_DEVICE_IP=127.0.0.1 
export THEOS_DEVICE_PORT=10010

include $(THEOS)/makefiles/common.mk

TWEAK_NAME = ting_tweak
ting_tweak_FILES = Tweak.xm

include $(THEOS_MAKE_PATH)/tweak.mk

after-install::
install.exec "killall -9 SpringBoard"
  • 如果不希望每个项目的Makefile都编写IP和端⼝环境变量,也可以添加到用户配置⽂件中(编辑完毕后,$ source ~/.bash_profile让配置生效或者重启终端)
1
2
3
4
5
6
7
8
$ vim ~/.bash_profile

export THEOS=~/theos
export PATH=$THEOS/bin:$PATH
export THEOS_DEVICE_IP=127.0.0.1
export THEOS_DEVICE_PORT=10010

$ source ~/.bash_profile

1.6. 编写代码

通过Reveal查看图层才知道是显示广告的类是XMAdAnimationViewXMSoundPatchPosterView。再通过class-dump分析可执行文件的头文件,让对应类的创建方法返回nil,广告就不会显示了。

  • 打开Tweak.xm⽂件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
%hook XMAdAnimationView

- (id)initWithImageUrl:(id)arg1 title:(id)arg2 iconType:(long long)arg3 jumpType:(long long)arg4 {
return nil;
}

%end

%hook XMSoundPatchPosterView

- (id)initWithFrame:(struct CGRect)arg1 {
return nil;
}

%end

1.7. 编译/打包/安装

  • 编译
1
make
  • 打包成deb
1
make package
  • 安装(默认会⾃动重启SpringBoard)
1
make install

1.8. 可能遇到的问题

1.8.1. make package的错误

1
2
3
4
$ make package
Can't locate IO/Compress/Lzma.pm in @INC (you may need to install the IO::Compress::Lzma module) (@INC contains: /Library/Perl/5.18/darwin- thread-multi-2level /Library/Perl/5.18 /Network/Library/Perl/5.18/darwin- thread-multi-2level /Network/Library/Perl/5.18 /Library/Perl/Updates/5.18.2 /System/Library/Perl/5.18/darwin-thread-multi-2level /System/Library/Perl/5.18 /System/Library/Perl/Extras/5.18/darwin-thread- multi-2level /System/Library/Perl/Extras/5.18 .) at /Users/mj/theos/bin/dm.pl line 12.
BEGIN failed--compilation aborted at /Users/mj/theos/bin/dm.pl line 12.
make: *** [internal-package] Error 2

是因为打包压缩方式有问题,改成gzip压缩就⾏。

解决方法:

  • 修改dm.pl⽂件,用#号注释掉下⾯两句
1
2
3
4
$ vim $THEOS/vendor/dm.pl/dm.pl

#use IO::Compress::Lzma;
#use IO::Compress::Xz;
  • 修改deb.mk文件第6⾏的压缩方式为gzip
1
2
$ vim $THEOS/makefiles/package/deb.mk 
_THEOS_PLATFORM_DPKG_DEB_COMPRESSION ?= gzip

1.8.2. make的错误

错误一:

1
2
$ make
Error: You do not have an SDK in /Library/Developer/CommandLineTools/Platforms/iPhoneOS.platform/Developer/S DKs

是因为多个xcode导致(有可能安装了好⼏个Xcode),需要指定一下Xcode路径

解决方法:

1
$ sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer/

错误二:

1
2
3
$ make
> Making all for tweak xxx...
make[2]: Nothing to be done for `internal-library-compile'.

是因为之前已经编译过,有缓存导致的,clean⼀下即可。

解决方法:

1
2
$ make clean
$ make