首先,让我说我明白使用 PT_DENY_ATTACH 的方法如下:
Bugging Debuggers很没用。
但是,为了理解iOS的工作原理,我还是想知道:在Swift项目中是否可以做类似的事情?由于 Objective-C 是基于 C 构建的,因此可以利用 main(int argc, char *argv[])
函数来防止 gdb 附加到进程。
这在 Swift 中如何实现?我主要是想了解 Swift 中的应用程序生命周期,但是,我能找到的大多数解释都是针对 ObjC 的。
最佳答案
感谢用户ahruss这是非常有用的链接,这是我找到的解决方案:
我使用了 this question 中引用的方法创建一个 main.swift 文件。然后我创建了一个包含此方法定义的 c 文件(和 header ):
typedef int (*command_ptr_t)(int _request, pid_t _pid, caddr_t _addr, int _data);
#if !defined(PT_DENY_ATTACH)
#define PT_DENY_ATTACH 31
#endif
//Anti-debug method
void disable_attach() {
void* handle = dlopen(0, RTLD_GLOBAL | RTLD_NOW);
command_ptr_t command_ptr = dlsym(handle, "ptrace");
command_ptr(PT_DENY_ATTACH, 0, 0, 0);
dlclose(handle);
}
我在我的桥接头文件中添加了 disableAttach.h 头文件,然后直接在我的 UIApplicationMain(Process.argc, Process.unsafeArgv, nil, NSStringFromClass(AppDelegate) 上面调用了
在 main.swift 中调用。disable_attach()
)
你最终应该得到一个与此类似的 main.swift 文件:
import Foundation
import UIKit
disable_attach()
UIApplicationMain(
CommandLine.argc,
UnsafeMutableRawPointer(CommandLine.unsafeArgv)
.bindMemory(
to: UnsafeMutablePointer<Int8>.self,
capacity: Int(CommandLine.argc)),
nil,
NSStringFromClass(AppDelegate.self)
)
正如我之前在评论中所述,生命周期似乎是相同的,但是 @UIApplicationMain
指令隐藏了 main 文件本身。
更新:从 Xcode 10\iOS 12 开始,main.swift 文件应该如下所示:
UIApplicationMain(
CommandLine.argc, CommandLine.unsafeArgv,
nil, NSStringFromClass(AppDelegate.self)
)
关于Swift 中的 iOS 反盗版,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29826028/