HoneyHandler.java 7.7 KB

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