AbstractHandler.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.MyLocationManager;
  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. public abstract class AbstractHandler implements Runnable {
  23. /**
  24. * Time until the socket throws a time out. The time is in milliseconds.
  25. */
  26. private int TIMEOUT;
  27. protected Protocol protocol;
  28. private Socket client;
  29. protected Thread thread;
  30. private int attack_id;
  31. private int message_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. TIMEOUT = pref.getInt("timeout", 30) * 1000;
  55. //TODO ThreadSicher?
  56. attack_id = getAndIncrementAttackID(pref);
  57. message_id = 0;
  58. SharedPreferences sessionPref = service.getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
  59. BSSID = sessionPref.getString(MainActivity.BSSID, null);
  60. SSID = sessionPref.getString(MainActivity.SSID, null);
  61. externalIP = sessionPref.getString(MainActivity.EXTERNAL_IP, null);
  62. setSoTimeout(client);
  63. thread.start();
  64. }
  65. /**
  66. * Set the timeout of the socket to the hard coded time out variable.
  67. * @param client The socket
  68. * @see #TIMEOUT
  69. */
  70. private void setSoTimeout(Socket client) {
  71. try {
  72. client.setSoTimeout(TIMEOUT);
  73. } catch (Exception e) {
  74. e.printStackTrace();
  75. }
  76. }
  77. /**
  78. * Creates InputStream and OutputStream for the socket.
  79. * Starts communication with client.
  80. * When the client closes the connection or a time out occurs the handler is finished.
  81. */
  82. @Override
  83. public void run() {
  84. InputStream in;
  85. OutputStream out;
  86. try {
  87. in = client.getInputStream();
  88. out = client.getOutputStream();
  89. talkToClient(in, out);
  90. } catch (Exception e) {
  91. e.printStackTrace();
  92. }
  93. kill();
  94. }
  95. /**
  96. * Sets the interrupt flag of the thread and tries to close the socket.
  97. */
  98. public void kill() {
  99. thread.interrupt();
  100. try {
  101. client.close();
  102. } catch (Exception e) {
  103. e.printStackTrace();
  104. }
  105. listener.refreshHandlers();
  106. }
  107. /**
  108. * Determines if the interrupt flag of the thread is set.
  109. * @return True when the flag is set, else false.
  110. */
  111. public boolean isTerminated() {
  112. return thread.isInterrupted();
  113. }
  114. /**
  115. * Communicates with a client using the corresponding protocol implementation.
  116. * @param in InputStream of the socket.
  117. * @param out OutputStream of the socket.
  118. * @throws IOException
  119. */
  120. abstract protected void talkToClient(InputStream in, OutputStream out)
  121. throws IOException;
  122. /**
  123. * Creates a Record for a message exchanged with a client.
  124. * @param type The type of the message.
  125. * @param packet The content of the message.
  126. * @return The Record representing the communication message.
  127. */
  128. protected Record createRecord(TYPE type, String packet) {
  129. Record record = new Record();
  130. record.setId(getAndIncrementMessageCounter());
  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. if(MyLocationManager.getNewestLocation() != null){
  144. record.setLatitude(MyLocationManager.getNewestLocation().getLatitude() );
  145. record.setLongitude(MyLocationManager.getNewestLocation().getLongitude() );
  146. record.setAccuracy(MyLocationManager.getNewestLocation().getAccuracy() );
  147. record.setTimestampLocation(MyLocationManager.getNewestLocation().getTime() );
  148. }else {
  149. record.setLatitude(0.0);
  150. record.setLongitude(0.0);
  151. record.setAccuracy(Float.MAX_VALUE);
  152. record.setTimestampLocation(0);
  153. }
  154. return record;
  155. }
  156. /**
  157. * Gets attack ID for the attack. Also increases the attack ID counter by one. Method is synchronized for thread safety.
  158. * @param pref The default SharedPreference of the application
  159. * @return Unique integer attack ID
  160. */
  161. private synchronized int getAndIncrementAttackID(SharedPreferences pref){
  162. Editor editor = pref.edit();
  163. int attackID = pref.getInt("ATTACK_ID_COUNTER", 0);
  164. editor.putInt("ATTACK_ID_COUNTER", attack_id + 1);
  165. editor.commit();
  166. return attackID;
  167. }
  168. /**
  169. * Gets current message ID for the record. Also increases the message ID counter by one. Method is synchronized for thread safety.
  170. * @return Unique integer message ID
  171. */
  172. private synchronized int getAndIncrementMessageCounter(){
  173. int msgID = message_id;
  174. message_id++;
  175. return msgID;
  176. }
  177. }