在Mac OS X上检测调试器

时间:2010-02-04 14:19:05

标签: c++ process macos debugging reverse-engineering

我正在尝试检测我的进程是否在调试器中运行,而在Windows中有许多解决方案,在Linux中我使用:

ptrace(PTRACE_ME,0,0,0) 

并检查其返回值,我无法在Mac OS X上执行相同的基本检查。 我试着用

ptrace(PT_TRACE_ME,0,0,0)

调用但总是即使在gdb下运行也会返回0。

如果我将请求更改为PT_DENY_ATTACH,它会正确停止调试,但这不是我想要实现的。有什么想法吗?

3 个答案:

答案 0 :(得分:6)

你可以从Apple Technical Q&A QA1361调用函数AmIBeingDebugged(),这里转载,因为Apple有时会破坏文档链接,并且很难找到旧的文档:

#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 );
}

答案 1 :(得分:4)

#include <mach/task.h>
#include <mach/mach_init.h>
#include <stdbool.h>

static bool amIAnInferior(void)
{
    mach_msg_type_number_t count = 0;
    exception_mask_t masks[EXC_TYPES_COUNT];
    mach_port_t ports[EXC_TYPES_COUNT];
    exception_behavior_t behaviors[EXC_TYPES_COUNT];
    thread_state_flavor_t flavors[EXC_TYPES_COUNT];

    exception_mask_t mask = EXC_MASK_ALL & ~(EXC_MASK_RESOURCE | EXC_MASK_GUARD);
    kern_return_t result = task_get_exception_ports(mach_task_self(), mask, masks, &count, ports, behaviors, flavors);
    if (result == KERN_SUCCESS)
    {
        for (mach_msg_type_number_t portIndex = 0; portIndex < count; portIndex++)
        {
            if (MACH_PORT_VALID(ports[portIndex]))
            {
                return true;
            }
        }
    }
    return false;
}

这会查看并查看我们的进程中是否存在活动异常处理程序(对于EXC_BREAKPOINT,EXC_BAD_ACCESS等)。 Ptrace不需要在调试器中实现这一点,因此在ptrace标志上仅依赖 来设置并不是很理想。

http://reverse.put.as/wp-content/uploads/2012/07/Secuinside-2012-Presentation.pdf

中提到了这种方法

我的blog post更详细地描述了这一点。

答案 2 :(得分:0)

这是Apple Technical Q&A QA1361中函数的Swift版本:

import Foundation

extension ProcessInfo {
    /// - returns: true if the process is being debugged, else false.
    public func isBeingDebugged() -> Bool {
        // https://developer.apple.com/library/archive/qa/qa1361/_index.html
        // Technical Q&A QA1361: Detecting the Debugger

        var mib: [Int32] = [
            CTL_KERN,
            KERN_PROC,
            KERN_PROC_PID,
            processIdentifier
        ]

        var info = kinfo_proc()
        var size: size_t = MemoryLayout.size(ofValue: info)

        let rc = sysctl(&mib, UInt32(mib.count), &info, &size, nil, 0)
        assert(rc == 0)

        return (info.kp_proc.p_flag & P_TRACED) != 0
    }
}

用法:

if ProcessInfo.processInfo.isBeingDebugged() {
     print("running under the debugger")
}