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.

strncat( buf, "mp", buf_len - strlen(buf));<br />

break;<br />

default:<br />

strncat( buf, "/", buf_len);<br />

}<br />

}<br />

buf[buf_len] = 0;<br />

return buf;<br />

}<br />

int main(int argc, char *argv[ ]) {<br />

char filename[32];<br />

/* 0x01040603 is 03 . 06 . 04 . 01 -- note that the number is passed as little<br />

* endian but read as big endian!<br />

*/<br />

printf("get_filename( ) returns \"%s\"\n",<br />

get_filename(0x01040603, filename, sizeof(filename)));<br />

return 0;<br />

}<br />

Strings can also be stored encrypted in the binary and in memory. You can achieve<br />

this by generating separate object files with the encrypted strings in them, by<br />

encrypting the strings in the binary after compilation, or by initializing the strings<br />

with encrypted characters. The following code demonstrates the last technique,<br />

using the A macro to subtract a constant value from each character in the string. Note<br />

that this is not a strong encryption method, but rather a quick and simple obfuscation<br />

of the value of each character.<br />

#define A(c) (c) - 0x19<br />

#define UNHIDE_STR(str) do { char *p = str; while (*p) *p++ += 0x19; } while (0)<br />

#define HIDE_STR(str) do { char *p = str; while (*p) *p++ -= 0x19; } while (0)<br />

Each character of the string must be initialized, which makes this method somewhat<br />

cumbersome, but it allows the obfuscation to take place at compile time:<br />

#include <br />

int main(int argc, char *argv[ ]) {<br />

char str[ ] = {<br />

A('/'), A('e'), A('t'), A('c'), A('/'),<br />

A('p'), A('a'), A('s'), A('s'), A('w'), A('d'), 0<br />

};<br />

UNHIDE_STR(str);<br />

printf("%s\n", str);<br />

HIDE_STR(str);<br />

return 0;<br />

}<br />

680 | 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!