21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Discussion<br />

The spc_trap_detect( ) function is used to install a signal handler to catch trap signals<br />

sent to the target, then issue a trap signal. The SPC_DEBUGGER_PRESENT macro<br />

checks the num_traps counter managed by the trap signal handler; if the counter is<br />

zero, a debugger is capturing the trap signals and is not sending them to the process.<br />

#include <br />

#include <br />

#define SPC_DEBUGGER_PRESENT (num_traps = = 0)<br />

static int num_traps = 0;<br />

static void dbg_trap(int signo) {<br />

num_traps++;<br />

}<br />

int spc_trap_detect(void) {<br />

if (signal(SIGTRAP, dbg_trap) = = SIG_ERR) return 0;<br />

raise(SIGTRAP);<br />

return 1;<br />

}<br />

The following example demonstrates the use of spc_trap_detect( ) to initialize the<br />

debugger detection, and SPC_DEBUGGER_PRESENT to check for the presence of a debugger:<br />

int main(int argc, char *argv[ ]) {<br />

int x;<br />

spc_trap_detect( );<br />

for (x = 0; x < 10; x++) {<br />

if (SPC_DEBUGGER_PRESENT) printf("being debugged!\n");<br />

else printf("y\n");<br />

}<br />

return(0);<br />

}<br />

This detection method is not particularly effective because most Unix debuggers<br />

allow the trap signal to be sent through to the process; however, tools that automatically<br />

single step through their targets (to record system calls, data access, etc.) will be<br />

detected using this method.<br />

Most Unix debuggers are based on the ptrace system service, which is an interface to<br />

process control services in the kernel. ptrace-based debuggers were designed with<br />

source code debugging in mind, so they are incapable of dealing with hostile code.<br />

Detecting a ptrace debugger is simple, and the technique is well-known: ptrace prevents<br />

a process that is currently being traced from tracing itself or another process, so<br />

an attempt to ptrace another process will always fail if the current process is being<br />

traced. The following code demonstrates how to detect a ptrace-based debugger by<br />

creating a child process and attempting to attach to it.<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.<br />

Detecting Unix Debuggers | 683

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!