CVE-2021-3817 (PHP, published 2021-12-09T11:15Z)
Description: value: wbce_cms is vulnerable to Improper Neutralization of Special Elements used in an SQL Command
Severity: HIGH (CVSS v3 base score: 9.8)
Weakness: CWE-89 \u2014 Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')
The product constructs all or part of an SQL command using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the intended SQL command when it is sent to a downstream component. Without sufficient removal or quoting of SQL syntax in user-controllable inputs, the generated SQL query can cause those inputs to be interpreted as SQL instead of ordinary user data.
Repository: https://github.com/wbce/wbce_cms
Files changed:
wbce/admin/login/forgot/index.php
wbce/modules/tool_account_settings/account/form_forgot.php
wbce/modules/tool_account_settings/templates/form_forgot.twig
wbce/templates/argos_theme_reloaded/css/login.css
wbce/templates/argos_theme_reloaded/info.php
wbce/templates/argos_theme_reloaded/templates/login_forgot.htt
wbce/templates/wbce_flat_theme/css/login.css
wbce/templates/wbce_flat_theme/info.php
wbce/templates/wbce_flat_theme/templates/login_forgot.htt
Commit message: add captcha for login forgot


Vulnerable code:
if (isset($_POST['email']) and $_POST['email'] != "") {
    if ($admin->validate_email($email) == false) {
        $oMsgBox->error($MESSAGE['USERS_INVALID_EMAIL']);

    // Check if the email exists in the database
    $sSql = "SELECT * FROM `{TP}users` WHERE `email` = '" . $email . "'";
    $rRow = $database->query($sSql);
    if ($rRow->numRows() > 0) {

        // Get the id, username, email, and last_reset from the above db query
        $aUser = $rRow->fetchRow();
        if (strlen($aUser['signup_confirmcode']) > 25) {
            header("Location: " . WB_URL . "/account/signup_continue_page.php?switch=wrong_inputs");
            exit(0); // break up the script here
        }


        // Check if the password has been reset in the last 2 hours
        if ((time() - intval($aUser['last_reset'])) < (2 * 3600)) {
            // Tell the user that their password cannot be reset more than once per hour
            $oMsgBox->error($MESSAGE['FORGOT_PASS_ALREADY_RESET']);
        } else {
            $sCurrentPw = $aUser['password'];

            // Generate a random password then update the database with it
            $sNewPw = '';
            $salt = "abchefghjkmnpqrstuvwxyz0123456789";
            srand((double)microtime() * 1000000);
            $i = 0;
            while ($i <= 7) {
                $num = rand() % 33;
                $tmp = substr($salt, $num, 1);
                $sNewPw = $sNewPw . $tmp;
                $i++;
            // update the new password in the database
            $aUpdateUser = array(
                'user_id' => $aUser['user_id'],
                'password' => $wb->doPasswordEncode($sNewPw),
                'last_reset' => time(),
            );
            $database->updateRow('{TP}users', 'user_id', $aUpdateUser);

            if ($database->is_error()) {
                // Error updating database
                $oMsgBox->error($database->get_error());
            } else {
                // Setup email to send
                $mail_to = $email;
                $mail_subject = $MESSAGE['SIGNUP2_SUBJECT_LOGIN_INFO'];

                // Replace placeholders from language variable with values
                $search = array('{LOGIN_DISPLAY_NAME}', '{LOGIN_WEBSITE_TITLE}', '{LOGIN_NAME}', '{LOGIN_PASSWORD}');
                $replace = array($aUser['display_name'], WEBSITE_TITLE, $aUser['username'], $sNewPw);

                $aTokenReplace = array(
                    '{LOGIN_DISPLAY_NAME}' => $aUser['display_name'],
                    '{LOGIN_NAME}' => $aUser['username'],
                    '{LOGIN_WEBSITE_TITLE}' => WEBSITE_TITLE,
                    '{LOGIN_PASSWORD}' => $sNewPw
                );
                $mail_message = strtr($MESSAGE['SIGNUP2_BODY_LOGIN_FORGOT'], $aTokenReplace);
                // Try sending the email
                if ($admin->mail(SERVER_EMAIL, $mail_to, $mail_subject, $mail_message)) {
                    $oMsgBox->error($MESSAGE['FORGOT_PASS_PASSWORD_RESET']);
                    $display_form = false;
                    $aUpdateUser = array(
                        'user_id' => $aUser['user_id'],
                        'password' => $sCurrentPw
                    $database->updateRow('{TP}users', 'user_id', $aUpdateUser);
                    $oMsgBox->error($MESSAGE['FORGOT_PASS_CANNOT_EMAIL']);
    } else {
        // Email doesn't exist, so tell the user
        $oMsgBox->error($MESSAGE['FORGOT_PASS_EMAIL_NOT_FOUND']);
        // and delete the wrong Email
        $email = '';
    if($admin->validate_email($sEmail) == false) {
        $oMsgBox->error($MESSAGE['USERS_INVALID_EMAIL']);
        $sEmail  = '';
        // Check if the email exists in the database
        $sSql  = "SELECT * FROM `{TP}users` WHERE `email`='".$sEmail."'";
        if(($rRow = $database->query($sSql))){
            if($aUser = $rRow->fetchRow(MYSQLI_ASSOC)) {
                if(strlen($aUser['signup_confirmcode']) > 25){
                    header("Location: ".ACCOUNT_URL."/signup_continue_page.php?switch=wrong_inputs");
                    exit(0); // break up the script here
                }
                $iUserID = (int) $aUser['user_id'];
                // Get the id, username, email, and last_reset from the above db query
                // Check if the password has been reset in the last 2 hours
                if( ( time() - intval($aUser['last_reset']) ) < (2 * 3600) ) {
                    // Tell the user that their password cannot be reset more than once per hour
                    $oMsgBox->error($MESSAGE['FORGOT_PASS_ALREADY_RESET']);
                } else {
                    // current password
                    $sCurrentPw = $aUser['password'];
                    // generate new password
                    $sNewPasswordRaw = $oAccounts->GenerateRandomPassword();
                    $sNewPasswordEnc = $oAccounts->doPasswordEncode($sNewPasswordRaw);
                    // prepare E-Mail with login details to send to the user via email
                    $aTokenReplace = array(
                        'LOGIN_DISPLAY_NAME'  => $aUser['display_name'],
                        'LOGIN_NAME'          => $aUser['username'],
                        'LOGIN_WEBSITE_TITLE' => WEBSITE_TITLE,
                        'LOGIN_PASSWORD'      => $sNewPasswordRaw,
                        'LOGIN_URL'           => ACCOUNT_URL . '/login.php'
                    );
                    $sOnScreenSwitch    = 'forgot_login_details_sent';
                    $sEmailTemplateName = 'password_recovery_mail';
                    $sEmailSubject      = '';
                    $sMailTo            = $sEmail;
                    $checkSend = $oAccounts->sendEmail($sMailTo, $aTokenReplace, $sEmailTemplateName, $sEmailSubject);
                    if ($checkSend === true) {
                        // update the new password in the database
                        $aUpdateUser = array(
                            'user_id'    => $iUserID,
                            'password'   => $sNewPasswordEnc,
                            'last_reset' => time(),
                        );
                        if($database->updateRow('{TP}users', 'user_id', $aUpdateUser)){
                            header("Location: ".ACCOUNT_URL."/signup_continue_page.php?lc=".$sLC."&switch=".$sOnScreenSwitch."&email=".$sMailTo);
                            exit(0);
                        } else {
                            // Error updating database
                            $oMsgBox->error($MESSAGE['RECORD_MODIFIED_FAILED']);
                            if(WB_DEBUG) {
                                $oMsgBox->error($database->get_error().'<br />'.$sSql);
                        }
                    } else {
                        // tell user: WRONG INPUTS
                        $aUpdateUser = array(
                            'user_id'    => $aUser['user_id'],
                            'password'   => $sCurrentPw
                        );
                        $database->updateRow('{TP}users', 'user_id', $aUpdateUser);
                        header("Location: ".ACCOUNT_URL."/signup_continue_page.php?lc=".$sLC."&switch=wrong_inputs&from=resend_forgot_pass&mail_err=".$checkSend);
                        exit(0);
            } else { // no record found - Email doesn't exist, so tell the user
                $oMsgBox->error($MESSAGE['FORGOT_PASS_EMAIL_NOT_FOUND']);
            }
        } else {
            // Query failed
            if(WB_DEBUG) {
                $oMsgBox->error('SystemError:: Database query failed!');
                $oMsgBox->error($database->get_error().'<br />'.$sSql);
<h2>{{ L_('MENU:FORGOT') }}</h2>
<br />

$template_version = '2.1.3';
$template_platform = '1.4.0';
<body onload="document.forgot_pass.email.focus();">
							<p>{MESSAGE}:</p>
    font-size: 13px;
$template_version = '1.7.5';
$template_platform = '1.4';

Fixed code:
require_once(WB_PATH.'/include/captcha/captcha.php');
if (isset($_POST['email']) && $_POST['email'] != "" ) {
    if(isset($_POST['captcha']) AND $_POST['captcha'] != ''){
        $ccheck = time(); $ccheck1 = time();
        if(isset($_SESSION['captchaloginforgot'])) $ccheck1 = $_SESSION['captchaloginforgot'];
        if(isset($_SESSION['captcha'])) $ccheck = $_SESSION['captcha'];
        if($_POST['captcha'] != $ccheck && $_POST['captcha'] != $ccheck1) {
            $oMsgBox->error($MESSAGE['MOD_FORM_INCORRECT_CAPTCHA']);
            $email = '';
        }
    } else {
        $oMsgBox->error($MESSAGE['MOD_FORM_INCORRECT_CAPTCHA']);
    
   
    
    if ($email != '') {
         
            if ($admin->validate_email($email) == false) {
                $oMsgBox->error($MESSAGE['USERS_INVALID_EMAIL']);
            }
        
        // Check if the email exists in the database
        $sSql = "SELECT * FROM `{TP}users` WHERE `email`='".$database->escapeString($email)."'";
        $rRow = $database->query($sSql);
        if ($rRow->numRows() > 0) {

            // Get the id, username, email, and last_reset from the above db query
            $aUser = $rRow->fetchRow();
            if (strlen($aUser['signup_confirmcode']) > 25) {
                header("Location: " . WB_URL . "/account/signup_continue_page.php?switch=wrong_inputs");
                exit(0); // break up the script here
            // Check if the password has been reset in the last 2 hours
            if ((time() - intval($aUser['last_reset'])) < (2 * 3600)) {
                // Tell the user that their password cannot be reset more than once per hour
                $oMsgBox->error($MESSAGE['FORGOT_PASS_ALREADY_RESET']);
            } else {
                $sCurrentPw = $aUser['password'];

                // Generate a random password then update the database with it
                $sNewPw = '';
                $salt = "abchefghjkmnpqrstuvwxyz0123456789";
                srand((double)microtime() * 1000000);
                $i = 0;
                while ($i <= 7) {
                    $num = rand() % 33;
                    $tmp = substr($salt, $num, 1);
                    $sNewPw = $sNewPw . $tmp;
                    $i++;
                }
                // update the new password in the database
                $aUpdateUser = array(
                    'user_id' => $aUser['user_id'],
                    'password' => $wb->doPasswordEncode($sNewPw),
                    'last_reset' => time(),
                );
                $database->updateRow('{TP}users', 'user_id', $aUpdateUser);
                if ($database->is_error()) {
                    // Error updating database
                    $oMsgBox->error($database->get_error());
                    // Setup email to send
                    $mail_to = $email;
                    $mail_subject = $MESSAGE['SIGNUP2_SUBJECT_LOGIN_INFO'];

                    // Replace placeholders from language variable with values
                    $search = array('{LOGIN_DISPLAY_NAME}', '{LOGIN_WEBSITE_TITLE}', '{LOGIN_NAME}', '{LOGIN_PASSWORD}');
                    $replace = array($aUser['display_name'], WEBSITE_TITLE, $aUser['username'], $sNewPw);

                    $aTokenReplace = array(
                        '{LOGIN_DISPLAY_NAME}' => $aUser['display_name'],
                        '{LOGIN_NAME}' => $aUser['username'],
                        '{LOGIN_WEBSITE_TITLE}' => WEBSITE_TITLE,
                        '{LOGIN_PASSWORD}' => $sNewPw


                    $mail_message = strtr($MESSAGE['SIGNUP2_BODY_LOGIN_FORGOT'], $aTokenReplace);

                    // Try sending the email
                    if ($admin->mail(SERVER_EMAIL, $mail_to, $mail_subject, $mail_message)) {
                        $oMsgBox->error($MESSAGE['FORGOT_PASS_PASSWORD_RESET']);
                        $display_form = false;
                    } else {
                        $aUpdateUser = array(
                            'user_id' => $aUser['user_id'],
                            'password' => $sCurrentPw
                        );
                        $database->updateRow('{TP}users', 'user_id', $aUpdateUser);
                        $oMsgBox->error($MESSAGE['FORGOT_PASS_CANNOT_EMAIL']);
                    }
        } else {
            // Email doesn't exist, so tell the user
            $oMsgBox->error($MESSAGE['FORGOT_PASS_EMAIL_NOT_FOUND']);
            // and delete the wrong Email
            $email = '';
ob_start();
call_captcha("all","",'loginforgot');
$captcha = ob_get_contents();
ob_end_clean();



    'CAPTCHA' => $captcha
require_once(WB_PATH.'/include/captcha/captcha.php');
    
    if(isset($_POST['captcha']) AND $_POST['captcha'] != ''){
        $ccheck = time(); $ccheck1 = time();
        if(isset($_SESSION['captchaaccountforgot'])) $ccheck1 = $_SESSION['captchaaccountforgot'];
        if(isset($_SESSION['captcha'])) $ccheck = $_SESSION['captcha'];
        if($_POST['captcha'] != $ccheck && $_POST['captcha'] != $ccheck1) {
            $oMsgBox->error($MESSAGE['MOD_FORM_INCORRECT_CAPTCHA']);
            $sEmail = '';
        }
        $oMsgBox->error($MESSAGE['MOD_FORM_INCORRECT_CAPTCHA']);
        $sEmail = '';
    }
    
    if ($sEmail != '') {
    
        if($admin->validate_email($sEmail) == false) {
            $oMsgBox->error($MESSAGE['USERS_INVALID_EMAIL']);
            $sEmail  = '';
        } else {
            // Check if the email exists in the database
            $sSql = "SELECT * FROM `{TP}users` WHERE `email`='".$database->escapeString($sEmail)."'";
            if(($rRow = $database->query($sSql))){
                if($aUser = $rRow->fetchRow(MYSQLI_ASSOC)) {
                    if(strlen($aUser['signup_confirmcode']) > 25){
                        header("Location: ".ACCOUNT_URL."/signup_continue_page.php?switch=wrong_inputs");
                        exit(0); // break up the script here
                    }
                    $iUserID = (int) $aUser['user_id'];
                    // Get the id, username, email, and last_reset from the above db query
                    // Check if the password has been reset in the last 2 hours
                    if( ( time() - intval($aUser['last_reset']) ) < (2 * 3600) ) {
                        // Tell the user that their password cannot be reset more than once per hour
                        $oMsgBox->error($MESSAGE['FORGOT_PASS_ALREADY_RESET']);
                    } else {
                        // current password
                        $sCurrentPw = $aUser['password'];
                        // generate new password
                        $sNewPasswordRaw = $oAccounts->GenerateRandomPassword();
                        $sNewPasswordEnc = $oAccounts->doPasswordEncode($sNewPasswordRaw);
                        // prepare E-Mail with login details to send to the user via email
                        $aTokenReplace = array(
                            'LOGIN_DISPLAY_NAME'  => $aUser['display_name'],
                            'LOGIN_NAME'          => $aUser['username'],
                            'LOGIN_WEBSITE_TITLE' => WEBSITE_TITLE,
                            'LOGIN_PASSWORD'      => $sNewPasswordRaw,
                            'LOGIN_URL'           => ACCOUNT_URL . '/login.php'
                        );
                        $sOnScreenSwitch    = 'forgot_login_details_sent';
                        $sEmailTemplateName = 'password_recovery_mail';
                        $sEmailSubject      = '';
                        $sMailTo            = $sEmail;
                        $checkSend = $oAccounts->sendEmail($sMailTo, $aTokenReplace, $sEmailTemplateName, $sEmailSubject);
                        if ($checkSend === true) {
                            // update the new password in the database
                            $aUpdateUser = array(
                                'user_id'    => $iUserID,
                                'password'   => $sNewPasswordEnc,
                                'last_reset' => time(),
                            );
                            if($database->updateRow('{TP}users', 'user_id', $aUpdateUser)){
                                header("Location: ".ACCOUNT_URL."/signup_continue_page.php?lc=".$sLC."&switch=".$sOnScreenSwitch."&email=".$sMailTo);
                                exit(0);
                            } else {
                                // Error updating database
                                $oMsgBox->error($MESSAGE['RECORD_MODIFIED_FAILED']);
                                if(WB_DEBUG) {
                                    $oMsgBox->error($database->get_error().'<br />'.$sSql);
                                }
                        } else {
                            // tell user: WRONG INPUTS
                            $aUpdateUser = array(
                                'user_id'    => $aUser['user_id'],
                                'password'   => $sCurrentPw
                            );
                            $database->updateRow('{TP}users', 'user_id', $aUpdateUser);
                            header("Location: ".ACCOUNT_URL."/signup_continue_page.php?lc=".$sLC."&switch=wrong_inputs&from=resend_forgot_pass&mail_err=".$checkSend);
                            exit(0);
                        }
                } else { // no record found - Email doesn't exist, so tell the user
                    $oMsgBox->error($MESSAGE['FORGOT_PASS_EMAIL_NOT_FOUND']);
                }
            } else {
                // Query failed
                if(WB_DEBUG) {
                    $oMsgBox->error('SystemError:: Database query failed!');
                    $oMsgBox->error($database->get_error().'<br />'.$sSql);
ob_start();
call_captcha("all","","accountforgot");
$captcha = ob_get_contents();
ob_end_clean();
    'CAPTCHA'       => $captcha,

<p>{{ L_('MENU:FORGOT') }}</p>
		<div class="formRow">
		{{CAPTCHA}}
		</div>
.captcha_table {
	display:inline-block;
	margin:auto;
}

.error {
	color:firebrick;
}

$template_version = '2.1.4';
$template_platform = '1.5.0';
<body onload="document.login.email.focus();">
							<div class="error">{MESSAGE}</div>
						<div class="fg12 center">
							{CAPTCHA}
						</div>

    display: block;
    margin: 50px 0;
.captcha_table {
    display: inline-block;
    margin: auto;
}

$template_version = '1.7.6';
$template_platform = '1.5.0';
            {CAPTCHA}