Device.java 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package de.tudarmstadt.informatik.hostage.system;
  2. import java.io.IOException;
  3. import android.util.Log;
  4. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  5. public class Device {
  6. private static String porthackFilepath = "/data/local/bind";
  7. private static boolean initialized = false;
  8. private static boolean root = false; // device is rooted
  9. private static boolean porthack = false; // porthack installed
  10. private static boolean iptables = false; // iptables redirection confirmed working
  11. public static void checkCapabilities() {
  12. // assume worst case
  13. initialized = false;
  14. root = false;
  15. porthack = false;
  16. iptables = false;
  17. porthackFilepath = HelperUtils.getPorthackFilepath();
  18. String porthackExists = "[ -e "+porthackFilepath+" ]"; // checks existence of porthack
  19. try {
  20. Process p = new ProcessBuilder("su", "-c", porthackExists).start();
  21. switch (p.waitFor()) {
  22. case 0: porthack = true;
  23. // fall through and don't break
  24. case 1: root = true; // 0 and 1 are valid return values of the porthack
  25. break;
  26. case 127: // command not found or executable
  27. root = false;
  28. porthack = false;
  29. break;
  30. }
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. } catch (InterruptedException e) {
  34. e.printStackTrace();
  35. }
  36. final String ipTablesList = "iptables -L -n -t nat"; // list all rules in NAT table
  37. try {
  38. Process p = new ProcessBuilder("su", "-c", ipTablesList).start();
  39. switch (p.waitFor()) {
  40. case 0: // everything is fine
  41. iptables = true; // iptables available and working
  42. break;
  43. case 3: // no such table
  44. case 127: // command not found
  45. default: // unexpected return code
  46. // while testing code 3 has been returned when table NAT is not available
  47. iptables = false;
  48. }
  49. } catch (IOException e) {
  50. e.printStackTrace();
  51. } catch (InterruptedException e) {
  52. e.printStackTrace();
  53. }
  54. initialized = true;
  55. }
  56. public static boolean isRooted() {
  57. assert(initialized);
  58. return root;
  59. }
  60. public static boolean isPorthackInstalled() {
  61. assert(initialized);
  62. return porthack;
  63. }
  64. public static boolean isPortRedirectionAvailable() { // using iptables
  65. assert(initialized);
  66. return iptables;
  67. }
  68. }