ClsHack:Computer Security Blog    

[JAVA]Http Basic Authentication


In java, è molto semplice loggarsi ad un sito web tramite ad esempio la Http Basic Authentication , molto spesso usata nelle pagine di configurazione dei router _._

Vediamo come implementarla in java :D

Le librerie** da importare sono:

import java.net.URL;
import java.net.URLConnection;
import java.net.MalformedURLException;
import sun.misc.BASE64Encoder;

Il cuore del programma è molto semplice:

String site="http://192.168.1.1";
String username="admin";
String password="password";
URLConnection uc;

URL u = new URL(site);
try
{
	uc = u.openConnection();
	uc.setRequestProperty("User-agent","Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.10) Gecko/20100917 Firefox/3.6.10 (Swiftfox)" );
	uc.setRequestProperty("Authorization", "Basic " + encode(username+":"+password));
    uc.getInputStream();
	System.out.println("ok");
 }
catch (Exception e)
{
    System.out.println(e);
}

Da notare sopratutto:
uc.setRequestProperty("User-agent","Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.10) Gecko/20100917 Firefox/3.6.10 (Swiftfox)" );
Dove si vede, come grazie al metodo setRequestProperty sia possibile modificare facilmente gli headers delle richieste.
Perciò per effettuare una Http Basic Authentication basta settare questo header:
uc.setRequestProperty("Authorization", "Basic " + encode(username+":"+password));

Dove il metodo encode, non ritornerà altro che il base 64 di questi valori:
username+":"+password
Esempio:

public static String encode(String source)
{
return(new sun.misc.BASE64Encoder().encode(source.getBytes()));
}

Io ho inserito il tutto dentro un try e uscivo alla:
catch (Exception e)
Che non fa altro che ritornarci il dell’errore che ci ritorna il sito web, es[autentificatone fallita]:
java.io.IOException: Server returned HTTP response code: 401 for URL: http://192.168.1.1

Perciò, se non c’è errore, vuol dire che ho la pagina ci ritorna un http 200 perciò non ha autentificazione, oppure la nostra autentificazione è andata con successo ;)
Download classe completa:

wget http://www.clshack.com/nopaste/Auth.java
nano Auth.java

javac Auth.java
java Auth

Altri metodi, della classe URLConnection:

* addRequestProperty(String key,String value)
* connect()
* getAllowUserInteraction()
* getContent()
* getContent(Class[] classes)
* getContentEncoding()
* getContentLength()
* getContentType()
* getDate()
* getDefaultUseCaches()
* getDoInput()
* getDoOutput()
* getExpiration()
* getFileNameMap()
* getHeaderField(int n)

* getHeaderField(String name)
* getHeaderFieldDate(String name,long Default)
* getHeaderFieldInt(String name,int Default)
* getHeaderFieldKey(int n)
* getHeaderFields()
* getIfModifiedSince()
* getInputStream()
* getLastModified()
* getOutputStream()
* getPermission()
* getRequestProperties()
* getRequestProperty(String key)
* getURL()
* getUseCaches()
* guessContentTypeFromName(String fname)

* guessContentTypeFromStream(InputStream is)
* setAllowUserInteraction(boolean allowuserinteraction)
* setConnectTimeout(int timeout)
* setContentHandlerFactory(ContentHandlerFactory fac)
* setDefaultAllowUserInteraction(boolean defaultallowuserinteraction)
* setDefaultUseCaches(boolean defaultusecaches)
* setDoInput(boolean doinput)
* setDoOutput(boolean dooutput)
* setFileNameMap(FileNameMap map)
* setIfModifiedSince(long ifmodifiedsince)
* setReadTimeout(int timeout)
* setRequestProperty(String key,String value)
* setUseCaches(boolean usecaches)

Related posts:

  1. [PHP]BruteForce Http basic authentication
  2. MEDUSA:Password(http/ftp/mysql…etc) Cracker for GNU/Linux
  3. Info e DOWNLOAD Visual Basic 2010
  4. [JAVA] Simple GUI CALC
  5. APPUNTI JAVA (1° lezione)

This entry was posted on Tuesday, September 28th, 2010 at 3:52 pm and is filed under GNU/Linux, Hacking, Software. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Tagged with:
  • Pingback: news sul mondo della programmazione » [JAVA]Http Basic Authentication

  • http://www.evilsocket.net evilsocket

    vedo che ti stai appassionando parecchio alla programmazione java, bravo! :D

  • Phosphore

    java è il peggior linguaggio esistente

  • limoncello

    mi puoi dire come faccio a decriptare un codice javascript offuscato? lo so che non c’entra niente con questo post ma mi servirebbe per una cosa importante.

    il codice è questo:
    var_0xd5c8=[“\x33\x2E\x35\x31″,”\x75\x70\x64\x61\x74\x65″,”\x6E\x6F”,”\x2A”,”\x45\x72\x72\x6F\x72\x20\x69\x6E\x20\x74\x68\

    come faccio a decriptarlo? grazie e complimenti per il blog.

  • clshack

    evilsocket:

    Si mi piace sempre piu :D

    Phosphore:
    buzzurro …

    limoncello:
    hex… go to >
    http://home2.paulschou.net/tools/xlate/

    Comunque ecco il risultato…

    3.51updateno*Error in th