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.

Most disassemblers can be fooled by a simple misalignment error—for example,<br />

jumping into the middle of an instruction so that the target of the jump is disassembled<br />

incorrectly. The typical technique of performing an unconditional jump into<br />

another instruction is not very effective with disassemblers that follow the flow of<br />

execution—the jump will be followed, and the bytes between the jump and the jump<br />

target will be ignored. Instead, you can use a conditional jump, followed by the first<br />

byte of a multibyte instruction (0x0F is ideal for this, because it is the first byte of all<br />

two-byte opcodes); this way, a flow-of-execution disassembler will disassemble the<br />

code after the conditional branch.<br />

#define DISASM_MISALIGN asm volatile ( \<br />

" pushl %eax \n" \<br />

" cmpl %eax, %eax \n" \<br />

" jz 0f \n" \<br />

" .byte 0x0F \n" \<br />

"0: \n" \<br />

" popl %eax \n")<br />

This macro compares the eax register to itself, forcing a true condition; the jz<br />

instruction is therefore always followed during execution. A disassembler will either<br />

ignore the jz instruction and interpret the 0x0F byte that follows as an instruction, or<br />

it will follow the jz instruction. If the jz instruction is followed, the disassembler can<br />

still interpret the code incorrectly if the address after the jz instruction is disassembled<br />

before the address to which the jz instruction jumps. For example:<br />

void my_func(void) {<br />

int x;<br />

DISASM_MISALIGN;<br />

for (x = 0; x < 10; x++) printf("%x\n", x);<br />

}<br />

IDA Pro disassembles the code after the jz instruction at address 0804837D before<br />

following the jump itself, resulting in an incorrect disassembly:<br />

08048374 my_func:<br />

08048374 55 push ebp<br />

08048375 89 E5 mov ebp, esp<br />

08048377 83 EC 08 sub esp, 8<br />

0804837A 50 push eax<br />

0804837B 39 C0 cmp eax, eax<br />

0804837D 74 01 jz short near ptr loc_804837F+1<br />

0804837F<br />

0804837F loc_804837F: ; CODE XREF: .text:0804837D#j<br />

0804837F 0F 58 C7 addps xmm0, xmm7<br />

08048382 45 inc ebp<br />

08048383 FC cld<br />

08048383 ; --------------------------------------------------------------------<br />

08048384 00 db 0 ;<br />

08048385 00 db 0 ;<br />

08048386 00 db 0 ;<br />

08048387 00 db 0 ;<br />

690 | Chapter 12: Anti-Tampering<br />

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

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

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

Saved successfully!

Ooh no, something went wrong!