AbstractHandler.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package de.tudarmstadt.informatik.hostage.handler;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.net.Socket;
  6. import android.content.Context;
  7. import android.content.SharedPreferences;
  8. import android.content.SharedPreferences.Editor;
  9. import android.preference.PreferenceManager;
  10. import de.tudarmstadt.informatik.hostage.HoneyListener;
  11. import de.tudarmstadt.informatik.hostage.HoneyService;
  12. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  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.protocol.Protocol;
  17. import de.tudarmstadt.informatik.hostage.ui.MainActivity;
  18. /**
  19. * Abstract class for a connection handler using a given protocol.
  20. * @author Mihai Plasoianu
  21. *
  22. */
  23. public abstract class AbstractHandler implements Runnable {
  24. /**
  25. * Time until the socket throws a time out. The time is in milliseconds.
  26. */
  27. private int TIMEOUT;
  28. protected Protocol protocol;
  29. private Socket client;
  30. protected Thread thread;
  31. private int attack_id;
  32. private String externalIP;
  33. private String BSSID;
  34. private String SSID;
  35. private HoneyListener listener;
  36. protected Logger log;
  37. /**
  38. * Constructor of the class.
  39. * Initializes class variables for communication and logging.
  40. * Then starts itself in a new Thread.
  41. * @param service The background service.
  42. * @param listener The Listener that called the service.
  43. * @param protocol The protocol on which the handler is running.
  44. * @param client A Socket for the communication with a remote client.
  45. */
  46. public AbstractHandler(HoneyService service, HoneyListener listener,
  47. Protocol protocol, Socket client) {
  48. this.listener = listener;
  49. this.log = service.getLog();
  50. this.protocol = protocol;
  51. this.client = client;
  52. this.thread = new Thread(this);
  53. SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(service);
  54. Editor editor = pref.edit();
  55. TIMEOUT = pref.getInt("timeout", 30) * 1000;
  56. attack_id = pref.getInt("ATTACK_ID_COUNTER", 0);
  57. editor.putInt("ATTACK_ID_COUNTER", attack_id + 1);
  58. editor.commit();
  59. SharedPreferences sessionPref = service.getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
  60. BSSID = sessionPref.getString(MainActivity.BSSID, null);
  61. SSID = sessionPref.getString(MainActivity.SSID, null);
  62. externalIP = sessionPref.getString(MainActivity.EXTERNAL_IP, null);
  63. setSoTimeout(client);
  64. thread.start();
  65. }
  66. /**
  67. * Set the timeout of the socket to the hard coded time out variable.
  68. * @param client The socket
  69. * @see #TIMEOUT
  70. */
  71. private void setSoTimeout(Socket client) {
  72. try {
  73. client.setSoTimeout(TIMEOUT);
  74. } catch (Exception e) {
  75. e.printStackTrace();
  76. }
  77. }
  78. /**
  79. * Creates InputStream and OutputStream for the socket.
  80. * Starts communication with client.
  81. * When the client closes the connection or a time out occurs the handler is finished.
  82. */
  83. @Override
  84. public void run() {
  85. InputStream in;
  86. OutputStream out;
  87. try {
  88. in = client.getInputStream();
  89. out = client.getOutputStream();
  90. talkToClient(in, out);
  91. } catch (Exception e) {
  92. e.printStackTrace();
  93. }
  94. kill();
  95. }
  96. /**
  97. * Sets the interrupt flag of the thread and tries to close the socket.
  98. */
  99. public void kill() {
  100. thread.interrupt();
  101. try {
  102. client.close();
  103. } catch (Exception e) {
  104. e.printStackTrace();
  105. }
  106. listener.refreshHandlers();
  107. }
  108. /**
  109. * Determines if the interrupt flag of the thread is set.
  110. * @return True when the flag is set, else false.
  111. */
  112. public boolean isTerminated() {
  113. return thread.isInterrupted();
  114. }
  115. /**
  116. * Communicates with a client using the corresponding protocol implementation.
  117. * @param in InputStream of the socket.
  118. * @param out OutputStream of the socket.
  119. * @throws IOException
  120. */
  121. abstract protected void talkToClient(InputStream in, OutputStream out)
  122. throws IOException;
  123. /**
  124. * Creates a Record for a message exchanged with a client.
  125. * @param type The type of the message.
  126. * @param packet The content of the message.
  127. * @return The Record representing the communication message.
  128. */
  129. protected Record createRecord(TYPE type, String packet) {
  130. Record record = new Record();
  131. record.setAttack_id(attack_id);
  132. record.setProtocol(protocol.toString());
  133. record.setType(type);
  134. record.setTimestamp(System.currentTimeMillis());
  135. record.setExternalIP(externalIP);
  136. record.setLocalIP(client.getLocalAddress());
  137. record.setLocalPort(protocol.getPort());
  138. record.setRemoteIP(client.getInetAddress());
  139. record.setRemotePort(client.getPort());
  140. record.setBSSID(BSSID);
  141. record.setSSID(SSID);
  142. record.setPacket(packet);
  143. return record;
  144. }
  145. }