AbstractHandler.java 4.2 KB

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