HoneyHandler.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package de.tudarmstadt.informatik.hostage;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.net.Socket;
  6. import java.util.List;
  7. import android.content.Context;
  8. import android.content.SharedPreferences;
  9. import android.content.SharedPreferences.Editor;
  10. import android.preference.PreferenceManager;
  11. import de.tudarmstadt.informatik.hostage.io.ByteArrayReaderWriter;
  12. import de.tudarmstadt.informatik.hostage.io.ReaderWriter;
  13. import de.tudarmstadt.informatik.hostage.io.StringReaderWriter;
  14. import de.tudarmstadt.informatik.hostage.logging.Logger;
  15. import de.tudarmstadt.informatik.hostage.logging.MyLocationManager;
  16. import de.tudarmstadt.informatik.hostage.logging.Record;
  17. import de.tudarmstadt.informatik.hostage.logging.Record.TYPE;
  18. import de.tudarmstadt.informatik.hostage.protocol.Protocol;
  19. import de.tudarmstadt.informatik.hostage.protocol.Protocol.TALK_FIRST;
  20. import de.tudarmstadt.informatik.hostage.ui.MainActivity;
  21. import de.tudarmstadt.informatik.hostage.wrapper.Packet;
  22. /**
  23. * Abstract class for a connection handler using a given protocol.
  24. * @author Mihai Plasoianu
  25. * @author Wulf Pfeiffer
  26. */
  27. public class HoneyHandler implements Runnable {
  28. /** Time until the socket throws a time out. The time is in milliseconds. */
  29. private int TIMEOUT;
  30. /** Time that a inputstream waits if no content is available to read again from stream. */
  31. private int SLEEPTIME;
  32. protected Protocol protocol;
  33. private Socket client;
  34. protected Thread thread;
  35. private int attack_id;
  36. private int message_id;
  37. private String externalIP;
  38. private String BSSID;
  39. private String SSID;
  40. private HoneyListener listener;
  41. protected Logger log;
  42. /**
  43. * Constructor of the class.
  44. * Initializes class variables for communication and logging.
  45. * Then starts itself in a new Thread.
  46. * @param service The background service.
  47. * @param listener The Listener that called the service.
  48. * @param protocol The protocol on which the handler is running.
  49. * @param client A Socket for the communication with a remote client.
  50. */
  51. public HoneyHandler(HoneyService service, HoneyListener listener,
  52. Protocol protocol, Socket client) {
  53. this.listener = listener;
  54. this.log = service.getLog();
  55. this.protocol = protocol;
  56. this.client = client;
  57. this.thread = new Thread(this);
  58. SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(service);
  59. SLEEPTIME = pref.getInt("sleeptime", 500);
  60. TIMEOUT = pref.getInt("timeout", 30) * 1000;
  61. //TODO ThreadSicher?
  62. attack_id = getAndIncrementAttackID(pref);
  63. message_id = 0;
  64. SharedPreferences sessionPref = service.getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
  65. BSSID = sessionPref.getString(MainActivity.BSSID, null);
  66. SSID = sessionPref.getString(MainActivity.SSID, null);
  67. externalIP = sessionPref.getString(MainActivity.EXTERNAL_IP, null);
  68. setSoTimeout(client);
  69. thread.start();
  70. }
  71. /**
  72. * Set the timeout of the socket to the hard coded time out variable.
  73. * @param client The socket
  74. * @see #TIMEOUT
  75. */
  76. private void setSoTimeout(Socket client) {
  77. try {
  78. client.setSoTimeout(TIMEOUT);
  79. } catch (Exception e) {
  80. e.printStackTrace();
  81. }
  82. }
  83. /**
  84. * Creates InputStream and OutputStream for the socket.
  85. * Starts communication with client.
  86. * When the client closes the connection or a time out occurs the handler is finished.
  87. */
  88. @Override
  89. public void run() {
  90. InputStream in;
  91. OutputStream out;
  92. try {
  93. in = client.getInputStream();
  94. out = client.getOutputStream();
  95. talkToClient(in, out);
  96. } catch (Exception e) {
  97. e.printStackTrace();
  98. }
  99. kill();
  100. }
  101. /**
  102. * Sets the interrupt flag of the thread and tries to close the socket.
  103. */
  104. public void kill() {
  105. thread.interrupt();
  106. try {
  107. client.close();
  108. } catch (Exception e) {
  109. e.printStackTrace();
  110. }
  111. listener.refreshHandlers();
  112. }
  113. /**
  114. * Determines if the interrupt flag of the thread is set.
  115. * @return True when the flag is set, else false.
  116. */
  117. public boolean isTerminated() {
  118. return thread.isInterrupted();
  119. }
  120. /**
  121. * Communicates with a client using the corresponding protocol implementation.
  122. * @param in InputStream of the socket.
  123. * @param out OutputStream of the socket.
  124. * @throws IOException
  125. */
  126. protected void talkToClient(InputStream in, OutputStream out) throws IOException {
  127. ReaderWriter stream;
  128. if(protocol.getType().equals(byte[].class)) {
  129. stream = new ByteArrayReaderWriter(in, out, SLEEPTIME);
  130. } else {
  131. stream = new StringReaderWriter(in, out);
  132. }
  133. Packet inputLine;
  134. List<Packet> outputLine;
  135. if (protocol.whoTalksFirst() == TALK_FIRST.SERVER) {
  136. outputLine = protocol.processMessage(null);
  137. stream.write(outputLine);
  138. for (Packet o : outputLine) {
  139. log.write(createRecord(TYPE.SEND, o.toString()));
  140. }
  141. }
  142. while (!thread.isInterrupted() && (inputLine = stream.read()) != null) {
  143. outputLine = protocol.processMessage(inputLine);
  144. log.write(createRecord(TYPE.RECEIVE, inputLine.toString()));
  145. if (outputLine != null) {
  146. stream.write(outputLine);
  147. for (Packet o : outputLine) {
  148. log.write(createRecord(TYPE.SEND, o.toString()));
  149. }
  150. }
  151. if (protocol.isClosed()) {
  152. break;
  153. }
  154. }
  155. }
  156. /**
  157. * Creates a Record for a message exchanged with a client.
  158. * @param type The type of the message.
  159. * @param packet The content of the message.
  160. * @return The Record representing the communication message.
  161. */
  162. protected Record createRecord(TYPE type, String packet) {
  163. Record record = new Record();
  164. record.setId(getAndIncrementMessageCounter());
  165. record.setAttack_id(attack_id);
  166. record.setProtocol(protocol.toString());
  167. record.setType(type);
  168. record.setTimestamp(System.currentTimeMillis());
  169. record.setExternalIP(externalIP);
  170. record.setLocalIP(client.getLocalAddress());
  171. record.setLocalPort(protocol.getPort());
  172. record.setRemoteIP(client.getInetAddress());
  173. record.setRemotePort(client.getPort());
  174. record.setBSSID(BSSID);
  175. record.setSSID(SSID);
  176. record.setPacket(packet);
  177. if(MyLocationManager.getNewestLocation() != null){
  178. record.setLatitude(MyLocationManager.getNewestLocation().getLatitude() );
  179. record.setLongitude(MyLocationManager.getNewestLocation().getLongitude() );
  180. record.setAccuracy(MyLocationManager.getNewestLocation().getAccuracy() );
  181. record.setTimestampLocation(MyLocationManager.getNewestLocation().getTime() );
  182. }else {
  183. record.setLatitude(0.0);
  184. record.setLongitude(0.0);
  185. record.setAccuracy(Float.MAX_VALUE);
  186. record.setTimestampLocation(0);
  187. }
  188. return record;
  189. }
  190. /**
  191. * Gets attack ID for the attack. Also increases the attack ID counter by one. Method is synchronized for thread safety.
  192. * @param pref The default SharedPreference of the application
  193. * @return Unique integer attack ID
  194. */
  195. private synchronized int getAndIncrementAttackID(SharedPreferences pref){
  196. Editor editor = pref.edit();
  197. int attackID = pref.getInt("ATTACK_ID_COUNTER", 0);
  198. editor.putInt("ATTACK_ID_COUNTER", attack_id + 1);
  199. editor.commit();
  200. return attackID;
  201. }
  202. /**
  203. * Gets current message ID for the record. Also increases the message ID counter by one. Method is synchronized for thread safety.
  204. * @return Unique integer message ID
  205. */
  206. private synchronized int getAndIncrementMessageCounter(){
  207. int msgID = message_id;
  208. message_id++;
  209. return msgID;
  210. }
  211. }