Browse Source

-Fixed byte protocol bug
-Minor fixes

Wulf Pfeiffer 10 years ago
parent
commit
02ed55cc0c

+ 1 - 0
res/values/protocols.xml

@@ -8,6 +8,7 @@
         <item>HTTP</item>
         <item>HTTPS</item>
         <item>MySQL</item>
+        <item>SIP</item>
         <item>SMB</item>
         <item>SSH</item>
         <item>TELNET</item>

+ 46 - 1
src/de/tudarmstadt/informatik/hostage/Hostage.java

@@ -1,5 +1,9 @@
 package de.tudarmstadt.informatik.hostage;
 
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.net.Socket;
+import java.security.SecureRandom;
 import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.List;
@@ -35,6 +39,7 @@ import android.widget.Toast;
 import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
 import de.tudarmstadt.informatik.hostage.location.MyLocationManager;
 import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
+import de.tudarmstadt.informatik.hostage.protocol.HTTP;
 import de.tudarmstadt.informatik.hostage.protocol.Protocol;
 import de.tudarmstadt.informatik.hostage.ui.MainActivity;
 
@@ -56,6 +61,41 @@ public class Hostage extends Service {
 		}
 	}
 
+	/**
+	 * Task for accuiring a qotd from one of four possible servers.
+	 * 
+	 * @author Wulf Pfeiffer
+	 */
+	private class QotdTask extends AsyncTask<String, Void, String> {
+		@Override
+		protected String doInBackground(String... unused) {
+			String[] sources = new String[] { "djxmmx.net", "ota.iambic.com",
+					"alpha.mike-r.com", "electricbiscuit.org" };
+			SecureRandom rndm = new SecureRandom();
+			StringBuffer sb = new StringBuffer();
+			try {
+				Socket client = new Socket(sources[rndm.nextInt(4)], 17);
+				BufferedReader in = new BufferedReader(new InputStreamReader(
+						client.getInputStream()));
+				while (!in.ready())
+					;
+				while (in.ready()) {
+					sb.append(in.readLine());
+				}
+				in.close();
+				client.close();
+			} catch (Exception e) {
+				e.printStackTrace();
+			}
+			return sb.toString();
+		}
+
+		@Override
+		protected void onPostExecute(String result) {
+			if (result != null)
+				HTTP.setHtmlDocumentContent(result);
+		}
+	}
 	/**
 	 * Task to find out the external IP.
 	 * 
@@ -250,7 +290,12 @@ public class Hostage extends Service {
 		createNotification();
 		registerNetReceiver();
 		updateConnectionInfo();
-		getLocationData();
+		getLocationData();		
+		boolean useQotd = context.getSharedPreferences(getString(R.string.shared_preference_path),
+				Hostage.MODE_PRIVATE).getBoolean("useQotd", true);
+		if (useQotd) {
+			new QotdTask().execute(new String[] {});
+		}
 	}
 
 	@Override

+ 0 - 1
src/de/tudarmstadt/informatik/hostage/nio/Reader.java

@@ -19,7 +19,6 @@ public class Reader {
 		ByteArrayOutputStream payload = new ByteArrayOutputStream();
 		do {
 			payload.write(in.read());
-			Thread.yield();
 		} while (in.available() > 0);
 		return new Packet(payload.toByteArray());
 	}

+ 0 - 1
src/de/tudarmstadt/informatik/hostage/nio/Writer.java

@@ -18,7 +18,6 @@ public class Writer {
 	public void write(List<Packet> packets) throws IOException {
 		for (Packet packet : packets) {
 			out.write(packet.getBytes());
-			out.write('\n');
 		}
 		out.flush();
 	}

+ 11 - 14
src/de/tudarmstadt/informatik/hostage/protocol/FTP.java

@@ -27,11 +27,8 @@ public class FTP implements Protocol {
 
 	// commands
 	private static final String REPLY_CODE_220 = "220 Service ready for new user.";
-
 	private static final String REPLY_CODE_221 = "221 Service closing control connection.";
-
 	private static final String REPLY_CODE_230 = "230 User logged in.";
-
 	private static final String REPLY_CODE_331 = "331 User name ok, need password.";
 	private static final String REPLY_CODE_332 = "332 Need account for login.";
 	private static final String REPLY_CODE_421 = "421 Service not available, closing control connection.";
@@ -64,10 +61,10 @@ public class FTP implements Protocol {
 		case NONE:
 			if (request == null) {
 				state = STATE.OPEN;
-				responsePackets.add(new Packet(REPLY_CODE_220));
+				responsePackets.add(new Packet(REPLY_CODE_220 + "\r\n"));
 			} else {
 				state = STATE.CLOSED;
-				responsePackets.add(new Packet(REPLY_CODE_421));
+				responsePackets.add(new Packet(REPLY_CODE_421 + "\r\n"));
 			}
 			break;
 		case OPEN:
@@ -75,37 +72,37 @@ public class FTP implements Protocol {
 				state = STATE.CLOSED;
 				return null;
 			} else if (request.equals("USER \r\n")) {
-				responsePackets.add(new Packet(REPLY_CODE_501));
+				responsePackets.add(new Packet(REPLY_CODE_501 + "\r\n"));
 			} else if (request.contains("USER")) {
 				state = STATE.USER;
-				responsePackets.add(new Packet(REPLY_CODE_331));
+				responsePackets.add(new Packet(REPLY_CODE_331 + "\r\n"));
 			} else {
-				responsePackets.add(new Packet(REPLY_CODE_332));
+				responsePackets.add(new Packet(REPLY_CODE_332 + "\r\n"));
 			}
 			break;
 		case USER:
 			if (request.equals("PASS \r\n")) {
 				state = STATE.OPEN;
-				responsePackets.add(new Packet(REPLY_CODE_501));
+				responsePackets.add(new Packet(REPLY_CODE_501 + "\r\n"));
 			} else if (request.contains("PASS")) {
 				state = STATE.LOGGED_IN;
-				responsePackets.add(new Packet(REPLY_CODE_230));
+				responsePackets.add(new Packet(REPLY_CODE_230 + "\r\n"));
 			} else {
 				state = STATE.CLOSED;
-				responsePackets.add(new Packet(REPLY_CODE_221));
+				responsePackets.add(new Packet(REPLY_CODE_221 + "\r\n"));
 			}
 			break;
 		case LOGGED_IN:
 			if (request != null && !request.contains("QUIT")) {
-				responsePackets.add(new Packet(REPLY_CODE_500));
+				responsePackets.add(new Packet(REPLY_CODE_500 + "\r\n"));
 			} else {
 				state = STATE.CLOSED;
-				responsePackets.add(new Packet(REPLY_CODE_221));
+				responsePackets.add(new Packet(REPLY_CODE_221 + "\r\n"));
 			}
 			break;
 		default:
 			state = STATE.CLOSED;
-			responsePackets.add(new Packet(REPLY_CODE_421));
+			responsePackets.add(new Packet(REPLY_CODE_421 + "\r\n"));
 		}
 		return responsePackets;
 	}

+ 29 - 61
src/de/tudarmstadt/informatik/hostage/protocol/HTTP.java

@@ -1,8 +1,5 @@
 package de.tudarmstadt.informatik.hostage.protocol;
 
-import java.io.BufferedReader;
-import java.io.InputStreamReader;
-import java.net.Socket;
 import java.security.SecureRandom;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
@@ -12,8 +9,6 @@ import java.util.Locale;
 import java.util.TimeZone;
 
 import android.content.Context;
-import android.os.AsyncTask;
-
 import de.tudarmstadt.informatik.hostage.Hostage;
 import de.tudarmstadt.informatik.hostage.R;
 import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
@@ -27,60 +22,13 @@ import de.tudarmstadt.informatik.hostage.wrapper.Packet;
  * @author Wulf Pfeiffer
  */
 public class HTTP implements Protocol {
-	
-	public HTTP() {
-		htmlDocumentContent = HelperUtils.getRandomString(32, false);
-		Context context = Hostage.getContext();
-		String sharedPreferencePath = context.getString(R.string.shared_preference_path);
-		boolean useQotd = context.getSharedPreferences(sharedPreferencePath,
-				Hostage.MODE_PRIVATE).getBoolean("useQotd", true);
-		if (useQotd) {
-			new QotdTask().execute(new String[] {});
-		}
-	}
-
-	/**
-	 * Task for accuiring a qotd from one of four possible servers.
-	 * 
-	 * @author Wulf Pfeiffer
-	 */
-	private class QotdTask extends AsyncTask<String, Void, String> {
-		@Override
-		protected String doInBackground(String... unused) {
-			String[] sources = new String[] { "djxmmx.net", "ota.iambic.com",
-					"alpha.mike-r.com", "electricbiscuit.org" };
-			SecureRandom rndm = new SecureRandom();
-			StringBuffer sb = new StringBuffer();
-			try {
-				Socket client = new Socket(sources[rndm.nextInt(4)], 17);
-				BufferedReader in = new BufferedReader(new InputStreamReader(
-						client.getInputStream()));
-				while (!in.ready())
-					;
-				while (in.ready()) {
-					sb.append(in.readLine());
-				}
-				in.close();
-				client.close();
-			} catch (Exception e) {
-				e.printStackTrace();
-			}
-			return sb.toString();
-		}
-
-		@Override
-		protected void onPostExecute(String result) {
-			if (result != null)
-				HTTP.setHtmlDocumentContent(result);
-		}
-	}
 
 	/**
 	 * Get the current time in html header format.
 	 * 
 	 * @return the formatted server time.
 	 */
-	private static String getServerTime() {
+	private String getServerTime() {
 		Calendar calendar = Calendar.getInstance();
 		SimpleDateFormat dateFormat = new SimpleDateFormat(
 				"EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
@@ -92,7 +40,7 @@ public class HTTP implements Protocol {
 	private String request = "";
 
 	// version stuff
-	private static String[][][] possibleHttpVersions = {
+	private String[][][] possibleHttpVersions = {
 			{
 					{ "Apache/2.0." },
 					{ "28", "32", "35", "36", "39", "40", "42", "43", "44",
@@ -106,21 +54,41 @@ public class HTTP implements Protocol {
 							"21", "22", "23", "24", "25" } },
 			{ { "Apache/2.3." },
 					{ "4", "5", "6", "8", "10", "11", "12", "14", "15", "16" } },
-			{ { "Apache/2.4." }, { "1", "2", "3", "4", "6" } } };
+			{ { "Apache/2.4." }, { "1", "2", "3", "4", "6" } },
+			{ { "Microsoft-IIS/" }, { "5.1", "7.0", "8.0" } } };
 
-	private static String serverVersion = initServerVersion();
+	private String serverVersion = initServerVersion();
 
-	private static String initServerVersion() {
+	private String initServerVersion() {
 		SecureRandom rndm = new SecureRandom();
 		int majorVersion = rndm.nextInt(possibleHttpVersions.length);
-		return possibleHttpVersions[majorVersion][0][0]
-				+ possibleHttpVersions[majorVersion][1][rndm
-						.nextInt(possibleHttpVersions[majorVersion][1].length)];
+		
+		String version;
+		String sharedPreferencePath = Hostage.getContext().getString(
+				R.string.shared_preference_path);
+		String profile = Hostage
+				.getContext()
+				.getSharedPreferences(sharedPreferencePath,
+						Context.MODE_PRIVATE).getString("os", "");
+		System.out.println(profile);
+		if (profile.equals("Windows 7") || profile.equals("Windows Server 2008")) {
+			version = "Microsoft-IIS/7.5";
+		} else if (profile.equals("Windows Server 2012") || profile.equals("Windows 8")) {
+			version = "Microsoft-IIS/8.0";
+		} else if (profile.equals("Windows XP")) {
+			version = "Microsoft-IIS/5.1";
+		} else {
+			version = possibleHttpVersions[majorVersion][0][0]
+					+ possibleHttpVersions[majorVersion][1][rndm
+					          .nextInt(possibleHttpVersions[majorVersion][1].length)];
+		}
+		
+		return version;
 	}
 
 	private String httpVersion = "HTTP/1.1";
 
-	private static String htmlDocumentContent;
+	private static String htmlDocumentContent = HelperUtils.getRandomString(32, false);
 
 	// request codes
 	private static final String OPTIONS = "OPTIONS";

+ 3 - 3
src/de/tudarmstadt/informatik/hostage/protocol/MySQL.java

@@ -30,7 +30,7 @@ public class MySQL implements Protocol {
 	private byte[] lastReceivedMessage;
 
 	// version stuff
-	private static String[][][] possibleMysqlVersions = {
+	private String[][][] possibleMysqlVersions = {
 			{ { "5.7." }, { "1", "2" } },
 			{
 					{ "5.6." },
@@ -38,9 +38,9 @@ public class MySQL implements Protocol {
 							"13", "14" } },
 			{ { "5.5." }, { "27", "28", "29", "30", "31", "32", "33", "34" } } };
 
-	private static String serverVersion = initMysqlVersion();
+	private String serverVersion = initMysqlVersion();
 
-	private static String initMysqlVersion() {
+	private String initMysqlVersion() {
 		SecureRandom rndm = new SecureRandom();
 		int majorVersion = rndm.nextInt(possibleMysqlVersions.length);
 		return possibleMysqlVersions[majorVersion][0][0]

+ 8 - 3
src/de/tudarmstadt/informatik/hostage/protocol/SIP.java

@@ -12,6 +12,12 @@ import de.tudarmstadt.informatik.hostage.wrapper.Packet;
  * @author Wulf Pfeiffer
  */
 public class SIP implements Protocol {
+	
+	private enum STATE {
+		NONE, CLOSED
+	}
+	
+	private STATE state = STATE.NONE;
 		
 	private static final String VERSION = "SIP/2.0";
 	private static final String INVITE = "INVITE";
@@ -32,8 +38,7 @@ public class SIP implements Protocol {
 
 	@Override
 	public boolean isClosed() {
-		// TODO Auto-generated method stub
-		return false;
+		return (state == STATE.CLOSED);
 	}
 
 	@Override
@@ -43,7 +48,6 @@ public class SIP implements Protocol {
 
 	@Override
 	public List<Packet> processMessage(Packet requestPacket) {
-		// TODO Auto-generated method stub
 		String request = null;
 		if (requestPacket != null) {
 			request = requestPacket.toString();
@@ -60,6 +64,7 @@ public class SIP implements Protocol {
 			responsePackets.add(getOkResponseWithSDP());
 		} else if(lines[0].contains(BYE)) {
 			responsePackets.add(getOkResponse());
+			state = STATE.CLOSED;
 		} else if(lines[0].contains(ACK)) {
 			//nothing here
 		} else {

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

@@ -31,7 +31,7 @@ public class SMB implements Protocol {
 	}
 
 	// version stuff
-	private static String[][] possibleSmbVersions = {
+	private String[][] possibleSmbVersions = {
 			{ "Windows 7 Professional 7600", "Windows 7 Professional 6.1" },
 			{ "Windows 8 Enterprise 9200", "Windows 8 Enterprise 9200" },
 			{ "Windows Server 2008 R2 Enterprise 7600", "Windows Server 2008 R2 Enterprise 6.1" },
@@ -94,7 +94,7 @@ public class SMB implements Protocol {
 		return timezoneBytes;
 	}
 
-	private static String[] initServerVersion() {
+	private String[] initServerVersion() {
 		String sharedPreferencePath = Hostage.getContext().getString(
 				R.string.shared_preference_path);
 		String profile = Hostage
@@ -127,10 +127,10 @@ public class SMB implements Protocol {
 
 	private byte[] lastMessage;
 
-	private static byte[] serverName = HelperUtils.fillWithZero(HelperUtils
+	private byte[] serverName = HelperUtils.fillWithZero(HelperUtils
 			.getRandomString(16, true).getBytes());
 
-	private static String[] serverVersion = initServerVersion();
+	private String[] serverVersion = initServerVersion();
 
 	private byte[] message = null;
 
@@ -830,19 +830,10 @@ public class SMB implements Protocol {
 	 */
 	private byte[] wrapNetbios(byte[] response) {
 		byte[] netbios = { 0x00 };
-		byte[] buffer = ByteBuffer.allocate(4).putInt(response.length).array(); // allocate(4)
-																				// because
-																				// int
-																				// is
-																				// 4
-																				// bytes
-																				// long
-		byte[] netbiosLength = { buffer[1], buffer[2], buffer[3] }; // only
-																	// bytes 1-3
-																	// needed,
-																	// byte 0 is
-																	// not
-																	// needed
+		// allocate(4) because int is 4 bytes long
+		byte[] buffer = ByteBuffer.allocate(4).putInt(response.length).array();
+		// only bytes 1-3 needed, byte 0 is not  needed
+		byte[] netbiosLength = { buffer[1], buffer[2], buffer[3] }; 
 		return HelperUtils.concat(netbios, netbiosLength, response);
 	}
 

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

@@ -44,7 +44,7 @@ public class SSH implements Protocol {
 	 *            that are converted
 	 * @return converted byte[] as int
 	 */
-	private static int byteToInt(byte[] bytes) {
+	private int byteToInt(byte[] bytes) {
 		int convertedInteger = 0;
 		for (int i = 0; i < bytes.length; i++) {
 			convertedInteger <<= 8;
@@ -53,7 +53,7 @@ public class SSH implements Protocol {
 		return convertedInteger;
 	}
 
-	private static String initSshType() {
+	private String initSshType() {
 		SecureRandom rnd = new SecureRandom();
 		int majorVersion = rnd.nextInt(possibleSshTypes.length);
 		return "OpenSSH_"
@@ -70,18 +70,18 @@ public class SSH implements Protocol {
 	private boolean useEncryption = false;
 
 	// version stuff
-	private static String[][][] possibleSshTypes = {
+	private String[][][] possibleSshTypes = {
 			{ { "3." }, { "4", "5", "6", "7", "8", "9" } },
 			{ { "4." }, { "0", "1", "2", "3", "4", "5", "6", "7", "9" } },
 			{ { "5." }, { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" } },
 			{ { "6." }, { "0", "1", "2", "3", "4" } } };
 
 	// server infos
-	private static String serverVersion = "SSH-2.0-";
+	private String serverVersion = "SSH-2.0-";
 
-	private static String serverType = initSshType();
+	private String serverType = initSshType();
 
-	private static String serverName = HelperUtils.getRandomString(16, false);
+	private String serverName = HelperUtils.getRandomString(16, false);
 
 	private int packetNumber = 0;
 

+ 9 - 8
src/de/tudarmstadt/informatik/hostage/protocol/TELNET.java

@@ -16,6 +16,7 @@ import de.tudarmstadt.informatik.hostage.wrapper.Packet;
  * @author Wulf Pfeiffer
  */
 public class TELNET implements Protocol {
+	
 	/**
 	 * Represents the states of the protocol
 	 */
@@ -29,19 +30,19 @@ public class TELNET implements Protocol {
 	private STATE state = STATE.NONE;
 
 	/** user entered by the client */
-	private static byte[] user;
+	private byte[] user;
 
 	/** last command sent by the client */
 	private byte[] command;
 
 	/** name of the server */
-	private static String serverName = HelperUtils.getRandomString(16, false);
+	private String serverName = HelperUtils.getRandomString(16, false);
 
-	private static String serverVersion = initServerVersion();
+	private String serverVersion = initServerVersion();
 	
-	private static String login = initLogin();
+	private String login = initLogin();
 	
-	private static String serverBanner = initServerBanner();
+	private String serverBanner = initServerBanner();
 	
 	/** command line prefix */
 	private static byte[] sessionToken = null;
@@ -112,7 +113,7 @@ public class TELNET implements Protocol {
 				responsePackets.add(new Packet("password: "));
 				state = STATE.AUTHENTICATE;
 				if (serverVersion.contains("Windows")) {
-					sessionToken = HelperUtils.concat("C:\\Users\\".getBytes(), user);
+					sessionToken = HelperUtils.concat("C:\\Users\\".getBytes(), user, ">".getBytes());
 				} else {
 					sessionToken = HelperUtils.concat(sessionPrefix, user,
 							"@".getBytes(), serverName.getBytes(), sessionMiddle,
@@ -213,7 +214,7 @@ public class TELNET implements Protocol {
 		return profile;
 	}
 	
-	private static String initServerBanner() {
+	private String initServerBanner() {
 		if (serverVersion.contains("Windows")) {
 			return "\r\n*===============================================================\r\n"
 					+ "Microsoft Telnet Server.\r\n"
@@ -222,7 +223,7 @@ public class TELNET implements Protocol {
 		return "";
 	}
 	
-	private static String initLogin() {
+	private String initLogin() {
 		if (serverVersion.contains("Windows")) {
 			return "Welcome to Microsoft Telnet Service \r\n\r\n";
 		}