Handler.java 8.3 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. listener.refreshHandlers();
  99. }
  100. /**
  101. * Creates InputStream and OutputStream for the socket. Starts communication
  102. * with client. When the client closes the connection or a time out occurs
  103. * the handler is finished.
  104. */
  105. @Override
  106. public void run() {
  107. service.notifyUI(this.getClass().getName(),
  108. new String[] { service.getString(R.string.broadcast_started), protocol.toString(), Integer.toString(listener.getPort()) });
  109. InputStream in;
  110. OutputStream out;
  111. try {
  112. in = client.getInputStream();
  113. out = client.getOutputStream();
  114. talkToClient(in, out);
  115. } catch (Exception e) {
  116. e.printStackTrace();
  117. }
  118. kill();
  119. }
  120. /**
  121. * Gets attack ID for the attack. Also increases the attack ID counter by
  122. * one. Method is synchronized for thread safety.
  123. *
  124. * @param pref
  125. * The default SharedPreference of the application
  126. * @return Unique integer attack ID
  127. */
  128. private synchronized void getAndIncrementAttackID(SharedPreferences pref) {
  129. Editor editor = pref.edit();
  130. attack_id = pref.getInt("ATTACK_ID_COUNTER", 0);
  131. editor.putInt("ATTACK_ID_COUNTER", attack_id + 1);
  132. editor.commit();
  133. }
  134. /**
  135. * Set the timeout of the socket to the hard coded time out variable.
  136. *
  137. * @param client
  138. * The socket
  139. * @see #TIMEOUT
  140. */
  141. private void setSoTimeout(Socket client) {
  142. try {
  143. client.setSoTimeout(TIMEOUT);
  144. } catch (Exception e) {
  145. e.printStackTrace();
  146. }
  147. }
  148. /**
  149. * Creates a MessageRecord for a message exchanged with a client.
  150. *
  151. * @param type
  152. * The type of the message.
  153. * @param packet
  154. * The content of the message.
  155. * @return The Record representing the communication message.
  156. */
  157. protected MessageRecord createMessageRecord(TYPE type, String packet) {
  158. MessageRecord record = new MessageRecord();
  159. record.setId(message_id++);
  160. record.setAttack_id(attack_id);
  161. record.setType(type);
  162. record.setTimestamp(System.currentTimeMillis());
  163. record.setPacket(packet);
  164. return record;
  165. }
  166. /**
  167. * Creates a AttackRecord for a specific attack from a client.
  168. *
  169. * @return The AttackRecord representing the attack.
  170. */
  171. protected AttackRecord createAttackRecord() {
  172. AttackRecord record = new AttackRecord();
  173. record.setAttack_id(attack_id);
  174. record.setProtocol(protocol.toString());
  175. record.setExternalIP(externalIP);
  176. record.setLocalIP(client.getLocalAddress().getHostAddress());
  177. record.setLocalPort(protocol.getPort());
  178. record.setRemoteIP(client.getInetAddress().getHostAddress());
  179. record.setRemotePort(client.getPort());
  180. record.setBssid(BSSID);
  181. return record;
  182. }
  183. /**
  184. * Creates a NetworkRecord containing information about the current network.
  185. *
  186. * @return The NetworkRecord representing the current network.
  187. */
  188. protected NetworkRecord createNetworkRecord() {
  189. NetworkRecord record = new NetworkRecord();
  190. record.setBssid(BSSID);
  191. record.setSsid(SSID);
  192. if (MyLocationManager.getNewestLocation() != null) {
  193. record.setLatitude(MyLocationManager.getNewestLocation().getLatitude());
  194. record.setLongitude(MyLocationManager.getNewestLocation().getLongitude());
  195. record.setAccuracy(MyLocationManager.getNewestLocation().getAccuracy());
  196. record.setTimestampLocation(MyLocationManager.getNewestLocation().getTime());
  197. } else {
  198. record.setLatitude(0.0);
  199. record.setLongitude(0.0);
  200. record.setAccuracy(Float.MAX_VALUE);
  201. record.setTimestampLocation(0);
  202. }
  203. return record;
  204. }
  205. /**
  206. * Communicates with a client using the corresponding protocol
  207. * implementation.
  208. *
  209. * @param in
  210. * InputStream of the socket.
  211. * @param out
  212. * OutputStream of the socket.
  213. * @throws IOException
  214. */
  215. protected void talkToClient(InputStream in, OutputStream out) throws IOException {
  216. Reader reader = new Reader(in, protocol.toString());
  217. Writer writer = new Writer(out);
  218. Packet inputLine;
  219. List<Packet> outputLine;
  220. if (protocol.whoTalksFirst() == TALK_FIRST.SERVER) {
  221. outputLine = protocol.processMessage(null);
  222. writer.write(outputLine);
  223. for (Packet o : outputLine) {
  224. Logger.log(Hostage.getContext(), createMessageRecord(TYPE.SEND, o.toString()));
  225. }
  226. }
  227. while (!thread.isInterrupted() && (inputLine = reader.read()) != null) {
  228. outputLine = protocol.processMessage(inputLine);
  229. Logger.log(Hostage.getContext(), createMessageRecord(TYPE.RECEIVE, inputLine.toString()));
  230. if (outputLine != null) {
  231. writer.write(outputLine);
  232. for (Packet o : outputLine) {
  233. Logger.log(Hostage.getContext(), createMessageRecord(TYPE.SEND, o.toString()));
  234. }
  235. }
  236. if (protocol.isClosed()) {
  237. break;
  238. }
  239. }
  240. }
  241. }