iOS逆向学习笔记之--Theos的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wj610671226/article/details/81209785

iOS逆向学习笔记之–Theos的基本使用

  • Theos安装
sudo git clone --recursive https://github.com/theos/theos.git /opt/theos
// 修改theos目录的所有者
sudo chown -R $(id -u):$(id -g)   /opt/theos
  • 配置ldid
    ldid是专门用来签名iOS可执行文件的工具,用在越狱环境中取代Xcode自带的codesign

方法一:
http://joedj.net/ldid下载ldid
然后拷贝到opt/theos/bin目录下面
赋予执行权限

sudo chmod 777 /opt/theos/bin/ldid

方法二:
brew install ldid fakeroot

  • theos的使用

创建tweak工程,编写xm文件分析目标APP类和方法
1、创建tweak工程
这里写图片描述

操作解释如下:

// 选择工程模板,这里选择iphone/tweak
  Choose a Template (required): 11   
  //工程名称
  Project Name (required): DingDingTweakProject  
  //deb包的名字(类似于bundle identifier)
  Package Name [com.yourcompany.myfirstreproject]: com.yifu.dingding 
  // tweak作者
  Author/Maintainer Name [System Administrator]: wj  
  // tweak作用应用的bundle identifier
  [iphone/tweak] MobileSubstrate Bundle filter [com.apple.springboard]: com.laiwang.DingTalk 
  // tweak安装完成后需要重启的应用
  [iphone/tweak] List of applications to terminate upon installation (space-separated, '-' for none) [SpringBoard]: DingTalk
  Instantiating iphone/tweak in myfirstreproject/...
  Done.

2、查看创建的工程文件开始编写对应文件
Makefile

// 工程包含的通用头文件
include $(THEOS)/makefiles/common.mk  
// Tweak工程名称
TWEAK_NAME = DingDingTweakProject
// tweak包含的源文件,指定多个文件时用空格隔开
DingDingTweakProject_FILES = Tweak.xm
// tweak工程的头文件,一般有application.mk、tweak.mk和tool.mk几类
include $(THEOS_MAKE_PATH)/tweak.mk
// tweak安装之后需要做的事情,杀掉原有进程
after-install::
    install.exec "killall -9 DingTalk"

补充:
// 编译选项debug和release
DEBUG = 0
// 越狱iPhone的IP地址 
THEOS_DEVICE_IP = 192.168.2.122
// 如果用usbmuxd转发ssh之后填localhost
THEOS_DEVICE_IP = localhost
THEOS_DEVICE_PORT = 2222

