19.09.2017 Views

the-web-application-hackers-handbook

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

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

Chapter 14 n Automating Customized Attacks 581<br />

}<br />

if (params[i].type == Param.Type.URL)<br />

urlParams.append(params[i].name + “=” + value + “&”);<br />

else if (params[i].type == Param.Type.COOKIE)<br />

cookieParams.append(params[i].name + “=” + value + “; “);<br />

else if (params[i].type == Param.Type.BODY)<br />

bodyParams.append(params[i].name + “=” + value + “&”);<br />

// build request<br />

StringBuffer req = new StringBuffer();<br />

req.append(method + “ “ + url);<br />

if (urlParams.length() > 0)<br />

req.append(“?” + urlParams.substring(0, urlParams.length() - 1));<br />

req.append(“ HTTP/1.0\r\nHost: “ + host);<br />

if (cookieParams.length() > 0)<br />

req.append(“\r\nCookie: “ + cookieParams.toString());<br />

if (bodyParams.length() > 0)<br />

{<br />

req.append(“\r\nContent-Type: <strong>application</strong>/x-www-form-urlencoded”);<br />

req.append(“\r\nContent-Length: “ + (bodyParams.length() - 1));<br />

req.append(“\r\n\r\n”);<br />

req.append(bodyParams.substring(0, bodyParams.length() - 1));<br />

}<br />

else req.append(“\r\n\r\n”);<br />

}<br />

return req.toString();<br />

NOTE If you write your own code to generate POST requests, you need to<br />

include a valid Content-Length header that specifies <strong>the</strong> actual length of <strong>the</strong><br />

HTTP body in each request, as in <strong>the</strong> preceding code. If an invalid Content-<br />

Length is submitted, most <strong>web</strong> servers ei<strong>the</strong>r truncate <strong>the</strong> data you submit or<br />

wait indefinitely for more data to be supplied.<br />

To send our requests, we need to open network connections to <strong>the</strong> target <strong>web</strong><br />

server. Java makes it easy to open a TCP connection, submit data, and read <strong>the</strong><br />

server’s response:<br />

String issueRequest(String req) throws UnknownHostException, IOException<br />

{<br />

Socket socket = new Socket(host, port);<br />

OutputStream os = socket.getOutputStream();<br />

os.write(req.getBytes());<br />

os.flush();<br />

BufferedReader br = new BufferedReader(new InputStreamReader(<br />

socket.getInputStream()));<br />

StringBuffer response = new StringBuffer();

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

Saved successfully!

Ooh no, something went wrong!