Advertisement
TShiva

NetworkPacketAnalyzer 0.2

Jun 7th, 2019
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. #include <fstream>
  2. #include <sstream>
  3. #include <string>
  4. #include <iostream>
  5. #include <vector>
  6. #include <string>
  7. #include <filesystem>
  8. #include "UInt24_t.h"
  9.  
  10. std::vector<unsigned int> keys{};
  11. std::string path = "C:\\Users\\trixter\\source\\repos\\BinPacketAnalyzer\\BinPacketAnalyzer\\pn";
  12. std::string key_ints_path = "C:\\Users\\trixter\\source\\repos\\BinPacketAnalyzer\\BinPacketAnalyzer\\key_ints.txt";
  13.  
  14. template <typename T>
  15. T GetBigEndianBytes(char* pData)
  16. {
  17. T seg {};
  18. for (int i = 0; i < sizeof(T); i++) {
  19. seg = (seg << 8) + *reinterpret_cast<unsigned char*>(pData);
  20. pData++;
  21. }
  22. return seg;
  23. };
  24.  
  25. template<typename T>
  26. void CuttingByteArray(std::ifstream& data, std::ofstream& output, int file_size, std::filesystem::path& file_path)
  27. {
  28. std::vector<std::pair<T, T>> blocks{};
  29. int type_size = sizeof(T);
  30. int counter = 0;
  31. while (counter <= file_size - type_size)
  32. {
  33. char* le = new char[8];
  34. data.seekg(counter, std::ios_base::beg);
  35. data.read(le , type_size);
  36. unsigned int leb = *reinterpret_cast<T*>(le);
  37. unsigned int beb = GetBigEndianBytes<T>(le);
  38. for(auto&i: keys)
  39. {
  40. if(leb == i || beb == i)
  41. {
  42. output << "In file: " << file_path << std::endl;
  43. output << "Data shift " << counter << std::endl;
  44. output << "Key found " << i << std::endl;
  45. }
  46. }
  47. counter++;
  48. delete[] le;
  49. }
  50. };
  51.  
  52. void GetKeyIntsFromFile(std::string& path)
  53. {
  54. std::ifstream keyFileBuffer(path, std::ios::in);
  55. if (!keyFileBuffer.is_open())
  56. {
  57. std::cerr << " File don't open" << std::endl;
  58. }
  59. std::string key;
  60. while(getline(keyFileBuffer, key))
  61. {
  62. keys.push_back(stoi(key));
  63. }
  64. keyFileBuffer.close();
  65. }
  66.  
  67.  
  68. int main() {
  69.  
  70. GetKeyIntsFromFile(key_ints_path);
  71. std::ofstream outputBuffer("Logi.txt", std::ios::out);
  72.  
  73. for(const auto& entry : std::filesystem::directory_iterator(path)) // обход по всем файлам, не зависимости от типа
  74. {
  75. if (!entry.is_regular_file()) {continue;}
  76. int file_size = std::filesystem::file_size(entry.path());
  77. std::filesystem::path file_path = entry.path();
  78. std::ifstream fileBuffer(entry.path(), std::ios::in | std::ios::binary);
  79. if (!fileBuffer.is_open())
  80. {
  81. std::cerr << " Файл не открыт" << std::endl;
  82. }
  83. CuttingByteArray<uint8_t>(fileBuffer, outputBuffer, file_size, file_path);
  84. CuttingByteArray<uint16_t>(fileBuffer, outputBuffer, file_size, file_path);
  85. //CuttingByteArray<UInt24_t>(fileBuffer, outputBuffer, file_size);
  86. CuttingByteArray<uint32_t>(fileBuffer, outputBuffer, file_size, file_path);
  87. fileBuffer.close();
  88. }
  89. outputBuffer.close();
  90. std::cout << " Application finished" << std::endl;
  91. system("PAUSE");
  92. return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement