Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Passwordless Account check (#84) #86

Merged
merged 4 commits into from
Jan 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -11,11 +11,12 @@
],
"minimum-stability": "stable",
"require": {
"php": "^7.4 || ^8.0",
"php": "^8.0",
"doctrine/dbal": "^2.10",
"symfony/console": "^5.0",
"monolog/monolog": "^2.1",
"greenlion/php-sql-parser": "^4.5"
"greenlion/php-sql-parser": "^4.5",
"ext-json": "*"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.5",
5 changes: 3 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions resources/mysql/sample.sql
Original file line number Diff line number Diff line change
@@ -80,3 +80,6 @@ WITH RECURSIVE cte AS
)
SELECT i, value
FROM cte;

# Create a passwordless account
CREATE USER IF NOT EXISTS 'localhost_passwordless_user'@'localhost';
2 changes: 2 additions & 0 deletions src/Cli/Command/RunCommand.php
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
use Cadfael\Cli\Formatter\Json;
use Cadfael\Engine\Check\Account\NotConnecting;
use Cadfael\Engine\Check\Account\NotProperlyClosingConnections;
use Cadfael\Engine\Check\Account\PasswordlessAccount;
use Cadfael\Engine\Check\Column\CorrectUtf8Encoding;
use Cadfael\Engine\Check\Column\LowCardinalityExpensiveStorage;
use Cadfael\Engine\Check\Column\ReservedKeywords;
@@ -166,6 +167,7 @@ protected function runChecksAgainstSchema(
new IndexPrefix(),
new UUIDStorage(),
new LowCardinalityExpensiveStorage(),
new PasswordlessAccount(),
);

if ($load_performance_schema) {
89 changes: 89 additions & 0 deletions src/Engine/Check/Account/PasswordlessAccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types = 1);

namespace Cadfael\Engine\Check\Account;

use Cadfael\Engine\Check;
use Cadfael\Engine\Entity\Account;
use Cadfael\Engine\Report;

class PasswordlessAccount implements Check
{
public function supports($entity): bool
{
return $entity instanceof Account;
}

public function run($entity): ?Report
{
$pass = !is_null($entity->getUser()->authentication_string)
&& $entity->getUser()->authentication_string;

$version = $entity->getDatabase()->getVersion();

// If we're running MySQL 5.6.*, we only flag an issue if the plugin field is mysql_native_password
$pass |= (
version_compare($version, '5.7', '<')
&& version_compare($version, '5.6', '>=')
&& $entity->getUser()->plugin !== 'mysql_native_password'
);

// If we're running MySQL 5.5.*, we only flag an issue if the plugin field is NULL or empty
$pass |= (
version_compare($version, '5.6', '<')
&& version_compare($version, '5.5', '>=')
&& $entity->getUser()->plugin
);

if ($pass) {
return new Report(
$this,
$entity,
Report::STATUS_OK
);
}

$status = Report::STATUS_WARNING;
$messages = [
"Passwordless account detected.",
];

// Can this user be accessed from outside the server localhost?
if ($entity->getUser()->host !== 'localhost' && $entity->getUser()->host !== '127.0.0.1') {
$status = Report::STATUS_CRITICAL;
$messages[] = "Account has access from outside the server.";
}

return new Report(
$this,
$entity,
$status,
$messages
);
}

/**
* @codeCoverageIgnore
*/
public function getReferenceUri(): string
{
return 'https://github.com/xsist10/cadfael/wiki/Passwordless-Account';
}

/**
* @codeCoverageIgnore
*/
public function getName(): string
{
return 'Passwordless Account';
}

/**
* @codeCoverageIgnore
*/
public function getDescription(): string
{
return "Accounts that don't have a password set could be abused.";
}
}
37 changes: 28 additions & 9 deletions src/Engine/Entity/Account.php
Original file line number Diff line number Diff line change
@@ -6,23 +6,32 @@

use Cadfael\Engine\Entity;
use Cadfael\Engine\Entity\Account\NotClosedProperly;
use Cadfael\Engine\Entity\Account\User;

class Account implements Entity
{
protected string $username;
protected string $host;
protected int $current_connections = 0;
protected int $total_connections = 0;

protected User $user;

protected Database $database;

public ?NotClosedProperly $account_not_closed_properly = null;

public function __construct(string $username, string $host)
protected function __construct()
{
}

public static function withUser(User $user): Account
{
$account = new Account();
$account->setUser($user);
return $account;
}

public static function withRaw(string $username, string $host): Account
{
$this->username = $username;
$this->host = $host;
return self::withUser(new User($username, $host));
}

/**
@@ -99,7 +108,7 @@ public function getDatabase(): Database
*/
public function getHost(): string
{
return $this->host;
return $this->user->host;
}

/**
@@ -110,7 +119,7 @@ public function getHost(): string
*/
public function getName(): string
{
return $this->username;
return $this->user->user;
}

/**
@@ -127,11 +136,21 @@ public function setAccountNotClosedProperly(NotClosedProperly $account_not_close
*/
public function __toString(): string
{
return $this->username . '@' . $this->host;
return $this->user->user . '@' . $this->user->host;
}

public function isVirtual(): bool
{
return false;
}

public function getUser(): User
{
return $this->user;
}

public function setUser(User $user): void
{
$this->user = $user;
}
}
131 changes: 131 additions & 0 deletions src/Engine/Entity/Account/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

declare(strict_types=1);

namespace Cadfael\Engine\Entity\Account;

