AbstractHandler.java 4.4 KB

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