8. 第一个逆向程序

创建tweak工程

➜  iOS /opt/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
//选择tweak工程
Choose a Template (required): 11

//工程名称
Project Name (required): MyFirstReProject

//deb包的名字(类似于bundle identifier)
Package Name [com.yourcompany.myfirstreproject]: com.iosre.myfirstreproject

//tweak作者
Author/Maintainer Name [System Administrator]: luz

//tweak作用对象的bundle identifier
[iphone/tweak] MobileSubstrate Bundle filter [com.apple.springboard]: com.apple.springboard

//tweak安装完成后需要重启的应用
[iphone/tweak] List of applications to terminate upon installation (space-separated, '-' for none) [SpringBoard]: SpringBoard
Instantiating iphone/tweak in myfirstreproject/...
Done.

工程文件结构

Makefile
//工程包含的通用头文件
include $(THEOS)/makefiles/common.mk

//创建工程时指定的“Project Name,指定好之后一般不要再更改
TWEAK_NAME = MyFirstReProject

//tweak包含的源文件,指定多个文件时用空格隔开
MyFirstReProject_FILES = Tweak.xm

//tweak工程的头文件,一般有application.mk、tweak.mk和tool.mk几类
include $(THEOS_MAKE_PATH)/tweak.mk

//指定tweak安装之后,需要做的事情,这里是杀掉SpringBoard进程
after-install::
install.exec "killall -9 SpringBoard"

补充:
//编译debug或者release
DEBUG = 0

//越狱iPhone的ip地址
THEOS_DEVICE_IP = 192.168.1.113

//指定支持的处理器架构
ARCHS = armv7 arm64

//指定需要的SDK版本iphone:Base SDK:Deployment Target
TARGET = iphone:latest:8.0  //最新的SDK,程序发布在iOS8.0以上

//导入框架,多个框架时用空格隔开
MyFirstReProject_FRAMEWORKS = UIKit
MyFirstReProject_PRIVATE_FRAMEWORKS = AppSupport

//链接libsqlite3.0.dylib、libz.dylib和dylib1.o
MyFirstReProject_LDFLAGS = -lz –lsqlite3.0 –dylib1.o

//make clean
clean::
rm -rf ./packages/*
tweak文件

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

/* 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
 */

参数说明

- %hook 指定需要hook的class,必须以%end结尾

- %log 该指令在%hook内部使用,将函数的类名、参数等信息写入syslog
Cydia内搜索安装syslogd

- %orig该指令在%hook内部使用,执行被钩住(hook)的函数的原始代码。
control

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

编译工程

tweakxm 文件
%hook SpringBoard
-  (void)applicationDidFinishLaunching:(id)application
{
    %orig;
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Hello,Tanzhou!"
                          message:nil
                          delegate:self cancelButtonTitle:@"OK"
                          otherButtonTitles:nil];
    [alert show];
}

- (void)_menuButtonDown:(id)down
{
    NSLog(@"x=%d, y=%d", 10, 20);
    %log((NSString *)@"iOSRE", (NSString *)@"Debug");
    %orig; // call the original _menuButtonDown:
}
%end

%hook SBLockScreenDateViewController
- (void)setCustomSubtitleText:(id)arg1 withColor:(id)arg2
{
    /*
     NSDate *date=[NSDate date];
     NSDateFormatter *format1=[[NSDateFormatter alloc]init];
     [format1 setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
     NSString *str1=[format1 stringFromDate:date];
     */
    struct tm *loctime;
    char timeBuf[1024] = {0};
    time_t now = time(NULL);
    loctime = localtime(&now);
    strftime(timeBuf, 30, "[%Y/%m/%d %H:%M:%S]", loctime);
    %orig([NSString stringWithUTF8String:timeBuf],arg2);
}
%end
MakeFile文件
DEBUG = 0
THEOS_DEVICE_IP = 10.171.4.22
ARCHS = armv7 arm64
TARGET = iphone:latest:8.0
include $(THEOS)/makefiles/common.mk

TWEAK_NAME = MyFirstReProject
MyFirstReProject_FILES = Tweak.xm
MyFirstReProject_FRAMEWORKS = UIKit
include $(THEOS_MAKE_PATH)/tweak.mk

after-install::
install.exec "killall -9 SpringBoard"
clean::
rm -rf ./packages/*
control文件
Package: com.iosre.myfirstreproject
Name: MyFirstReProject
Depends: mobilesubstrate
Version: 1.0.1
Architecture: iphoneos-arm
Description: My first reproject!
Maintainer: luz
Author: luz
Section: Tweaks
Homepage: https://www.baidu.com
编译命令
make  //编译

make package  //打包

make install  //安装
验证结果
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容