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\AppInfo; |
23
|
|
|
|
24
|
|
|
use OC\Files\Filesystem; |
|
|
|
|
25
|
|
|
use OCA\RansomwareDetection\Monitor; |
26
|
|
|
use OCA\RansomwareDetection\Events\FilesEvents; |
27
|
|
|
use OCA\RansomwareDetection\FilesHooks; |
28
|
|
|
use OCA\RansomwareDetection\Classifier; |
29
|
|
|
use OCA\RansomwareDetection\Analyzer\EntropyAnalyzer; |
30
|
|
|
use OCA\RansomwareDetection\Analyzer\SequenceAnalyzer; |
31
|
|
|
use OCA\RansomwareDetection\Analyzer\SequenceSizeAnalyzer; |
32
|
|
|
use OCA\RansomwareDetection\Analyzer\FileTypeFunnellingAnalyzer; |
33
|
|
|
use OCA\RansomwareDetection\Analyzer\EntropyFunnellingAnalyzer; |
34
|
|
|
use OCA\RansomwareDetection\Analyzer\FileCorruptionAnalyzer; |
35
|
|
|
use OCA\RansomwareDetection\Analyzer\FileExtensionAnalyzer; |
36
|
|
|
use OCA\RansomwareDetection\Entropy\Entropy; |
37
|
|
|
use OCA\RansomwareDetection\Controller\DetectionController; |
38
|
|
|
use OCA\RansomwareDetection\Notification\Notifier; |
39
|
|
|
use OCA\RansomwareDetection\StorageWrapper; |
|
|
|
|
40
|
|
|
use OCA\RansomwareDetection\Connector\Sabre\RequestPlugin; |
41
|
|
|
use OCA\RansomwareDetection\Service\FileOperationService; |
42
|
|
|
use OCA\RansomwareDetection\Service\RecoveredFileOperationService; |
43
|
|
|
use OCA\RansomwareDetection\Service\DetectionService; |
44
|
|
|
use OCA\RansomwareDetection\Mapper\FileOperationMapper; |
|
|
|
|
45
|
|
|
use OCA\RansomwareDetection\Mapper\RecoveredFileOperationMapper; |
|
|
|
|
46
|
|
|
use OCP\AppFramework\App; |
|
|
|
|
47
|
|
|
use OCP\App\IAppManager; |
|
|
|
|
48
|
|
|
use OCP\Files\IRootFolder; |
|
|
|
|
49
|
|
|
use OCP\AppFramework\Utility\ITimeFactory; |
|
|
|
|
50
|
|
|
use OCP\Files\Storage\IStorage; |
|
|
|
|
51
|
|
|
use OCP\Notification\IManager; |
|
|
|
|
52
|
|
|
use OCP\Util; |
|
|
|
|
53
|
|
|
use OCP\SabrePluginEvent; |
|
|
|
|
54
|
|
|
use OCP\ILogger; |
|
|
|
|
55
|
|
|
use OCP\IConfig; |
|
|
|
|
56
|
|
|
use OCP\IUserSession; |
|
|
|
|
57
|
|
|
use OCP\ISession; |
|
|
|
|
58
|
|
|
use OCP\IRequest; |
|
|
|
|
59
|
|
|
|
60
|
|
|
class Application extends App |
61
|
|
|
{ |
62
|
|
|
const APP_ID = 'ransomware_detection'; |
63
|
|
|
|
64
|
|
|
public function __construct() |
65
|
|
|
{ |
66
|
|
|
parent::__construct(self::APP_ID); |
67
|
|
|
|
68
|
|
|
$container = $this->getContainer(); |
69
|
|
|
|
70
|
|
|
// mapper |
71
|
|
|
$container->registerService('FileOperationMapper', function ($c) { |
72
|
|
|
return new FileOperationMapper( |
73
|
|
|
$c->query('ServerContainer')->getDb() |
74
|
|
|
); |
75
|
|
|
}); |
76
|
|
|
|
77
|
|
|
$container->registerService('RecoveredFileOperationMapper', function ($c) { |
78
|
|
|
return new RecoveredFileOperationMapper( |
79
|
|
|
$c->query('ServerContainer')->getDb() |
80
|
|
|
); |
81
|
|
|
}); |
82
|
|
|
|
83
|
|
|
$container->registerService('DetectionDeserializer', function ($c) { |
84
|
|
|
return new DetectionDeserializer( |
|
|
|
|
85
|
|
|
$c->query('FileOperationMapper') |
86
|
|
|
); |
87
|
|
|
}); |
88
|
|
|
|
89
|
|
|
// services |
90
|
|
|
$container->registerService('FileOperationService', function ($c) { |
91
|
|
|
return new FileOperationService( |
|
|
|
|
92
|
|
|
$c->query(FileOperationMapper::class), |
93
|
|
|
$c->query('ServerContainer')->getUserSession()->getUser()->getUID() |
94
|
|
|
); |
95
|
|
|
}); |
96
|
|
|
|
97
|
|
|
$container->registerService('RecoveredFileOperationService', function ($c) { |
98
|
|
|
return new RecoveredFileOperationService( |
99
|
|
|
$c->query(FileOperationMapper::class), |
100
|
|
|
$c->query(RecoveredFileOperationMapper::class), |
101
|
|
|
$c->query('ServerContainer')->getUserSession()->getUser()->getUID() |
102
|
|
|
); |
103
|
|
|
}); |
104
|
|
|
|
105
|
|
|
$container->registerService('DetectionService', function ($c) { |
106
|
|
|
return new DetectionService( |
|
|
|
|
107
|
|
|
$c->query(ILogger::class), |
108
|
|
|
$c->query('FileOperationService'), |
109
|
|
|
$c->query('DetectionDeserializer'), |
110
|
|
|
$c->query('OCP\IConfig'), |
111
|
|
|
$c->query(Classifier::class), |
112
|
|
|
$c->query('ServerContainer')->getUserSession()->getUser()->getUID() |
113
|
|
|
); |
114
|
|
|
}); |
115
|
|
|
|
116
|
|
|
// classifier |
117
|
|
|
$container->registerService('Classifier', function ($c) { |
118
|
|
|
return new Classifier( |
119
|
|
|
$c->query(ILogger::class), |
120
|
|
|
$c->query(FileOperationMapper::class), |
121
|
|
|
$c->query(FileOperationService::class) |
122
|
|
|
); |
123
|
|
|
}); |
124
|
|
|
|
125
|
|
|
// controller |
126
|
|
|
$container->registerService('DetectionController', function ($c) { |
127
|
|
|
return new DetectionController( |
128
|
|
|
$c->query('AppName'), |
129
|
|
|
$c->query('Request'), |
130
|
|
|
$c->query('DetectionService') |
131
|
|
|
); |
132
|
|
|
}); |
133
|
|
|
|
134
|
|
|
// entropy |
135
|
|
|
$container->registerService('Entropy', function ($c) { |
136
|
|
|
return new Entropy( |
137
|
|
|
$c->query(ILogger::class) |
138
|
|
|
); |
139
|
|
|
}); |
140
|
|
|
|
141
|
|
|
// analyzer |
142
|
|
|
$container->registerService('SequenceSizeAnalyzer', function ($c) { |
|
|
|
|
143
|
|
|
return new SequenceSizeAnalyzer(); |
144
|
|
|
}); |
145
|
|
|
|
146
|
|
|
$container->registerService('FileTypeFunnellingAnalyzer', function ($c) { |
|
|
|
|
147
|
|
|
return new FileTypeFunnellingAnalyzer(); |
148
|
|
|
}); |
149
|
|
|
|
150
|
|
|
$container->registerService('EntropyFunnellingAnalyzer', function ($c) { |
151
|
|
|
return new EntropyFunnellingAnalyzer( |
152
|
|
|
$c->query(ILogger::class) |
153
|
|
|
); |
154
|
|
|
}); |
155
|
|
|
|
156
|
|
|
$container->registerService('FileExtensionAnalyzer', function ($c) { |
157
|
|
|
return new FileExtensionAnalyzer( |
158
|
|
|
$c->query(ILogger::class), |
159
|
|
|
$c->query(Entropy::class) |
|
|
|
|
160
|
|
|
|
161
|
|
|
); |
162
|
|
|
}); |
163
|
|
|
|
164
|
|
|
$container->registerService('SequenceAnalyzer', function ($c) { |
165
|
|
|
return new SequenceAnalyzer( |
166
|
|
|
$c->query(SequenceSizeAnalyzer::class), |
167
|
|
|
$c->query(FileTypeFunnellingAnalyzer::class), |
168
|
|
|
$c->query(EntropyFunnellingAnalyzer::class) |
169
|
|
|
); |
170
|
|
|
}); |
171
|
|
|
|
172
|
|
|
$container->registerService('EntropyAnalyzer', function ($c) { |
173
|
|
|
return new EntropyAnalyzer( |
174
|
|
|
$c->query(ILogger::class), |
175
|
|
|
$c->query(IRootFolder::class), |
176
|
|
|
$c->query(Entropy::class), |
177
|
|
|
$c->query('ServerContainer')->getUserSession()->getUser()->getUID() |
178
|
|
|
); |
179
|
|
|
}); |
180
|
|
|
|
181
|
|
|
$container->registerService('FileCorruptionAnalyzer', function ($c) { |
182
|
|
|
return new FileCorruptionAnalyzer( |
183
|
|
|
$c->query(ILogger::class), |
184
|
|
|
$c->query(IRootFolder::class), |
185
|
|
|
$c->query('ServerContainer')->getUserSession()->getUser()->getUID() |
186
|
|
|
); |
187
|
|
|
}); |
188
|
|
|
|
189
|
|
|
$container->registerService('Monitor', function ($c) { |
190
|
|
|
return new Monitor( |
191
|
|
|
$c->query(IRequest::class), |
192
|
|
|
$c->query(IConfig::class), |
193
|
|
|
$c->query(ITimeFactory::class), |
194
|
|
|
$c->query(IAppManager::class), |
195
|
|
|
$c->query(ILogger::class), |
196
|
|
|
$c->query(IRootFolder::class), |
197
|
|
|
$c->query(EntropyAnalyzer::class), |
198
|
|
|
$c->query(FileOperationMapper::class), |
199
|
|
|
$c->query(FileExtensionAnalyzer::class), |
200
|
|
|
$c->query(FileCorruptionAnalyzer::class), |
201
|
|
|
$c->query('ServerContainer')->getUserSession()->getUser()->getUID() |
202
|
|
|
); |
203
|
|
|
}); |
204
|
|
|
|
205
|
|
|
$container->registerService('FilesEvents', function ($c) { |
206
|
|
|
return new FilesEvents( |
207
|
|
|
$c->query(ILogger::class), |
208
|
|
|
$c->query(Monitor::class), |
209
|
|
|
$c->query('ServerContainer')->getUserSession()->getUser()->getUID() |
210
|
|
|
); |
211
|
|
|
}); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Register hooks. |
216
|
|
|
*/ |
217
|
|
|
public function register() |
218
|
|
|
{ |
219
|
|
|
// register sabre plugin to catch the profind requests |
220
|
|
|
$eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher(); |
221
|
|
|
$eventDispatcher->addListener('OCA\DAV\Connector\Sabre::addPlugin', function (SabrePluginEvent $event) { |
222
|
|
|
$logger = $this->getContainer()->query(ILogger::class); |
223
|
|
|
$config = $this->getContainer()->query(IConfig::class); |
224
|
|
|
$userSession = $this->getContainer()->query(IUserSession::class); |
225
|
|
|
$session = $this->getContainer()->query(ISession::class); |
226
|
|
|
$service = $this->getContainer()->query(FileOperationService::class); |
227
|
|
|
$notifications = $this->getContainer()->query(IManager::class); |
228
|
|
|
$classifier = $this->getContainer()->query(Classifier::class); |
229
|
|
|
$sequenceAnalyzer = $this->getContainer()->query(SequenceAnalyzer::class); |
230
|
|
|
$event->getServer()->addPlugin(new RequestPlugin($logger, $config, $userSession, $session, $service, $notifications, $classifier, $sequenceAnalyzer)); |
231
|
|
|
}); |
232
|
|
|
Util::connectHook('OC_Filesystem', 'post_create', FilesHooks::class, 'onFileCreate'); |
233
|
|
|
// Util::connectHook('OC_Filesystem', 'post_update', FilesHooks::class, 'onFileUpdate'); |
234
|
|
|
Util::connectHook('OC_Filesystem', 'post_rename', FilesHooks::class, 'onFileRename'); |
235
|
|
|
Util::connectHook('OC_Filesystem', 'post_write', FilesHooks::class, 'onFileWrite'); |
236
|
|
|
Util::connectHook('OC_Filesystem', 'post_delete', FilesHooks::class, 'onFileDelete'); |
237
|
|
|
// Util::connectHook('OC_Filesystem', 'post_touch', FilesHooks::class, 'onFileTouch'); |
238
|
|
|
// Util::connectHook('OC_Filesystem', 'post_copy', FilesHooks::class, 'onFileCopy'); |
239
|
|
|
$this->registerNotificationNotifier(); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
protected function registerNotificationNotifier() |
243
|
|
|
{ |
244
|
|
|
$this->getContainer()->getServer()->getNotificationManager()->registerNotifierService(Notifier::class); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
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