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.

Because library functions are loaded into a predictable range of memory, the upper<br />

half of a library function’s address can be used as a runtime constant. In the previous<br />

code, the second half of the logical AND operation always evaluates to true.<br />

Most programs link to shared libraries using dynamic linking resolved by the program<br />

loader, which creates references to the shared library functions at the point<br />

where they are called. To make compiled code more difficult to understand, shared<br />

library functions should be referenced as far away as possible from the calls to<br />

them—if not replaced entirely with custom code. By explicitly loading a library with<br />

functions like dlopen( ) on Unix or LoadLibrary( ) on Windows, you can refer only to<br />

the function pointers where the function is called. The function pointers can be reused<br />

during the course of execution so that different library functions are stored in<br />

the same function pointer. Alternatively, a function can be used to return the function<br />

pointer from a list or table of such pointers, thereby frustrating automatic analysis:<br />

#ifdef WIN32<br />

#include <br />

#define SPC_C_RUNTIME "msvcrt.dll"<br />

#define SPC_LIBRARY_TYPE_HMODULE<br />

#define SPC_LOAD_LIBRARY(name) LoadLibrary((name))<br />

#define SPC_RESOLVE_SYM(lib, name) GetProcAddress((lib), (name))<br />

#else<br />

#include <br />

#define SPC_C_RUNTIME "libc.so"<br />

#define SPC_LIBRARY_TYPE void *<br />

#define SPC_LOAD_LIBRARY(name) dlopen((name), RTLD_LAZY);<br />

#define SPC_RESOLVE_SYM(lib, name) dlsym((lib), (name))<br />

#endif<br />

enum file_op_enum {<br />

fileop_open, fileop_close, fileop_read, fileop_write, fileop_seek<br />

};<br />

void *file_op(enum file_op_enum op) {<br />

static SPC_LIBRARY_TYPE lib = 0;<br />

static struct FILEOP {<br />

void *open, *close, *read, *write, *seek;<br />

} s = {0};<br />

if (!lib) lib = SPC_LOAD_LIBRARY(SPC_C_RUNTIME);<br />

switch (op) {<br />

case fileop_open:<br />

if (!s.open) s.open = SPC_RESOLVE_SYM(lib, "open");<br />

return s.open;<br />

case fileop_close:<br />

if (!s.close) s.close = SPC_RESOLVE_SYM(lib, "close");<br />

return s.close;<br />

case fileop_read:<br />

if (!s.read) s.read = SPC_RESOLVE_SYM(lib, "read");<br />

return s.read;<br />

case fileop_write:<br />

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

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

Obfuscating Code | 663

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

Saved successfully!

Ooh no, something went wrong!