23.03.2013 Views

Quick introduction to reverse engineering for beginners

Quick introduction to reverse engineering for beginners

Quick introduction to reverse engineering for beginners

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.

So then we’ll work only with the addresses aligned by 16 byte border, what in combination with a<br />

knowledge of operation system page size is usually aligned by 16 byte <strong>to</strong>o, give us some warranty our<br />

function will not read from unallocated memory.<br />

Let’s back <strong>to</strong> our function.<br />

_mm_setzero_si128() — is a macro generating pxor xmm0, xmm0 — instruction just clear /XMMZERO<br />

register<br />

_mm_load_si128() — is a macro <strong>for</strong> MOVDQA, it just loading 16 bytes from the address in XMM1.<br />

_mm_cmpeq_epi8() — is a macro <strong>for</strong> PCMPEQB, is an instruction comparing two XMM-registers bytewise.<br />

And if some byte was equals <strong>to</strong> other, there will be 0xff at this place in result or 0 if otherwise.<br />

For example.<br />

XMM1: 11223344556677880000000000000000<br />

XMM0: 11ab3444007877881111111111111111<br />

After pcmpeqb xmm1, xmm0 execution, XMM1 register will contain:<br />

XMM1: ff0000ff0000ffff0000000000000000<br />

In our case, this instruction comparing each 16-byte block with the block of 16 zero-bytes, was set in<br />

XMM0 by pxor xmm0, xmm0.<br />

The next macro is _mm_movemask_epi8() — that is PMOVMSKB instruction.<br />

It is very useful if <strong>to</strong> use it with PCMPEQB.<br />

pmovmskb eax, xmm1<br />

This instruction will set first EAX bit in<strong>to</strong> 1 if most significant bit of the first byte in XMM1 is 1. In other<br />

words, if first byte of XMM1 register is 0xff, first EAX bit will be set <strong>to</strong> 1 <strong>to</strong>o.<br />

If second byte in XMM1 register is 0xff, then second EAX bit will be set <strong>to</strong> 1 <strong>to</strong>o. In other words, the<br />

instruction is answer <strong>to</strong> the question which bytes in XMM1 are 0xff? And will prepare 16 bits in EAX. Other<br />

EAX bits will be cleared.<br />

By the way, do not <strong>for</strong>get about this feature of our algorithm:<br />

There might be 16 bytes on input like hello\x00garbage\x00ab<br />

It’s a ’hello’ string, terminating zero after and some random noise in memory.<br />

If we load these 16 bytes in<strong>to</strong> XMM1 and compare them with zeroed XMM0, we will get something like (I<br />

use here order from MSB 77 <strong>to</strong> LSB 78 ):<br />

XMM1: 0000ff00000000000000ff0000000000<br />

This mean, the instruction found two zero bytes, and that’s not surprising.<br />

PMOVMSKB in our case will prepare EAX like (in binary representation): 0010000000100000b.<br />

Obviously, our function should consider only first zero and ignore others.<br />

The next instruction — BSF (Bit Scan Forward). This instruction find first bit set <strong>to</strong> 1 and s<strong>to</strong>res its<br />

position in<strong>to</strong> first operand.<br />

EAX=0010000000100000b<br />

After bsf eax, eax instruction execution, EAX will contain 5, this mean, 1 found at 5th bit position<br />

(starting from zero).<br />

MSVC has a macro <strong>for</strong> this instruction: _BitScanForward.<br />

Now it’s simple. If zero byte found, its position added <strong>to</strong> what we already counted and now we have<br />

ready <strong>to</strong> return result.<br />

Almost all.<br />

By the way, it’s also should be noted, MSVC compiler emitted two loop bodies side by side, <strong>for</strong> optimization.<br />

By the way, SSE 4.2 (appeared in Intel Core i7) offers more instructions where these string manipulations<br />

might be even easier: http://www.strchr.com/strcmp_and_strlen_using_sse_4.2<br />

77 most significant bit<br />

78 least significant bit<br />

102

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

Saved successfully!

Ooh no, something went wrong!