TCPUrgencyChannel.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #ifndef TCPURGENCYCHANNEL_H
  2. #define TCPURGENCYCHANNEL_H
  3. #include "CovertChannel.h"
  4. #include "CovertProtocol.hpp"
  5. /**
  6. * @class TCPUrgencyChannel
  7. *
  8. * A CovertChannel which hides data in the TCP urgency pointer
  9. *
  10. * @param PASSIVE true - server only reacts to incoming channel | false - server initiates channel
  11. */
  12. template <bool PASSIVE> class TCPUrgencyChannel : public CovertChannel {
  13. public:
  14. /**
  15. * Sets up a CovertChannel.
  16. *
  17. * Creates a CovertChannel, sets the network interfaces for sniffing and sending and sets the filter.
  18. *
  19. * @param innerInterface name of the interface of the inner network
  20. * @param outerInterface name of the interface of the outer network
  21. * @param ownIP IP of this server
  22. * @param targetIP IP of the target server
  23. * @param targetPort Port of the target server
  24. */
  25. TCPUrgencyChannel(const std::string &innerInterface, const std::string &outerInterface, const std::string &ownIP, const std::string &targetIP,
  26. const std::string &targetPort)
  27. : CovertChannel(innerInterface, outerInterface,
  28. "(not (tcp and " + std::string(PASSIVE ? "src" : "dst") + " host " + targetIP + " and " + std::string(PASSIVE ? "src" : "dst") +
  29. " port " + targetPort + ")) and (not (dst host " + ownIP + "))",
  30. "(not (tcp and " + std::string(PASSIVE ? "dst" : "src") + " host " + targetIP + " and " + std::string(PASSIVE ? "dst" : "src") +
  31. " port " + targetPort + ")) and (not (dst host " + ownIP + "))",
  32. "tcp and " + std::string(PASSIVE ? "src" : "dst") + " host " + targetIP + " and " + std::string(PASSIVE ? "src" : "dst") + " port " +
  33. targetPort,
  34. "tcp and " + std::string(PASSIVE ? "dst" : "src") + " host " + targetIP + " and " + std::string(PASSIVE ? "dst" : "src") + " port " +
  35. targetPort) {}
  36. /**
  37. * Destroys the CovertChannel.
  38. */
  39. virtual ~TCPUrgencyChannel() {}
  40. /**
  41. * Send a file over the covert channel.
  42. *
  43. * @param fileName name of the file in the file directory
  44. * @return true - file will be sent | false - file was not accepted
  45. */
  46. virtual bool sendFile(const std::string &fileName) {
  47. if constexpr (PASSIVE) {
  48. return false;
  49. } else {
  50. return protocol.sendFile(fileName);
  51. }
  52. }
  53. protected:
  54. /**
  55. * Handler for sniffed packets filterd to forward from the outer network.
  56. *
  57. * Handles incoming packets and forwards them.
  58. *
  59. * @param pdu sniffed packet
  60. *
  61. * @return false = stop loop | true = continue loop
  62. */
  63. virtual bool handleChannelFromOuter(Tins::PDU &pdu) {
  64. Tins::TCP &tcp = pdu.rfind_pdu<Tins::TCP>();
  65. if constexpr (PASSIVE) {
  66. uint16_t data = tcp.urg_ptr();
  67. protocol.receive((uint8_t *)(&data));
  68. tcp.urg_ptr(0);
  69. innerSender.send(pdu);
  70. } else {
  71. // uint16_t urg = tcp.urg_ptr();
  72. // tcp.urg_ptr(0);
  73. innerSender.send(pdu);
  74. }
  75. return true;
  76. }
  77. /**
  78. * Handler for sniffed packets filterd to forward from the inner network.
  79. *
  80. * Handles incoming packets and forwards them.
  81. *
  82. * @param pdu sniffed packet
  83. *
  84. * @return false = stop loop | true = continue loop
  85. */
  86. virtual bool handleChannelFromInner(Tins::PDU &pdu) {
  87. Tins::TCP &tcp = pdu.rfind_pdu<Tins::TCP>();
  88. if constexpr (PASSIVE) {
  89. outerSender.send(pdu);
  90. } else {
  91. uint16_t data = 0;
  92. protocol.send((uint8_t *)(&data));
  93. tcp.urg_ptr(data);
  94. outerSender.send(pdu);
  95. }
  96. return true;
  97. }
  98. /**
  99. * protocol used to transmit data
  100. */
  101. CovertProtocol<2, PASSIVE> protocol;
  102. };
  103. #endif