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 />

Detecting whether portions of a binary have been modified is essentially an errordetection<br />

problem; therefore, a checksum algorithm such as CRC32, MD5, or SHA1<br />

can be used to generate a signature for an arbitrary block of code or data. This signature<br />

can then be checked at runtime to determine whether any modification has<br />

taken place.<br />

Discussion<br />

We have chosen the CRC32 algorithm both for its ease of implementation<br />

and for its speed. It is ideal for detecting changes to short<br />

sequences of bytes; however, because there are only 2 32 possible<br />

checksum values, and because it is not cryptographically secure, the<br />

likelihood of a collision is high, giving the attacker a realistic chance to<br />

replace code without changing the checksum. For this kind of application,<br />

cryptographic strength is probably overkill, as there are easier<br />

attacks than forcing a collision in the checksums (e.g., simply patch<br />

the checksumming code).<br />

The checksum API presented here is an implementation of CRC32, which consists of<br />

macros for marking the start and end of the block to be checked, as well as a function<br />

to calculate the checksum of the block. The function crc32_calc( ) is used to<br />

compute the checksum of a buffer.<br />

#define CRC_START_BLOCK(label) void label(void) { }<br />

#define CRC_END_BLOCK(label) void _##label(void) { }<br />

#define CRC_BLOCK_LEN(label) (int)_##label - (int)label<br />

#define CRC_BLOCK_ADDR(label) (unsigned char *)label<br />

static unsigned long crc32_table[256] = {0};<br />

#define CRC_TABLE_LEN 256<br />

#define CRC_POLY 0xEDB88320L<br />

static int crc32(unsigned long a, unsigned long b) {<br />

int idx, prev;<br />

prev = (a >> 8) & 0x00FFFFFF;<br />

idx = (a ^ b) & 0xFF;<br />

return (prev ^ crc32_table[idx] ^ 0xFFFFFFFF);<br />

}<br />

static unsigned long crc32_table_init(void) {<br />

int i, j;<br />

unsigned long crc;<br />

for (i = 0; i < CRC_TABLE_LEN; i++) {<br />

crc = i;<br />

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