Issues (195)

lib/Analyzer/FileExtensionAnalyzer.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * @copyright Copyright (c) 2017 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\FileSignatures;
25
use OCA\RansomwareDetection\Entropy\Entropy;
26
use OCP\ILogger;
0 ignored issues
show
The type OCP\ILogger was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
28
class FileExtensionAnalyzer
29
{
30
31
    /** @var ILogger */
32
    private $logger;
33
34
    /**
35
     * @param ILogger $logger
36
     */
37
    public function __construct(
38
        ILogger $logger
39
    ) {
40
        $this->logger = $logger;
41
    }
42
43
    /**
44
     * Classifies a file extension in NOT_SUSPICIOUS or SUSPICIOUS,
45
     * if the file extension are suspicious.
46
     *
47
     * @param string $path
48
     *
49
     * @return FileExtensionResult
50
     */
51
    public function analyze($path)
52
    {
53
        $extension = $this->getFileExtension($path);
54
        $class = FileExtensionResult::NOT_SUSPICIOUS;
55
56
        $isFileExtensionKnown = $this->isFileExtensionKnown($extension);
57
        if (!$isFileExtensionKnown) {
58
            $class = FileExtensionResult::SUSPICIOUS;
59
        }
60
61
        return new FileExtensionResult($class);
62
    }
63
64
    /**
65
     * Checks if the file extension is known.
66
     *
67
     * @param string $extension
68
     *
69
     * @return bool
70
     */
71
    private function isFileExtensionKnown($extension)
72
    {
73
        $signatures = FileSignatures::getSignatures();
74
        foreach ($signatures as $signature) {
75
            if (in_array(strtolower($extension), $signature['extensions'])) {
76
                return true;
77
            }
78
        }
79
80
        return false;
81
    }
82
83
    /**
84
     * Returns the file extension of a file name.
85
     *
86
     * @param string $fileName
87
     *
88
     * @return string
89
     */
90
    private function getFileExtension($fileName)
91
    {
92
        $file = pathinfo($fileName);
93
94
        return isset($file['extension']) ? $file['extension'] : '';
95
    }
96
}
97