HoneyHandler.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package de.tudarmstadt.informatik.hostage;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.net.Socket;
  6. import java.util.List;
  7. import android.content.Context;
  8. import android.content.SharedPreferences;
  9. import android.content.SharedPreferences.Editor;
  10. import android.preference.PreferenceManager;
  11. import android.util.Log;
  12. import de.tudarmstadt.informatik.hostage.io.ByteArrayReaderWriter;
  13. import de.tudarmstadt.informatik.hostage.io.ReaderWriter;
  14. import de.tudarmstadt.informatik.hostage.io.StringReaderWriter;
  15. import de.tudarmstadt.informatik.hostage.logging.Logger;
  16. import de.tudarmstadt.informatik.hostage.logging.MyLocationManager;
  17. import de.tudarmstadt.informatik.hostage.logging.Record;
  18. import de.tudarmstadt.informatik.hostage.logging.Record.TYPE;
  19. import de.tudarmstadt.informatik.hostage.protocol.Protocol;
  20. import de.tudarmstadt.informatik.hostage.protocol.Protocol.TALK_FIRST;
  21. import de.tudarmstadt.informatik.hostage.ui.MainActivity;
  22. import de.tudarmstadt.informatik.hostage.wrapper.Packet;
  23. /**
  24. * Abstract class for a connection handler using a given protocol.
  25. *
  26. * @author Mihai Plasoianu
  27. * @author Wulf Pfeiffer
  28. */
  29. public class HoneyHandler implements Runnable {
  30. /** Time until the socket throws a time out. The time is in milliseconds. */
  31. private int TIMEOUT;
  32. /**
  33. * Time that a inputstream waits if no content is available to read again
  34. * from stream.
  35. */
  36. private int SLEEPTIME;
  37. protected Protocol protocol;
  38. private Socket client;
  39. protected Thread thread;
  40. private int attack_id;
  41. private int message_id = 0;
  42. private String externalIP;
  43. private String BSSID;
  44. private String SSID;
  45. private HoneyListener listener;
  46. /**
  47. * Constructor of the class. Initializes class variables for communication
  48. * and logging. Then starts itself in a new Thread.
  49. *
  50. * @param service
  51. * The background service.
  52. * @param listener
  53. * The Listener that called the service.
  54. * @param protocol
  55. * The protocol on which the handler is running.
  56. * @param client
  57. * A Socket for the communication with a remote client.
  58. */
  59. public HoneyHandler(HoneyService service, HoneyListener listener,
  60. Protocol protocol, Socket client) {
  61. this.listener = listener;
  62. this.protocol = protocol;
  63. this.client = client;
  64. this.thread = new Thread(this);
  65. SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(service);
  66. SLEEPTIME = pref.getInt("sleeptime", 1); // 1 ms already removes ressource leak
  67. TIMEOUT = pref.getInt("timeout", 30) * 1000;
  68. // TODO ThreadSicher?
  69. getAndIncrementAttackID(pref);
  70. SharedPreferences sessionPref = service.getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
  71. BSSID = sessionPref.getString(MainActivity.BSSID, null);
  72. SSID = sessionPref.getString(MainActivity.SSID, null);
  73. externalIP = sessionPref.getString(MainActivity.EXTERNAL_IP, null);
  74. setSoTimeout(client);
  75. thread.start();
  76. }
  77. /**
  78. * Set the timeout of the socket to the hard coded time out variable.
  79. *
  80. * @param client
  81. * The socket
  82. * @see #TIMEOUT
  83. */
  84. private void setSoTimeout(Socket client) {
  85. try {
  86. client.setSoTimeout(TIMEOUT);
  87. } catch (Exception e) {
  88. e.printStackTrace();
  89. }
  90. }
  91. /**
  92. * Creates InputStream and OutputStream for the socket. Starts communication
  93. * with client. When the client closes the connection or a time out occurs
  94. * the handler is finished.
  95. */
  96. @Override
  97. public void run() {
  98. InputStream in;
  99. OutputStream out;
  100. try {
  101. in = client.getInputStream();
  102. out = client.getOutputStream();
  103. talkToClient(in, out);
  104. } catch (Exception e) {
  105. e.printStackTrace();
  106. }
  107. Log.i("HoneyHandler", "Connection closed, close Socket and kill Thread");
  108. kill();
  109. }
  110. /**
  111. * Sets the interrupt flag of the thread and tries to close the socket.
  112. */
  113. public void kill() {
  114. thread.interrupt();
  115. try {
  116. client.close();
  117. Log.i("HoneyHandler", "Socket closed: " + client.isClosed());
  118. } catch (Exception e) {
  119. e.printStackTrace();
  120. }
  121. listener.refreshHandlers();
  122. }
  123. /**
  124. * Determines if the interrupt flag of the thread is set.
  125. *
  126. * @return True when the flag is set, else false.
  127. */
  128. public boolean isTerminated() {
  129. return thread.isInterrupted();
  130. }
  131. /**
  132. * Communicates with a client using the corresponding protocol
  133. * implementation.
  134. *
  135. * @param in
  136. * InputStream of the socket.
  137. * @param out
  138. * OutputStream of the socket.
  139. * @throws IOException
  140. */
  141. protected void talkToClient(InputStream in, OutputStream out)
  142. throws IOException {
  143. ReaderWriter stream;
  144. if (protocol.getType().equals(byte[].class)) {
  145. stream = new ByteArrayReaderWriter(in, out, SLEEPTIME);
  146. } else {
  147. stream = new StringReaderWriter(in, out);
  148. }
  149. Packet inputLine;
  150. List<Packet> outputLine;
  151. if (protocol.whoTalksFirst() == TALK_FIRST.SERVER) {
  152. outputLine = protocol.processMessage(null);
  153. stream.write(outputLine);
  154. for (Packet o : outputLine) {
  155. Logger.log(HoneyService.getContext(),
  156. createRecord(TYPE.SEND, o.toString()));
  157. }
  158. }
  159. while (!thread.isInterrupted() && (inputLine = stream.read()) != null) {
  160. outputLine = protocol.processMessage(inputLine);
  161. Logger.log(HoneyService.getContext(),
  162. createRecord(TYPE.RECEIVE, inputLine.toString()));
  163. if (outputLine != null) {
  164. stream.write(outputLine);
  165. for (Packet o : outputLine) {
  166. Logger.log(HoneyService.getContext(),
  167. createRecord(TYPE.SEND, o.toString()));
  168. }
  169. }
  170. if (protocol.isClosed()) {
  171. break;
  172. }
  173. }
  174. }
  175. /**
  176. * Creates a Record for a message exchanged with a client.
  177. *
  178. * @param type
  179. * The type of the message.
  180. * @param packet
  181. * The content of the message.
  182. * @return The Record representing the communication message.
  183. */
  184. protected Record createRecord(TYPE type, String packet) {
  185. Record record = new Record();
  186. record.setId(message_id++);
  187. record.setAttack_id(attack_id);
  188. record.setProtocol(protocol.toString());
  189. record.setType(type);
  190. record.setTimestamp(System.currentTimeMillis());
  191. record.setExternalIP(externalIP);
  192. record.setLocalIP(client.getLocalAddress().getHostAddress());
  193. record.setLocalHost(client.getLocalAddress().getHostName());
  194. record.setLocalPort(protocol.getPort());
  195. record.setRemoteIP(client.getInetAddress().getHostAddress());
  196. record.setRemoteHost(client.getInetAddress().getHostName());
  197. record.setRemotePort(client.getPort());
  198. record.setBssid(BSSID);
  199. record.setSsid(SSID);
  200. record.setPacket(packet);
  201. if (MyLocationManager.getNewestLocation() != null) {
  202. record.setLatitude(MyLocationManager.getNewestLocation()
  203. .getLatitude());
  204. record.setLongitude(MyLocationManager.getNewestLocation()
  205. .getLongitude());
  206. record.setAccuracy(MyLocationManager.getNewestLocation()
  207. .getAccuracy());
  208. record.setTimestampLocation(MyLocationManager.getNewestLocation()
  209. .getTime());
  210. } else {
  211. record.setLatitude(0.0);
  212. record.setLongitude(0.0);
  213. record.setAccuracy(Float.MAX_VALUE);
  214. record.setTimestampLocation(0);
  215. }
  216. return record;
  217. }
  218. /**
  219. * Gets attack ID for the attack. Also increases the attack ID counter by
  220. * one. Method is synchronized for thread safety.
  221. *
  222. * @param pref
  223. * The default SharedPreference of the application
  224. * @return Unique integer attack ID
  225. */
  226. private synchronized void getAndIncrementAttackID(SharedPreferences pref) {
  227. Editor editor = pref.edit();
  228. attack_id = pref.getInt("ATTACK_ID_COUNTER", 0);
  229. editor.putInt("ATTACK_ID_COUNTER", attack_id + 1);
  230. editor.commit();
  231. }
  232. }