26.07.2018 Views

hacking-the-art-of-exploitation

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

0x691<br />

Polymorphic Printable ASCII Shellcode<br />

Polymorphic shellcode refers to any shellcode that changes itself. The encoding<br />

shellcode from <strong>the</strong> previous section is technically polymorphic, since it<br />

modifies <strong>the</strong> string it uses while it’s running. The new NOP sled uses instructions<br />

that assemble into printable ASCII bytes. There are o<strong>the</strong>r instructions<br />

that fall into this printable range (from 0x33 to 0x7e); however, <strong>the</strong> total set<br />

is actually ra<strong>the</strong>r small.<br />

The goal is to write shellcode that will get past <strong>the</strong> printable character<br />

check. Trying to write complex shellcode with such a limited instruction set<br />

would simply be masochistic, so instead, <strong>the</strong> printable shellcode will use simple<br />

methods to build more complex shellcode on <strong>the</strong> stack. In this way, <strong>the</strong> printable<br />

shellcode will actually be instructions to make <strong>the</strong> real shellcode.<br />

The first step is figuring out a way to zero out registers. Unfortunately, <strong>the</strong><br />

XOR instruction on <strong>the</strong> various registers doesn’t assemble into <strong>the</strong> printable<br />

ASCII character range. One option is to use <strong>the</strong> AND bitwise operation, which<br />

assembles into <strong>the</strong> percent character (%) when using <strong>the</strong> EAX register. The<br />

assembly instruction <strong>of</strong> and eax, 0x41414141 will assemble to <strong>the</strong> printable<br />

machine code <strong>of</strong> %AAAA, since 0x41 in hexadecimal is <strong>the</strong> printable character A.<br />

An AND operation transforms bits as follows:<br />

1 and 1 = 1<br />

0 and 0 = 0<br />

1 and 0 = 0<br />

0 and 1 = 0<br />

Since <strong>the</strong> only case where <strong>the</strong> result is 1 is when both bits are 1, if two<br />

inverse values are ANDed onto EAX, EAX will become zero.<br />

Binary<br />

Hexadecimal<br />

1000101010011100100111101001010 0x454e4f4a<br />

AND 0111010001100010011000000110101 AND 0x3a313035<br />

------------------------------------ ---------------<br />

0000000000000000000000000000000 0x00000000<br />

Thus, by using two printable 32-bit values that are bitwise inverses <strong>of</strong> each<br />

o<strong>the</strong>r, <strong>the</strong> EAX register can be zeroed without using any null bytes, and <strong>the</strong><br />

resulting assembled machine code will be printable text.<br />

and eax, 0x454e4f4a ; Assembles into %JONE<br />

and eax, 0x3a313035 ; Assembles into %501:<br />

So %JONE%501: in machine code will zero out <strong>the</strong> EAX register. Interesting.<br />

Some o<strong>the</strong>r instructions that assemble into printable ASCII characters are<br />

shown in <strong>the</strong> box below.<br />

sub eax, 0x41414141 -AAAA<br />

push eax<br />

P<br />

pop eax<br />

X<br />

push esp<br />

T<br />

pop esp \<br />

366 0x600

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

Saved successfully!

Ooh no, something went wrong!