Handler.java 8.4 KB

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