TCPOptionCustomChannel.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 targetIP IP of the target server
  27. * @param targetPort Port of the target server
  28. */
  29. TCPOptionCustomChannel(const std::string &innerInterface, const std::string &outerInterface, const std::string &targetIP, const std::string &targetPort)
  30. : BidirectionalChannels<N, PASSIVE>(innerInterface, outerInterface, targetIP, targetPort) {}
  31. /**
  32. * Destroys the CovertChannel.
  33. */
  34. virtual ~TCPOptionCustomChannel() {}
  35. protected:
  36. // this is the id of the option as found in the article found at the top
  37. const unsigned int target_options_id = 11;
  38. /**
  39. * Handler for sniffed packets filterd to forward from the outer network.
  40. *
  41. * Handles incoming packets and forwards them.
  42. *
  43. * @param pdu sniffed packet
  44. *
  45. * @return false = stop loop | true = continue loop
  46. */
  47. virtual bool handleChannelFromOuter(Tins::PDU &pdu) {
  48. Tins::TCP &tcp = pdu.rfind_pdu<Tins::TCP>();
  49. const Tins::TCP::options_type &options = tcp.options();
  50. Tins::TCP::option op;
  51. size_t i;
  52. // find option field
  53. for (i = 0; i < options.size(); i++) {
  54. if (options[i].option() == target_options_id) {
  55. op = options[i];
  56. break;
  57. }
  58. }
  59. if (i != options.size() && options[i].data_size()) {
  60. // found the option
  61. BidirectionalChannels<N, PASSIVE>::protocol.receive((uint8_t *)(options[i].data_ptr()));
  62. tcp.remove_option((Tins::TCP::OptionTypes)target_options_id);
  63. }
  64. BidirectionalChannels<N, PASSIVE>::innerSender.send(pdu);
  65. return true;
  66. }
  67. /**
  68. * Handler for sniffed packets filterd to forward from the inner network.
  69. *
  70. * Handles incoming packets and forwards them.
  71. *
  72. * @param pdu sniffed packet
  73. *
  74. * @return false = stop loop | true = continue loop
  75. */
  76. virtual bool handleChannelFromInner(Tins::PDU &pdu) {
  77. Tins::TCP &tcp = pdu.rfind_pdu<Tins::TCP>();
  78. uint8_t data[N];
  79. BidirectionalChannels<N, PASSIVE>::protocol.send(data);
  80. Tins::TCP::option op(target_options_id, N, data);
  81. tcp.add_option(op);
  82. BidirectionalChannels<N, PASSIVE>::outerSender.send(pdu);
  83. return true;
  84. }
  85. };
  86. #endif