Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
marlkiller committed Sep 30, 2024
1 parent 40ffa1d commit 88c6ff9
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 110 deletions.
2 changes: 1 addition & 1 deletion dylib_dobby_hook/apps/PaddleBaseHack.m
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ - (BOOL)hack {
swizzledSelector:NSSelectorFromString(@"hook_dataTaskWithRequest:completionHandler:")
];

if ([[Constant currentAppName] containsString:@"mindmac"]) {
if ([[Constant getCurrentAppName] containsString:@"mindmac"]) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"Basic" forKey:@"licenseType"];
[defaults setObject:@YES forKey:@"licenseStatusChanged"];
Expand Down
51 changes: 26 additions & 25 deletions dylib_dobby_hook/dylib_dobby_hook.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@

@implementation dylib_dobby_hook

#ifdef DEBUG
const bool SHOW_ALARM = true;
#else
const bool SHOW_ALARM = false;
#endif

// INIT TEST START
int sum(int a, int b) {
return a+b;
Expand All @@ -43,9 +37,6 @@ void initTest(void){
// INIT TEST END





BOOL shouldExcludeCurrentApp(void) {
NSArray *excludedPrefixes = @[
@"/System/",
Expand All @@ -61,6 +52,28 @@ BOOL shouldExcludeCurrentApp(void) {
return NO;
}


BOOL canShowAlert(void) {
NSString *path = [[[NSProcessInfo processInfo] arguments] firstObject];

// 检查路径
if ([path hasPrefix:@"/System/Library/"] || [path hasPrefix:@"/usr/bin/"]) {
NSLog(@">>>>>> Path starts with /usr/bin/, UI alert not allowed.");
return NO;
}

if ([Constant isHelper]) {
NSLog(@">>>>>> Process is a helper, UI alert not allowed.");
return NO;
}
// NSApplicationActivationPolicyRegular: 普通应用,能在 Dock 中显示,接受用户输入。例如:Safari、Mail。
// NSApplicationActivationPolicyAccessory: 辅助应用,不在 Dock 中显示,但可以在菜单栏中显示图标。通常用于后台工具。
// NSApplicationActivationPolicyProhibited: 被禁止激活的应用,不显示在 Dock 中,也无法接受输入。常用于后台服务。
BOOL isForeground = [NSRunningApplication currentApplication].activationPolicy == NSApplicationActivationPolicyRegular;
NSLog(@">>>>>> Is current application canShowAlert: %@", isForeground ? @"YES" : @"NO");
return isForeground;
}

+ (void) load {


Expand All @@ -72,27 +85,15 @@ + (void) load {
return;
}

if ([Constant isFirstOpen] && ![Constant isHelper]) {

BOOL showAlarm = canShowAlert();
if ([Constant isFirstOpen] && showAlarm) {
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:@"Cracked By\n[marlkiller/dylib_dobby_hook]"];
[alert setInformativeText:@"仅供研究学习使用,请勿用于非法用途"];
[alert addButtonWithTitle:@"OK"];
[alert runModal];
}
if (SHOW_ALARM && ![Constant isHelper]) {
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:@"FBI warning"];
[alert setInformativeText:@"Please confirm if the app has been backed up.\nIf there are any issues, please restore it yourself!"];
[alert addButtonWithTitle:@"Confirm"];
[alert addButtonWithTitle:@"Cancel"];
NSInteger response = [alert runModal];
if (response == NSAlertFirstButtonReturn) {
[Constant doHack];
} else {
return;
}
}else {
[Constant doHack];
}
[Constant doHack];
}
@end
19 changes: 5 additions & 14 deletions dylib_dobby_hook/utils/Constant.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,13 @@

@interface Constant : NSObject

@property (class, nonatomic, strong) NSString *G_EMAIL_ADDRESS;
@property (class, nonatomic, strong) NSString *G_EMAIL_ADDRESS_FMT;
@property (class, nonatomic, strong) NSString *G_DYLIB_NAME;

@property (class, nonatomic, strong) NSString *currentAppPath;
@property (class, nonatomic, strong) NSString *currentAppName;
@property (class, nonatomic, strong) NSString *currentAppVersion;
@property (class, nonatomic, strong) NSString *currentAppCFBundleVersion;
@property (class, nonatomic, assign) BOOL arm;
@property (class, nonatomic, assign) BOOL helper;



+ (BOOL) isFirstOpen;
+ (NSString *)G_EMAIL_ADDRESS;
+ (NSString *)G_EMAIL_ADDRESS_FMT;
+ (NSString *)G_DYLIB_NAME;
+ (BOOL)isFirstOpen;
+ (BOOL)isArm;
+ (BOOL)isHelper;
+ (NSString *)getCurrentAppName;
+ (NSString *)getCurrentAppPath;
+ (NSString *)getCurrentAppVersion;
+ (NSString *)getCurrentAppCFBundleVersion;
Expand Down
126 changes: 62 additions & 64 deletions dylib_dobby_hook/utils/Constant.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,51 +17,49 @@
#import "HackProtocolDefault.h"
#import "HackHelperProtocolDefault.h"

@implementation Constant

// 使用构造函数属性 (constructor attribute) 的方法
// 这个方法会在 main 函数执行之前自动调用
static void __attribute__ ((constructor)) initialize(void){

NSLog(@">>>>>> Constant ((constructor)) initialize(void)");

printf(">>>>>> Constant ((constructor)) initialize(void)\n");
}

@implementation Constant

static NSString *G_EMAIL_ADDRESS = @"X'rq ol: [email protected]";;
static NSString *G_EMAIL_ADDRESS_FMT = @"[email protected]";;
static NSString *G_DYLIB_NAME = @"libdylib_dobby_hook.dylib";
static NSString *_G_EMAIL_ADDRESS = @"X'rq ol: [email protected]";;
static NSString *_G_EMAIL_ADDRESS_FMT = @"[email protected]";;
static NSString *_G_DYLIB_NAME = @"libdylib_dobby_hook.dylib";

static NSString *currentAppPath;
static NSString *currentAppName;
static NSString *currentAppVersion;
static NSString *currentAppCFBundleVersion;
static BOOL arm;
static BOOL helper;
static NSString *_currentAppPath;
static NSString *_currentAppName;
static NSString *_currentAppVersion;
static NSString *_currentAppCFBundleVersion;
static BOOL _arm;
static BOOL _helper;

// 告诉编译器不生成默认的 getter 和 setter 方法
@dynamic G_EMAIL_ADDRESS;
@dynamic G_EMAIL_ADDRESS_FMT;
@dynamic G_DYLIB_NAME;
@dynamic currentAppPath;
@dynamic currentAppName;
@dynamic currentAppVersion;
@dynamic currentAppCFBundleVersion;
@dynamic arm;
@dynamic helper;
//@dynamic G_EMAIL_ADDRESS;
//@dynamic G_EMAIL_ADDRESS_FMT;
//@dynamic G_DYLIB_NAME;
//@dynamic currentAppPath;
//@dynamic currentAppName;
//@dynamic currentAppVersion;
//@dynamic currentAppCFBundleVersion;
//@dynamic arm;
//@dynamic helper;

+ (NSString *)G_EMAIL_ADDRESS {
return love69(G_EMAIL_ADDRESS);
return love69(_G_EMAIL_ADDRESS);
}
+ (NSString *)G_EMAIL_ADDRESS_FMT {
return love69(G_EMAIL_ADDRESS_FMT);
return love69(_G_EMAIL_ADDRESS_FMT);
}
+ (NSString *)G_DYLIB_NAME {
return G_DYLIB_NAME;
return _G_DYLIB_NAME;
}

+ (NSString *)currentAppName {
return currentAppName;
+ (NSString *)getCurrentAppName {
return _currentAppName;
}

+ (BOOL) isFirstOpen {
Expand All @@ -86,10 +84,10 @@ + (void)initialize {
NSLog(@">>>>>> DobbyGetVersion: %s", DobbyGetVersion());

NSBundle *app = [NSBundle mainBundle];
currentAppName = [[app bundleIdentifier] copy];
currentAppVersion =[ [app objectForInfoDictionaryKey:@"CFBundleShortVersionString"] copy];
currentAppCFBundleVersion = [[app objectForInfoDictionaryKey:@"CFBundleVersion"] copy];
NSLog(@">>>>>> AppName is [%s],Version is [%s], myAppCFBundleVersion is [%s].", currentAppName.UTF8String, currentAppVersion.UTF8String, currentAppCFBundleVersion.UTF8String);
_currentAppName = [[app bundleIdentifier] copy];
_currentAppVersion =[ [app objectForInfoDictionaryKey:@"CFBundleShortVersionString"] copy];
_currentAppCFBundleVersion = [[app objectForInfoDictionaryKey:@"CFBundleVersion"] copy];
NSLog(@">>>>>> AppName is [%s],Version is [%s], myAppCFBundleVersion is [%s].", _currentAppName.UTF8String, _currentAppVersion.UTF8String, _currentAppCFBundleVersion.UTF8String);
NSLog(@">>>>>> App Architecture is: %@", [Constant getSystemArchitecture]);
NSLog(@">>>>>> App DebuggerAttached is: %d", [Constant isDebuggerAttached]);
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
Expand All @@ -99,16 +97,16 @@ + (void)initialize {
NSString *NSUserDefaultsPath = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"Preferences/%@.plist", bundleIdentifier]];
NSLog(@">>>>>> NSUserDefaultsPath is %@", NSUserDefaultsPath);
NSRange range = [[Constant getSystemArchitecture] rangeOfString:@"arm" options:NSCaseInsensitiveSearch];
arm = range.location != NSNotFound;
_arm = range.location != NSNotFound;

// 这里不用 copy 的话, clion cmake 编译的产物会内存泄漏,字符串对象乱飞...不知道为什么
// 返回包的完整路径。
currentAppPath = [[app bundlePath] copy];
NSLog(@">>>>>> [app bundlePath] %@",currentAppPath);
_currentAppPath = [[app bundlePath] copy];
NSLog(@">>>>>> [app bundlePath] %@",_currentAppPath);
// /Library/PrivilegedHelperTools
if ([currentAppPath isEqualToString:@"/Library/PrivilegedHelperTools"]) {
if ([_currentAppPath isEqualToString:@"/Library/PrivilegedHelperTools"]) {
NSLog(@">>>>>> helper is True");
helper = YES;
_helper = YES;
}

// 返回应用程序执行文件的路径。
Expand All @@ -121,23 +119,23 @@ + (void)initialize {
}

+ (BOOL)isHelper {
return helper;
return _helper;
}

+ (BOOL)isArm {
return arm;
return _arm;
}

+ (NSString *)getCurrentAppPath {
return currentAppPath;
return _currentAppPath;
}
+ (NSString *)getCurrentAppVersion {
return currentAppVersion;
return _currentAppVersion;
}
// currentAppVersion 有时会影响计算偏移位置,
// 所以 cache 偏移用这个 currentAppCFBundleVersion
+ (NSString *)getCurrentAppCFBundleVersion {
return currentAppCFBundleVersion;
return _currentAppCFBundleVersion;
}
+ (NSString *)getSystemArchitecture {
size_t size;
Expand Down Expand Up @@ -208,31 +206,31 @@ + (BOOL)isSubclassOfClass:(Class)class parentClass:(Class)parentClass {
}

+ (void)doHack {
NSArray<Class> *personClasses = [Constant getAllHackClasses];

NSLog(@">>>>>> Constant: Initiating doHack operation...");
for (Class class in personClasses) {
NSLog(@">>>>>> Constant: Processing class - %@", NSStringFromClass(class));
id<HackProtocol> it = [[class alloc] init];
if ([it shouldInject:currentAppName]) {
NSString *supportAppVersion = [it getSupportAppVersion];
if (supportAppVersion==NULL ||
supportAppVersion.length==0 ||
currentAppVersion==NULL ||
[currentAppVersion hasPrefix:supportAppVersion] ) {
[it hack];
}else{
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:@"OK"];
alert.messageText = [NSString stringWithFormat:@"Unsupported current appVersion !!\nSuppert appVersion: [%s]\nCurrent appVersion: [%s]",[it getSupportAppVersion].UTF8String, currentAppVersion.UTF8String];;
[alert runModal];

@try {
NSArray<Class> *personClasses = [Constant getAllHackClasses];
NSLog(@">>>>>> Constant: Initiating doHack operation...");
for (Class class in personClasses) {
NSLog(@">>>>>> Constant: Processing class - %@", NSStringFromClass(class));
id<HackProtocol> it = [[class alloc] init];
if ([it shouldInject:_currentAppName]) {
NSString *supportAppVersion = [it getSupportAppVersion];
if (supportAppVersion==NULL ||
supportAppVersion.length==0 ||
_currentAppVersion==NULL ||
(_currentAppVersion!=NULL && [_currentAppVersion hasPrefix:supportAppVersion]) ) {
[it hack];
return;

}else{
NSLog(@">>>>>> [ERROR] Unsupported current appVersion !! Suppert appVersion: [%@] Current appVersion: [%@]",
[it getSupportAppVersion], _currentAppVersion);
}
}
return;
}
NSLog(@">>>>>>> [ERROR] Unsupported current app: [%@]",_currentAppName);
} @catch (NSException *exception) {
NSLog(@">>>>>> [Caught exception]: %@", exception);
}
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:@"OK"];
alert.messageText = [NSString stringWithFormat:@"Unsupported current app: [%s]", currentAppName.UTF8String];;
[alert runModal];
}
@end
13 changes: 7 additions & 6 deletions dylib_dobby_hook/utils/common_ret.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,28 @@
#include <sys/ptrace.h>
#import <sys/sysctl.h>
#include <mach/mach_types.h>
#include <mach/i386/thread_status.h>


int ret2 (void){
printf(">>>>>> ret2\n");
return 2;
}
int ret1 (void){
// uint8_t ret1Hex[6] = {0xB8, 0x01, 0x00, 0x00, 0x00, 0xC3}; // mov eax, 1; ret
// uint8_t ret1HexARM[8] = {0x20, 0x00, 0x80, 0xD2, 0xC0, 0x03, 0x5F, 0xD6}; // mov x0, #1; ret
printf(">>>>>> ret1\n");
return 1;
}
int ret0 (void){
// uint8_t ret0Hex[3] = {0x31, 0xC0, 0xC3}; // xor eax, eax; ret
// uint8_t ret0HexARM[8] = {0x00, 0x00, 0x80, 0xD2, 0xC0, 0x03, 0x5F, 0xD6}; // mov x0, #0; ret
printf(">>>>>> ret0\n");
return 0;
}

void ret(void){
// uint8_t retHex[1] = {0xC3}; // ret
// uint8_t retHexARM[4] = {0xC0, 0x03, 0x5F, 0xD6}; // ret
printf(">>>>>> ret\n");
}

Expand Down Expand Up @@ -151,9 +157,4 @@ OSStatus hk_SecCodeCheckValidityWithErrors(SecCodeRef code, SecCSFlags flags, Se
return output;
}
//char *global_dylib_name = "libdylib_dobby_hook.dylib";
// 注入函数

int inject_dylib(pid_t pid, const char *lib) {
// Ref: https://juejin.cn/post/7277787934863835171
return 0;
}
Binary file added tools/process_inject
Binary file not shown.

0 comments on commit 88c6ff9

Please sign in to comment.