
i wonder, if someone can tell me, how to get loged in to hts via java and (eg) download some file?
assumate the app will be a webbrowser, so i'll send something like
GET /index.php HTTP1.0\r\nReferer:http://www.hackthissite.org\r\nAccept-Charset:UTF-8\r\nCookie:[PHPSESSID=session goes here; path=/}
i just want the string to send, cause in some cases sockets will be faster than url-connection etc
and sry 4 my bad english

-- Mon Nov 12, 2012 6:21 pm --
Some democode 4 loging in - the problem: login-failed site will appear
plz take notice of the red color down there - you may enter your pwd

- Code: Select all
/**
* STREETRULEZ
**/
package browser_script;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class browser_script
{
private String PHPSESSID = "";
private String LOGIN_URL = "http://www.hackthissite.org/user/login";
private String REFERER = "http://www.hackthissite.org/";
private String USERAGENT = "User-Agent=Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";
private URL u;
private URLConnection conn;
public browser_script()
{
doLogin();
}
private void sendHeaders(final URLConnection conn)
{
conn.addRequestProperty("Referer", REFERER);
conn.addRequestProperty("User-agent", USERAGENT);
conn.addRequestProperty("Keep-Alive", "115");
conn.addRequestProperty("Connection", "keep-alive");
System.err.println(PHPSESSID);
if (PHPSESSID.length() > 4) conn.addRequestProperty("Cookie", "PHPSESSID=" + PHPSESSID + "; path=/");
}
private void openConn(final String url)
{
try
{
u = new URL(url);
conn = u.openConnection();
}
catch (MalformedURLException ex)
{
ex.printStackTrace();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
public String loadPage(final String url)
{
String rec = "";
try
{
String tmp = "";
rec = "";
openConn(url);
conn.setDoOutput(true);
sendHeaders(conn);
conn.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((tmp = br.readLine()) != null)
rec += "\r\n" + tmp;
br.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
return rec;
}
public void doLogin()
{
try
{
openConn(LOGIN_URL);
conn.setDoOutput(true);
conn.setRequestProperty("Request-Method", "POST");
sendHeaders(conn);
conn.connect();
String data = "username=helluser&password=[color=#FF0000]@Call%20Autoexec.bat[/color]&btn_submit=Login";
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
bw.write(data);
bw.flush();
bw.close();
String sid = conn.getHeaderFields().toString();
System.out.println(sid);
int start = sid.indexOf("PHPSESSID=") + 10;
int end = sid.indexOf(';', start);
this.PHPSESSID = sid.substring(start, end);
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = "";
while ((line = br.readLine()) != null)
System.out.println(line);
}
catch (MalformedURLException ex)
{
ex.printStackTrace();
}
catch (UnsupportedEncodingException ex)
{
ex.printStackTrace();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
public static void main(String args[])
{
new browser_script();
}
}