1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) 2018 Matthias Held <[email protected]> |
5
|
|
|
* @author Matthias Held <[email protected]> |
6
|
|
|
* @license GNU AGPL version 3 or any later version |
7
|
|
|
* |
8
|
|
|
* This program is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Affero General Public License as |
10
|
|
|
* published by the Free Software Foundation, either version 3 of the |
11
|
|
|
* License, or (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Affero General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Affero General Public License |
19
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace OCA\RansomwareDetection\Analyzer; |
23
|
|
|
|
24
|
|
|
use OCA\RansomwareDetection\Classifier; |
25
|
|
|
use OCP\ILogger; |
|
|
|
|
26
|
|
|
|
27
|
|
|
class EntropyFunnellingAnalyzer |
28
|
|
|
{ |
29
|
|
|
/** @var ILogger */ |
30
|
|
|
protected $logger; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param ILogger $logger |
34
|
|
|
*/ |
35
|
|
|
public function __construct( |
36
|
|
|
ILogger $logger |
37
|
|
|
) { |
38
|
|
|
$this->logger = $logger; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The entropy funnelling can be divided into two parts:. |
43
|
|
|
* |
44
|
|
|
* 1. The range of the entropy of the deleted files must be wider |
45
|
|
|
* than the range of the entropy of the written files without the info files. |
46
|
|
|
* 2. The range of the standard deviation of the deleted files must be wider |
47
|
|
|
* than the range of the standard deviation of the written files without the info files. |
48
|
|
|
* |
49
|
|
|
* Each of the indicators increase the entropy funnelling score by one. |
50
|
|
|
* |
51
|
|
|
* @param array $deletedFiles |
52
|
|
|
* @param array $writtenFiles |
53
|
|
|
* |
54
|
|
|
* @return EntropyFunnellingResult |
55
|
|
|
*/ |
56
|
|
|
public function analyze($deletedFiles, $writtenFiles) |
57
|
|
|
{ |
58
|
|
|
// prepare data |
59
|
|
|
$entropyOfDeletedFiles = []; |
60
|
|
|
$entropyOfWrittenFiles = []; |
61
|
|
|
$standardDeviationOfDeletedFiles = []; |
62
|
|
|
$standardDeviationOfWrittenFiles = []; |
63
|
|
|
$numberOfInfoFiles = 0; |
64
|
|
|
|
65
|
|
|
foreach ($deletedFiles as $deletedFile) { |
66
|
|
|
array_push($entropyOfDeletedFiles, $deletedFile->getEntropy()); |
67
|
|
|
array_push($standardDeviationOfDeletedFiles, $deletedFile->getStandardDeviation()); |
68
|
|
|
} |
69
|
|
|
foreach ($writtenFiles as $writtenFile) { |
70
|
|
|
// remove the entropy of info files from $entropyOfWrittenFiles |
71
|
|
|
if ($writtenFile->getSuspicionClass() === Classifier::NOT_SUSPICIOUS) { |
72
|
|
|
if ($numberOfInfoFiles < SequenceAnalyzer::NUMBER_OF_INFO_FILES) { |
73
|
|
|
$numberOfInfoFiles++; |
74
|
|
|
break; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
array_push($entropyOfWrittenFiles, $writtenFile->getEntropy()); |
78
|
|
|
array_push($standardDeviationOfWrittenFiles, $writtenFile->getStandardDeviation()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// analyze data |
82
|
|
|
$entropyFunnellingResult = new EntropyFunnellingResult(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0); |
83
|
|
|
$medianOfEntropyDeleted = $this->median($entropyOfDeletedFiles); |
84
|
|
|
$medianOfEntropyWritten = $this->median($entropyOfWrittenFiles); |
85
|
|
|
$entropyFunnellingResult->setMedianDeleted($medianOfEntropyDeleted); |
86
|
|
|
$entropyFunnellingResult->setMedianWritten($medianOfEntropyWritten); |
87
|
|
|
|
88
|
|
|
$entropyFunnellingScore = 0; |
89
|
|
|
|
90
|
|
|
$rangeOfEntropyDeleted = $this->range($entropyOfDeletedFiles); |
91
|
|
|
$rangeOfStandardDeviationDeleted = $this->average($standardDeviationOfDeletedFiles); |
92
|
|
|
$rangeOfEntropyWritten = $this->range($entropyOfWrittenFiles); |
93
|
|
|
$rangeOfStandardDeviationWritten = $this->average($standardDeviationOfWrittenFiles); |
94
|
|
|
$entropyFunnellingResult->setRangeOfEntropyDeleted($rangeOfEntropyDeleted); |
95
|
|
|
$entropyFunnellingResult->setRangeOfEntropyWritten($rangeOfEntropyWritten); |
96
|
|
|
$entropyFunnellingResult->setRangeOfStandardDeviationDeleted($rangeOfStandardDeviationDeleted); |
97
|
|
|
$entropyFunnellingResult->setRangeOfStandardDeviationWritten($rangeOfStandardDeviationWritten); |
98
|
|
|
|
99
|
|
|
// range of $entropyOfDeletedFiles must be wider than range of $entropyOfWrittenFiles |
100
|
|
|
if ($rangeOfEntropyDeleted > $rangeOfEntropyWritten) { |
101
|
|
|
$entropyFunnellingScore = $entropyFunnellingScore + 1; |
102
|
|
|
// range of $standardDeviationOfDeletedFiles must be wider than range of $standardDeviationOfWrittenFiles |
103
|
|
|
if ($rangeOfStandardDeviationDeleted > $rangeOfStandardDeviationWritten) { |
104
|
|
|
$entropyFunnellingScore = $entropyFunnellingScore + 1; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$entropyFunnellingResult->setEntropyFunnelling($entropyFunnellingScore); |
109
|
|
|
|
110
|
|
|
return $entropyFunnellingResult; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Calculates the average of an array. |
115
|
|
|
* |
116
|
|
|
* @param array $array |
117
|
|
|
* |
118
|
|
|
* @return float |
119
|
|
|
*/ |
120
|
|
|
private function average($array) |
121
|
|
|
{ |
122
|
|
|
if (is_array($array) && count($array) > 0) { |
123
|
|
|
return array_sum($array) / count($array); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return 0.0; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Calculates the range of an array. |
131
|
|
|
* |
132
|
|
|
* @param array $array |
133
|
|
|
* |
134
|
|
|
* @return float |
135
|
|
|
*/ |
136
|
|
|
private function range($array) |
137
|
|
|
{ |
138
|
|
|
if (is_array($array) && count($array) > 0) { |
139
|
|
|
sort($array); |
140
|
|
|
|
141
|
|
|
return $array[count($array) - 1] - $array[0]; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return 0.0; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Calculates the median of an array. |
149
|
|
|
* |
150
|
|
|
* @param array $array |
151
|
|
|
* |
152
|
|
|
* @return float |
153
|
|
|
*/ |
154
|
|
|
public function median($array) |
155
|
|
|
{ |
156
|
|
|
if (is_array($array) && count($array) > 0) { |
157
|
|
|
$count = count($array); |
158
|
|
|
sort($array); |
159
|
|
|
$mid = floor(($count - 1) / 2); |
160
|
|
|
|
161
|
|
|
return ($array[$mid] + $array[$mid + 1 - $count % 2]) / 2; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return 0.0; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths