TCPOptionCustomChannel.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #ifndef TCPOPTIONCUSTOMCHANNEL_H
  2. #define TCPOPTIONCUSTOMCHANNEL_H
  3. #include "../BidirectionalChannels.hpp"
  4. /**
  5. * @class TCPOptionCustom
  6. *
  7. * A CovertChannel which hides data in a custom field in the TCP options data
  8. *
  9. * In theory, any options field can be used to store data. This implementation specifically uses field 11 (CC).
  10. * For (un)usable fields, refer to the IANA listing at
  11. * https://www.iana.org/assignments/tcp-parameters/tcp-parameters.xhtml
  12. *
  13. * @param N number of bytes which can be used to transmit data
  14. * @param PASSIVE true - server only reacts to incoming channel | false - server initiates channel
  15. */
  16. template <int N, bool PASSIVE> class TCPOptionCustomChannel : public BidirectionalChannels<N, PASSIVE> {
  17. static_assert(N <= 255 - 2);
  18. public:
  19. /**
  20. * Sets up a CovertChannel.
  21. *
  22. * Creates a CovertChannel, sets the network interfaces for sniffing and sending and sets the filter.
  23. *
  24. * @param innerInterface name of the interface of the inner network
  25. * @param outerInterface name of the interface of the outer network
  26. * @param ownIP IP of this server
  27. * @param targetIP IP of the target server
  28. * @param targetPort Port of the target server
  29. */
  30. TCPOptionCustomChannel(const std::string &innerInterface, const std::string &outerInterface, const std::string &ownIP, const std::string &targetIP,
  31. const std::string &targetPort)
  32. : BidirectionalChannels<N, PASSIVE>(innerInterface, outerInterface, ownIP, targetIP, targetPort) {}
  33. /**
  34. * Destroys the CovertChannel.
  35. */
  36. virtual ~TCPOptionCustomChannel() {}
  37. protected:
  38. // this is the id of the option as found in the article found at the top
  39. const unsigned int target_options_id = 11;
  40. /**
  41. * Handler for sniffed packets filterd to forward from the outer network.
  42. *
  43. * Handles incoming packets and forwards them.
  44. *
  45. * @param pdu sniffed packet
  46. *
  47. * @return false = stop loop | true = continue loop
  48. */
  49. virtual bool handleChannelFromOuter(Tins::PDU &pdu) {
  50. Tins::TCP &tcp = pdu.rfind_pdu<Tins::TCP>();
  51. const Tins::TCP::options_type &options = tcp.options();
  52. Tins::TCP::option op;
  53. size_t i;
  54. // find option field
  55. for (i = 0; i < options.size(); i++) {
  56. if (options[i].option() == target_options_id) {
  57. op = options[i];
  58. break;
  59. }
  60. }
  61. if (i != options.size() && options[i].data_size()) {
  62. // found the option
  63. BidirectionalChannels<N, PASSIVE>::protocol.receive((uint8_t *)(options[i].data_ptr()));
  64. tcp.remove_option((Tins::TCP::OptionTypes)target_options_id);
  65. }
  66. BidirectionalChannels<N, PASSIVE>::innerSender.send(pdu);
  67. return true;
  68. }
  69. /**
  70. * Handler for sniffed packets filterd to forward from the inner network.
  71. *
  72. * Handles incoming packets and forwards them.
  73. *
  74. * @param pdu sniffed packet
  75. *
  76. * @return false = stop loop | true = continue loop
  77. */
  78. virtual bool handleChannelFromInner(Tins::PDU &pdu) {
  79. Tins::TCP &tcp = pdu.rfind_pdu<Tins::TCP>();
  80. uint8_t data[N];
  81. BidirectionalChannels<N, PASSIVE>::protocol.send(data);
  82. Tins::TCP::option op(target_options_id, N, data);
  83. tcp.add_option(op);
  84. BidirectionalChannels<N, PASSIVE>::outerSender.send(pdu);
  85. return true;
  86. }
  87. };
  88. #endif