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.

Solution<br />

Obfuscating compiled code requires understanding how the code will look at an<br />

assembly-language level. The purpose of obfuscating C code is to create maintainable<br />

source code that will run at close to the speed of the original, but that is difficult<br />

to understand when represented in assembly language. This difficulty may arise<br />

from an increase in the complexity of the algorithm, from an apparent increase in<br />

complexity, or from a misrepresentation of the constants, data types, and conditional<br />

expressions used in an algorithm.<br />

The examples presented in the discussion for this recipe represent only a handful of<br />

ways in which code can be obfuscated. More involved transformations include blurring<br />

the boundaries between functions by interleaving the code of two or more functions<br />

into a multipurpose function, using custom virtual machines or emulators to<br />

execute a byte-code representation of a function, and spawning new threads or processes<br />

to perform trivial or irrelevant tasks.<br />

Discussion<br />

Increased code obfuscation comes at the price of code maintainability.<br />

In general, it is preferable to combine several simple techniques<br />

along with data obfuscation than to dedicate development and debugging<br />

time to perfecting a single, advanced obfuscation technique.<br />

The most common idiom in C programs is “test-and-branch”: a value is tested, and<br />

the result of the test determines the next statement to be executed. The test-andbranch<br />

idiom is the underlying mechanism for conditional expressions (if, if-else,<br />

switch) and loops (for, while, do-while), and it is usually implemented in assembly<br />

language as:<br />

cmp value, constant<br />

jcc if_true_handler<br />

where jcc is a conditional branch determined by the type of test being performed.<br />

Table 12-1 lists the Intel conditional branch instructions and their corresponding C<br />

comparison operators.<br />

Table 12-1. Intel conditional branch instructions and their C comparison operators<br />

C operator Asm mnemonic Flags tested<br />

= = jz, je ZF = = 1<br />

!= jnz, jne ZF = = 0<br />

>= jge, jnlSF = = OF<br />

jae, jnb, jnc CF = = 0<br />

> jg, jnle ZF = = 0 && SF = = OF<br />

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

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

Obfuscating Code | 659

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

Saved successfully!

Ooh no, something went wrong!