コンテンツにスキップ

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 をチェックすることで、アプリケーションがデバッガー経由で起動されたかどうかを検出できます。

リンク