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.

63 if (listen(sockfd, 20) == -1)<br />

64 fatal("listening on socket");<br />

65<br />

66 while(1) { // Accept loop<br />

67 sin_size = size<strong>of</strong>(struct sockaddr_in);<br />

68 new_sockfd = accept(sockfd, (struct sockaddr *)&client_addr, &sin_size);<br />

69 if(new_sockfd == -1)<br />

70 fatal("accepting connection");<br />

71<br />

72 handle_connection(new_sockfd, &client_addr, logfd);<br />

(gdb) list handle_connection<br />

77 /* This function handles <strong>the</strong> connection on <strong>the</strong> passed socket from <strong>the</strong><br />

78 * passed client address and logs to <strong>the</strong> passed FD. The connection is<br />

79 * processed as a web request, and this function replies over <strong>the</strong> connected<br />

80 * socket. Finally, <strong>the</strong> passed socket is closed at <strong>the</strong> end <strong>of</strong> <strong>the</strong> function.<br />

81 */<br />

82 void handle_connection(int sockfd, struct sockaddr_in *client_addr_ptr, int logfd) {<br />

83 unsigned char *ptr, request[500], resource[500], log_buffer[500];<br />

84 int fd, length;<br />

85<br />

86 length = recv_line(sockfd, request);<br />

(gdb) break 86<br />

Breakpoint 1 at 0x8048fc3: file tinywebd.c, line 86.<br />

(gdb) cont<br />

Continuing.<br />

The execution pauses while <strong>the</strong> tinyweb daemon waits for a connection.<br />

Once again, a connection is made to <strong>the</strong> webserver using a browser to advance<br />

<strong>the</strong> code execution to <strong>the</strong> breakpoint.<br />

Breakpoint 1, handle_connection (sockfd=5, client_addr_ptr=0xbffff810) at tinywebd.c:86<br />

86 length = recv_line(sockfd, request);<br />

(gdb) bt<br />

#0 handle_connection (sockfd=5, client_addr_ptr=0xbffff810, logfd=3) at tinywebd.c:86<br />

#1 0x08048fb7 in main () at tinywebd.c:72<br />

(gdb) x/x request<br />

0xbffff5c0: 0x080484ec<br />

(gdb) x/16x request + 500<br />

0xbffff7b4: 0xb7fd5ff4 0xb8000ce0 0x00000000 0xbffff848<br />

0xbffff7c4: 0xb7ff9300 0xb7fd5ff4 0xbffff7e0 0xb7f691c0<br />

0xbffff7d4: 0xb7fd5ff4 0xbffff848 0x08048fb7 0x00000005<br />

0xbffff7e4: 0xbffff810 0x00000003 0xbffff838 0x00000004<br />

(gdb) x/x 0xbffff7d4 + 8<br />

0xbffff7dc: 0x08048fb7<br />

(gdb) p /x 0xbffff7dc - 0xbffff5c0<br />

$1 = 0x21c<br />

(gdb) p 0xbffff7dc - 0xbffff5c0<br />

$2 = 540<br />

(gdb) p /x 0xbffff5c0 + 100<br />

$3 = 0xbffff624<br />

(gdb) quit<br />

The program is running. Quit anyway (and detach it)? (y or n) y<br />

Detaching from program: , process 25830<br />

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

Countermeasures 331

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

Saved successfully!

Ooh no, something went wrong!