Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. #include "stdafx.h"
  2.  
  3.  
  4.  
  5. std::pair<std::string, std::string> split_word(const std::string & word)
  6. {
  7. std::vector<std::string> name_list =
  8. {
  9. "inject",
  10. "syscall",
  11. "filetracer",
  12. "regmon",
  13. "procmon",
  14. "filedelete",
  15. "socketmon",
  16. "cpuidmon",
  17. "finish"
  18. };
  19. for (auto & i : name_list)
  20. {
  21. if (word.size() >= i.size()
  22. && word.substr(word.size() - i.size(), i.size()) == i)
  23. {
  24. return std::pair<std::string, std::string>(word.substr(0, word.size() - i.size()), i);
  25. }
  26. }
  27. return std::pair<std::string, std::string>(word, word);
  28. }
  29.  
  30. std::vector<Event> get_events(const std::vector<std::string> & raw_log)
  31. {
  32. std::vector<Event> events;
  33. events.reserve(400000);
  34. std::string buf;
  35. std::string name = "";
  36. std::map<std::string, std::string> parametrs;
  37. std::string key;
  38. bool search_value = false;
  39. for (auto & i : raw_log)
  40. {
  41. for (auto & j : i)
  42. {
  43. if (j != *"\""
  44. && j != *"="
  45. && j != *","
  46. && (j != *" " || search_value))
  47. {
  48. if (j == *" ")
  49. {
  50. std::pair<std::string, std::string> buf_pair = split_word(buf);
  51. if (buf_pair.first != buf)
  52. {
  53. parametrs.insert(std::pair<std::string, std::string>(key, buf_pair.first));
  54. search_value = false;
  55. Event buf_event(name, parametrs);
  56. events.push_back(buf_event);
  57. name = buf_pair.second;
  58. parametrs.clear();
  59. buf.clear();
  60. }
  61. else
  62. {
  63. buf += j;
  64. }
  65. }
  66. else
  67. {
  68. buf += j;
  69. }
  70. }
  71. else if (j == *" " && !search_value && !buf.empty() && !name.empty())
  72. {
  73. std::pair<std::string, std::string> buf_pair = split_word(buf);
  74. parametrs.insert(std::pair<std::string, std::string>(key, buf_pair.first));
  75. search_value = false;
  76. Event buf_event(name, parametrs);
  77. events.push_back(buf_event);
  78. name = buf_pair.second;
  79. parametrs.clear();
  80. buf.clear();
  81. }
  82. else
  83. {
  84. if (name.empty())
  85. {
  86. name = buf;
  87. buf.clear();
  88. }
  89. else if (j == *"=" && !search_value)
  90. {
  91. key = buf;
  92. search_value = true;
  93. buf.clear();
  94. }
  95. else if (search_value
  96. && (j == *"," || j == *" "))
  97. {
  98. parametrs.insert(std::pair<std::string, std::string>(key, buf));
  99. search_value = false;
  100. if (j == *" ")
  101. {
  102. Event buf_event(name, parametrs);
  103. events.push_back(buf_event);
  104. name.clear();
  105. parametrs.clear();
  106. }
  107. buf.clear();
  108. }
  109. else if (j == *"," && !search_value)
  110. {
  111. parametrs.insert(std::pair<std::string, std::string>(buf, ""));
  112. buf.clear();
  113. }
  114. }
  115. }
  116. }
  117. Event buf_event(name, parametrs);
  118. events.push_back(buf_event);
  119. return events;
  120. }
  121.  
  122. void make_events(const std::string & path, std::vector<Event> & events)
  123. {
  124. std::ifstream file_raw_log1(path);
  125. std::string buf;
  126. std::vector<std::string> raw_log;
  127. raw_log.reserve(400000);
  128. while (getline(file_raw_log1, buf))
  129. {
  130. raw_log.push_back(buf);
  131. }
  132. file_raw_log1.close();
  133. events = get_events(raw_log);
  134. }
  135.  
  136. void save_events(const std::string & path, const std::vector<Event> & events)
  137. {
  138. std::ofstream file_events(path);
  139. for (auto & i : events)
  140. {
  141. file_events << i.GetName() << std::endl;
  142. for (auto & j : i.GetParametrs())
  143. {
  144. file_events << j.first << "=" << j.second << std::endl;
  145. }
  146. }
  147. file_events.close();
  148. }
  149.  
  150. void read_events(const std::string & path, std::vector<Event> & events)
  151. {
  152. std::string buf;
  153. std::string name = "";
  154. std::map<std::string, std::string> parametrs;
  155. size_t pos;
  156. std::ifstream file_events(path);
  157. while (getline(file_events, buf))
  158. {
  159. if (name.empty())
  160. {
  161. name = buf;
  162. }
  163. else
  164. {
  165. pos = buf.find("=");
  166. if (pos != std::string::npos)
  167. {
  168. parametrs.insert(std::pair<std::string, std::string>(buf.substr(0, pos), buf.substr(pos + 1, buf.size() - pos - 1)));
  169. }
  170. else
  171. {
  172. Event buf_event(name, parametrs);
  173. events.push_back(buf_event);
  174. name = buf;
  175. parametrs.clear();
  176. }
  177. }
  178. }
  179. Event buf_event(name, parametrs);
  180. events.push_back(buf_event);
  181. file_events.close();
  182. }
  183.  
  184. int main()
  185. {
  186. std::vector<Event> events;;
  187. events.reserve(400000);
  188. //make_events("C:\\Users\\Артём\\Desktop\\raw.log", events);
  189. //save_events("C:\\Users\\Артём\\Desktop\\events.log", events);
  190. std::vector<Event> new_events;
  191. new_events.reserve(400000);
  192. read_events("C:\\Users\\Артём\\Desktop\\events.log", new_events);
  193. int counter = 0;
  194. /*
  195. int suspects = 0;
  196. int no_ppid = 0;
  197. int no_pid = 0;
  198. int no_pids = 0;
  199. std::set<std::string> PIDs;
  200. PIDs.insert(new_events[1].GetValue("PID"));
  201. for (auto & i : new_events)
  202. {
  203. if (i.GetValue("PPID") != "")
  204. {
  205. if (PIDs.find(i.GetValue("PPID")) != PIDs.end())
  206. {
  207. ++suspects;
  208. if (i.GetValue("PID") != "")
  209. {
  210. PIDs.insert(i.GetValue("PID"));
  211. }
  212. else
  213. {
  214. ++no_pid;
  215. }
  216. }
  217. }
  218. else if (i.GetValue("PID") != "")
  219. {
  220. if (PIDs.find(i.GetValue("PID")) != PIDs.end())
  221. {
  222. ++suspects;
  223. }
  224. ++no_ppid;
  225. }
  226. else
  227. {
  228. ++no_pids;
  229. }
  230. }
  231. std::cout << "no_pid = " << no_pid << "\n"
  232. << "no_ppid = " << no_ppid << "\n"
  233. << "no_pids = " << no_pids << "\n"
  234. << "suspects = " << suspects << "\n"
  235. << 100 * float(suspects) / new_events.size() << "%\n";
  236. for (auto & i : PIDs)
  237. {
  238. std::cout << i << std::endl;
  239. }
  240. std::cout << PIDs.size() << std::endl;
  241. */
  242.  
  243. std::set<std::string> keys;
  244. for (auto & i : new_events)
  245. {
  246. //std::cout << counter++ << std::endl;
  247. for (auto & j : i.GetKeys())
  248. {
  249. if (keys.find(j) == keys.end())
  250. {
  251. keys.insert(j);
  252. }
  253. }
  254. }
  255. for (auto & i : keys)
  256. {
  257. std::cout << i << std::endl;
  258. }
  259. std::cout << keys.size() << std::endl;
  260.  
  261. int mas[200];
  262. for (int i = 0; i < 200; ++i)
  263. {
  264. mas[i] = 0;
  265. }
  266. for (auto & i : new_events)
  267. {
  268. ++mas[i.GetSize()];
  269. }
  270. int summ = 0;
  271. for (int i = 0; i < 200; ++i)
  272. {
  273. if (mas[i] != 0)
  274. {
  275. std::cout << i << " = " << mas[i] << std::endl;
  276. }
  277. summ += mas[i];
  278. }
  279. std::cout << new_events.size() << " = " << summ << std::endl;
  280.  
  281. system("pause");
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement