HoneyHandler.java 7.8 KB

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