Application implements anti-debug techniques
应用程序已实现反调试技术
描述
调试和动态探索应用程序是逆向工程中的常见技术。例如,逆向工程师可以跟踪应用程序的执行、窥探敏感数据并修改内存以改变其行为。
为了增加动态分析应用程序的难度,应用程序应当实现反调试技术。
建议
要实现反调试技术,请结合使用以下几种技术:
ptrace:使用带有 PT_DENY_ATTACH 标志的 ptrace 系统调用,防止调试器附加到该进程。该系统调用不属于 iOS 公开 API,需要使用 dlsym 函数获取函数指针才能调用它。
#import <dlfcn.h>
#import <sys/types.h>
#import <stdio.h>
typedef int (*ptrace_ptr_t)(int _request, pid_t _pid, caddr_t _addr, int _data);
void anti_debug() {
ptrace_ptr_t ptrace_ptr = (ptrace_ptr_t)dlsym(RTLD_SELF, "ptrace");
ptrace_ptr(31, 0, 0, 0); // PTRACE_DENY_ATTACH = 31
}
sysctl:此函数可用于检索有关当前进程的信息,包括确定应用程序是否正在被调试。
#include <assert.h>
#include <stdbool.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/sysctl.h>
static bool AmIBeingDebugged(void)
// Returns true if the current process is being debugged (either
// running under the debugger or has a debugger attached post facto).
{
int junk;
int mib[4];
struct kinfo_proc info;
size_t size;
// Initialize the flags so that, if sysctl fails for some bizarre
// reason, we get a predictable result.
info.kp_proc.p_flag = 0;
// Initialize mib, which tells sysctl the info we want, in this case
// we're looking for information about a specific process ID.
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PID;
mib[3] = getpid();
// Call sysctl.
size = sizeof(info);
junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0);
assert(junk == 0);
// We're being debugged if the P_TRACED flag is set.
return ( (info.kp_proc.p_flag & P_TRACED) != 0 );
}
getppid:iOS 应用程序可以检查父进程的 PID 以检测应用程序是否通过调试器启动。