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
multi_stats.h
Go to the documentation of this file.
1#pragma once
2
3#include <windows.h>
4#include <iostream>
5#include <string>
6#include <set>
7
8#include "entropy.h"
9#include "stats.h"
10#include "stats_util.h"
12#include "../utils/path_util.h"
13
14namespace pesieve {
15
18 {
19 public:
24
25 // Copy constructor
30
31 bool isFilled()
32 {
33 return (watchedStrings.size() != 0) ? true : false;
34 }
35
37 std::string hasWatchedSubstring(std::string& lastStr)
38 {
39 for (auto itr = watchedStrings.begin(); itr != watchedStrings.end(); ++itr) {
40 const std::string s = *itr;
41 if (lastStr.find(s) != std::string::npos && s.length()) {
42 //std::cout << "[+] KEY for string: " << lastStr << " found: " << s << "\n";
43 return s; // the current string contains searched string
44 }
45 }
46 //std::cout << "[-] KEY for string: " << lastStr << " NOT found!\n";
47 return "";
48 }
49
50 std::set<std::string> watchedStrings;
51 };
52
54 struct ChunkStats {
55 //
61
67
68 // Copy constructor
73 {
74#ifdef _KEEP_STR
75 allStrings = p1.allStrings;
76#endif //_KEEP_STR
77 histogram = p1.histogram;
78 frequencies = p1.frequencies;
79 settings = p1.settings;
80 foundStrings = p1.foundStrings;
81 fillSettings(p1.settings);
82 }
83
88
90 {
91
92 size++;
93 histogram[val]++;
94 prevVal = val;
95
96 // scan strings:
97 const bool isPrint = IS_PRINTABLE(val);
98 if (isPrint) {
99 lastStr += char(val);
100 }
101 else {
102 const bool isClean = (val == 0) ? true : false; //terminated cleanly?
104 lastStr.clear();
105 }
106 }
107
109 {
110 if (lastStr.length() < 2) {
111 return;
112 }
113 stringsCount++;
115
116 if (settings) {
117 std::string key = settings->hasWatchedSubstring(lastStr);
118 if (key.length()) {
119 foundStrings[key]++; // the current string contains searched string
120 }
121 }
122#ifdef _KEEP_STR
123 allStrings.push_back(lastStr);
124#endif //_KEEP_STR
125 //std::cout << "-----> lastStr:" << lastStr << "\n";
126 if (lastStr.length() > longestStr) {
127 longestStr = lastStr.length();
128 }
129 lastStr.clear();
130 }
131
132 const virtual void fieldsToJSON(std::stringstream& outs, size_t level)
133 {
134 OUT_PADDED(outs, level, "\"offset\" : ");
135 outs << std::hex << "\"" << offset << "\"";
136 outs << ",\n";
137 OUT_PADDED(outs, level, "\"size\" : ");
138 outs << std::hex << "\"" << size << "\"";
139 outs << ",\n";
140 OUT_PADDED(outs, level, "\"charset_size\" : ");
141 outs << std::dec << histogram.size();
142
143 std::set<BYTE> values;
144 size_t freq = stats::getMostFrequentValues<BYTE>(frequencies, values);
145 if (freq && values.size()) {
146 outs << ",\n";
147 OUT_PADDED(outs, level, "\"most_freq_vals\" : ");
148 outs << std::hex << "\"";
149 for (auto itr = values.begin(); itr != values.end(); ++itr) {
150 BYTE mVal = *itr;
152 }
153 outs << "\"";
154 }
155 outs << ",\n";
156 OUT_PADDED(outs, level, "\"entropy\" : ");
157 outs << std::dec << entropy;
158 }
159
161 {
163 finishLastStr(true);
164
165 for (auto itr = histogram.begin(); itr != histogram.end(); ++itr) {
166 const size_t count = itr->second;
167 const BYTE val = itr->first;
168 frequencies[count].insert(val);
169 }
170 }
171
172 double entropy;
173 size_t size;
174 size_t offset;
175
177 size_t longestStr; // the longest ASCII string in the chunk
178
179 std::string lastStr;
182 std::map<BYTE, size_t> histogram;
183 std::map<size_t, std::set<BYTE>> frequencies;
184
186
187 std::map<std::string, size_t> foundStrings;
188#ifdef _KEEP_STR
189 std::vector< std::string > allStrings;
190#endif
191 };
192
193 class AreaMultiStats : public AreaStats {
194 public:
196 {
197 }
198
199 // Copy constructor
204
206 {
207 MultiStatsSettings* multiSettings = dynamic_cast<MultiStatsSettings*>(settings);
208 if (!multiSettings) return false;
209
211 return true;
212 }
213
214 const virtual void fieldsToJSON(std::stringstream& outs, size_t level)
215 {
216 OUT_PADDED(outs, level, "\"full_area\" : {\n");
218 outs << "\n";
219 OUT_PADDED(outs, level, "}");
220 }
221
222 bool isFilled() const
223 {
224 return (currArea.size != 0) ? true : false;
225 }
226
228 {
230 }
231
232 ChunkStats currArea; // stats from the whole area
233
234 protected:
236 {
238 }
239
240 };
241};
void _appendVal(BYTE val)
bool fillSettings(StatsSettings *settings)
virtual const void fieldsToJSON(std::stringstream &outs, size_t level)
AreaMultiStats(const AreaMultiStats &p1)
Base class for the statistics from analyzed buffer.
Definition stats.h:20
#define OUT_PADDED(stream, field_size, str)
Definition format_util.h:12
double calcShannonEntropy(std::map< T, size_t > &histogram, size_t totalSize)
Definition entropy.h:22
size_t fill_iat(BYTE *vBuf, size_t vBufSize, IN const peconv::ExportsMapper *exportsMap, IN OUT IATBlock &iat, IN ThunkFoundCallback *callback)
Definition iat_finder.h:31
#define IS_PRINTABLE(c)
Definition strings_util.h:8
Statistics from a block of data.
Definition multi_stats.h:54
std::map< size_t, std::set< BYTE > > frequencies
MultiStatsSettings * settings
void finishLastStr(bool isClean)
void appendVal(BYTE val)
Definition multi_stats.h:89
ChunkStats(size_t _offset, size_t _size)
Definition multi_stats.h:62
std::string lastStr
void fillSettings(MultiStatsSettings *_settings)
Definition multi_stats.h:84
ChunkStats(const ChunkStats &p1)
Definition multi_stats.h:69
std::map< BYTE, size_t > histogram
virtual const void fieldsToJSON(std::stringstream &outs, size_t level)
std::map< std::string, size_t > foundStrings
Settings defining what type of stats should be collected.
Definition multi_stats.h:18
std::string hasWatchedSubstring(std::string &lastStr)
Searches a given substring among the watchedStrings. If the substring found, return the corresponding...
Definition multi_stats.h:37
MultiStatsSettings(const MultiStatsSettings &p1)
Definition multi_stats.h:26
std::set< std::string > watchedStrings
Definition multi_stats.h:50
Base class for settings defining what type of stats should be collected.
Definition stats.h:13