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.

need to use one of the three ring3 serializing instructions (cpuid, iret, and rsm) to<br />

invalidate the cache. This can be performed with a macro:<br />

#define INVALIDATE_CACHE asm volatile( \<br />

"pushad \n" \<br />

"cpuid \n" \<br />

"popad \n")<br />

The pushad and popad instructions are needed because the cpuid instruction overwrites<br />

the four general-purpose registers. Once again, as with the call to mprotect( ),<br />

note that the use of the cpuid instruction is suspicious and will draw attention to the<br />

code of the protection. It is better to place the call to the decrypted code far enough<br />

away (16 bytes should be sufficient, because only 486 and Pentium CPUs will be<br />

affected) from the actual decryption routine so that the decrypted code will not be in<br />

the instruction cache.<br />

This implementation of self-decrypting code is a simple one; it could be defeated by<br />

pulling the decryption code from the binary, decrypting the protected code, then<br />

replacing the call to the decryption routine with nop instructions. This is possible<br />

because the size of the encrypted code is the same as the decrypted code; a more<br />

robust solution would be to use a stronger encryption method or a compression<br />

method, and extract the protected code to a dynamically allocated region of memory.<br />

However, such a method requires extensive manipulation of the object files<br />

before and after linking. You might consider using a commercially available binary<br />

packer to reduce development and testing time.<br />

See Also<br />

Recipes 5.23, 12.3, 12.9<br />

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

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

Using Self-Modifying Code | 699

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

Saved successfully!

Ooh no, something went wrong!