Browse Source

Added some documentation
Added certificate to the app

qam 10 years ago
parent
commit
503728efdc

+ 0 - 0
res/raw/https_cert.bks → assets/https_cert.bks


+ 75 - 65
src/de/tudarmstadt/informatik/hostage/protocol/HTTPS.java

@@ -8,9 +8,10 @@ import java.util.List;
 import javax.net.ssl.KeyManagerFactory;
 import javax.net.ssl.SSLContext;
 
+import de.tudarmstadt.informatik.hostage.ui.MainActivity;
+
 /**
  * HTTPS protocol
- * 
  * @author Wulf Pfeiffer
  */
 public class HTTPS implements SSLProtocol<String> {
@@ -30,23 +31,23 @@ public class HTTPS implements SSLProtocol<String> {
 		List<String> response = new ArrayList<String>();
 		request = message + request;
 
-		if (!message.contains(version)) {
+		if(!message.contains(version)){
 			response.add(buildPacket(c505, ""));
-		} else if (message.contains(get)) {
+		} else if(message.contains(get)) {
 			response.add(buildPacket(c200, get));
-		} else if (message.contains(head)) {
+		} else if(message.contains(head)) {
 			response.add(buildPacket(c200, head));
-		} else if (message.contains(trace)) {
+		} else if(message.contains(trace)){
 			response.add(buildPacket(c200, trace));
-		} else if (message.contains(options)) {
+		} else if(message.contains(options)){
 			response.add(buildPacket(c400, options));
-		} else if (message.contains(post)) {
+		} else if(message.contains(post)){
 			response.add(buildPacket(c400, post));
-		} else if (message.contains(put)) {
+		} else if(message.contains(put)){
 			response.add(buildPacket(c400, put));
-		} else if (message.contains(delete)) {
+		} else if(message.contains(delete)){
 			response.add(buildPacket(c400, delete));
-		} else if (message.contains(connect)) {
+		} else if(message.contains(connect)){
 			response.add(buildPacket(c400, connect));
 		} else {
 			response.add(buildPacket(c400, ""));
@@ -76,14 +77,13 @@ public class HTTPS implements SSLProtocol<String> {
 
 	@Override
 	public SSLContext getSSLContext() {
-		String ksName = "/storage/sdcard0/server.bks";
+		String ksName = "https_cert.bks";
 		char ksPass[] = "password".toCharArray();
-
 		KeyStore ks;
 		KeyManagerFactory kmf = null;
 		try {
 			ks = KeyStore.getInstance(KeyStore.getDefaultType());
-			ks.load(new FileInputStream(ksName), ksPass);
+			ks.load(MainActivity.getContext().getAssets().open(ksName), ksPass);
 			kmf = KeyManagerFactory.getInstance(KeyManagerFactory
 					.getDefaultAlgorithm());
 			kmf.init(ks, ksPass);
@@ -103,63 +103,73 @@ public class HTTPS implements SSLProtocol<String> {
 
 	/**
 	 * Builds a html response that can be sent
-	 * 
-	 * @param code
-	 *            response code that was determined
-	 * @param type
-	 *            request type that was sent by the client
+	 * @param code response code that was determined
+	 * @param type request type that was sent by the client
 	 * @return the html response
 	 */
 	private String buildPacket(String code, String type) {
 		String doc = "";
-		if (type.equals(get))
-			doc = htmlDoc;
-		else if (type.equals(head))
-			doc = "";
-		else if (type.equals(trace))
-			doc = request;
-		else
-			doc = errorHtmlPrefix + code + errorHtmlSuffix;
-
-		return version + code + headerPrefix + doc.length() + headerSuffix
-				+ doc;
+		if(type.equals(get)) doc = htmlDoc;
+		else if(type.equals(head)) doc = "";
+		else if(type.equals(trace)) doc = request;
+		else doc = errorHtmlPrefix + code + errorHtmlSuffix;
+		
+		return version + code + headerPrefix + doc.length() + headerSuffix + doc;
 	}
 
 	/** Whole request that was sent by the client */
-	private String request = "";
-	private String version = "HTTP/1.1";
-	// request codes
-	private String options = "OPTIONS";
-	private String get = "GET";
-	private String head = "HEAD";
-	private String post = "POST";
-	private String put = "PUT";
-	private String delete = "DELETE";
-	private String trace = "TRACE";
-	private String connect = "CONNECT";
-	// response codes
-	private String c200 = " 200 OK\r\n";
-	private String c400 = " 400 Bad Request\r\n";
-	private String c505 = " 505 HTTP Version not supported\r\n";
-
-	// html header pre and suffix
-	private String headerPrefix = "Date: Mon, 01 Jul 2013 18:27:55 GMT\r\n"
-			+ "Server: Apache/2.2.22 (Debian)\r\n"
-			+ "Vary: Accept-Encoding\r\n" + "Content-Length: ";
-	private String headerSuffix = "\r\n" + "Keep-Alive: timeout=5, max=100\r\n"
-			+ "Connection: Keep-Alive\r\n" + "Content-Type: text/html\r\n"
-			+ "\r\n";
-	// html website
-	private String htmlDoc = "<!doctype html>\n" + "<html lang=\"en\">\n"
-			+ "<head>\n" + "<meta charset=\"UTF-8\">\n"
-			+ "<title>Test successful</title>\n" + "</head>\n" + "<body>\n"
-			+ "<h1>Test successful</h1>\n" + "<p>Congratulations.</p>\n"
-			+ "</body>\n" + "</html>";
-	// html error pre and suffix
-	private String errorHtmlPrefix = "<!doctype html>\n"
-			+ "<html lang=\"en\">\n" + "<head>\n"
-			+ "<meta charset=\"UTF-8\">\n" + "<title>";
-	private String errorHtmlSuffix = "</title>\n" + "</head>\n" + "<body>\n"
-			+ "</body>\n" + "</html>";
-
+	private String request	= "";
+	private String version	= "HTTP/1.1";
+	//request codes
+	private String options 	= "OPTIONS";
+	private String get 		= "GET";
+	private String head 	= "HEAD";
+	private String post		= "POST";
+	private String put		= "PUT";
+	private String delete	= "DELETE";
+	private String trace	= "TRACE";
+	private String connect	= "CONNECT";
+	//response codes
+	private String c200 	= " 200 OK\r\n";
+	private String c400 	= " 400 Bad Request\r\n";
+	private String c505 	= " 505 HTTP Version not supported\r\n";
+	
+	//html header pre and suffix
+	private String headerPrefix =				
+			"Date: Mon, 01 Jul 2013 18:27:55 GMT\r\n" +
+			"Server: Apache/2.2.22 (Debian)\r\n" +
+			"Vary: Accept-Encoding\r\n" +
+			"Content-Length: ";
+	private String headerSuffix =
+			"\r\n" +	
+			"Keep-Alive: timeout=5, max=100\r\n" +
+			"Connection: Keep-Alive\r\n" +
+			"Content-Type: text/html\r\n" +
+			"\r\n";
+	//html website
+	private String htmlDoc = 
+			"<!doctype html>\n" +
+			"<html lang=\"en\">\n" +
+			"<head>\n" +
+			"<meta charset=\"UTF-8\">\n" +
+			"<title>Test successful</title>\n" +
+			"</head>\n" +
+			"<body>\n" +
+			"<h1>Test successful</h1>\n" +
+			"<p>Congratulations.</p>\n" +
+			"</body>\n" +
+			"</html>";
+	//html error pre and suffix
+	private String errorHtmlPrefix =
+			"<!doctype html>\n" +
+			"<html lang=\"en\">\n" +
+			"<head>\n" +
+			"<meta charset=\"UTF-8\">\n" +
+			"<title>";
+	private String errorHtmlSuffix =
+			"</title>\n" +
+			"</head>\n" +
+			"<body>\n" +
+			"</body>\n" +
+			"</html>";
 }

+ 5 - 5
src/de/tudarmstadt/informatik/hostage/protocol/SMB.java

@@ -300,8 +300,8 @@ public final class SMB implements Protocol<ByteArray> {
 		}
 		
 		/**
-		 * Builds the setup challange packet
-		 * @return setup challange packet
+		 * Builds the session setup challange packet
+		 * @return session setup challange packet
 		 */
 		private byte[] getSetupChal() {
 			byte[] wordCount		= {0x04};
@@ -349,8 +349,8 @@ public final class SMB implements Protocol<ByteArray> {
 		}
 		
 		/**
-		 * Builds the setup authentication packet
-		 * @return setup authentication packet
+		 * Builds the session setup packet for authentication required
+		 * @return session setup authentication packet
 		 */
 		private byte[] getSetupAuth() {
 			byte[] wordCount		= {0x04};
@@ -588,7 +588,7 @@ public final class SMB implements Protocol<ByteArray> {
 		}
 		
 		/**
-		 * Builds the trans sub packet
+		 * Extracts the trans sub packet from message
 		 * @return trans sub packet
 		 */		
 		private byte[] getTransSub() {

+ 27 - 16
src/de/tudarmstadt/informatik/hostage/protocol/SSH.java

@@ -21,12 +21,12 @@ import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
 import de.tudarmstadt.informatik.hostage.wrapper.ByteArray;
 
 /**
- * SSH protocol
+ * SSH protocol.
  * @author Wulf Pfeiffer
  */
 public final class SSH implements Protocol<ByteArray> {
 	/**
-	 * Represents the states of the protocol
+	 * Represents the states of the protocol.
 	 */
 	private enum STATE {
 		NONE,
@@ -37,7 +37,7 @@ public final class SSH implements Protocol<ByteArray> {
 	}
 	
 	/**
-	 * Denotes in which state the protocol is right now
+	 * Denotes in which state the protocol is right now.
 	 */
 	private STATE connectionState = STATE.NONE;
 	
@@ -66,6 +66,7 @@ public final class SSH implements Protocol<ByteArray> {
       };
 	private final byte[] g = {0x02};
 	
+	//SSH Parameters for Kex etc.
     private byte[] V_S = serverType.getBytes();
     private byte[] V_C;
     private byte[] I_S;
@@ -77,8 +78,10 @@ public final class SSH implements Protocol<ByteArray> {
     private byte[] K_S;
     private byte[] sig;
 
+    //Keys for signature
     private KeyPair dsa;
 			
+    //allowed algorithms for kexinit
 	private String kex_alg = "diffie-hellman-group1-sha1";
 	private String server_alg = "ssh-dss";
 	private String encrypt_alg_c = "aes128-ctr";
@@ -90,6 +93,7 @@ public final class SSH implements Protocol<ByteArray> {
 	
 	private int cipherBlockSize = 16;
 	
+	/** Denotes in which state the protocol is right now */
 	private STATE state = STATE.NONE;
 
 	@Override
@@ -122,6 +126,8 @@ public final class SSH implements Protocol<ByteArray> {
 		case CLIENT_VERSION:
 			extractPubKey(request);
 			response.add(new ByteArray(dhKexReply()));
+			//FIXME signature in dhKexReply is wrong, don't know why
+			response.add(new ByteArray(newKeys()));
 			connectionState = STATE.KEX_INIT;
 			break;
 		case KEX_INIT:
@@ -158,9 +164,9 @@ public final class SSH implements Protocol<ByteArray> {
 	}
 
 	/**
-	 * Wraps the packets with packet length and padding
-	 * @param packet content that is wrapped
-	 * @return wrapped packet
+	 * Wraps the packets with packet length and padding.
+	 * @param packet content that is wrapped.
+	 * @return wrapped packet.
 	 */
 	private byte[] wrapPacket(byte[] packet) {
 		int packetLength = 5 + packet.length; 	//4 byte packet length, 1 byte padding length, payload length
@@ -180,8 +186,8 @@ public final class SSH implements Protocol<ByteArray> {
 	}
 	
 	/**
-	 * Builds the Kex Init packet that contains all the allowed algorithms by the server
-	 * @return Kex Init packet
+	 * Builds the Kex Init packet that contains all the allowed algorithms by the server.
+	 * @return Kex Init packet.
 	 */
 	private byte[] kexInit() {
 		byte[] msgCode = {0x14};
@@ -207,8 +213,8 @@ public final class SSH implements Protocol<ByteArray> {
 	}
 	
 	/**
-	 * Builds the Diffie-Hellman Kex Reply, containing the host key,f and the signature
-	 * @return Diffie-Hellman Kex Reply packet
+	 * Builds the Diffie-Hellman Kex Reply, containing the host key,f and the signature.
+	 * @return Diffie-Hellman Kex Reply packet.
 	 */
 	private byte[] dhKexReply() {
 		generateDHKeys();
@@ -226,13 +232,17 @@ public final class SSH implements Protocol<ByteArray> {
 		return wrapPacket(response);
 	}
 	
-//	private byte[] newKeys() {
-//		byte[] msgCode = {0x15};
-//		return wrapPckt(msgCode);
-//	}
+	/**
+	 * New Keys response.
+	 * @return New Keys response.
+	 */
+	private byte[] newKeys() {
+		byte[] msgCode = {0x15};
+		return wrapPacket(msgCode);
+	}
 	
 	/**
-	 * Generates the required Diffie-Hellman keys with p and g from Oakley Group 1
+	 * Generates the required Diffie-Hellman keys with p and g from Oakley Group 1.
 	 */
 	private void generateDHKeys() {	
 		try {
@@ -316,7 +326,8 @@ public final class SSH implements Protocol<ByteArray> {
 	/**
 	 * Generates the signature of the hash using DSA algorithm with SHA-1
 	 */
-	private void generateSignature() {		
+	private void generateSignature() {	
+		//FIXME something is wrong with this signature.. maybe one of the used components is generated wrong?! 
 		try {
 			Signature sig = Signature.getInstance("SHA1withDSA");
             sig.initVerify(dsa.getPublic());

+ 15 - 2
src/de/tudarmstadt/informatik/hostage/ui/MainActivity.java

@@ -52,7 +52,7 @@ import de.tudarmstadt.informatik.hostage.logging.SQLLogger;
  * The user can start and stop services.
  * @author Mihai Plasoianu
  * @author Lars Pandikow
- *
+ * @author Wulf Pfeiffer
  */
 public class MainActivity extends Activity {
 	// String constants for whole application
@@ -87,6 +87,9 @@ public class MainActivity extends Activity {
 	 * Integer representing a yellow light.
 	 */
 	public static final int LIGHT_YELLOW = 0x04;
+	
+    private static Context context;
+
 
 	private HoneyService mService;
 	private boolean serviceBound;
@@ -107,9 +110,11 @@ public class MainActivity extends Activity {
 	
 	private String protocolClicked;
 	
+	
 	@Override
 	protected void onCreate(Bundle savedInstanceState) {
 		super.onCreate(savedInstanceState);
+        MainActivity.context = getApplicationContext();	//set context
 		setContentView(R.layout.activity_main);
 		
 		// Create dynamic view elements
@@ -544,7 +549,7 @@ public class MainActivity extends Activity {
 	}
 
 	/**
-	 * Sets the connections count for a given protocol
+	 * Sets the connections count for a given protocol.
 	 * @param connections New value for recorded connections.
 	 * @param protocolName Name of the protocol which should be updated.
 	 */
@@ -661,4 +666,12 @@ public class MainActivity extends Activity {
 			return true;
 		}
 	};
+
+	/**
+	 * Returns the context of the App.
+	 * @return context.
+	 */
+    public static Context getContext() {
+        return MainActivity.context;
+    }
 }