/**
* Class User (readonly)
*
* Stores a representation of mysql.user
* @package Cadfael\Engine\Entity\Account
* @codeCoverageIgnore
*/
class User
{
public function __construct(
public string $user,
public string $host,
public bool $select_priv = false,
public bool $insert_priv = false,
public bool $update_priv = false,
public bool $delete_priv = false,
public bool $create_priv = false,
public bool $drop_priv = false,
public bool $reload_priv = false,
public bool $shutdown_priv = false,
public bool $process_priv = false,
public bool $file_priv = false,
public bool $grant_priv = false,
public bool $references_priv = false,
public bool $index_priv = false,
public bool $alter_priv = false,
public bool $show_db_priv = false,
public bool $super_priv = false,
public bool $create_tmp_table_priv = false,
public bool $lock_tables_priv = false,
public bool $execute_priv = false,
public bool $repl_slave_priv = false,
public bool $repl_client_priv = false,
public bool $create_view_priv = false,
public bool $show_view_priv = false,
public bool $create_routine_priv = false,
public bool $alter_routine_priv = false,
public bool $create_user_priv = false,
public bool $event_priv = false,
public bool $trigger_priv = false,
public bool $create_tablespace_priv = false,
public string $ssl_type = '',
public string|null $ssl_cipher = null,
public string|null $x509_issuer = null,
public string|null $x509_subject = null,
public int $max_questions = 0,
public int $max_updates = 0,
public int $max_connections = 0,
public int $max_user_connections = 0,
public string|null $plugin = 'caching_sha2_password',
public string|null $authentication_string = null,
public bool|null $password_expired = false,
public int|null $password_last_changed = null,
public int|null $password_lifetime = null,
public bool|null $account_locked = false,
public bool|null $create_role_priv = false,
public bool|null $drop_role_priv = false,
public int|null $password_reuse_history = null,
public int|null $password_reuse_time = null,
public bool|null $password_require_current = null,
public array|null $user_attributes = null
) {
}

/**
* @param array<string> $payload This is a query from mysql.user
* @return User
*/
public static function createFromUser(array $payload): User
{
return new User(
$payload['User'],
$payload['Host'],
$payload['Select_priv'] === 'Y',
$payload['Insert_priv'] === 'Y',
$payload['Update_priv'] === 'Y',
$payload['Delete_priv'] === 'Y',
$payload['Create_priv'] === 'Y',
$payload['Drop_priv'] === 'Y',
$payload['Reload_priv'] === 'Y',
$payload['Shutdown_priv'] === 'Y',
$payload['Process_priv'] === 'Y',
$payload['File_priv'] === 'Y',
$payload['Grant_priv'] === 'Y',
$payload['References_priv'] === 'Y',
$payload['Index_priv'] === 'Y',
$payload['Alter_priv'] === 'Y',
$payload['Show_db_priv'] === 'Y',
$payload['Super_priv'] === 'Y',
$payload['Create_tmp_table_priv'] === 'Y',
$payload['Lock_tables_priv'] === 'Y',
$payload['Execute_priv'] === 'Y',
$payload['Repl_slave_priv'] === 'Y',
$payload['Repl_client_priv'] === 'Y',
$payload['Create_view_priv'] === 'Y',
$payload['Show_view_priv'] === 'Y',
$payload['Create_routine_priv'] === 'Y',
$payload['Alter_routine_priv'] === 'Y',
$payload['Create_user_priv'] === 'Y',
$payload['Event_priv'] === 'Y',
$payload['Trigger_priv'] === 'Y',
$payload['Create_tablespace_priv'] === 'Y',
$payload['ssl_type'],
$payload['ssl_cipher'],
$payload['x509_issuer'],
$payload['x509_subject'],
(int)$payload['max_questions'],
(int)$payload['max_updates'],
(int)$payload['max_connections'],
(int)$payload['max_user_connections'],
$payload['plugin'],
$payload['authentication_string'],
$payload['password_expired'] === 'Y',
(int)$payload['password_last_changed'],
(int)$payload['password_lifetime'],
$payload['account_locked'] === 'Y',
$payload['Create_role_priv'] === 'Y',
$payload['Drop_role_priv'] === 'Y',
(int)$payload['Password_reuse_history'],
(int)$payload['Password_reuse_time'],
$payload['Password_require_current'] === 'Y',
json_decode($payload['User_attributes'] ?? '{}', true)
);
}
}
7 changes: 4 additions & 3 deletions src/Engine/Factory.php
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@

use Cadfael\Engine\Entity\Account;
use Cadfael\Engine\Entity\Account\NotClosedProperly;
use Cadfael\Engine\Entity\Account\User;
use Cadfael\Engine\Entity\Database;
use Cadfael\Engine\Entity\Index\Statistics;
use Cadfael\Engine\Entity\Query;
@@ -355,7 +356,7 @@ public function getAccounts(Connection $connection): array
$this->log()->info("Collecting MySQL user accounts.");
$query = 'SELECT * FROM mysql.user';
foreach ($connection->fetchAllAssociative($query) as $row) {
$accounts[] = new Account($row['User'], $row['Host']);
$accounts[] = Account::withUser(User::createFromUser($row));
}
}
return $accounts;
@@ -664,7 +665,7 @@ public function buildDatabase(Connection $connection, array $schema_names, bool
$accountNotClosedProperly['host']
);
if (!$account) {
$account = new Account(
$account = Account::withRaw(
$accountNotClosedProperly['user'],
$accountNotClosedProperly['host']
);
@@ -682,7 +683,7 @@ public function buildDatabase(Connection $connection, array $schema_names, bool
foreach ($accountConnections as $accountConnection) {
$account = $database->getAccount($accountConnection['USER'], $accountConnection['HOST']);
if (!$account) {
$account = new Account($accountConnection['USER'], $accountConnection['HOST']);
$account = Account::withRaw($accountConnection['USER'], $accountConnection['HOST']);
$database->addAccount($account);
}
$account->setCurrentConnections((int)$accountConnection['CURRENT_CONNECTIONS']);
Loading