5#define ENTROPY_DATA_TRESHOLD 3.0
6#define ENTROPY_CODE_TRESHOLD ENTROPY_DATA_TRESHOLD
7#define ENTROPY_ENC_TRESHOLD 6.0
8#define ENTROPY_STRONG_ENC_TRESHOLD 7.0
10#define CHARSET_SIZE 0xFF
18 auto val_itr = stats.currArea.histogram.find(val);
20 if (val_itr != stats.currArea.histogram.end()) {
21 ratio = ((double)val_itr->second / (double)stats.currArea.size);
29 if (!stats.currArea.size)
return 0;
30 size_t total_size = 0;
31 for (
auto itr = stats.currArea.histogram.begin(); itr != stats.currArea.histogram.end(); ++itr) {
32 BYTE val = itr->first;
33 size_t size = itr->second;
39 return (
double)total_size / (double)stats.currArea.size;
46 for (
auto itr = ratios.begin(); itr != ratios.end(); ++itr) {
47 BYTE val = itr->first;
49 if (currRatio >= itr->second) {
51 std::cout <<
"[+] OK " << std::hex << (UINT)val << std::dec <<
" : " << currRatio <<
"\n";
61 size_t totalCount = 0;
62 if (!stats.currArea.foundStrings.size()) {
65 for (
auto itr = neededStrings.begin(); itr != neededStrings.end(); ++itr)
67 const std::string& codeStr = *itr;
68 auto found = stats.currArea.foundStrings.find(codeStr);
69 if (found == stats.currArea.foundStrings.end()) {
72 size_t currCount = found->second;
73 if (currCount >= minOccurrence) {
82 if (!currArea.size)
return 0;
84 size_t peaksCount = 0;
85 size_t peakVal = currArea.frequencies.rbegin()->first;
87 for (
auto itr1 = currArea.frequencies.rbegin(); itr1 != currArea.frequencies.rend(); ++itr1, ++i) {
88 size_t counter = itr1->first;
89 double diff = (double)peakVal - (
double)counter;
90 if (diff > (devCount * stdDev))
break;
92 std::set<BYTE> vals = itr1->second;
93 peaksCount += vals.size();
94 peaks.insert(vals.begin(), vals.end());
101 size_t valsCount = 0;
102 for (
auto itr1 = currArea.frequencies.rbegin(); itr1 != currArea.frequencies.rend(); ++itr1) {
103 double counter = (double)itr1->first;
104 if (counter >= mean) {
105 valsCount += itr1->second.size();
120 const size_t patterns_count = 8;
121 char patterns[][patterns_count] = {
131 for (
size_t i = 0; i != patterns_count; ++i) {
132 codeStrings.insert(patterns[i]);
134 return codeStrings.size();
150 const size_t kMinCodePoints = 2;
151 const size_t kMinStrPoints = 2;
153 double entropy = stats.currArea.entropy;
157 std::cout <<
"FOUND strings: " << stats.currArea.foundStrings.size() <<
"\n";
159 for (
auto itr = stats.currArea.foundStrings.begin(); itr != stats.currArea.foundStrings.end(); ++itr)
161 const std::string& codeStr = itr->first;
162 size_t count = itr->second;
163 std::cout <<
"---->>> FOUND Str " << codeStr <<
" count: " << count <<
"\n";
166 std::set<std::string> codeStrings;
171 std::cout <<
"---->>> STR points: " << strPoints <<
"\n";
173 if (codeStrings.size() && !strPoints) {
177 size_t ratiosPoints = 0;
178 std::map<BYTE, double> ratios;
189 std::cout <<
"---->>> CODE points: " << ratiosPoints <<
"\n";
191 if (ratiosPoints < kMinCodePoints) {
194 if (ratiosPoints >= (ratios.size() / 2 + 1)) {
197 if (strPoints < kMinStrPoints) {
213 const double kMinNBRatio = 0.17;
215 double entropy = stats.currArea.entropy;
216 const size_t populationSize = stats.currArea.histogram.size();
227 const double mean = dev.
getMean();
229 const double nBRatio = (double)nB / (
double)populationSize;
236 if (printRatio > 0.8) {
239 if (entropy < ENTROPY_ENC_TRESHOLD && printRatio > 0.6) {
252 double peaksRatio = (double)peaksCount / (
double)populationSize;
253 if (peaksRatio > 0.4) {
256 if (peaks.find(0) == peaks.end()) {
260 if (nBRatio < kMinNBRatio) {
264 std::cout <<
"All peaks: \n";
265 for (
auto itr = peaks.begin(); itr != peaks.end(); itr++) {
266 std::cout << std::hex << (UINT)*itr <<
" ";
283 double entropy = stats.currArea.entropy;
287 if (stats.currArea.frequencies.size() > 1) {
288 auto fItr = stats.currArea.frequencies.begin();
289 auto eItr = stats.currArea.frequencies.rbegin();
291 double diff = ((double)(eItr->first - fItr->first)) / (
double)stats.currArea.size;
294 fullAreaEncrypted =
true;
298 return fullAreaEncrypted;
310 bool possibleText =
false;
312 if (printRatio > 0.8) {
339 if (!stats.isFilled()) {
340 std::cout <<
"Stat not filled!\n";
349 info.matchedRules.push_back(m->
name);
virtual bool _isMatching(IN const AreaMultiStats &stats)
virtual bool _isMatching(IN const AreaMultiStats &stats)
virtual bool _isMatching(IN const AreaMultiStats &stats)
bool isMatching(IN const AreaMultiStats &stats)
virtual bool _isMatching(IN const AreaMultiStats &stats)
double calcSampleStandardDeviation()
size_t valuesNotBelowMean(IN const ChunkStats &currArea, double mean)
double getPrintableRatio(IN const AreaMultiStats &stats)
size_t fillCodeStrings(OUT std::set< std::string > &codeStrings)
size_t fetchPeakValues(IN const ChunkStats &currArea, IN double stdDev, int devCount, OUT std::set< BYTE > &peaks)
BYTE getMostFrequentValue(IN const std::map< size_t, std::set< T > > &frequencies)
double getValRatio(IN const AreaMultiStats &stats, BYTE val)
size_t checkRatios(IN const AreaMultiStats &stats, IN std::map< BYTE, double > &ratios)
std::string info()
The string with the basic information about the scanner.
size_t countFoundStrings(IN const AreaMultiStats &stats, IN const std::set< std::string > &neededStrings, IN size_t minOccurrence)
#define ENTROPY_DATA_TRESHOLD
#define ENTROPY_CODE_TRESHOLD
#define ENTROPY_STRONG_ENC_TRESHOLD
#define ENTROPY_ENC_TRESHOLD
Statistics from a block of data.
void initRules(DWORD ruleTypes)
size_t findMatches(IN const AreaMultiStats &stats, OUT AreaInfo &info)
std::vector< RuleMatcher * > matchers