AbstractHandler.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 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
  52. .getDefaultSharedPreferences(service);
  53. Editor editor = pref.edit();
  54. TIMEOUT = pref.getInt("timeout", 30) * 1000;
  55. attack_id = pref.getInt("ATTACK_ID_COUNTER", 0);
  56. editor.putInt("ATTACK_ID_COUNTER", attack_id + 1);
  57. editor.commit();
  58. BSSID = HelperUtils.getBSSID(service.getApplicationContext());
  59. SSID = HelperUtils.getSSID(service.getApplicationContext());
  60. externalIP = HelperUtils.getExternalIP(service.getApplicationContext());
  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. @Override
  82. public void run() {
  83. InputStream in;
  84. OutputStream out;
  85. try {
  86. in = client.getInputStream();
  87. out = client.getOutputStream();
  88. talkToClient(in, out);
  89. } catch (Exception e) {
  90. e.printStackTrace();
  91. }
  92. kill();
  93. }
  94. /**
  95. * Sets the interrupt flag of the thread and tries to close the socket.
  96. */
  97. public void kill() {
  98. thread.interrupt();
  99. try {
  100. client.close();
  101. } catch (Exception e) {
  102. e.printStackTrace();
  103. }
  104. listener.refreshHandlers();
  105. }
  106. /**
  107. * Determines if the interrupt flag of the thread is set.
  108. * @return True when the flag is set, else false.
  109. */
  110. public boolean isTerminated() {
  111. return thread.isInterrupted();
  112. }
  113. /**
  114. * Communicates with a client using the corresponding protocol implementation.
  115. * @param in InputStream of the socket.
  116. * @param out OutputStream of the socket.
  117. * @throws IOException
  118. */
  119. abstract protected void talkToClient(InputStream in, OutputStream out)
  120. throws IOException;
  121. /**
  122. * Creates a Record for a message exchanged with a client.
  123. * @param type The type of the message.
  124. * @param packet The content of the message.
  125. * @return The Record representing the communication message.
  126. */
  127. protected Record createRecord(TYPE type, String packet) {
  128. Record record = new Record();
  129. record.setAttack_id(attack_id);
  130. record.setProtocol(protocol.toString());
  131. record.setType(type);
  132. record.setTimestamp(System.currentTimeMillis());
  133. record.setExternalIP(externalIP);
  134. record.setLocalIP(client.getLocalAddress());
  135. record.setLocalPort(protocol.getPort());
  136. record.setRemoteIP(client.getInetAddress());
  137. record.setRemotePort(client.getPort());
  138. record.setBSSID(BSSID);
  139. record.setSSID(SSID);
  140. record.setPacket(packet);
  141. return record;
  142. }
  143. }