// 指定处理器架构
ARCHS = armv7 arm64
// 指定需要的SDK版本iphone:Base SDK:Deployment Target
TARGET = iphone:latest:8.0  // 最新的SDK,程序发布在iOS8.0以上
//导入框架,多个框架时用空格隔开
DingDingTweakProject_FRAMEWORKS = UIKit 
DingDingTweakProject_PRIVATE_FRAMEWORKS = AppSupport
// 链接libsqlite3.0.dylib、libz.dylib和dylib1.o
DingDingTweakProject_LDFLAGS = -lz –lsqlite3.0 –dylib1.o
//make clean
clean::
    rm -rf ./packages/*
    rm -rf ./obj/*

control文件
control文件记录了deb包管理系统所需的基本信息,会被打包进deb包里

Package: com.yifu.dingding
Name: DingDingTweakProject
Depends: mobilesubstrate
Version: 0.0.1
Architecture: iphoneos-arm
Description: An awesome MobileSubstrate tweak!
Maintainer: wj
Author: wj
Section: Tweaks

Tweak.xm文件
xm”中的“x”代表这个文件支持Logos语法,如果后缀名是单独一个“x”,说明源文件支持Logos和C语法;如果后缀名是“xm”,说明源文件支持Logos和C/C++语法。

%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

编译命令:
make 编译
make package 打包
make install 安装

3、Logos语法介绍
Logs语法 详细语法可参考:http://iphonedevwiki.net/index.php/Logos
%hook
指定需要hook的class,必须以%end结尾。可以被%group包含

%group
该指令用于将%hook分组,便于代码管理及按条件初始化分组,必须以%end结尾。
一个%group可以包含多个%hook,所有不属于某个自定义group的%hook会被隐式归类到%group_ungrouped中。

%new
在%hook内部使用,给一个现有class添加新函数,功能与class_addMethod相同。
注: Objective-C的category与class_addMethod的区别: 前者是静态的而后者是动态的。

%ctor
tweak的构造函数,完成初始化工作;如果不显示定义,Theos会自动生成一个%ctor,并在其中调用%init(_ungrouped)。

%dtor
tweak的构造函数,完成收尾。如果不显示定义,Theos会自动生成一个%dtor。

%init
该指令用于初始化某个%group,必须在%hook或%ctor内调用;如果带参数,则初始化指定的group,如果不带参数,则初始化_ungrouped.
注: 切记,只有调用了%ini,对应的%group才能起作用!

%ctor {
  if (kCFCoreFoundationVersionNumber > 1200) %init(iOS9);
   else %init(iOS8);
}

%c
该指令的作用等同于objc_getClass或NSClassFromString,即动态获取一个类的定义,在%hook或%ctor内使用 。

%hook SpringBoard
- (void)_menuButtonDown:(id)down
{
    %orig;
    SBScreenShotter *shotter = [%c(SBScreenShotter) sharedInstance];
    [shotter saveScreenshot:YES]; 
}
%end@

%log
该指令在%hook内部使用,将函数的类名、参数等信息写入syslog,可以%log([(),…..])的格式追加其他打印信息。

Cydia内搜索安装syslogd
查看日志
tail -f /var/log/syslog | grep DingTalk
清空日志
cat /dev/null > /var/log/syslog
iOS设备tail命令不存在的解决办法:在Cydia中安装Core Utilities

%orig
该指令在%hook内部使用,执行被hook的函数的原始代码;也可以用%orig更改原始函数的参数。

%orig(arg1, …)

4、借助theos模块logify.pl来批量生成需要分析目标类的Tweak.xm

/opt/theos/bin/logify.pl ./Headers/DTAccountPasswordViewController.h > ./dingdingtweakproject/Tweak.xm 

5、layout
编写Tweak时需要加入图片或者文件,然后在代码中使用它们,这需要使用layout将文件放到指定的路径中,layout目录相对于设备的根目录
例如:在layout中放了一张图片 layout/mv.png
dpkg会将layout中的文件拷贝到目标设备, 这里mv.png就在根目录/mv.png下

// 利用dpkg 命令查看deb包的目录结构 
dpkg -c packages/com.yifu.haluo_0.0.1-40+debug_iphoneos-arm.deb 
drwxr-xr-x root/wheel        0 2018-07-10 16:30 .
-rw-r--r-- root/wheel   176983 2018-07-04 17:09 ./mv.png
drwxr-xr-x root/wheel        0 2018-07-10 16:30 ./Library
drwxr-xr-x root/wheel        0 2018-07-10 16:30 ./Library/MobileSubstrate
drwxr-xr-x root/wheel        0 2018-07-10 16:30 ./Library/MobileSubstrate/DynamicLibraries
-rw-r--r-- root/wheel       56 2018-07-10 16:30 ./Library/MobileSubstrate/DynamicLibraries/HaLuoTweakProject.plist
-rwxr-xr-x root/wheel    67872 2018-07-10 16:30 ./Library/MobileSubstrate/DynamicLibraries/HaLuoTweakProject.dylib

在手机根目录中查找mv.png
iPhone:~ root# cd /
iPhone:/ root# ls -al
total 198
drwxrwxr-t 18 root     admin       986 Jul 12 10:49 ./
drwxrwxr-t 18 root     admin       986 Jul 12 10:49 ../
d-wx-wx-wt  2 _unknown _unknown     68 Aug 20  2014 .Trashes/
----------  1 root     admin         0 Aug  7  2014 .file
drwx------  2 _unknown _unknown    442 Jul 12 10:47 .fseventsd/
lrwxr-xr-x  1 root     admin        32 Jun 16 15:26 Applications -> /var/stash/_.o3wJLh/Applications/
drwxrwxr-t  8 root     admin       340 Aug 20  2014 Developer/
drwxrwxr-x  9 root     wheel       374 Oct 22  2014 DeveloperPatch/
drwxrwxr-x 18 root     admin       816 Jul  2 11:06 Library/
drwxr-xr-x  3 root     wheel       102 Dec 26  2007 System/
lrwxr-xr-x  1 root     admin        11 Jul 12 10:49 User -> /var/mobile/
drwxr-xr-x  2 root     wheel      2040 Jun 19 11:08 bin/
drwxr-xr-x  2 root     wheel        68 Oct 28  2006 boot/
drwxrwxr-t  2 root     admin        68 Aug  7  2014 cores/
dr-xr-xr-x  3 root     wheel      1095 Jul 12 10:47 dev/
lrwxr-xr-x  1 root     wheel        12 Jun 16 15:22 etc -> private/etc//
drwxr-xr-x  2 root     wheel        68 Oct 28  2006 lib/
drwxr-xr-x  2 root     wheel        68 Oct 28  2006 mnt/
-rw-r--r--  1 root     wheel    176983 Jul  4 17:09 mv.png

猜你喜欢

转载自blog.csdn.net/wj610671226/article/details/81209785