CovertChannel.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #ifndef COVERTCHANNEL_H
  2. #define COVERTCHANNEL_H
  3. #include <mutex>
  4. #include <thread>
  5. #include <tins/tins.h>
  6. /**
  7. * @class CovertChannel
  8. *
  9. * Sniffs the network, redirects traffic and handles filtered traffic.
  10. *
  11. * CovertChannel class which will sniff on two network interfacees. It handles filtered traffic with a virtual handler
  12. * function.
  13. */
  14. class CovertChannel {
  15. public:
  16. /**
  17. * Sets up a CovertChannel.
  18. *
  19. * Creates a CovertChannel, sets the network interfaces for sniffing and sending and sets the filter.
  20. *
  21. * @param innerInterface name of the interface of the inner network
  22. * @param outerInterface name of the interface of the outer network
  23. * @param innerForwardFilter pcap filter string which will be set for the channel sniffers and negated for the forward sniffers
  24. * @param outerForwardFilter pcap filter string which will be set for the channel sniffers and negated for the forward sniffers
  25. * @param innerChannelFilter pcap filter string which will be set for the channel sniffers and negated for the forward sniffers
  26. * @param outerChannelFilter pcap filter string which will be set for the channel sniffers and negated for the forward sniffers
  27. */
  28. CovertChannel(const std::string &innerInterface, const std::string &outerInterface, const std::string &innerForwardFilter = "",
  29. const std::string &outerForwardFilter = "", const std::string &innerChannelFilter = "", const std::string &outerChannelFilter = "");
  30. /**
  31. * Destroys the CovertChannel.
  32. */
  33. virtual ~CovertChannel();
  34. /**
  35. * Send a file over the covert channel.
  36. *
  37. * @param fileName name of the file in the file directory
  38. * @return true - file will be sent | false - file was not accepted
  39. */
  40. virtual bool sendFile(const std::string &fileName);
  41. /**
  42. * Start sniffing on the interface.
  43. *
  44. * Starts a sniffing loop which calls handle. The loop will only be stopped if
  45. * handle returns false.
  46. */
  47. void startSniffing();
  48. /**
  49. * Sets a filter for the sniffers.
  50. *
  51. * Sets the filter for the forward and channel sniffers with a pcap filter string. E.g. "host 8.8.8.8".
  52. * The forward filter is the negated filter.
  53. *
  54. * @param filter pcap filter string which will be set for the channel sniffers and negated for the forward sniffers
  55. */
  56. void setFilter(const std::string &innerForwardFilter = "", const std::string &outerForwardFilter = "", const std::string &innerChannelFilter = "",
  57. const std::string &outerChannelFilter = "");
  58. protected:
  59. /**
  60. * Handler for sniffed packets filterd to forward from the outer 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. bool handleForwardFromOuter(Tins::PDU &pdu);
  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. bool handleForwardFromInner(Tins::PDU &pdu);
  79. /**
  80. * Handler for sniffed packets filterd to use as channel from the outer network.
  81. *
  82. * Handles incoming packets and redirets them.
  83. *
  84. * @param pdu sniffed packet
  85. *
  86. * @return false = stop loop | true = continue loop
  87. */
  88. virtual bool handleChannelFromOuter(Tins::PDU &pdu) = 0;
  89. /**
  90. * Handler for sniffed packets filterd to use as channel from the outer network.
  91. *
  92. * Handles incoming packets and redirets them.
  93. *
  94. * @param pdu sniffed packet
  95. *
  96. * @return false = stop loop | true = continue loop
  97. */
  98. virtual bool handleChannelFromInner(Tins::PDU &pdu) = 0;
  99. /**
  100. * Starts the sniffing loop of the inner forward sniffer.
  101. */
  102. void startInnerForwardSniffing();
  103. /**
  104. * Starts the sniffing loop of the outer forward sniffer.
  105. */
  106. void startOuterForwardSniffing();
  107. /**
  108. * Starts the sniffing loop of the inner channel sniffer.
  109. */
  110. void startInnerChannelSniffing();
  111. /**
  112. * Starts the sniffing loop of the outer channel sniffer.
  113. */
  114. void startOuterChannelSniffing();
  115. /**
  116. * Tins Sniffer to filter packets to which should be forwarded
  117. */
  118. Tins::Sniffer *innerForwardSniffer;
  119. /**
  120. * Tins Sniffer to filter packets to which should be forwarded
  121. */
  122. Tins::Sniffer *outerForwardSniffer;
  123. /**
  124. * Tins Sniffer to filter packets to which should be used for the covert channel
  125. */
  126. Tins::Sniffer *innerChannelSniffer;
  127. /**
  128. * Tins Sniffer to filter packets to which should be used for the covert channel
  129. */
  130. Tins::Sniffer *outerChannelSniffer;
  131. /**
  132. * Tins PacketSender which sends packets to the inner network
  133. */
  134. Tins::PacketSender innerSender;
  135. /**
  136. * Tins PacketSender which sends packets to the outer network
  137. */
  138. Tins::PacketSender outerSender;
  139. /**
  140. * lock sender to prevent serialization errors
  141. */
  142. std::mutex innerSenderMutex;
  143. /**
  144. * lock sender to prevent serialization errors
  145. */
  146. std::mutex outerSenderMutex;
  147. private:
  148. /**
  149. * Thread for the inner forward sniffer
  150. */
  151. std::thread innerForwardSnifferThread;
  152. /**
  153. * Thread for the outer forward sniffer
  154. */
  155. std::thread outerForwardSnifferThread;
  156. /**
  157. * Thread for the inner channel sniffer
  158. */
  159. std::thread innerChannelSnifferThread;
  160. /**
  161. * Thread for the outer channel sniffer
  162. */
  163. std::thread outerChannelSnifferThread;
  164. };
  165. #endif