Skip to content

Commit

Permalink
generate unpredictable unique session ids
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
  • Loading branch information
d00p committed Oct 2, 2021
1 parent e735235 commit 7feddf0
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 8 deletions.
2 changes: 1 addition & 1 deletion admin_admins.php
Expand Up @@ -129,7 +129,7 @@
'userid' => $userinfo['userid']
));

$s = md5(uniqid(microtime(), 1));
$s = \Froxlor\Froxlor::genSessionId();
$ins_stmt = Database::prepare("
INSERT INTO `" . TABLE_PANEL_SESSIONS . "` SET
`hash` = :hash, `userid` = :userid, `ipaddress` = :ip,
Expand Down
2 changes: 1 addition & 1 deletion admin_customers.php
Expand Up @@ -178,7 +178,7 @@
'hash' => $s
));

$s = md5(uniqid(microtime(), 1));
$s = \Froxlor\Froxlor::genSessionId();
$insert = Database::prepare("
INSERT INTO `" . TABLE_PANEL_SESSIONS . "` SET
`hash` = :hash,
Expand Down
2 changes: 1 addition & 1 deletion index.php
Expand Up @@ -675,7 +675,7 @@ function finishLogin($userinfo)
global $version, $dbversion, $remote_addr, $http_user_agent, $languages;

if (isset($userinfo['userid']) && $userinfo['userid'] != '') {
$s = md5(uniqid(microtime(), 1));
$s = \Froxlor\Froxlor::genSessionId();

if (isset($_POST['language'])) {
$language = \Froxlor\Validate\Validate::validate($_POST['language'], 'language');
Expand Down
28 changes: 26 additions & 2 deletions install/lib/class.FroxlorInstall.php
Expand Up @@ -363,6 +363,30 @@ private function _createUserdataConf()
return $content;
}

/**
* generate safe unique token
*
* @param int $length
* @return string
*/
private function genUniqueToken(int $length = 16)
{
if(!isset($length) || intval($length) <= 8 ){
$length = 16;
}
if (function_exists('random_bytes')) {
return bin2hex(random_bytes($length));
}
if (function_exists('mcrypt_create_iv')) {
return bin2hex(mcrypt_create_iv($length, MCRYPT_DEV_URANDOM));
}
if (function_exists('openssl_random_pseudo_bytes')) {
return bin2hex(openssl_random_pseudo_bytes($length));
}
// if everything else fails, use unsafe fallback
return md5(uniqid(microtime(), 1));
}

/**
* create corresponding entries in froxlor database
*
Expand Down Expand Up @@ -406,8 +430,8 @@ private function _doDataEntries(&$db)
$content .= $this->_status_message('begin', $this->_lng['install']['adding_admin_user']);
$ins_data = array(
'loginname' => $this->_data['admin_user'],
/* use SHA256 default crypt */
'password' => crypt($this->_data['admin_pass1'], '$5$' . md5(uniqid(microtime(), 1)) . md5(uniqid(microtime(), 1))),
/* use SHA256 default crypt */
'password' => crypt($this->_data['admin_pass1'], '$5$' . $this->genUniqueToken() . $this->genUniqueToken()),
'email' => 'admin@' . $this->_data['servername'],
'deflang' => $this->_languages[$this->_activelng]
);
Expand Down
4 changes: 2 additions & 2 deletions lib/Froxlor/Database/DbManager.php
Expand Up @@ -82,10 +82,10 @@ public function createDatabase($loginname = null, $password = null, $last_accnum
// get all usernames from db-manager
$allsqlusers = $this->getManager()->getAllSqlUsers();
// generate random username
$username = $loginname . '-' . substr(md5(uniqid(microtime(), 1)), 20, 3);
$username = $loginname . '-' . substr(\Froxlor\Froxlor::genSessionId(), 20, 3);
// check whether it exists on the DBMS
while (in_array($username, $allsqlusers)) {
$username = $loginname . '-' . substr(md5(uniqid(microtime(), 1)), 20, 3);
$username = $loginname . '-' . substr(\Froxlor\Froxlor::genSessionId(), 20, 3);
}
} elseif (strtoupper(Settings::Get('customer.mysqlprefix')) == 'DBNAME') {
$username = $loginname;
Expand Down
2 changes: 1 addition & 1 deletion lib/Froxlor/Domain/Domain.php
Expand Up @@ -340,7 +340,7 @@ public static function doLetsEncryptCleanUp($domainname = null)
// run remove command
\Froxlor\FileDir::safe_exec($acmesh . $params);
// remove certificates directory
@unlink($certificate_folder);
\Froxlor\FileDir::safe_exec('rm -rf ' . $certificate_folder);
}
}
return true;
Expand Down
24 changes: 24 additions & 0 deletions lib/Froxlor/Froxlor.php
Expand Up @@ -202,6 +202,30 @@ public static function isFroxlorVersion($to_check = null)
return false;
}

/**
* generate safe unique session id
*
* @param int $length
* @return string
*/
public static function genSessionId(int $length = 16)
{
if(!isset($length) || intval($length) <= 8 ){
$length = 16;
}
if (function_exists('random_bytes')) {
return bin2hex(random_bytes($length));
}
if (function_exists('mcrypt_create_iv')) {
return bin2hex(mcrypt_create_iv($length, MCRYPT_DEV_URANDOM));
}
if (function_exists('openssl_random_pseudo_bytes')) {
return bin2hex(openssl_random_pseudo_bytes($length));
}
// if everything else fails, use unsafe fallback
return md5(uniqid(microtime(), 1));
}

/**
* compare of froxlor versions
*
Expand Down

0 comments on commit 7feddf0

Please sign in to comment.