ConnectionGuard.java 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package de.tudarmstadt.informatik.hostage;
  2. import android.content.SharedPreferences;
  3. import android.preference.PreferenceManager;
  4. import android.util.Log;
  5. /**
  6. * Class used to detect port scans.
  7. * We assume a port scan if at least 2 different ports get a connection in a small amount of time.
  8. *
  9. */
  10. public class ConnectionGuard {
  11. private final static ConnectionGuard INSTANCE = new ConnectionGuard();
  12. private ConnectionGuard() {
  13. }
  14. private static long lastConnectionTimestamp = 0;
  15. private static long lastPortscanTimestamp = 0;
  16. private static String lastIP = "";
  17. private static int lastPort = 0;
  18. private static long getPortscanTimeout() {
  19. // this might be called in a time critical context so maybe
  20. // we don't want to do this each time and instead just once
  21. SharedPreferences defaultPref = PreferenceManager
  22. .getDefaultSharedPreferences(Hostage.getContext());
  23. // the pref value is in seconds
  24. int portscanTimeoutS = Integer.parseInt(defaultPref.getString("pref_timeout", "60"));
  25. long portscanTimeoutMS = 1000 * portscanTimeoutS;
  26. return portscanTimeoutMS;
  27. }
  28. /**
  29. * Register a connection for port scan detection. Stores information about the last connection.
  30. * @param port The local port used for communication.
  31. * @param ip The IP address of the remote device.
  32. * @return True if a port scan has been detected.
  33. */
  34. public synchronized static boolean registerConnection(int port, String ip) {
  35. long timestamp = System.currentTimeMillis();
  36. boolean result = detectedPortscan(port, ip, timestamp);
  37. lastConnectionTimestamp = timestamp;
  38. if (result) {
  39. lastPortscanTimestamp = timestamp;
  40. }
  41. lastIP = ip;
  42. lastPort = port;
  43. return result;
  44. }
  45. public synchronized static boolean portscanInProgress() {
  46. return (System.currentTimeMillis() - lastPortscanTimestamp) < getPortscanTimeout();
  47. }
  48. /**
  49. * Check if the new connection is part of a port scan attack.
  50. * @param port The local port used for communication.
  51. * @param ip The IP address of the remote device.
  52. * @param timestamp Time stamp of connection
  53. * @return True if a port scan has been detected.
  54. */
  55. private synchronized static boolean detectedPortscan(int port, String ip, long timestamp) {
  56. Log.i("Alte Werte:", "LastTime: " + lastConnectionTimestamp + " ,LastIP: " + lastIP + ", lastPort:" + port);
  57. Log.i("Alte Werte:", "Time: " + timestamp + " ,IP: " + ip + ", Port:" + port);
  58. boolean result = false;
  59. boolean belowThreshold = ((timestamp - lastConnectionTimestamp) < getPortscanTimeout());
  60. boolean sameIP = (lastIP.equals(ip));
  61. boolean samePort = (lastPort == port);
  62. if (sameIP && belowThreshold && !samePort) {
  63. result = true;
  64. }
  65. return result;
  66. }
  67. }