21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

" movl 0f( , %%ebx ), %%eax \n\t" \<br />

" jmp *%%eax \n" \<br />

"0: \n\t" \<br />

" .long 1f\n\t" \<br />

" .long 2f\n" \<br />

"1: \n" \<br />

: : "a" (val) : "%ebx");<br />

#define ELSE \<br />

asm(" jmp 3f\n\t" \<br />

"2: \n");<br />

#define ENDIF \<br />

asm("3: \n");<br />

The IF_ZERO macro places the value to be tested in the eax register, then uses the negl<br />

instruction to set the carry flag if the value in the eax register is nonzero. The carry<br />

flag is then rotated into a register and used as an index into a jump table. The macro<br />

can be used to test for equality by subtracting one value from another and passing it<br />

the result. The following example demonstrates how to use IF_ZERO to test the result<br />

of calloc( ). Note that the ELSE macro must be included even if an else condition is<br />

not needed.<br />

struct MY_STRUCT my_struct;<br />

my_struct = calloc(sizeof(struct MY_STRUCT), 1);<br />

IF_ZERO(my_struct)<br />

fprintf(stderr, "alloc failed\n");<br />

return 0;<br />

ELSE /* the else is required */<br />

ENDIF<br />

The C if statement itself is simple, and it is easy to recognize in a binary. For example:<br />

int value = check_input(user_input);<br />

if (value) {<br />

; /* success-handling code here */<br />

}<br />

This will usually be compiled as a test of value followed by a jnz instruction. Comparing<br />

value with a constant results in a jnz instruction following a compare of value<br />

with that constant. Changing the type of the value being tested from an integer to a<br />

floating-point number will change not only its representation in memory, but also<br />

the actual assembly-language comparison instruction:<br />

float value = check_input(user_input);<br />

if (value = = 1.0) {<br />

; /* success-handling code here */<br />

}<br />

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

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

Obfuscating Code | 661

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

Saved successfully!

Ooh no, something went wrong!