PrivilegedPort.java 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package de.tudarmstadt.informatik.hostage.system;
  2. import java.io.FileDescriptor;
  3. import android.net.LocalServerSocket;
  4. import android.net.LocalSocket;
  5. /**
  6. * Port loaded with porthack with root rights
  7. * @author Mihai Plasoianu
  8. */
  9. public class PrivilegedPort implements Runnable {
  10. /**
  11. * Path for UDS in abstract namespace.
  12. */
  13. private final static String NAME = "hostage";
  14. /**
  15. * Port to bind.
  16. */
  17. private int port;
  18. public PrivilegedPort(int port) {
  19. this.port = port;
  20. }
  21. /**
  22. * Run porthack in separate Thread.
  23. */
  24. @Override
  25. public void run() {
  26. Process p;
  27. try {
  28. String command = String.format("/data/local/p %d", port);
  29. p = Runtime.getRuntime().exec(new String[] { "su", "-c", command });
  30. p.waitFor();
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. /**
  36. * Start porthack, wait for connection through UDS and receive a file
  37. * descriptor for bound port.
  38. */
  39. public FileDescriptor bindAndGetFD() {
  40. FileDescriptor fd = null;
  41. try {
  42. new Thread(this).start();
  43. LocalServerSocket lss = new LocalServerSocket(NAME);
  44. LocalSocket ls = lss.accept();
  45. while (ls.getInputStream().read() != -1)
  46. ;
  47. FileDescriptor[] fdarr;
  48. fdarr = ls.getAncillaryFileDescriptors();
  49. if (fdarr != null) {
  50. fd = fdarr[0];
  51. }
  52. ls.close();
  53. lss.close();
  54. } catch (Exception e) {
  55. e.printStackTrace();
  56. }
  57. return fd;
  58. }
  59. }