75 lines
2.2 KiB
Java
75 lines
2.2 KiB
Java
/*
|
|
* Created on 02.02.2005
|
|
*/
|
|
package uebung05.aufgabe02;
|
|
|
|
import java.io.IOException;
|
|
import java.net.*;
|
|
|
|
import sun.misc.BASE64Encoder;
|
|
|
|
/**
|
|
* Verbindet zu einer gegebenen URL und versucht, via "Basic Authentication Scheme"
|
|
* Zugriff auf die Ressource zu bekommen. Hierfür werden sämtliche Permutation
|
|
* vorgegebener Namen/Passworte durchgegangen
|
|
*
|
|
* @author Tilman Walther
|
|
*/
|
|
public class AuthCheck {
|
|
|
|
// die URL, auf die zugegriffen wird
|
|
String resource = "http://www.inf.fu-berlin.de/inst/ag-nbi/lehre/0405/V_NP/geheim5/geheim.txt";
|
|
|
|
// Namen / Passworte für die Login-Versuche
|
|
final String[] nameDict = {"Alle", "Keiner", "Jeder"};
|
|
final String[] passDict = {"gelb", "blau", "rot"};
|
|
|
|
// Ein BASE64-Encoder zur Übermittlung des Logins
|
|
BASE64Encoder encoder = new BASE64Encoder();
|
|
|
|
public AuthCheck() throws IOException {
|
|
|
|
// Verbindung herstellen
|
|
System.out.println("Verbinde zu "+resource);
|
|
URL page = new URL(resource);
|
|
HttpURLConnection conn = (HttpURLConnection) page.openConnection();
|
|
conn.connect();
|
|
|
|
// prüfen, ob Authentifizierung verlangt wird
|
|
if (conn.getResponseCode() == 401) {
|
|
System.out.println("Realm: "+conn.getHeaderField("WWW-Authenticate")+"\n");
|
|
|
|
// sämtliche Name-Passwort-Kombinationen durchprobieren
|
|
for (int i = 0;i < nameDict.length; i++) {
|
|
for (int j = 0; j < passDict.length; j++) {
|
|
System.out.print("Login mit "+nameDict[i]+":"+passDict[j]+"... ");
|
|
|
|
// Login-Daten für die Übermittlung codieren
|
|
String login = encoder.encode((nameDict[i] + ":" + passDict[j]).getBytes());
|
|
|
|
// Authentifizierung per "Basic Authentication Scheme"
|
|
conn = (HttpURLConnection) page.openConnection();
|
|
conn.setRequestProperty("Authorization", "Basic "+login);
|
|
conn.connect();
|
|
|
|
// Login erfolgreich?
|
|
if (conn.getResponseCode() == 200) {
|
|
System.out.println("erfolgreich!");
|
|
}
|
|
else {
|
|
System.out.println("fehlgeschlagen");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
System.out.println("The resource is not available via 'Basic Authentication Scheme' ("+conn.getResponseCode()+").");
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) throws IOException {
|
|
new AuthCheck();
|
|
}
|
|
|
|
}
|