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.

which means that a small value like 19 will have to be padded with leading<br />

zeros resulting in null bytes.<br />

One way around this problem takes advantage <strong>of</strong> two’s complement. A<br />

small negative number will have its leading bits turned on, resulting in 0xff<br />

bytes. This means that, if we call using a negative value to move backward in<br />

execution, <strong>the</strong> machine code for that instruction won’t have any null bytes.<br />

The following revision <strong>of</strong> <strong>the</strong> helloworld shellcode uses a standard implementation<br />

<strong>of</strong> this trick: Jump to <strong>the</strong> end <strong>of</strong> <strong>the</strong> shellcode to a call instruction which,<br />

in turn, will jump back to a pop instruction at <strong>the</strong> beginning <strong>of</strong> <strong>the</strong> shellcode.<br />

helloworld2.s<br />

BITS 32<br />

jmp short one<br />

; Tell nasm this is 32-bit code.<br />

; Jump down to a call at <strong>the</strong> end.<br />

two:<br />

; ssize_t write(int fd, const void *buf, size_t count);<br />

pop ecx<br />

; Pop <strong>the</strong> return address (string ptr) into ecx.<br />

mov eax, 4 ; Write syscall #.<br />

mov ebx, 1 ; STDOUT file descriptor<br />

mov edx, 15 ; Length <strong>of</strong> <strong>the</strong> string<br />

int 0x80 ; Do syscall: write(1, string, 14)<br />

; void _exit(int status);<br />

mov eax, 1 ; Exit syscall #<br />

mov ebx, 0 ; Status = 0<br />

int 0x80<br />

; Do syscall: exit(0)<br />

one:<br />

call two ; Call back upwards to avoid null bytes<br />

db "Hello, world!", 0x0a, 0x0d ; with newline and carriage return bytes.<br />

After assembling this new shellcode, disassembly shows that <strong>the</strong> call<br />

instruction (shown in italics below) is now free <strong>of</strong> null bytes. This solves <strong>the</strong><br />

first and most difficult null-byte problem for this shellcode, but <strong>the</strong>re are still<br />

many o<strong>the</strong>r null bytes (shown in bold).<br />

reader@<strong>hacking</strong>:~/booksrc $ nasm helloworld2.s<br />

reader@<strong>hacking</strong>:~/booksrc $ ndisasm -b32 helloworld2<br />

00000000 EB1E jmp short 0x20<br />

00000002 59 pop ecx<br />

00000003 B804000000 mov eax,0x4<br />

00000008 BB01000000 mov ebx,0x1<br />

0000000D BA0F000000 mov edx,0xf<br />

00000012 CD80 int 0x80<br />

00000014 B801000000 mov eax,0x1<br />

00000019 BB00000000 mov ebx,0x0<br />

0000001E CD80<br />

int 0x80<br />

00000020 E8DDFFFFFF call 0x2<br />

00000025 48 dec eax<br />

00000026 656C gs insb<br />

00000028 6C insb<br />

Shellcode 291

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

Saved successfully!

Ooh no, something went wrong!