TCPAppendChannel.hpp 3.9 KB

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