From f1dcc36d1a7d6d6e4ac1f7cc57818c877357d32c Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 29 Mar 2020 15:28:37 +0200 Subject: try post file hooks --- lib/AppInfo/Application.php | 60 +++++++++++++++++++++- lib/Events/FilesEvents.php | 122 ++++++++++++++++++++++++++++++++++++++++++++ lib/FilesHooks.php | 109 +++++++++++++++++++++++++++++++++++++++ lib/Monitor.php | 24 ++++----- 4 files changed, 301 insertions(+), 14 deletions(-) create mode 100644 lib/Events/FilesEvents.php create mode 100644 lib/FilesHooks.php diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index dedb5af..287c7f2 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -23,11 +23,15 @@ namespace OCA\RansomwareDetection\AppInfo; use OC\Files\Filesystem; use OCA\RansomwareDetection\Monitor; +use OCA\RansomwareDetection\Events\FilesEvents; +use OCA\RansomwareDetection\FilesHooks; use OCA\RansomwareDetection\Classifier; +use OCA\RansomwareDetection\Analyzer\EntropyAnalyzer; use OCA\RansomwareDetection\Analyzer\SequenceAnalyzer; use OCA\RansomwareDetection\Analyzer\SequenceSizeAnalyzer; use OCA\RansomwareDetection\Analyzer\FileTypeFunnellingAnalyzer; use OCA\RansomwareDetection\Analyzer\EntropyFunnellingAnalyzer; +use OCA\RansomwareDetection\Analyzer\FileCorruptionAnalyzer; use OCA\RansomwareDetection\Analyzer\FileExtensionAnalyzer; use OCA\RansomwareDetection\Entropy\Entropy; use OCA\RansomwareDetection\Notification\Notifier; @@ -36,6 +40,9 @@ use OCA\RansomwareDetection\Connector\Sabre\RequestPlugin; use OCA\RansomwareDetection\Service\FileOperationService; use OCA\RansomwareDetection\Mapper\FileOperationMapper; use OCP\AppFramework\App; +use OCP\App\IAppManager; +use OCP\Files\IRootFolder; +use OCP\AppFramework\Utility\ITimeFactory; use OCP\Files\Storage\IStorage; use OCP\Notification\IManager; use OCP\Util; @@ -44,6 +51,7 @@ use OCP\ILogger; use OCP\IConfig; use OCP\IUserSession; use OCP\ISession; +use OCP\IRequest; class Application extends App { @@ -65,7 +73,7 @@ class Application extends App // services $container->registerService('FileOperationService', function ($c) { return new FileOperationService( - $c->query('FileOperationMapper'), + $c->query(FileOperationMapper::class), $c->query('ServerContainer')->getUserSession()->getUser()->getUID() ); }); @@ -116,6 +124,47 @@ class Application extends App $c->query(EntropyFunnellingAnalyzer::class) ); }); + + $container->registerService('EntropyAnalyzer', function ($c) { + return new EntropyAnalyzer( + $c->query(ILogger::class), + $c->query(IRootFolder::class), + $c->query(Entropy::class), + $c->query('ServerContainer')->getUserSession()->getUser()->getUID() + ); + }); + + $container->registerService('FileCorruptionAnalyzer', function ($c) { + return new FileCorruptionAnalyzer( + $c->query(ILogger::class), + $c->query(IRootFolder::class), + $c->query('ServerContainer')->getUserSession()->getUser()->getUID() + ); + }); + + $container->registerService('Monitor', function ($c) { + return new Monitor( + $c->query(IRequest::class), + $c->query(IConfig::class), + $c->query(ITimeFactory::class), + $c->query(IAppManager::class), + $c->query(ILogger::class), + $c->query(IRootFolder::class), + $c->query(EntropyAnalyzer::class), + $c->query(FileOperationMapper::class), + $c->query(FileExtensionAnalyzer::class), + $c->query(FileCorruptionAnalyzer::class), + $c->query('ServerContainer')->getUserSession()->getUser()->getUID() + ); + }); + + $container->registerService('FilesEvents', function ($c) { + return new FilesEvents( + $c->query(ILogger::class), + $c->query(Monitor::class), + $c->query('ServerContainer')->getUserSession()->getUser()->getUID() + ); + }); } /** @@ -136,7 +185,14 @@ class Application extends App $sequenceAnalyzer = $this->getContainer()->query(SequenceAnalyzer::class); $event->getServer()->addPlugin(new RequestPlugin($logger, $config, $userSession, $session, $service, $notifications, $classifier, $sequenceAnalyzer)); }); - Util::connectHook('OC_Filesystem', 'preSetup', $this, 'addStorageWrapper'); + //Util::connectHook('OC_Filesystem', 'preSetup', $this, 'addStorageWrapper'); + Util::connectHook('OC_Filesystem', 'post_create', FilesHooks::class, 'onFileCreate'); + Util::connectHook('OC_Filesystem', 'post_update', FilesHooks::class, 'onFileUpdate'); + Util::connectHook('OC_Filesystem', 'post_rename', FilesHooks::class, 'onFileRename'); + Util::connectHook('OC_Filesystem', 'post_write', FilesHooks::class, 'onFileWrite'); + Util::connectHook('OC_Filesystem', 'post_delete', FilesHooks::class, 'onFileDelete'); + Util::connectHook('OC_Filesystem', 'post_touch', FilesHooks::class, 'onFileTouch'); + Util::connectHook('OC_Filesystem', 'post_copy', FilesHooks::class, 'onFileCopy'); $this->registerNotificationNotifier(); } diff --git a/lib/Events/FilesEvents.php b/lib/Events/FilesEvents.php new file mode 100644 index 0000000..b1ba0c1 --- /dev/null +++ b/lib/Events/FilesEvents.php @@ -0,0 +1,122 @@ + + * @copyright 2018 + * @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 . + * + */ + + +namespace OCA\RansomwareDetection\Events; + +use OCA\RansomwareDetection\Monitor; +use OCA\RansomwareDetection\AppInfo\Application; +use OCP\ILogger; + +/** + * Class FilesEvents + * + * @package OCA\Files_FullTextSearch\Events + */ +class FilesEvents { + + /** @var string */ + private $userId; + + private $logger; + + private $monitor; + + + /** + * FilesEvents constructor. + * + * @param string $userId + */ + public function __construct( + ILogger $logger, + $monitor, + $userId + + ) { + $this->logger = $logger; + $this->monitor = $monitor; + $this->userId = $userId; + } + + /** + * @param array $params + * + * @throws InvalidPathException + * @throws NotFoundException + */ + public function onFileUpdate(array $params) { + $this->analyze([$params['path']], Monitor::WRITE); + $this->logger->error("Updating ".$params['path'], ['app' => Application::APP_ID]); + } + + + /** + * @param array $params + * + * @throws NotFoundException + * @throws InvalidPathException + */ + public function onFileRename(array $params) { + $this->logger->error("Renaming ".$params['oldpath']." to ".$params['newpath'], ['app' => Application::APP_ID]); + $this->analyze([$params['oldpath'], $params['newpath']], Monitor::RENAME); + } + + public function onFileCreate(array $params) { + $this->logger->error("Creating ".$params['path'], ['app' => Application::APP_ID]); + } + + public function onFileWrite(array $params) { + $this->logger->error("Writing ".$params['path'], ['app' => Application::APP_ID]); + } + + public function onFileDelete(array $params) { + $this->logger->error("Deleting ".$params['path'], ['app' => Application::APP_ID]); + } + + public function onFileCopy(array $params) { + $this->logger->error("Copying ".$params['path'], ['app' => Application::APP_ID]); + } + + public function onFileTouch(array $params) { + $this->logger->error("Touching ".$params['path'], ['app' => Application::APP_ID]); + } + + /** + * Makes it easier to test. + * + * @param IStorage $storage + * @param string $path + * @param int $mode + */ + protected function analyze($path, $mode) + { + return $this->monitor->analyze($path, $mode); + } +} \ No newline at end of file diff --git a/lib/FilesHooks.php b/lib/FilesHooks.php new file mode 100644 index 0000000..efd81d0 --- /dev/null +++ b/lib/FilesHooks.php @@ -0,0 +1,109 @@ + + * @copyright 2018 + * @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 . + * + */ + + +namespace OCA\RansomwareDetection; + +use OCA\RansomwareDetection\AppInfo\Application; +use OCA\RansomwareDetection\Events\FilesEvents; + +/** + * Class FilesHooks + * + * @package OCA\Files_FullTextSearch\Hooks + */ +class FilesHooks { + + /** + * retrieve the FilesEvents' Controller + * + * @return FilesEvents + * @throws QueryException + */ + protected static function getController(): FilesEvents { + $app = new Application(); + + return $app->getContainer() + ->query(FilesEvents::class); + } + + /** + * hook events: file is updated + * + * @param array $params + * + * @throws QueryException + * @throws InvalidPathException + * @throws NotFoundException + */ + public static function onFileUpdate(array $params) { + self::getController() + ->onFileUpdate($params); + } + + + /** + * hook events: file is renamed + * + * @param array $params + * + * @throws NotFoundException + * @throws QueryException + * @throws InvalidPathException + */ + public static function onFileRename(array $params) { + self::getController() + ->onFileRename($params); + } + + public static function onFileCreate(array $params) { + self::getController() + ->onFileCreate($params); + } + + public static function onFileWrite(array $params) { + self::getController() + ->onFileWrite($params); + } + + public static function onFileDelete(array $params) { + self::getController() + ->onFileDelete($params); + } + + public static function onFileTouch(array $params) { + self::getController() + ->onFileTouch($params); + } + + public static function onFileCopy(array $params) { + self::getController() + ->onFileCopy($params); + } +} \ No newline at end of file diff --git a/lib/Monitor.php b/lib/Monitor.php index 9c658b9..955d854 100644 --- a/lib/Monitor.php +++ b/lib/Monitor.php @@ -89,17 +89,17 @@ class Monitor protected $nestingLevel = 0; /** - * @param IRequest $request - * @param IConfig $config - * @param ITimeFactory $time - * @param IAppManager $appManager - * @param ILogger $logger - * @param IRootFolder $rootFolder - * @param EntropyAnalyzer $entropyAnalyzer - * @param FileOperationMapper $mapper + * @param IRequest $request + * @param IConfig $config + * @param ITimeFactory $time + * @param IAppManager $appManager + * @param ILogger $logger + * @param IRootFolder $rootFolder + * @param EntropyAnalyzer $entropyAnalyzer + * @param FileOperationMapper $mapper * @param FileExtensionAnalyzer $fileExtensionAnalyzer - * @param FileCorruptionAnalyzer $fileCorruptionAnalyzer - * @param string $userId + * @param FileCorruptionAnalyzer $fileCorruptionAnalyzer + * @param string $userId */ public function __construct( IRequest $request, @@ -130,13 +130,13 @@ class Monitor /** * Analyze file. * - * @param IStorage $storage * @param array $paths * @param int $mode */ - public function analyze(IStorage $storage, $paths, $mode) + public function analyze($paths, $mode) { $path = $paths[0]; + $storage = $this->rootFolder->get(dirname($path))->getStorage(); if ($this->userId === null || $this->nestingLevel !== 0 || !$this->isUploadedFile($storage, $path) || $this->isCreatingSkeletonFiles()) { // check only cloud files and no system files return; -- cgit v1.2.3 From 892cf6bd9200e36aa89c2f3e4333d2601f5da268 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 29 Mar 2020 17:12:41 +0200 Subject: fix dependency injecting the monitor --- lib/Events/FilesEvents.php | 19 ++++++++++++------- lib/Monitor.php | 9 +++++++-- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/Events/FilesEvents.php b/lib/Events/FilesEvents.php index b1ba0c1..a996e61 100644 --- a/lib/Events/FilesEvents.php +++ b/lib/Events/FilesEvents.php @@ -56,7 +56,7 @@ class FilesEvents { */ public function __construct( ILogger $logger, - $monitor, + Monitor $monitor, $userId ) { @@ -72,8 +72,8 @@ class FilesEvents { * @throws NotFoundException */ public function onFileUpdate(array $params) { + $this->logger->error("Updating ".$params['path'], ['app' => Application::APP_ID]); $this->analyze([$params['path']], Monitor::WRITE); - $this->logger->error("Updating ".$params['path'], ['app' => Application::APP_ID]); } @@ -89,23 +89,28 @@ class FilesEvents { } public function onFileCreate(array $params) { - $this->logger->error("Creating ".$params['path'], ['app' => Application::APP_ID]); + $this->logger->error("Creating ".$params['path'], ['app' => Application::APP_ID]); + $this->analyze([$params['path']], Monitor::CREATE); } public function onFileWrite(array $params) { - $this->logger->error("Writing ".$params['path'], ['app' => Application::APP_ID]); + $this->logger->error("Writing ".$params['path'], ['app' => Application::APP_ID]); + $this->analyze([$params['path']], Monitor::WRITE); } public function onFileDelete(array $params) { - $this->logger->error("Deleting ".$params['path'], ['app' => Application::APP_ID]); + $this->logger->error("Deleting ".$params['path'], ['app' => Application::APP_ID]); + $this->analyze([$params['path']], Monitor::DELETE); } public function onFileCopy(array $params) { - $this->logger->error("Copying ".$params['path'], ['app' => Application::APP_ID]); + $this->logger->error("Copying ".$params['oldpath']." to ".$params['newpath'], ['app' => Application::APP_ID]); + $this->analyze([$params['oldpath'], $params['newpath']], Monitor::RENAME); } public function onFileTouch(array $params) { - $this->logger->error("Touching ".$params['path'], ['app' => Application::APP_ID]); + $this->logger->error("Touching ".$params['path'], ['app' => Application::APP_ID]); + $this->analyze([$params['path']], Monitor::WRITE); } /** diff --git a/lib/Monitor.php b/lib/Monitor.php index 955d854..e77ba09 100644 --- a/lib/Monitor.php +++ b/lib/Monitor.php @@ -136,7 +136,8 @@ class Monitor public function analyze($paths, $mode) { $path = $paths[0]; - $storage = $this->rootFolder->get(dirname($path))->getStorage(); + + $storage = $this->rootFolder->getUserFolder($this->userId)->get(dirname($path))->getStorage(); if ($this->userId === null || $this->nestingLevel !== 0 || !$this->isUploadedFile($storage, $path) || $this->isCreatingSkeletonFiles()) { // check only cloud files and no system files return; @@ -353,7 +354,11 @@ class Monitor $fullPath = $path; if (property_exists($storage, 'mountPoint')) { /* @var StorageWrapper $storage */ - $fullPath = $storage->mountPoint.$path; + try { + $fullPath = $storage->mountPoint.$path; + } catch (\Exception $ex) { + return false; + } } // ignore transfer files -- cgit v1.2.3 From 1bee2eee08ec714efe15265f109f6c59d1773ff6 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 29 Mar 2020 18:55:55 +0200 Subject: add logging for debugging --- lib/Analyzer/EntropyAnalyzer.php | 2 ++ lib/Monitor.php | 27 +++++++++++++++------------ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/Analyzer/EntropyAnalyzer.php b/lib/Analyzer/EntropyAnalyzer.php index 6aa39ba..b87d3e6 100644 --- a/lib/Analyzer/EntropyAnalyzer.php +++ b/lib/Analyzer/EntropyAnalyzer.php @@ -170,9 +170,11 @@ class EntropyAnalyzer $entropy = 0.0; $total = 0; + $this->logger->error("Calculate entropy", ['app' => Application::APP_ID]); while (!feof($handle)) { $data = fread($handle, 1024); + $this->logger->error("Data ".$data, ['app' => Application::APP_ID]); $total = $total + 1; if (strlen($data) === 1024) { $entropy = $entropy + $this->entropy->calculateEntropy($data); diff --git a/lib/Monitor.php b/lib/Monitor.php index e77ba09..06f1d56 100644 --- a/lib/Monitor.php +++ b/lib/Monitor.php @@ -138,7 +138,7 @@ class Monitor $path = $paths[0]; $storage = $this->rootFolder->getUserFolder($this->userId)->get(dirname($path))->getStorage(); - if ($this->userId === null || $this->nestingLevel !== 0 || !$this->isUploadedFile($storage, $path) || $this->isCreatingSkeletonFiles()) { + if ($this->userId === null || $this->nestingLevel !== 0 /*|| !$this->isUploadedFile($storage, $path)*/ || $this->isCreatingSkeletonFiles()) { // check only cloud files and no system files return; } @@ -156,6 +156,8 @@ class Monitor switch ($mode) { case self::RENAME: + $path = $paths[1]; + $this->logger->error("Rename ".$paths[0]." to ".$paths[1], ['app' => Application::APP_ID]); if (preg_match('/.+\.d[0-9]+/', pathinfo($paths[1])['basename']) > 0) { return; } @@ -163,9 +165,10 @@ class Monitor $this->resetProfindCount(); try { - $userRoot = $this->rootFolder->getUserFolder($this->userId)->getParent(); + $userRoot = $this->rootFolder->getUserFolder($this->userId); $node = $userRoot->get($path); } catch (\OCP\Files\NotFoundException $exception) { + $this->logger->error("File Not Found ".$path, ['app' => Application::APP_ID]); return; } @@ -177,23 +180,21 @@ class Monitor return; } - $node->changeLock(\OCP\Lock\ILockingProvider::LOCK_SHARED); - $this->addFileOperation($paths, $node, self::RENAME); - $node->changeLock(\OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE); - $this->nestingLevel--; return; case self::WRITE: + $this->logger->error("Write ".$path, ['app' => Application::APP_ID]); // reset PROPFIND_COUNT $this->resetProfindCount(); try { - $userRoot = $this->rootFolder->getUserFolder($this->userId)->getParent(); + $userRoot = $this->rootFolder->getUserFolder($this->userId); $node = $userRoot->get($path); } catch (\OCP\Files\NotFoundException $exception) { + $this->logger->error("File Not Found ".$path, ['app' => Application::APP_ID]); return; } @@ -215,13 +216,15 @@ class Monitor return; case self::DELETE: + $this->logger->error("Delete", ['app' => Application::APP_ID]); // reset PROPFIND_COUNT $this->resetProfindCount(); try { - $userRoot = $this->rootFolder->getUserFolder($this->userId)->getParent(); + $userRoot = $this->rootFolder->getUserFolder($this->userId); $node = $userRoot->get($path); } catch (\OCP\Files\NotFoundException $exception) { + $this->logger->error("File Not Found ".$path, ['app' => Application::APP_ID]); return; } @@ -233,15 +236,13 @@ class Monitor return; } - $node->changeLock(\OCP\Lock\ILockingProvider::LOCK_SHARED); - $this->addFileOperation($paths, $node, self::DELETE); - $node->changeLock(\OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE); $this->nestingLevel--; return; case self::CREATE: + $this->logger->error("Create", ['app' => Application::APP_ID]); // only folders are created // reset PROPFIND_COUNT @@ -357,7 +358,7 @@ class Monitor try { $fullPath = $storage->mountPoint.$path; } catch (\Exception $ex) { - return false; + return true; } } @@ -425,6 +426,7 @@ class Monitor */ private function addFileOperation($paths, $node, $operation) { + $this->logger->error("Add file operation", ['app' => Application::APP_ID]); $fileOperation = new FileOperation(); $fileOperation->setUserId($this->userId); $fileOperation->setPath(str_replace('files', '', pathinfo($node->getInternalPath())['dirname'])); @@ -457,6 +459,7 @@ class Monitor $fileOperation->setStandardDeviation($entropyResult->getStandardDeviation()); $fileOperation->setFileClass($entropyResult->getFileClass()); + $this->logger->error("Entropy ".$entropyResult->getEntropy(), ['app' => Application::APP_ID]); $entity = $this->mapper->insert($fileOperation); } -- cgit v1.2.3 From 1cbc676f20f9f0d983df4ee1f6deceb0df922d08 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 29 Mar 2020 19:06:37 +0200 Subject: check if file or folder is created --- lib/Monitor.php | 64 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/lib/Monitor.php b/lib/Monitor.php index 06f1d56..dc5aa1e 100644 --- a/lib/Monitor.php +++ b/lib/Monitor.php @@ -138,7 +138,7 @@ class Monitor $path = $paths[0]; $storage = $this->rootFolder->getUserFolder($this->userId)->get(dirname($path))->getStorage(); - if ($this->userId === null || $this->nestingLevel !== 0 /*|| !$this->isUploadedFile($storage, $path)*/ || $this->isCreatingSkeletonFiles()) { + if ($this->userId === null || $this->nestingLevel !== 0 || /*!$this->isUploadedFile($storage, $path) ||*/ $this->isCreatingSkeletonFiles()) { // check only cloud files and no system files return; } @@ -243,34 +243,46 @@ class Monitor return; case self::CREATE: $this->logger->error("Create", ['app' => Application::APP_ID]); - // only folders are created - // reset PROPFIND_COUNT $this->resetProfindCount(); - $fileOperation = new FileOperation(); - $fileOperation->setUserId($this->userId); - $fileOperation->setPath(str_replace('files', '', pathinfo($path)['dirname'])); - $fileOperation->setOriginalName(pathinfo($path)['basename']); - $fileOperation->setType('folder'); - $fileOperation->setMimeType('httpd/unix-directory'); - $fileOperation->setSize(0); - $fileOperation->setTimestamp(time()); - $fileOperation->setCorrupted(false); - $fileOperation->setCommand(self::CREATE); - $sequenceId = $this->config->getUserValue($this->userId, Application::APP_ID, 'sequence_id', 0); - $fileOperation->setSequence($sequenceId); - - // entropy analysis - $fileOperation->setEntropy(0.0); - $fileOperation->setStandardDeviation(0.0); - $fileOperation->setFileClass(EntropyResult::NORMAL); - - // file extension analysis - $fileOperation->setFileExtensionClass(FileExtensionResult::NOT_SUSPICIOUS); - - $this->mapper->insert($fileOperation); - $this->nestingLevel--; + try { + $userRoot = $this->rootFolder->getUserFolder($this->userId); + $node = $userRoot->get($path); + } catch (\OCP\Files\NotFoundException $exception) { + $this->logger->error("File Not Found ".$path, ['app' => Application::APP_ID]); + return; + } + if (!($node instanceof File)) { + + $fileOperation = new FileOperation(); + $fileOperation->setUserId($this->userId); + $fileOperation->setPath(str_replace('files', '', pathinfo($path)['dirname'])); + $fileOperation->setOriginalName(pathinfo($path)['basename']); + $fileOperation->setType('folder'); + $fileOperation->setMimeType('httpd/unix-directory'); + $fileOperation->setSize(0); + $fileOperation->setTimestamp(time()); + $fileOperation->setCorrupted(false); + $fileOperation->setCommand(self::CREATE); + $sequenceId = $this->config->getUserValue($this->userId, Application::APP_ID, 'sequence_id', 0); + $fileOperation->setSequence($sequenceId); + + // entropy analysis + $fileOperation->setEntropy(0.0); + $fileOperation->setStandardDeviation(0.0); + $fileOperation->setFileClass(EntropyResult::NORMAL); + + // file extension analysis + $fileOperation->setFileExtensionClass(FileExtensionResult::NOT_SUSPICIOUS); + + $this->mapper->insert($fileOperation); + $this->nestingLevel--; + } else { + $this->addFileOperation($paths, $node, self::CREATE); + + $this->nestingLevel--; + } return; default: -- cgit v1.2.3 From f5dc1c05f0907a282bd91db89a460d23b70a5c99 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 30 Mar 2020 21:19:03 +0200 Subject: update changelog --- CHANGELOG.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0517815..f134279 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,11 +2,17 @@ All notable changes to this project will be documented in this file. +## 0.7.1 + +### Fixed + +- Fix deadlock during sync of notes via the Android Notes app by using post file hooks instead of storage wrapper with pre setup hook. + ## 0.7.0 ### Added -- Nextcloud version 17 support. +- Nextcloud version 18 support. ## 0.6.0 -- cgit v1.2.3 From 9bc1a9180af141ed6349344ce2fd00a7b2ddefe7 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 31 Mar 2020 19:15:30 +0200 Subject: remove storage wrapper --- lib/AppInfo/Application.php | 33 ----- lib/StorageWrapper.php | 259 -------------------------------------- tests/Unit/StorageWrapperTest.php | 203 ------------------------------ 3 files changed, 495 deletions(-) delete mode 100644 lib/StorageWrapper.php delete mode 100644 tests/Unit/StorageWrapperTest.php diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 287c7f2..a07ee35 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -185,7 +185,6 @@ class Application extends App $sequenceAnalyzer = $this->getContainer()->query(SequenceAnalyzer::class); $event->getServer()->addPlugin(new RequestPlugin($logger, $config, $userSession, $session, $service, $notifications, $classifier, $sequenceAnalyzer)); }); - //Util::connectHook('OC_Filesystem', 'preSetup', $this, 'addStorageWrapper'); Util::connectHook('OC_Filesystem', 'post_create', FilesHooks::class, 'onFileCreate'); Util::connectHook('OC_Filesystem', 'post_update', FilesHooks::class, 'onFileUpdate'); Util::connectHook('OC_Filesystem', 'post_rename', FilesHooks::class, 'onFileRename'); @@ -196,38 +195,6 @@ class Application extends App $this->registerNotificationNotifier(); } - /** - * @internal - */ - public function addStorageWrapper() - { - Filesystem::addStorageWrapper(self::APP_ID, [$this, 'addStorageWrapperCallback'], -10); - } - - /** - * @internal - * - * @param string $mountPoint - * @param IStorage $storage - * - * @return StorageWrapper|IStorage - */ - public function addStorageWrapperCallback($mountPoint, IStorage $storage) - { - if (!\OC::$CLI && !$storage->instanceOfStorage('OCA\Files_Sharing\SharedStorage')) { - /** @var Monitor $monitor */ - $monitor = $this->getContainer()->query(Monitor::class); - - return new StorageWrapper([ - 'storage' => $storage, - 'mountPoint' => $mountPoint, - 'monitor' => $monitor, - ]); - } - - return $storage; - } - protected function registerNotificationNotifier() { $this->getContainer()->getServer()->getNotificationManager()->registerNotifierService(Notifier::class); diff --git a/lib/StorageWrapper.php b/lib/StorageWrapper.php deleted file mode 100644 index 17f18e3..0000000 --- a/lib/StorageWrapper.php +++ /dev/null @@ -1,259 +0,0 @@ - - * @author Matthias Held - * @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 - * 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 . - */ - -namespace OCA\RansomwareDetection; - -use OC\Files\Storage\Wrapper\Wrapper; -use OCP\Files\Storage\IStorage; - -class StorageWrapper extends Wrapper -{ - /** @var Monitor */ - protected $monitor; - - /** @var string */ - public $mountPoint; - - /** - * @param array $parameters - */ - public function __construct( - $parameters - ) { - parent::__construct($parameters); - $this->monitor = $parameters['monitor']; - $this->mountPoint = $parameters['mountPoint']; - } - - /** - * see http://php.net/manual/en/function.mkdir.php. - * - * @param string $path - * - * @return bool - */ - public function mkdir($path) - { - $this->analyze($this, [$path], Monitor::CREATE); - - return $this->storage->mkdir($path); - } - - /** - * see http://php.net/manual/en/function.rmdir.php. - * - * @param string $path - * - * @return bool - */ - public function rmdir($path) - { - $this->analyze($this, [$path], Monitor::DELETE); - - return $this->storage->rmdir($path); - } - - /** - * see http://php.net/manual/en/function.file_get_contents.php. - * - * @param string $path - * - * @return string - */ - public function file_get_contents($path) - { - $this->analyze($this, [$path], Monitor::READ); - - return $this->storage->file_get_contents($path); - } - - /** - * see http://php.net/manual/en/function.file_put_contents.php. - * - * @param string $path - * @param string $data - * - * @return bool - */ - public function file_put_contents($path, $data) - { - $this->analyze($this, [$path], Monitor::WRITE); - - return $this->storage->file_put_contents($path, $data); - } - - /** - * see http://php.net/manual/en/function.unlink.php. - * - * @param string $path - * - * @return bool - */ - public function unlink($path) - { - $this->analyze($this, [$path], Monitor::DELETE); - - return $this->storage->unlink($path); - } - - /** - * see http://php.net/manual/en/function.rename.php. - * - * @param string $path1 - * @param string $path2 - * - * @return bool - */ - public function rename($path1, $path2) - { - $this->analyze($this, [$path1, $path2], Monitor::RENAME); - - return $this->storage->rename($path1, $path2); - } - - /** - * see http://php.net/manual/en/function.copy.php. - * - * @param string $path1 - * @param string $path2 - * - * @return bool - */ - public function copy($path1, $path2) - { - $this->analyze($this, [$path1, $path2], Monitor::WRITE); - - return $this->storage->copy($path1, $path2); - } - - /** - * see http://php.net/manual/en/function.fopen.php. - * - * @param string $path - * @param string $mode - * - * @return resource - */ - public function fopen($path, $mode) - { - $fileMode = Monitor::READ; - switch ($mode) { - case 'r+': - case 'rb+': - case 'w+': - case 'wb+': - case 'x+': - case 'xb+': - case 'a+': - case 'ab+': - case 'w': - case 'wb': - case 'x': - case 'xb': - case 'a': - case 'ab': - $fileMode = Monitor::WRITE; - } - $this->analyze($this, [$path], $fileMode); - - return $this->storage->fopen($path, $mode); - } - - /** - * see http://php.net/manual/en/function.touch.php - * If the backend does not support the operation, false should be returned. - * - * @param string $path - * @param int $mtime - * - * @return bool - */ - public function touch($path, $mtime = null) - { - $this->analyze($this, [$path], Monitor::WRITE); - - return $this->storage->touch($path, $mtime); - } - - /** - * get a cache instance for the storage. - * - * @param string $path - * @param \OC\Files\Storage\Storage (optional) the storage to pass to the cache - * - * @return \OC\Files\Cache\Cache - */ - public function getCache($path = '', $storage = null) - { - if (!$storage) { - $storage = $this; - } - $cache = $this->storage->getCache($path, $storage); - - return new CacheWrapper($cache, $storage, $this->monitor); - } - - /** - * @param \OCP\Files\Storage $sourceStorage - * @param string $sourceInternalPath - * @param string $targetInternalPath - * - * @return bool - */ - public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) - { - if ($sourceStorage === $this) { - return $this->copy($sourceInternalPath, $targetInternalPath); - } - $this->analyze($this, [$targetInternalPath], Monitor::WRITE); - - return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); - } - - /** - * @param \OCP\Files\Storage $sourceStorage - * @param string $sourceInternalPath - * @param string $targetInternalPath - * - * @return bool - */ - public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) - { - if ($sourceStorage === $this) { - return $this->rename($sourceInternalPath, $targetInternalPath); - } - $this->analyze($this, [$targetInternalPath], Monitor::WRITE); - - return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); - } - - /** - * Makes it easier to test. - * - * @param IStorage $storage - * @param string $path - * @param int $mode - */ - protected function analyze(IStorage $storage, $path, $mode) - { - return $this->monitor->analyze($storage, $path, $mode); - } -} diff --git a/tests/Unit/StorageWrapperTest.php b/tests/Unit/StorageWrapperTest.php deleted file mode 100644 index b3b2f23..0000000 --- a/tests/Unit/StorageWrapperTest.php +++ /dev/null @@ -1,203 +0,0 @@ - - * @author Matthias Held - * @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 . - */ - -namespace OCA\RansomwareDetection\tests\Unit; - -use OCA\RansomwareDetection\Monitor; -use Test\TestCase; - -class StorageWrapperTest extends TestCase -{ - /** @var \OCP\Files\Storage\IStorage|\PHPUnit_Framework_MockObject_MockObject */ - protected $storage; - - /** @var \OCA\RansomwareDetection\Monitor|\PHPUnit_Framework_MockObject_MockObject */ - protected $monitor; - - protected function setUp(): void - { - parent::setUp(); - - $this->storage = $this->getMockBuilder('OCP\Files\Storage\IStorage') - ->setConstructorArgs([array()]) - ->getMock(); - - $this->monitor = $this->getMockBuilder('OCA\RansomwareDetection\Monitor') - ->disableOriginalConstructor() - ->getMock(); - } - - protected function getInstance(array $methods = []) - { - return $this->getMockBuilder('OCA\RansomwareDetection\StorageWrapper') - ->setConstructorArgs([ - [ - 'storage' => $this->storage, - 'mountPoint' => 'mountPoint', - 'monitor' => $this->monitor, - ], - ]) - ->setMethods($methods) - ->getMock(); - } - - public function dataAnalyze() - { - return [ - ['path1', Monitor::READ], - ['path2', Monitor::WRITE], - ['path3', Monitor::RENAME], - ['path4', Monitor::DELETE], - ]; - } - - /** - * @dataProvider dataAnalyze - * - * @param string $path - * @param int $mode - */ - public function testAnalyze($path, $mode) - { - $storage = $this->getInstance(); - - $this->monitor->expects($this->once()) - ->method('analyze') - ->with($storage, $path, $mode); - - $this->monitor->analyze($storage, $path, $mode); - } - - public function dataSinglePath() - { - $tests = []; - $tests[] = ['file_get_contents', 'path1', Monitor::READ, true]; - $tests[] = ['file_get_contents', 'path2', Monitor::READ, false]; - $tests[] = ['unlink', 'path1', Monitor::DELETE, true]; - $tests[] = ['unlink', 'path2', Monitor::DELETE, false]; - $tests[] = ['mkdir', 'path1', Monitor::CREATE, true]; - $tests[] = ['mkdir', 'path2', Monitor::CREATE, false]; - $tests[] = ['rmdir', 'path1', Monitor::DELETE, true]; - $tests[] = ['rmdir', 'path2', Monitor::DELETE, false]; - - return $tests; - } - - /** - * @dataProvider dataSinglePath - * - * @param string $method - * @param string $path - * @param int $mode - * @param bool $return - */ - public function testSinglePath($method, $path, $mode, $return) - { - $storage = $this->getInstance(['analyze']); - - $storage->expects($this->once()) - ->method('analyze') - ->with($storage, [$path], $mode); - - $this->storage->expects($this->once()) - ->method($method) - ->with($path) - ->willReturn($return); - - $this->assertSame($return, $this->invokePrivate($storage, $method, [$path, $mode])); - } - - public function dataDoublePath() - { - $tests = []; - $tests[] = ['rename', 'path1', 'path1', Monitor::RENAME, true]; - $tests[] = ['rename', 'path2', 'path2', Monitor::RENAME, false]; - $tests[] = ['copy', 'path1', 'path1', Monitor::WRITE, true]; - $tests[] = ['copy', 'path2', 'path2', Monitor::WRITE, false]; - - return $tests; - } - - /** - * @dataProvider dataDoublePath - * - * @param string $method - * @param string $path1 - * @param string $path2 - * @param int $mode - * @param bool $return - */ - public function testDoublePath($method, $path1, $path2, $mode, $return) - { - $storage = $this->getInstance(['analyze']); - - $storage->expects($this->once()) - ->method('analyze') - ->with($storage, [$path2, $path1], $mode); - - $this->storage->expects($this->once()) - ->method($method) - ->with($path1, $path2) - ->willReturn($return); - - $this->assertSame($return, $this->invokePrivate($storage, $method, [$path1, $path2, $mode])); - } - - public function dataTwoParameters() - { - $tests = []; - $tests[] = ['file_put_contents', 'path1', 'data', Monitor::WRITE, true]; - $tests[] = ['file_put_contents', 'path1', 'data', Monitor::WRITE, false]; - $tests[] = ['fopen', 'path1', 'z', Monitor::READ, true]; - $tests[] = ['fopen', 'path1', 'z', Monitor::READ, false]; - $tests[] = ['fopen', 'path1', 'x', Monitor::WRITE, true]; - $tests[] = ['fopen', 'path1', 'x', Monitor::WRITE, false]; - $tests[] = ['touch', 'path1', null, Monitor::WRITE, true]; - $tests[] = ['touch', 'path1', null, Monitor::WRITE, false]; - - return $tests; - } - - /** - * @dataProvider dataTwoParameters - * - * @param string $method - * @param string $path - * @param string $param2 - * @param int $mode - * @param bool $return - */ - public function testTwoParameters($method, $path, $param2, $mode, $return) - { - $storage = $this->getInstance(['analyze']); - - $storage->expects($this->once()) - ->method('analyze') - ->with($storage, [$path], $mode); - - $this->storage->expects($this->once()) - ->method($method) - ->with($path, $param2) - ->willReturn($return); - - $this->assertSame($return, $this->invokePrivate($storage, $method, [$path, $param2, $mode])); - } -} -- cgit v1.2.3 From 017faddef4f8d716bf4733e9463678ce027aa100 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 31 Mar 2020 19:15:50 +0200 Subject: bump version --- appinfo/info.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appinfo/info.xml b/appinfo/info.xml index db55c2a..448fc43 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -5,7 +5,7 @@ Ransomware recovery - 0.7.0 + 0.7.1 agpl Matthias Held RansomwareDetection -- cgit v1.2.3 From 38d4f122cf013a61f1c7c072ade58f17fef36797 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 31 Mar 2020 19:16:18 +0200 Subject: remove storage wrapper test --- tests/Unit/AppInfo/ApplicationTest.php | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/tests/Unit/AppInfo/ApplicationTest.php b/tests/Unit/AppInfo/ApplicationTest.php index f838b7f..5f646ca 100644 --- a/tests/Unit/AppInfo/ApplicationTest.php +++ b/tests/Unit/AppInfo/ApplicationTest.php @@ -65,15 +65,4 @@ class ApplicationTest extends TestCase { $this->assertTrue($this->container->query($service) instanceof $expected); } - - public function testAddStorageWrapperCallback() - { - $storage = $this->getMockBuilder('OCP\Files\Storage\IStorage') - ->setConstructorArgs([array()]) - ->getMock(); - - $result = $this->application->addStorageWrapperCallback('mountPoint', $storage); - // Request from CLI, so $results is instanceof IStorage and not StorageWrapper - $this->assertTrue($result instanceof IStorage); - } } -- cgit v1.2.3 From 19e40832019c7d072981e804450c2aa6e0c26090 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 31 Mar 2020 21:36:19 +0200 Subject: fix comments --- lib/Events/FilesEvents.php | 51 ++++++++++++++++----------------- lib/FilesHooks.php | 70 ++++++++++++++++++++++++---------------------- 2 files changed, 60 insertions(+), 61 deletions(-) diff --git a/lib/Events/FilesEvents.php b/lib/Events/FilesEvents.php index a996e61..abd514b 100644 --- a/lib/Events/FilesEvents.php +++ b/lib/Events/FilesEvents.php @@ -1,15 +1,8 @@ - * @copyright 2018 + * @copyright Copyright (c) 2020 Matthias Held + * @author Matthias Held * @license GNU AGPL version 3 or any later version * * This program is free software: you can redistribute it and/or modify @@ -23,36 +16,31 @@ declare(strict_types=1); * 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 . - * + * along with this program. If not, see . */ - namespace OCA\RansomwareDetection\Events; use OCA\RansomwareDetection\Monitor; use OCA\RansomwareDetection\AppInfo\Application; use OCP\ILogger; -/** - * Class FilesEvents - * - * @package OCA\Files_FullTextSearch\Events - */ class FilesEvents { /** @var string */ private $userId; + /** @var ILogger */ private $logger; + /** @var Monitor */ private $monitor; /** - * FilesEvents constructor. - * - * @param string $userId + * @param ILogger $logger + * @param Monitor $monitor + * @param string $userId */ public function __construct( ILogger $logger, @@ -67,9 +55,6 @@ class FilesEvents { /** * @param array $params - * - * @throws InvalidPathException - * @throws NotFoundException */ public function onFileUpdate(array $params) { $this->logger->error("Updating ".$params['path'], ['app' => Application::APP_ID]); @@ -79,35 +64,47 @@ class FilesEvents { /** * @param array $params - * - * @throws NotFoundException - * @throws InvalidPathException */ public function onFileRename(array $params) { $this->logger->error("Renaming ".$params['oldpath']." to ".$params['newpath'], ['app' => Application::APP_ID]); $this->analyze([$params['oldpath'], $params['newpath']], Monitor::RENAME); } + /** + * @param array $params + */ public function onFileCreate(array $params) { $this->logger->error("Creating ".$params['path'], ['app' => Application::APP_ID]); $this->analyze([$params['path']], Monitor::CREATE); } + /** + * @param array $params + */ public function onFileWrite(array $params) { - $this->logger->error("Writing ".$params['path'], ['app' => Application::APP_ID]); + $this->logger->error("Writing ".$params['path']." whole array ".implode($params), ['app' => Application::APP_ID]); $this->analyze([$params['path']], Monitor::WRITE); } + /** + * @param array $params + */ public function onFileDelete(array $params) { $this->logger->error("Deleting ".$params['path'], ['app' => Application::APP_ID]); $this->analyze([$params['path']], Monitor::DELETE); } + /** + * @param array $params + */ public function onFileCopy(array $params) { $this->logger->error("Copying ".$params['oldpath']." to ".$params['newpath'], ['app' => Application::APP_ID]); $this->analyze([$params['oldpath'], $params['newpath']], Monitor::RENAME); } + /** + * @param array $params + */ public function onFileTouch(array $params) { $this->logger->error("Touching ".$params['path'], ['app' => Application::APP_ID]); $this->analyze([$params['path']], Monitor::WRITE); diff --git a/lib/FilesHooks.php b/lib/FilesHooks.php index efd81d0..6a74261 100644 --- a/lib/FilesHooks.php +++ b/lib/FilesHooks.php @@ -1,15 +1,8 @@ - * @copyright 2018 + * @copyright Copyright (c) 2020 Matthias Held + * @author Matthias Held * @license GNU AGPL version 3 or any later version * * This program is free software: you can redistribute it and/or modify @@ -23,28 +16,20 @@ declare(strict_types=1); * 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 . - * + * along with this program. If not, see . */ - namespace OCA\RansomwareDetection; use OCA\RansomwareDetection\AppInfo\Application; use OCA\RansomwareDetection\Events\FilesEvents; -/** - * Class FilesHooks - * - * @package OCA\Files_FullTextSearch\Hooks - */ class FilesHooks { /** - * retrieve the FilesEvents' Controller + * Retrieve the FilesEvents' Controller. * * @return FilesEvents - * @throws QueryException */ protected static function getController(): FilesEvents { $app = new Application(); @@ -54,13 +39,9 @@ class FilesHooks { } /** - * hook events: file is updated + * Hook events: file is updated. * * @param array $params - * - * @throws QueryException - * @throws InvalidPathException - * @throws NotFoundException */ public static function onFileUpdate(array $params) { self::getController() @@ -69,39 +50,60 @@ class FilesHooks { /** - * hook events: file is renamed + * Hook events: file is renamed. * * @param array $params - * - * @throws NotFoundException - * @throws QueryException - * @throws InvalidPathException */ public static function onFileRename(array $params) { self::getController() ->onFileRename($params); } - + + /** + * Hook events: file is created. + * + * @param array $params + */ public static function onFileCreate(array $params) { self::getController() ->onFileCreate($params); } - + + /** + * Hook events: file is written. + * + * @param array $params + */ public static function onFileWrite(array $params) { self::getController() ->onFileWrite($params); } - + + /** + * Hook events: file is deleted. + * + * @param array $params + */ public static function onFileDelete(array $params) { self::getController() ->onFileDelete($params); } - + + /** + * Hook events: file is touched. + * + * @param array $params + */ public static function onFileTouch(array $params) { self::getController() ->onFileTouch($params); } - + + /** + * Hook events: file is copied. + * + * @param array $params + */ public static function onFileCopy(array $params) { self::getController() ->onFileCopy($params); -- cgit v1.2.3 From 019bd2f4397e99a4e70fa672a41b49ed803c858c Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 1 Apr 2020 19:15:09 +0200 Subject: change icon to plus --- js/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/utils.js b/js/utils.js index 5820eba..ac11271 100644 --- a/js/utils.js +++ b/js/utils.js @@ -120,7 +120,7 @@ td = $('').append($('

').attr({"title": "READ"}).tooltip({placement: 'top'}).prepend('')); } else if (fileData.command === 5) { // create - td = $('').append($('

').attr({"title": "CREATE"}).tooltip({placement: 'top'}).prepend('')); + td = $('').append($('

').attr({"title": "CREATE"}).tooltip({placement: 'top'}).prepend('')); } else { // error td = $('').append($('

').attr({"title": "ERROR"}).tooltip({placement: 'top'}).prepend('')); -- cgit v1.2.3 From 2290de6a9e7408ead2fa4ee9d09f1e2710cc243a Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 1 Apr 2020 19:15:27 +0200 Subject: remove hooks for update, touch and copy --- lib/AppInfo/Application.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index a07ee35..9172b33 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -186,12 +186,12 @@ class Application extends App $event->getServer()->addPlugin(new RequestPlugin($logger, $config, $userSession, $session, $service, $notifications, $classifier, $sequenceAnalyzer)); }); Util::connectHook('OC_Filesystem', 'post_create', FilesHooks::class, 'onFileCreate'); - Util::connectHook('OC_Filesystem', 'post_update', FilesHooks::class, 'onFileUpdate'); + // Util::connectHook('OC_Filesystem', 'post_update', FilesHooks::class, 'onFileUpdate'); Util::connectHook('OC_Filesystem', 'post_rename', FilesHooks::class, 'onFileRename'); Util::connectHook('OC_Filesystem', 'post_write', FilesHooks::class, 'onFileWrite'); Util::connectHook('OC_Filesystem', 'post_delete', FilesHooks::class, 'onFileDelete'); - Util::connectHook('OC_Filesystem', 'post_touch', FilesHooks::class, 'onFileTouch'); - Util::connectHook('OC_Filesystem', 'post_copy', FilesHooks::class, 'onFileCopy'); + // Util::connectHook('OC_Filesystem', 'post_touch', FilesHooks::class, 'onFileTouch'); + // Util::connectHook('OC_Filesystem', 'post_copy', FilesHooks::class, 'onFileCopy'); $this->registerNotificationNotifier(); } -- cgit v1.2.3 From 597ca3e65cc30faca13fead373b0a59e37fb4ccc Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 1 Apr 2020 19:15:40 +0200 Subject: refactor logging --- lib/Analyzer/EntropyAnalyzer.php | 4 +--- lib/Controller/ScanController.php | 1 - lib/Events/FilesEvents.php | 14 +++++++------- lib/Monitor.php | 13 ++++++------- 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/lib/Analyzer/EntropyAnalyzer.php b/lib/Analyzer/EntropyAnalyzer.php index b87d3e6..9e40b62 100644 --- a/lib/Analyzer/EntropyAnalyzer.php +++ b/lib/Analyzer/EntropyAnalyzer.php @@ -163,18 +163,16 @@ class EntropyAnalyzer { $handle = $node->fopen('r'); if (!$handle) { - $this->logger->debug('calculateEntropyOfFile: Getting data failed.', array('app' => Application::APP_ID)); + $this->logger->warning('calculateEntropyOfFile: Getting data failed.', array('app' => Application::APP_ID)); return 0.0; } $entropy = 0.0; $total = 0; - $this->logger->error("Calculate entropy", ['app' => Application::APP_ID]); while (!feof($handle)) { $data = fread($handle, 1024); - $this->logger->error("Data ".$data, ['app' => Application::APP_ID]); $total = $total + 1; if (strlen($data) === 1024) { $entropy = $entropy + $this->entropy->calculateEntropy($data); diff --git a/lib/Controller/ScanController.php b/lib/Controller/ScanController.php index bd59fcc..a7eca58 100644 --- a/lib/Controller/ScanController.php +++ b/lib/Controller/ScanController.php @@ -165,7 +165,6 @@ class ScanController extends OCSController return new JSONResponse(['status' => 'error', 'message' => 'File does not exist.', 'path' => $trashPath, 'name' => $name, 'mtime' => $timestamp], Http::STATUS_OK); } else { - // wubalubadubdub // Scan can only detect WRITE and DELETE this should never happen. $this->logger->error('postRecover: RENAME or CREATE operation.', array('app' => Application::APP_ID)); return new JSONResponse(['status' => 'error', 'message' => 'Wrong command.'], Http::STATUS_BAD_REQUEST); diff --git a/lib/Events/FilesEvents.php b/lib/Events/FilesEvents.php index abd514b..04ea567 100644 --- a/lib/Events/FilesEvents.php +++ b/lib/Events/FilesEvents.php @@ -57,7 +57,7 @@ class FilesEvents { * @param array $params */ public function onFileUpdate(array $params) { - $this->logger->error("Updating ".$params['path'], ['app' => Application::APP_ID]); + $this->logger->debug("Updating ".$params['path'].": Params: ".print_r($params, true), ['app' => Application::APP_ID]); $this->analyze([$params['path']], Monitor::WRITE); } @@ -66,7 +66,7 @@ class FilesEvents { * @param array $params */ public function onFileRename(array $params) { - $this->logger->error("Renaming ".$params['oldpath']." to ".$params['newpath'], ['app' => Application::APP_ID]); + $this->logger->debug("Renaming ".$params['oldpath']." to ".$params['newpath'].": Params: ".print_r($params, true), ['app' => Application::APP_ID]); $this->analyze([$params['oldpath'], $params['newpath']], Monitor::RENAME); } @@ -74,7 +74,7 @@ class FilesEvents { * @param array $params */ public function onFileCreate(array $params) { - $this->logger->error("Creating ".$params['path'], ['app' => Application::APP_ID]); + $this->logger->debug("Creating ".$params['path'].": Params: ".print_r($params, true), ['app' => Application::APP_ID]); $this->analyze([$params['path']], Monitor::CREATE); } @@ -82,7 +82,7 @@ class FilesEvents { * @param array $params */ public function onFileWrite(array $params) { - $this->logger->error("Writing ".$params['path']." whole array ".implode($params), ['app' => Application::APP_ID]); + $this->logger->debug("Writing ".$params['path'].": Params: ".print_r($params, true), ['app' => Application::APP_ID]); $this->analyze([$params['path']], Monitor::WRITE); } @@ -90,7 +90,7 @@ class FilesEvents { * @param array $params */ public function onFileDelete(array $params) { - $this->logger->error("Deleting ".$params['path'], ['app' => Application::APP_ID]); + $this->logger->debug("Deleting ".$params['path'].": Params: ".print_r($params, true), ['app' => Application::APP_ID]); $this->analyze([$params['path']], Monitor::DELETE); } @@ -98,7 +98,7 @@ class FilesEvents { * @param array $params */ public function onFileCopy(array $params) { - $this->logger->error("Copying ".$params['oldpath']." to ".$params['newpath'], ['app' => Application::APP_ID]); + $this->logger->debug("Copying ".$params['oldpath']." to ".$params['newpath'].": Params: ".print_r($params, true), ['app' => Application::APP_ID]); $this->analyze([$params['oldpath'], $params['newpath']], Monitor::RENAME); } @@ -106,7 +106,7 @@ class FilesEvents { * @param array $params */ public function onFileTouch(array $params) { - $this->logger->error("Touching ".$params['path'], ['app' => Application::APP_ID]); + $this->logger->debug("Touching ".$params['path'].": Params: ".print_r($params, true), ['app' => Application::APP_ID]); $this->analyze([$params['path']], Monitor::WRITE); } diff --git a/lib/Monitor.php b/lib/Monitor.php index dc5aa1e..b1dad47 100644 --- a/lib/Monitor.php +++ b/lib/Monitor.php @@ -157,7 +157,7 @@ class Monitor switch ($mode) { case self::RENAME: $path = $paths[1]; - $this->logger->error("Rename ".$paths[0]." to ".$paths[1], ['app' => Application::APP_ID]); + $this->logger->debug("Rename ".$paths[0]." to ".$paths[1], ['app' => Application::APP_ID]); if (preg_match('/.+\.d[0-9]+/', pathinfo($paths[1])['basename']) > 0) { return; } @@ -186,7 +186,7 @@ class Monitor return; case self::WRITE: - $this->logger->error("Write ".$path, ['app' => Application::APP_ID]); + $this->logger->debug("Write ".$path, ['app' => Application::APP_ID]); // reset PROPFIND_COUNT $this->resetProfindCount(); @@ -216,7 +216,7 @@ class Monitor return; case self::DELETE: - $this->logger->error("Delete", ['app' => Application::APP_ID]); + $this->logger->debug("Delete", ['app' => Application::APP_ID]); // reset PROPFIND_COUNT $this->resetProfindCount(); @@ -242,7 +242,7 @@ class Monitor return; case self::CREATE: - $this->logger->error("Create", ['app' => Application::APP_ID]); + $this->logger->debug("Create", ['app' => Application::APP_ID]); // reset PROPFIND_COUNT $this->resetProfindCount(); @@ -402,6 +402,7 @@ class Monitor */ private function addFolderOperation($paths, $node, $operation) { + $this->logger->debug("Add folder operation.", ['app' => Application::APP_ID]); $fileOperation = new FileOperation(); $fileOperation->setUserId($this->userId); $fileOperation->setPath(str_replace('files', '', pathinfo($node->getInternalPath())['dirname'])); @@ -438,7 +439,7 @@ class Monitor */ private function addFileOperation($paths, $node, $operation) { - $this->logger->error("Add file operation", ['app' => Application::APP_ID]); + $this->logger->debug("Add file operation.", ['app' => Application::APP_ID]); $fileOperation = new FileOperation(); $fileOperation->setUserId($this->userId); $fileOperation->setPath(str_replace('files', '', pathinfo($node->getInternalPath())['dirname'])); @@ -471,8 +472,6 @@ class Monitor $fileOperation->setStandardDeviation($entropyResult->getStandardDeviation()); $fileOperation->setFileClass($entropyResult->getFileClass()); - $this->logger->error("Entropy ".$entropyResult->getEntropy(), ['app' => Application::APP_ID]); - $entity = $this->mapper->insert($fileOperation); } } -- cgit v1.2.3