AbstractHandler.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package de.tudarmstadt.informatik.hostage.handler;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.net.Socket;
  6. import android.content.SharedPreferences;
  7. import android.content.SharedPreferences.Editor;
  8. import android.preference.PreferenceManager;
  9. import de.tudarmstadt.informatik.hostage.HoneyListener;
  10. import de.tudarmstadt.informatik.hostage.HoneyService;
  11. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  12. import de.tudarmstadt.informatik.hostage.logging.Logger;
  13. import de.tudarmstadt.informatik.hostage.logging.Record;
  14. import de.tudarmstadt.informatik.hostage.logging.Record.TYPE;
  15. import de.tudarmstadt.informatik.hostage.protocol.Protocol;
  16. public abstract class AbstractHandler implements Runnable {
  17. protected static final int TIMEOUT = 30 * 1000;
  18. protected Protocol protocol;
  19. private Socket client;
  20. protected Thread thread;
  21. private int attack_id;
  22. private String BSSID;
  23. private String SSID;
  24. private HoneyListener listener;
  25. protected Logger log;
  26. public AbstractHandler(HoneyService service, HoneyListener listener,
  27. Protocol protocol, Socket client) {
  28. this.listener = listener;
  29. this.log = service.getLog();
  30. this.protocol = protocol;
  31. this.client = client;
  32. this.thread = new Thread(this);
  33. // TODO attack_id für jeden Logger
  34. SharedPreferences pref = PreferenceManager
  35. .getDefaultSharedPreferences(service);
  36. Editor editor = pref.edit();
  37. attack_id = pref.getInt("ATTACK_ID_COUNTER", 0);
  38. editor.putInt("ATTACK_ID_COUNTER", attack_id + 1);
  39. editor.commit();
  40. BSSID = HelperUtils.getBSSID(service.getApplicationContext());
  41. SSID = HelperUtils.getSSID(service.getApplicationContext());
  42. setSoTimeout(client);
  43. thread.start();
  44. }
  45. private void setSoTimeout(Socket client) {
  46. try {
  47. client.setSoTimeout(TIMEOUT);
  48. } catch (Exception e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. @Override
  53. public void run() {
  54. InputStream in;
  55. OutputStream out;
  56. try {
  57. in = client.getInputStream();
  58. out = client.getOutputStream();
  59. talkToClient(in, out);
  60. } catch (Exception e) {
  61. e.printStackTrace();
  62. }
  63. kill();
  64. }
  65. public void kill() {
  66. thread.interrupt();
  67. try {
  68. client.close();
  69. } catch (Exception e) {
  70. e.printStackTrace();
  71. }
  72. listener.refreshHandlers();
  73. }
  74. public boolean isTerminated() {
  75. return thread.isInterrupted();
  76. }
  77. abstract protected void talkToClient(InputStream in, OutputStream out)
  78. throws IOException;
  79. protected Record createRecord(TYPE type, String packet) {
  80. Record record = new Record();
  81. record.setAttack_id(attack_id);
  82. record.setProtocol(protocol.toString());
  83. record.setType(type);
  84. record.setTimestamp(System.currentTimeMillis());
  85. record.setLocalIP(client.getLocalAddress());
  86. record.setLocalPort(protocol.getPort());
  87. record.setRemoteIP(client.getInetAddress());
  88. record.setRemotePort(client.getPort());
  89. record.setBSSID(BSSID);
  90. record.setSSID(SSID);
  91. record.setPacket(packet);
  92. return record;
  93. }
  94. }