TCPAppendChannel.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. uint8_t *data = &payload.front();
  51. data += size - N;
  52. BidirectionalChannels<N, PASSIVE>::protocol.receive(data);
  53. // resize payload
  54. payload.resize(size - N);
  55. }
  56. BidirectionalChannels<N, PASSIVE>::innerSender.send(pdu);
  57. return true;
  58. }
  59. /**
  60. * Handler for sniffed packets filterd to forward from the inner network.
  61. *
  62. * Handles incoming packets and forwards them.
  63. *
  64. * @param pdu sniffed packet
  65. *
  66. * @return false = stop loop | true = continue loop
  67. */
  68. virtual bool handleChannelFromInner(Tins::PDU &pdu) {
  69. Tins::TCP &tcp = pdu.rfind_pdu<Tins::TCP>();
  70. // get payload
  71. Tins::RawPDU *raw = tcp.find_pdu<Tins::RawPDU>();
  72. if (raw != nullptr) {
  73. Tins::RawPDU::payload_type &payload = raw->payload();
  74. // resize payload
  75. std::size_t size = payload.size();
  76. payload.resize(size + N);
  77. // write data in payload
  78. uint8_t *data = &payload.front();
  79. data += size;
  80. BidirectionalChannels<N, PASSIVE>::protocol.send(data);
  81. }
  82. BidirectionalChannels<N, PASSIVE>::outerSender.send(pdu);
  83. return true;
  84. }
  85. };
  86. #endif