TCPOptionTimestampChannel.hpp 3.6 KB

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