一、Tweak原理分析
1、获取APPID,com.tencent.xin

2、theos创建


3、cd进来,
make
编译一下,生成隐藏文件.theos
,最后是把theosDemo.dylib
注入到手机
4、
make package
一下,生成packages
文件。make package
打包,打包成deb
的包。安装的时候其实就是安装的这个.deb
的包
5、
make install
注入。微信重启了,Cydia
中出现theosDemo


6、打开
iFunBox
,在Device/Library/MobileSubstrate/DynamicLibraries
中会出现theosDemo.dylib
和theosDemo.plist
注意点:正常情况下这里是会出现的
theosDemo.dylib
和theosDemo.plist
,但是我这里用theos它没有出现,用Monkey写Tweak,他就出现了。这是为什么了? 待解决中
7、打开Xcode,可以看到打印

8、把手机上的MachO拷贝出来,用
MachOView
打开,Load Commands
中并没有theosDemo
。所以咋们的越狱插件是通过DYLD_INSERT_LIBRARIES
环境变量插入的。
两种方式
1、Framework注入、修改了MachO
2、DYLD_INSERT_LIBRARIES
环境变量插入,MachO没有改变


二、DYLD源码分析找到防护突破口

搜索
DYLD_INSERT_LIBRARIES

command+shift+j

来到5693行;
pruneEnvironmentVariables(envp, &apple);
移除环境变量; gLinkContext.processIsRestricted
保证这个为真,就可以移除环境变量
processIsRestricted
搜索一下,看这个属性是在那里赋值的。processIsRestricted = true

搜索一下
hasRestrictedSegment
。所以如果MachO中segname
有__RESTRICT
和sectname
有__restrict
,则hasRestrictedSegment(mainExecutableMH)
为ture,则gLinkContext.processIsRestricted
为true,则环境变量移除,则不会插入动态库
三、修改RESTRICT段防护Tweak
1、创建一个普通的工程

2、
theos
创建一个Tweak
3、把Tweak拖入
Sublime Text
。编写logos,然后保存一下
4、
theos
执行make
和make package;make install
。应用重新启动了
5、
command+shift+2
、Open Console
注意:正常情况下这里是会打印
不好意思我把你干掉了
,但是我这里用theos它没有出现,用Monkey写Tweak,它就打印了。这是为什么了? 待解决中(同上面的问题)
下面的是用Monkey写的Tweak。成功了。

6、
Other Linker Flags
添加-Wl,-sectcreate,__RESTRICT,__restrict,/dev/null
。然后编译一下
查看MachO,如图所示,生成了
Section64(__RESTRICT,__restrict)

7、
command+R
运行项目。打印点击了屏幕
,防住了。
8、把App拷贝下来,查看MachO。


9、修改二进制,破坏防护

保存,退出MachOView,在此打开MachOView

10、再把MachO拷贝回手机,
scp -P 12345 -r ~/Desktop/antiTweak.app root@localhost:/var/mobile/Containers/Bundle/Application/07DDF178-A970-4227-A3B4-644944E25E9F/
注意:此时的app启动不了,因为破坏了签名

11、重签名。创建一个MonkeyApp项目,项目名随便取,
Bundle Identifier
也随便取,我这里取了一个和原工程一样的。然后重新运行MonkeyApp项目。


打印
不好意思我把你干掉了
,说明成功了。
四、参考dyld源码防护
1、dyld源码


2、原项目按如下方式修改。并且
Other Linker Flags
中也添加了-Wl,-sectcreate,__RESTRICT,__restrict,/dev/null
。
#import "ViewController.h"
#import <mach-o/loader.h>
#import <mach-o/dyld.h>
#if __LP64__
#define LC_SEGMENT_COMMAND LC_SEGMENT_64
#define LC_SEGMENT_COMMAND_WRONG LC_SEGMENT
#define LC_ENCRYPT_COMMAND LC_ENCRYPTION_INFO
#define macho_segment_command segment_command_64
#define macho_section section_64
#define macho_header mach_header_64
#else
#define LC_SEGMENT_COMMAND LC_SEGMENT
#define LC_SEGMENT_COMMAND_WRONG LC_SEGMENT_64
#define LC_ENCRYPT_COMMAND LC_ENCRYPTION_INFO_64
#define macho_segment_command segment_command
#define macho_section section
#define macho_header mach_header
#endif
@interface ViewController ()
@end
@implementation ViewController
+ (void)load {
//根据imagelist可以看出,imagelist 里面第0个是我们自己的可执行文件
const struct mach_header * header = _dyld_get_image_header(0);
if (hasRestrictedSegment(header)) {
NSLog(@"没毛病");
} else {
NSLog(@"请删除插件!!!");
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"提示" message:@"点击了屏幕" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[ac addAction:action];
[self presentViewController:ac animated:true completion:nil];
NSLog(@"点击了屏幕");
}
static bool hasRestrictedSegment(const struct macho_header* mh)
{
const uint32_t cmd_count = mh->ncmds;
const struct load_command* const cmds = (struct load_command*)(((char*)mh)+sizeof(struct macho_header));
const struct load_command* cmd = cmds;
for (uint32_t i = 0; i < cmd_count; ++i) {
switch (cmd->cmd) {
case LC_SEGMENT_COMMAND:
{
const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
//dyld::log("seg name: %s\n", seg->segname);
if (strcmp(seg->segname, "__RESTRICT") == 0) {
const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
const struct macho_section* const sectionsEnd = §ionsStart[seg->nsects];
for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
if (strcmp(sect->sectname, "__restrict") == 0)
return true;
}
}
}
break;
}
cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
}
return false;
}
@end

3、修改二进制,用MachOView修改,方法如上。
4、重签名。使用MonkeyApp重签名。方法如上。打印请删除插件!!!
,说明又防住了,✌️。

网友评论