CovertProtocol.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #ifndef COVERTPROTOCOL_H
  2. #define COVERTPROTOCOL_H
  3. #include <fstream>
  4. #include <iostream>
  5. #include <string>
  6. #include <type_traits>
  7. #include "../../include/Config.h"
  8. /**
  9. * @class CovertProtocol
  10. *
  11. * An unidirectional Covert Channel protocol with a variable size template.
  12. *
  13. * The protocol works unidirectional so the active side uses send to encode data to be sent and the passive side uses receive to extract data.
  14. *
  15. * @param N number of bytes which can be used to transmit data
  16. * @param PASSIVE passive mode
  17. */
  18. template <int N, bool PASSIVE> class CovertProtocol {
  19. static_assert(N >= 1);
  20. public:
  21. /**
  22. * CovertProtocol constructor
  23. */
  24. CovertProtocol() : fileDirectory(Config::getValue("filedirectory")) {}
  25. /**
  26. * CovertProtocol destructor
  27. *
  28. * Closes the file
  29. */
  30. ~CovertProtocol() { file.close(); }
  31. /**
  32. * Starts sending a file.
  33. *
  34. * Starts sending a file if no transmission is running and the file exists.
  35. *
  36. * @param fileName name of the file in the file directory
  37. * @return true - file will be sent | false - file was not accepted
  38. */
  39. bool sendFile(const std::string &fileName) {
  40. static_assert(!PASSIVE);
  41. if (state != ProtocolState::idle || file.is_open()) {
  42. return false;
  43. }
  44. file.open(fileDirectory + fileName, std::ios::in);
  45. if (file.is_open()) {
  46. file.close();
  47. this->fileName = fileName;
  48. state = ProtocolState::fileNameSize;
  49. std::cout << "starting sending file \"" << fileName << "\"" << std::endl;
  50. return true;
  51. } else {
  52. return false;
  53. }
  54. }
  55. /**
  56. * send encodes the data into the data array
  57. *
  58. * 1. Send file name size (length)
  59. * 2. Send file name
  60. * 3. Send data size (length)
  61. * 4. Send data
  62. *
  63. * @param data must be an array of size N
  64. */
  65. void send(uint8_t *const data) {
  66. static_assert(!PASSIVE);
  67. switch (state) {
  68. case ProtocolState::idle:
  69. return;
  70. case ProtocolState::fileNameSize:
  71. fileNameSize = fileName.size();
  72. data[0] = fileNameSize;
  73. fileNamePosition = 0;
  74. state = ProtocolState::fileName;
  75. break;
  76. case ProtocolState::fileName:
  77. if (fileNameSize - fileNamePosition >= N) {
  78. for (int i = 0; i < N; i++) {
  79. data[i] = fileName.at(fileNamePosition++);
  80. }
  81. } else {
  82. for (int i = 0; i < fileNameSize - fileNamePosition; i++) {
  83. data[i] = fileName.at(fileNamePosition++);
  84. }
  85. }
  86. if (fileNamePosition == fileNameSize) {
  87. file.open(fileDirectory + fileName, std::ios::in | std::ios::binary | std::ios::ate);
  88. if (!file.is_open()) {
  89. file.close();
  90. std::cerr << "File \"" << fileName << "\" does exists. Error state!!!" << std::endl;
  91. state = ProtocolState::error;
  92. return;
  93. }
  94. dataSize = file.tellg();
  95. dataPosition = 0;
  96. file.seekg(std::ios::beg);
  97. state = ProtocolState::dataSize;
  98. }
  99. break;
  100. case ProtocolState::dataSize:
  101. if constexpr (N >= 4) {
  102. for (; dataPosition < 4; dataPosition++) {
  103. data[dataPosition] = dataSize >> ((3 - dataPosition) * 8);
  104. }
  105. } else {
  106. uint32_t oldDataPosition = dataPosition;
  107. uint32_t limit = dataPosition + N;
  108. if (limit > 4) {
  109. limit = 4;
  110. }
  111. for (; dataPosition < limit; dataPosition++) {
  112. data[dataPosition - oldDataPosition] = dataSize >> ((3 - dataPosition) * 8);
  113. }
  114. }
  115. if (dataPosition == 4) {
  116. dataPosition = 0;
  117. state = ProtocolState::data;
  118. }
  119. break;
  120. case ProtocolState::data:
  121. if (dataSize - dataPosition >= N) {
  122. file.read((char *)data, N);
  123. dataPosition += N;
  124. } else {
  125. file.read((char *)data, dataSize - dataPosition);
  126. dataPosition = dataSize;
  127. }
  128. std::cout << "sent " << dataPosition << "/" << dataSize << std::endl;
  129. if (dataPosition == dataSize) {
  130. file.close();
  131. state = ProtocolState::idle;
  132. std::cout << "finished sending file \"" << fileName << "\"" << std::endl;
  133. }
  134. break;
  135. case ProtocolState::error:
  136. break;
  137. }
  138. return;
  139. }
  140. /**
  141. * receive extracts data from the data array
  142. *
  143. * 1. Receive file name size (length)
  144. * 2. Receive file name
  145. * 3. Receive data size (length)
  146. * 4. Receive data
  147. *
  148. * @param data must be an array of size N
  149. */
  150. void receive(const uint8_t *const data) {
  151. static_assert(PASSIVE);
  152. switch (state) {
  153. case ProtocolState::idle:
  154. if (data[0] == 0) {
  155. return;
  156. }
  157. // no break because the first data received is the filename size
  158. case ProtocolState::fileNameSize:
  159. std::cout << "incoming file transmission" << std::endl;
  160. fileNameSize = data[0];
  161. fileNamePosition = 0;
  162. fileName = "";
  163. state = ProtocolState::fileName;
  164. break;
  165. case ProtocolState::fileName:
  166. if (fileNameSize - fileNamePosition >= N) {
  167. uint8_t oldFileNamePosition = fileNamePosition;
  168. for (; fileNamePosition < oldFileNamePosition + N; fileNamePosition++) {
  169. fileName += data[fileNamePosition - oldFileNamePosition];
  170. }
  171. } else {
  172. uint8_t oldFileNamePosition = fileNamePosition;
  173. for (; fileNamePosition < fileNameSize; fileNamePosition++) {
  174. fileName += data[fileNamePosition - oldFileNamePosition];
  175. }
  176. }
  177. if (fileNamePosition == fileNameSize) {
  178. file.open(fileDirectory + fileName, std::ios::in);
  179. if (file.is_open()) {
  180. file.close();
  181. std::cerr << "File \"" << fileName << "\" already exists. Error state!!!" << std::endl;
  182. state = ProtocolState::error;
  183. return;
  184. }
  185. file.close();
  186. file.open(fileDirectory + fileName, std::ios::out | std::ios::binary | std::ios::app);
  187. if (!file.is_open()) {
  188. std::cerr << "File \"" << fileName << "\" could not be opened! Error state!!!" << std::endl;
  189. state = ProtocolState::error;
  190. return;
  191. }
  192. dataSize = 0;
  193. dataPosition = 0;
  194. state = ProtocolState::dataSize;
  195. std::cout << "starting receiving file \"" << fileName << "\"" << std::endl;
  196. }
  197. break;
  198. case ProtocolState::dataSize:
  199. if constexpr (N >= 4) {
  200. for (; dataPosition < 4; dataPosition++) {
  201. dataSize = dataSize | data[dataPosition] << ((3 - dataPosition) * 8);
  202. }
  203. } else {
  204. uint32_t oldDataPosition = dataPosition;
  205. uint32_t limit = dataPosition + N;
  206. if (limit > 4) {
  207. limit = 4;
  208. }
  209. for (; dataPosition < limit; dataPosition++) {
  210. dataSize = dataSize | data[dataPosition - oldDataPosition] << ((3 - dataPosition) * 8);
  211. }
  212. }
  213. if (dataPosition == 4) {
  214. dataPosition = 0;
  215. state = ProtocolState::data;
  216. }
  217. break;
  218. case ProtocolState::data:
  219. if (dataSize - dataPosition >= N) {
  220. file.write((char *)data, N);
  221. dataPosition += 2;
  222. } else {
  223. file.write((char *)data, dataSize - dataPosition);
  224. dataPosition = dataSize;
  225. }
  226. std::cout << "received " << dataPosition << "/" << dataSize << std::endl;
  227. if (dataPosition == dataSize) {
  228. file.close();
  229. state = ProtocolState::idle;
  230. std::cout << "finished receiving file \"" << fileName << "\"" << std::endl;
  231. }
  232. break;
  233. case ProtocolState::error:
  234. //
  235. break;
  236. }
  237. }
  238. private:
  239. /**
  240. * folder of the files
  241. */
  242. const std::string fileDirectory;
  243. /**
  244. * States of the data transmission
  245. *
  246. * @warning error state cannot be recovered!!!
  247. */
  248. enum struct ProtocolState { idle, fileNameSize, fileName, dataSize, data, error };
  249. /**
  250. * State of the data transmission
  251. */
  252. ProtocolState state = ProtocolState::idle;
  253. /**
  254. * size of the file to be sent/received
  255. */
  256. uint32_t dataSize;
  257. /**
  258. * position in the file to be sent/received
  259. */
  260. uint32_t dataPosition;
  261. /**
  262. * file to be sent/received
  263. */
  264. std::fstream file;
  265. /**
  266. * size of the file name
  267. */
  268. uint8_t fileNameSize;
  269. /**
  270. * position of the file name so far sent/received
  271. */
  272. uint8_t fileNamePosition;
  273. /**
  274. * name of the file to be sent/received
  275. */
  276. std::string fileName;
  277. };
  278. #endif