CovertProtocol.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. int diff = fileNameSize - fileNamePosition;
  83. for (int i = 0; i < diff; i++) {
  84. data[i] = fileName.at(fileNamePosition++);
  85. }
  86. }
  87. if (fileNamePosition == fileNameSize) {
  88. file.open(fileDirectory + fileName, std::ios::in | std::ios::binary | std::ios::ate);
  89. if (!file.is_open()) {
  90. file.close();
  91. std::cerr << "File \"" << fileName << "\" does exists. Error state!!!" << std::endl;
  92. state = ProtocolState::error;
  93. return;
  94. }
  95. dataSize = file.tellg();
  96. dataPosition = 0;
  97. file.seekg(std::ios::beg);
  98. state = ProtocolState::dataSize;
  99. }
  100. break;
  101. case ProtocolState::dataSize:
  102. if constexpr (N >= 4) {
  103. for (; dataPosition < 4; dataPosition++) {
  104. data[dataPosition] = dataSize >> ((3 - dataPosition) * 8);
  105. }
  106. } else {
  107. uint32_t oldDataPosition = dataPosition;
  108. uint32_t limit = dataPosition + N;
  109. if (limit > 4) {
  110. limit = 4;
  111. }
  112. for (; dataPosition < limit; dataPosition++) {
  113. data[dataPosition - oldDataPosition] = dataSize >> ((3 - dataPosition) * 8);
  114. }
  115. }
  116. if (dataPosition == 4) {
  117. dataPosition = 0;
  118. state = ProtocolState::data;
  119. }
  120. break;
  121. case ProtocolState::data:
  122. if (dataSize - dataPosition >= N) {
  123. file.read((char *)data, N);
  124. dataPosition += N;
  125. } else {
  126. file.read((char *)data, dataSize - dataPosition);
  127. dataPosition = dataSize;
  128. }
  129. std::cout << "sent " << dataPosition << "/" << dataSize << std::endl;
  130. if (dataPosition == dataSize) {
  131. file.close();
  132. state = ProtocolState::idle;
  133. std::cout << "finished sending file \"" << fileName << "\"" << std::endl;
  134. }
  135. break;
  136. case ProtocolState::error:
  137. break;
  138. }
  139. return;
  140. }
  141. /**
  142. * receive extracts data from the data array
  143. *
  144. * 1. Receive file name size (length)
  145. * 2. Receive file name
  146. * 3. Receive data size (length)
  147. * 4. Receive data
  148. *
  149. * @param data must be an array of size N
  150. */
  151. void receive(const uint8_t *const data) {
  152. static_assert(PASSIVE);
  153. switch (state) {
  154. case ProtocolState::idle:
  155. if (data[0] == 0) {
  156. return;
  157. }
  158. // no break because the first data received is the filename size
  159. case ProtocolState::fileNameSize:
  160. std::cout << "incoming file transmission" << std::endl;
  161. fileNameSize = data[0];
  162. fileNamePosition = 0;
  163. fileName = "";
  164. state = ProtocolState::fileName;
  165. break;
  166. case ProtocolState::fileName:
  167. if (fileNameSize - fileNamePosition >= N) {
  168. uint8_t oldFileNamePosition = fileNamePosition;
  169. for (; fileNamePosition < oldFileNamePosition + N; fileNamePosition++) {
  170. fileName += data[fileNamePosition - oldFileNamePosition];
  171. }
  172. } else {
  173. uint8_t oldFileNamePosition = fileNamePosition;
  174. for (; fileNamePosition < fileNameSize; fileNamePosition++) {
  175. fileName += data[fileNamePosition - oldFileNamePosition];
  176. }
  177. }
  178. if (fileNamePosition == fileNameSize) {
  179. file.open(fileDirectory + fileName, std::ios::in);
  180. if (file.is_open()) {
  181. file.close();
  182. std::cerr << "File \"" << fileName << "\" already exists. Error state!!!" << std::endl;
  183. state = ProtocolState::error;
  184. return;
  185. }
  186. file.close();
  187. file.open(fileDirectory + fileName, std::ios::out | std::ios::binary | std::ios::app);
  188. if (!file.is_open()) {
  189. std::cerr << "File \"" << fileName << "\" could not be opened! Error state!!!" << std::endl;
  190. state = ProtocolState::error;
  191. return;
  192. }
  193. dataSize = 0;
  194. dataPosition = 0;
  195. state = ProtocolState::dataSize;
  196. std::cout << "starting receiving file \"" << fileName << "\"" << std::endl;
  197. }
  198. break;
  199. case ProtocolState::dataSize:
  200. if constexpr (N >= 4) {
  201. for (; dataPosition < 4; dataPosition++) {
  202. dataSize = dataSize | data[dataPosition] << ((3 - dataPosition) * 8);
  203. }
  204. } else {
  205. uint32_t oldDataPosition = dataPosition;
  206. uint32_t limit = dataPosition + N;
  207. if (limit > 4) {
  208. limit = 4;
  209. }
  210. for (; dataPosition < limit; dataPosition++) {
  211. dataSize = dataSize | data[dataPosition - oldDataPosition] << ((3 - dataPosition) * 8);
  212. }
  213. }
  214. if (dataPosition == 4) {
  215. dataPosition = 0;
  216. state = ProtocolState::data;
  217. }
  218. break;
  219. case ProtocolState::data:
  220. if (dataSize - dataPosition >= N) {
  221. file.write((char *)data, N);
  222. dataPosition += N;
  223. } else {
  224. file.write((char *)data, dataSize - dataPosition);
  225. dataPosition = dataSize;
  226. }
  227. std::cout << "received " << dataPosition << "/" << dataSize << std::endl;
  228. if (dataPosition == dataSize) {
  229. file.close();
  230. state = ProtocolState::idle;
  231. std::cout << "finished receiving file \"" << fileName << "\"" << std::endl;
  232. }
  233. break;
  234. case ProtocolState::error:
  235. //
  236. break;
  237. }
  238. }
  239. private:
  240. /**
  241. * folder of the files
  242. */
  243. const std::string fileDirectory;
  244. /**
  245. * States of the data transmission
  246. *
  247. * @warning error state cannot be recovered!!!
  248. */
  249. enum struct ProtocolState { idle, fileNameSize, fileName, dataSize, data, error };
  250. /**
  251. * State of the data transmission
  252. */
  253. ProtocolState state = ProtocolState::idle;
  254. /**
  255. * size of the file to be sent/received
  256. */
  257. uint32_t dataSize;
  258. /**
  259. * position in the file to be sent/received
  260. */
  261. uint32_t dataPosition;
  262. /**
  263. * file to be sent/received
  264. */
  265. std::fstream file;
  266. /**
  267. * size of the file name
  268. */
  269. uint8_t fileNameSize;
  270. /**
  271. * position of the file name so far sent/received
  272. */
  273. uint8_t fileNamePosition;
  274. /**
  275. * name of the file to be sent/received
  276. */
  277. std::string fileName;
  278. };
  279. #endif