GHOST.java 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package de.tudarmstadt.informatik.hostage.protocol;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.IOException;
  5. import java.net.Socket;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import de.tudarmstadt.informatik.hostage.wrapper.Packet;
  9. /**
  10. * Ghost Protocol. This protocol mirrors an incoming connection back to the
  11. * attacker on the same port, that it is running on. It will send all incoming
  12. * requests back to the attacker on the mirrored connection and will relpy with
  13. * the responses it get's from this mirrored connection.
  14. *
  15. * @author Wulf Pfeiffer
  16. */
  17. public class GHOST implements Protocol {
  18. private boolean isClosed = false;
  19. private Socket mirroredConnection;
  20. private BufferedInputStream mirrorInputStream;
  21. private BufferedOutputStream mirrorOutputStream;
  22. @Override
  23. public int getPort() {
  24. return 5050; // TODO dynamic port / whats the default!? (1433)
  25. }
  26. @Override
  27. public boolean isClosed() {
  28. return isClosed;
  29. }
  30. @Override
  31. public boolean isSecure() {
  32. return false;
  33. }
  34. @Override
  35. public List<Packet> processMessage(Packet requestPacket) {
  36. List<Packet> responsePackets = new ArrayList<Packet>();
  37. try {
  38. if (mirroredConnection == null) {
  39. mirroredConnection = new Socket("192.168.178.86", 5050); // FIXME
  40. mirrorInputStream = new BufferedInputStream(
  41. mirroredConnection.getInputStream());
  42. mirrorOutputStream = new BufferedOutputStream(
  43. mirroredConnection.getOutputStream());
  44. }
  45. if (mirroredConnection.isInputShutdown()
  46. || mirroredConnection.isOutputShutdown()) {
  47. mirrorInputStream.close();
  48. mirrorOutputStream.close();
  49. mirroredConnection.close();
  50. isClosed = true;
  51. }
  52. mirrorOutputStream.write(requestPacket.getBytes());
  53. mirrorOutputStream.flush();
  54. int availableBytes;
  55. while ((availableBytes = mirrorInputStream.available()) <= 0) {
  56. try {
  57. Thread.sleep(1);
  58. } catch (InterruptedException e) {
  59. e.printStackTrace();
  60. }
  61. }
  62. byte[] mirrorResponse = new byte[availableBytes];
  63. mirrorInputStream.read(mirrorResponse);
  64. responsePackets.add(new Packet(mirrorResponse));
  65. } catch (IOException e) {
  66. e.printStackTrace();
  67. }
  68. return responsePackets;
  69. }
  70. @Override
  71. public String toString() {
  72. return "GhostProtocol";
  73. }
  74. @Override
  75. public TALK_FIRST whoTalksFirst() {
  76. return TALK_FIRST.CLIENT;
  77. }
  78. }