Welcome to mirror list, hosted at ThFree Co, Russian Federation.

EntropyAnalyzer.php « Analyzer « lib - github.com/undo-ransomware/ransomware_detection.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1a696043c339a6a1c6fe3c344c31906b18466801 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php

/**
 * @copyright Copyright (c) 2017 Matthias Held <matthias.held@uni-konstanz.de>
 * @author Matthias Held <matthias.held@uni-konstanz.de>
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

namespace OCA\RansomwareDetection\Analyzer;

use OCA\RansomwareDetection\AppInfo\Application;
use OCA\RansomwareDetection\Entropy\Entropy;
use OCP\Files\File;
use OCP\Files\IRootFolder;
use OCP\Files\Storage\IStorage;
use OCP\Files\NotFoundException;
use OCP\ILogger;

class EntropyAnalyzer
{
    /**
     * Entropy cut-off point between normal and compressed or encrypted files.
     *
     * @var float
     */
    const ENTROPY_CUT_OFF = 7.69;

    /**
     * Size of the data blocks in bytes.
     *
     * @var int
     */
    const BLOCK_SIZE = 256;

    /**
     * Standard deviation cut-off point between compressed and encrypted files.
     *
     * @var float
     */
    const SD_CUT_OFF = 0.06;

    /** @var ILogger */
    protected $logger;

    /** @var IRootFolder */
    protected $rootFolder;

    /** @var Entropy */
    protected $entropy;

    /** @var string */
    protected $userId;

    /**
     * @param ILogger     $logger
     * @param IRootFolder $rootFolder
     * @param Entropy     $entropy
     * @param int         $userId
     */
    public function __construct(
        ILogger $logger,
        IRootFolder $rootFolder,
        Entropy $entropy,
        $userId
    ) {
        $this->logger = $logger;
        $this->rootFolder = $rootFolder;
        $this->entropy = $entropy;
        $this->userId = $userId;
    }

    /**
     * Classifies a file using entropy measurements. It first calculates the
     * native entropy of the file to decide wether it's a normal file with
     * low entropy or a compressed or encrypted file with high entropy.
     *
     * If the file is identified as class B, it measures the
     * standard deviation of the entropy of all blocks with a size of 256 bytes.
     * To decide if the file is compressed or encrypted.
     *
     * The results classifies the file in the following three classes:
     * ENCRYPTED
     * COMPRESSED
     * NORMAL
     *
     * @param File     $node
     *
     * @return EntropyResult
     */
    public function analyze($node)
    {
        $entropy = $this->calculateEntropyOfFile($node);
        if ($entropy > self::ENTROPY_CUT_OFF) {
            $standardDeviation = $this->calculateStandardDeviationOfEntropy($node, self::BLOCK_SIZE);
            if ($standardDeviation > self::SD_CUT_OFF) {
                return new EntropyResult(EntropyResult::COMPRESSED, $entropy, $standardDeviation);
            }

            return new EntropyResult(EntropyResult::ENCRYPTED, $entropy, $standardDeviation);
        }

        return new EntropyResult(EntropyResult::NORMAL, $entropy, 0.0);
    }

    /**
     * Creates an array with the entropy of the data blocks.
     *
     * @param File   $node
     * @param int    $blockSize
     *
     * @return float
     */
    protected function calculateStandardDeviationOfEntropy($node, $blockSize)
    {
        $sum = 0.0;
        $standardDeviation = 0.0;
        $mean = 1;
        $step = 1;

        $handle = $node->fopen('r');
        if (!$handle) {
            $this->logger->debug('createEntropyArrayFromFile: Getting file handle failed.', array('app' => Application::APP_ID));

            return 0.0;
        }

        while (!feof($handle)) {
            $data = fread($handle, $blockSize);
            $step = $step + 1;
            if (strlen($data) === $blockSize) {
                $entropy = $this->entropy->calculateEntropy($data);
                $sum = $sum + pow($entropy, 2);
                $mean = $this->entropy->streamMean($mean, $entropy, $step);
                $standardDeviation = $this->entropy->streamStandardDeviation($step, $sum, $mean);
            }
        }
        fclose($handle);

        return $standardDeviation;
    }

    /**
     * Calculates the entropy of a file.
     *
     * @param File $node
     *
     * @return float
     */
    protected function calculateEntropyOfFile($node)
    {
        $handle = $node->fopen('r');
        if (!$handle) {
            $this->logger->debug('calculateEntropyOfFile: Getting data failed.', array('app' => Application::APP_ID));

            return 0.0;
        }

        $entropy = 0.0;
        $total = 0;

        while (!feof($handle)) {
            $data = fread($handle, 1024);
            $total = $total + 1;
            if (strlen($data) === 1024) {
                $entropy = $entropy + $this->entropy->calculateEntropy($data);
            }
        }
        fclose($handle);

        $entropy = $entropy / $total;

        if ($entropy >= 0) {
            return $entropy;
        } else {
            return -$entropy;
        }
    }
}