PE-sieve
Scans all running processes. Recognizes and dumps a variety of potentially malicious implants (replaced/implanted PEs, shellcodes, hooks, in-memory patches).
Loading...
Searching...
No Matches
stats_util.h
Go to the documentation of this file.
1#pragma once
2
3#include <windows.h>
4#include <iostream>
5#include <string>
6#include <map>
7#include <set>
8
9namespace pesieve {
10 namespace stats {
11
12 template <typename T>
13 std::string hexdumpValue(const BYTE* in_buf, const size_t max_size)
14 {
15 std::stringstream ss;
16 for (size_t i = 0; i < max_size; i++) {
17 ss << "\\x" << std::setfill('0') << std::setw(2) << std::hex << (unsigned int)in_buf[i];
18 }
19 return ss.str();
20 }
21
22 template <typename T>
23 std::string hexdumpValues(std::set<T> &values)
24 {
25 std::stringstream outs;
26 for (auto itr = values.begin(); itr != values.end(); ++itr) {
27 T mVal = *itr;
28 outs << stats::hexdumpValue<T>(&mVal, sizeof(T));
29 }
30 return outs.str();
31 }
32
33 // return the most frequent value
34 template <typename T>
35 BYTE getMostFrequentValue(IN const std::map<size_t, std::set< T >>& frequencies)
36 {
37 auto itr = frequencies.rbegin();
38 if (itr == frequencies.rend()) {
39 return 0;
40 }
41 auto setItr = itr->second.begin();
42 T mVal = *setItr;
43 return mVal;
44 }
45
46 // return the number of occurrencies
47 template <typename T>
48 size_t getMostFrequentValues(IN const std::map<size_t, std::set< T >> &frequencies, OUT std::set<T>& values, IN OPTIONAL size_t top = 0, IN OPTIONAL size_t maxDiff = 0)
49 {
50 auto itr = frequencies.rbegin();
51 if (itr == frequencies.rend()) {
52 return 0;
53 }
54 //the highest frequency
55 const size_t mFreq = itr->first;
56 size_t prev = mFreq;
57 for (size_t i = 0; i < top && itr != frequencies.rend(); ++itr, ++i) {
58 const size_t diff = prev - itr->first;
59#ifdef _DEBUG
60 std::cout << "Freq: " << itr->first << " diff : " << diff << "\n";
61#endif
62 if (diff > maxDiff) break;
63 prev = itr->first;
64 values.insert(itr->second.begin(), itr->second.end());
65 }
66 return mFreq;
67 }
68
69 template <typename T>
70 bool isAllPrintable(IN std::map<T, size_t>& histogram)
71 {
72 if (!histogram.size()) return false;
73
74 bool is_printable = true;
75 for (auto itr = histogram.begin(); itr != histogram.end(); ++itr) {
76 T val = itr->first;
77 if (val && !IS_PRINTABLE(val)) {
78 is_printable = false;
79 break;
80 }
81 }
82 return is_printable;
83 }
84
85 }; // namespace stats
86}; //namespace pesieve
size_t getMostFrequentValues(IN const std::map< size_t, std::set< T > > &frequencies, OUT std::set< T > &values, IN OPTIONAL size_t top=0, IN OPTIONAL size_t maxDiff=0)
Definition stats_util.h:48
bool isAllPrintable(IN std::map< T, size_t > &histogram)
Definition stats_util.h:70
std::string hexdumpValue(const BYTE *in_buf, const size_t max_size)
Definition stats_util.h:13
BYTE getMostFrequentValue(IN const std::map< size_t, std::set< T > > &frequencies)
Definition stats_util.h:35
std::string hexdumpValues(std::set< T > &values)
Definition stats_util.h:23
#define IS_PRINTABLE(c)
Definition strings_util.h:8