AbstractHandler.java 4.6 KB

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