|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Startwind\Inventorio\Collector\Website\WordPress; |
|
4
|
|
|
|
|
5
|
|
|
use Startwind\Inventorio\Collector\BasicCollector; |
|
6
|
|
|
use Startwind\Inventorio\Collector\InventoryAwareCollector; |
|
7
|
|
|
use Startwind\Inventorio\Exec\File; |
|
8
|
|
|
use Startwind\Inventorio\Util\PasswordUtil; |
|
9
|
|
|
|
|
10
|
|
|
class DatabaseCredentialCollector extends BasicCollector implements InventoryAwareCollector |
|
11
|
|
|
{ |
|
12
|
|
|
private array $inventory; |
|
13
|
|
|
|
|
14
|
|
|
public function setInventory(array $inventory): void |
|
15
|
|
|
{ |
|
16
|
|
|
$this->inventory = $inventory; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function collect(): array |
|
20
|
|
|
{ |
|
21
|
|
|
if (!array_key_exists(WordPressCollector::COLLECTOR_IDENTIFIER, $this->inventory) |
|
22
|
|
|
|| !is_array($this->inventory[WordPressCollector::COLLECTOR_IDENTIFIER]) |
|
23
|
|
|
) return []; |
|
24
|
|
|
|
|
25
|
|
|
$credentials = []; |
|
26
|
|
|
|
|
27
|
|
|
$wordpressSites = $this->inventory[WordPressCollector::COLLECTOR_IDENTIFIER]; |
|
28
|
|
|
|
|
29
|
|
|
foreach ($wordpressSites as $site) { |
|
30
|
|
|
$configFile = File::getInstance()->getContents($site['path']); |
|
31
|
|
|
$credentialArray = $this->extractCredentials($configFile); |
|
32
|
|
|
|
|
33
|
|
|
$credentials[] = [ |
|
34
|
|
|
'passwordStrength' => PasswordUtil::evaluateStrength($credentialArray['password']), |
|
35
|
|
|
'user' => $credentialArray['user'], |
|
36
|
|
|
]; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return $credentials; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
private function extractCredentials(string $wp_config_path): ?array |
|
43
|
|
|
{ |
|
44
|
|
|
if (!file_exists($wp_config_path)) { |
|
45
|
|
|
return null; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$content = file_get_contents($wp_config_path); |
|
49
|
|
|
$user = $pass = null; |
|
50
|
|
|
|
|
51
|
|
|
if (preg_match("/define\s*\(\s*['\"]DB_USER['\"]\s*,\s*['\"](.*?)['\"]\s*\)/", $content, $matches)) { |
|
52
|
|
|
$user = $matches[1]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if (preg_match("/define\s*\(\s*['\"]DB_PASSWORD['\"]\s*,\s*['\"](.*?)['\"]\s*\)/", $content, $matches)) { |
|
56
|
|
|
$pass = $matches[1]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if ($user !== null && $pass !== null) { |
|
60
|
|
|
return [ |
|
61
|
|
|
'user' => $user, |
|
62
|
|
|
'password' => $pass |
|
63
|
|
|
]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return null; |
|
67
|
|
|
} |
|
68
|
|
|
} |