HoneyHandler.java 7.5 KB

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