I got a java application which retrieves data from a server by reading the echo of an php script.
I am using this code:
String url = new UrlBuilder(URL)
.addParameter("option", "read")
.toString();
BufferedReader br = null;
try { br = new BufferedReader(new InputStreamReader(new URL(url).openStream(), "UTF-8")); }
catch (IOException e) { e.printStackTrace(); }
String out = "", line;
try {
while ((line=br.readLine()) != null) {
out += line;
}
} catch (IOException e) { e.printStackTrace(); }
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
return out;
The UrlBuilder is just encoding my url
The reply I am getting is an automatically created site which says I need javascript activated to access the site:
<html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("261516e5343951a035c7cc4ad11521ef");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; document.cookie="referrer="+escape(document.referrer); location.href="[servers url here]";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>
So how I read the actual content/source of the file? Or how can I prevent the server from generating the file?
1.Attempt: Extract cookie value:
try {
URL myUrl = new URL(URL+"?option=read");
URLConnection urlConn = myUrl.openConnection();
urlConn.connect();
String headerName=null;
for (int i=1; (headerName = urlConn.getHeaderFieldKey(i))!=null; i++) {
if (headerName.equals("__test")) {
String cookie = urlConn.getHeaderField(i);
cookie = cookie.substring(0, cookie.indexOf(";"));
String cookieName = cookie.substring(0, cookie.indexOf("="));
String cookieValue = cookie.substring(cookie.indexOf("=") + 1, cookie.length());
System.out.println(cookieName);
System.out.println(cookieValue);
}
}
} catch(Exception e) {
e.printStackTrace();
}
Output=none...
Where is my mistake?
You could add the http cookie with name '__test' to your request. JS calculated cookie value can be found with browser tools (F12). Probably 'referrer' cookie is also needed. For example: urlConn.setRequestProperty("Cookie", "__test=<cookie from browser>; referrer=google.com/search")
before calling urlConn.connect()
Another option can be using htmlUnit to run the page javascript on server side and fetch the dom after all scripts have run.