GhostProtocol.java 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 GhostProtocol implements Protocol {
  18. private boolean isClosed = false;
  19. @Override
  20. public int getDefaultPort() {
  21. return 5050; // TODO dynamic port / whats the default!? (1433)
  22. }
  23. @Override
  24. public TALK_FIRST whoTalksFirst() {
  25. return TALK_FIRST.CLIENT;
  26. }
  27. @Override
  28. public List<Packet> processMessage(Packet requestPacket) {
  29. List<Packet> responsePackets = new ArrayList<Packet>();
  30. try {
  31. if (mirroredConnection == null) {
  32. mirroredConnection = new Socket("192.168.178.86", 5050); // TODO
  33. mirrorInputStream = new BufferedInputStream(
  34. mirroredConnection.getInputStream());
  35. mirrorOutputStream = new BufferedOutputStream(
  36. mirroredConnection.getOutputStream());
  37. }
  38. if (mirroredConnection.isInputShutdown()
  39. || mirroredConnection.isOutputShutdown()) {
  40. mirrorInputStream.close();
  41. mirrorOutputStream.close();
  42. mirroredConnection.close();
  43. isClosed = true;
  44. }
  45. mirrorOutputStream.write(requestPacket.getMessage());
  46. mirrorOutputStream.flush();
  47. int availableBytes;
  48. while ((availableBytes = mirrorInputStream.available()) <= 0) {
  49. try {
  50. Thread.sleep(1);
  51. } catch (InterruptedException e) {
  52. e.printStackTrace();
  53. }
  54. }
  55. byte[] mirrorResponse = new byte[availableBytes];
  56. mirrorInputStream.read(mirrorResponse);
  57. responsePackets.add(new Packet(mirrorResponse));
  58. } catch (IOException e) {
  59. e.printStackTrace();
  60. }
  61. return responsePackets;
  62. }
  63. @Override
  64. public boolean isClosed() {
  65. return isClosed;
  66. }
  67. @Override
  68. public boolean isSecure() {
  69. return false;
  70. }
  71. @Override
  72. public Class<byte[]> getType() {
  73. return byte[].class;
  74. }
  75. @Override
  76. public String toString() {
  77. return "GhostProtocol";
  78. }
  79. private Socket mirroredConnection;
  80. private BufferedInputStream mirrorInputStream;
  81. private BufferedOutputStream mirrorOutputStream;
  82. }