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

SequenceAnalyzer.php « Analyzer « lib - github.com/undo-ransomware/ransomware_detection.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e1b8c6caa4a19c4adec93a1638e2ad41b9d0e4dd (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
<?php

/**
 * @copyright Copyright (c) 2018 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\Monitor;
use OCA\RansomwareDetection\Classifier;

class SequenceAnalyzer
{
    /**
     * Number of information files.
     *
     * @var int
     */
    const NUMBER_OF_INFO_FILES = 10;

    /** @var SequenceSizeAnalyzer */
    private $sequenceSizeAnalyzer;

    /** @var FileTypeFunnellingAnalyzer */
    private $fileTypeFunnellingAnalyzer;

    /** @var EntropyFunnellingAnalyzer */
    private $entropyFunnellingAnalyzer;

    /**
     * SequenceAnalyzer constructor.
     *
     * @param SequenceSizeAnalyzer       $sequenceSizeAnalyzer
     * @param FileTypeFunnellingAnalyzer $fileTypeFunnellingAnalyzer
     * @param EntropyFunnellingAnalyzer  $entropyFunnellingAnalyzer
     */
    public function __construct(
        SequenceSizeAnalyzer $sequenceSizeAnalyzer,
        FileTypeFunnellingAnalyzer $fileTypeFunnellingAnalyzer,
        EntropyFunnellingAnalyzer $entropyFunnellingAnalyzer
    ) {
        $this->sequenceSizeAnalyzer = $sequenceSizeAnalyzer;
        $this->fileTypeFunnellingAnalyzer = $fileTypeFunnellingAnalyzer;
        $this->entropyFunnellingAnalyzer = $entropyFunnellingAnalyzer;
    }

    /**
     * The analysis of the sequence is seperated in three parts:
     * The analysis: If the same number of files is deletedFilesd as written,
     * with the special addition that the number of written files is in the
     * range of [number of deletedFilesd files, number of deletedFilesd files + 4].
     * To enhance this analysis the sum of the size of the files deletedFilesd and
     * the sum of the size of the written files is compared.
     *
     * The next part is the analysis of the suspicion levels of the files written.
     * Therefor the suspicions levels are weighted:
     * Suspicious - 1
     * Maybe suspicious - 0.5
     * Not suspicious - 0.25
     *
     * summed up and divided by the sum of all written files. The higher the result,
     * the higher is the suspicion of the hole sequence.
     *
     * The last part is the file type funneling analysis.
     *
     * @param int   $sequenceId
     * @param array $sequence
     *
     * @return SequenceResult
     */
    public function analyze($sequenceId, $sequence)
    {
        $sequenceResult = new SequenceResult($sequenceId, 0, 0, 0, 0, $sequence);
        if (sizeof($sequence) === 0) {
            return $sequenceResult;
        }

        $files = ['written' => [], 'sizeWritten' => 0, 'deleted' => [], 'sizeDeleted' => 0, 'suspicious' => [], 'maybeSuspicious' => [], 'notSuspicious' => []];
        $suspicionScore = 0;

        foreach ($sequence as $file) {
            if ($file->getType() === 'file') {
                switch ($file->getCommand()) {
                    case Monitor::WRITE:
                        $files['written'][] = $file;
                        $files['sizeWritten'] = $files['sizeWritten'] + $file->getSize();
                        break;
                    case Monitor::READ:
                        break;
                    case Monitor::RENAME:
                        break;
                    case Monitor::DELETE:
                        $files['deleted'][] = $file;
                        $files['sizeDeleted'] = $files['sizeDeleted'] + $file->getSize();
                        break;
                    case Monitor::CREATE:
                        break;
                    default:
                        break;
                }
                switch ($file->getSuspicionClass()) {
                    case Classifier::SUSPICIOUS:
                        $files['suspicious'][] = $file;
                        break;
                    case Classifier::MAYBE_SUSPICIOUS:
                        $files['maybeSuspicious'][] = $file;
                        break;
                    case Classifier::NOT_SUSPICIOUS:
                        $files['notSuspicious'][] = $file;
                        break;
                    case Classifier::NO_INFORMATION:
                        break;
                    default:
                        break;
                }
            }
        }

        // compare files written and files deleted
        if (sizeof($files['written']) > 0 && sizeof($files['deleted']) > 0) {
            $sequenceResult->setSizeWritten($files['sizeWritten']);
            $sequenceResult->setSizeDeleted($files['sizeDeleted']);
            $upperBound = sizeof($files['deleted']) + self::NUMBER_OF_INFO_FILES;
            if (sizeof($files['written']) <= $upperBound && sizeof($files['written']) >= sizeof($files['deleted'])) {
                if ($this->sequenceSizeAnalyzer->analyze($sequence) === SequenceSizeAnalyzer::EQUAL_SIZE) {
                    $sequenceResult->setQuantities(2);
                    $suspicionScore += 2;
                } else {
                    $sequenceResult->setQuantities(1);
                    $suspicionScore += 1;
                }
            }
        }

        $numberOfWrittenFiles = sizeof($files['suspicious']) + sizeof($files['maybeSuspicious']) + sizeof($files['notSuspicious']);

        // remove info files from the weight
        $numberOfInfoFiles = self::NUMBER_OF_INFO_FILES;
        if (sizeof($files['notSuspicious']) < self::NUMBER_OF_INFO_FILES) {
            $numberOfInfoFiles = sizeof($files['notSuspicious']);
        }

        // weight the suspicion levels.
        $suspicionSum = (sizeof($files['suspicious']) * 1) + (sizeof($files['maybeSuspicious']) * 0.5) + ((sizeof($files['notSuspicious']) - $numberOfInfoFiles) * 0.25);

        // check for division by zero.
        if (($numberOfWrittenFiles - $numberOfInfoFiles) > 0) {
            $sequenceResult->setFileSuspicion($suspicionSum / ($numberOfWrittenFiles - $numberOfInfoFiles));
            $suspicionScore += $suspicionSum / ($numberOfWrittenFiles - $numberOfInfoFiles);
        }

        // entropy funnelling
        $entropyFunnelling = $this->entropyFunnellingAnalyzer->analyze($files['deleted'], $files['written']);
        $sequenceResult->setEntropyFunnelling($entropyFunnelling);
        $suspicionScore += $entropyFunnelling->getEntropyFunnelling();

        // check for file type funneling
        $fileTypeFunnelling = $this->fileTypeFunnellingAnalyzer->analyze($sequence);
        $sequenceResult->setFileTypeFunnelling($fileTypeFunnelling);
        $suspicionScore += $fileTypeFunnelling;

        $sequenceResult->setSuspicionScore($suspicionScore);

        return $sequenceResult;
    }
}