TCPAppendChannel.hpp 2.9 KB

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