Handler.java 7.4 KB

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