TCPAppendChannel.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #ifndef TCPAPPENDCHANNEL_H
  2. #define TCPAPPENDCHANNEL_H
  3. #include "../BidirectionalChannels.hpp"
  4. /**
  5. * @class TCPAppendChannel
  6. *
  7. * A CovertChannel which appends data to the TCP payload
  8. *
  9. * @param N number of bytes which can be used to transmit data
  10. * @param PASSIVE true - server only reacts to incoming channel | false - server initiates channel
  11. */
  12. template <int N, bool PASSIVE> class TCPAppendChannel : public BidirectionalChannels<N, PASSIVE> {
  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 targetIP IP of the target server
  22. * @param targetPort Port of the target server
  23. */
  24. TCPAppendChannel(const std::string &innerInterface, const std::string &outerInterface, const std::string &targetIP, const std::string &targetPort)
  25. : BidirectionalChannels<N, PASSIVE>(innerInterface, outerInterface, targetIP, targetPort) {}
  26. /**
  27. * Destroys the CovertChannel.
  28. */
  29. virtual ~TCPAppendChannel() {}
  30. protected:
  31. /**
  32. * Handler for sniffed packets filterd to forward from the outer network.
  33. *
  34. * Handles incoming packets and forwards them.
  35. *
  36. * @param pdu sniffed packet
  37. *
  38. * @return false = stop loop | true = continue loop
  39. */
  40. virtual bool handleChannelFromOuter(Tins::PDU &pdu) {
  41. Tins::TCP &tcp = pdu.rfind_pdu<Tins::TCP>();
  42. // get payload
  43. Tins::RawPDU *raw = tcp.find_pdu<Tins::RawPDU>();
  44. if (raw != nullptr) {
  45. Tins::RawPDU::payload_type &payload = raw->payload();
  46. // read data from payload
  47. std::size_t size = payload.size();
  48. if (size < N) {
  49. std::cerr << __PRETTY_FUNCTION__ << " payload size is too small" << std::endl;
  50. } else {
  51. uint8_t *data = &payload.front();
  52. data += size - N;
  53. BidirectionalChannels<N, PASSIVE>::protocol.receive(data);
  54. // resize payload
  55. payload.resize(size - N);
  56. }
  57. }
  58. BidirectionalChannels<N, PASSIVE>::innerSender.send(pdu);
  59. return true;
  60. }
  61. /**
  62. * Handler for sniffed packets filterd to forward from the inner network.
  63. *
  64. * Handles incoming packets and forwards them.
  65. *
  66. * @param pdu sniffed packet
  67. *
  68. * @return false = stop loop | true = continue loop
  69. */
  70. virtual bool handleChannelFromInner(Tins::PDU &pdu) {
  71. Tins::TCP &tcp = pdu.rfind_pdu<Tins::TCP>();
  72. // get payload
  73. Tins::RawPDU *raw = tcp.find_pdu<Tins::RawPDU>();
  74. if (raw != nullptr) {
  75. Tins::RawPDU::payload_type &payload = raw->payload();
  76. // resize payload
  77. std::size_t size = payload.size();
  78. payload.resize(size + N);
  79. // write data in payload
  80. uint8_t *data = &payload.front();
  81. data += size;
  82. BidirectionalChannels<N, PASSIVE>::protocol.send(data);
  83. }
  84. BidirectionalChannels<N, PASSIVE>::outerSender.send(pdu);
  85. return true;
  86. }
  87. };
  88. #endif