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. public void run() {
  25. Process p;
  26. try {
  27. String command = String.format("/data/local/p %d", port);
  28. p = Runtime.getRuntime().exec(new String[] { "su", "-c", command });
  29. p.waitFor();
  30. } catch (Exception e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. /**
  35. * Start porthack, wait for connection through UDS and receive a file
  36. * descriptor for bound port.
  37. */
  38. public FileDescriptor bindAndGetFD() {
  39. FileDescriptor fd = null;
  40. try {
  41. new Thread(this).start();
  42. LocalServerSocket lss = new LocalServerSocket(NAME);
  43. LocalSocket ls = lss.accept();
  44. while (ls.getInputStream().read() != -1)
  45. ;
  46. FileDescriptor[] fdarr;
  47. fdarr = ls.getAncillaryFileDescriptors();
  48. if (fdarr != null) {
  49. fd = fdarr[0];
  50. }
  51. ls.close();
  52. lss.close();
  53. } catch (Exception e) {
  54. e.printStackTrace();
  55. }
  56. return fd;
  57. }
  58. }