CVE-2017-1000444 (Other, published 2018-01-02T15:29Z)
Description: value: Eleix Openhacker version 0.1.47 is vulnerable to an SQL injection in the account registration and login component resulting in information disclosure and remote code execution
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/Eleix/openhacker
Files changed:
conf/global.php
cron/watchdog.php
game/guest/lgin.htm
game/guest/rebuild.htm
game/guest/reg.htm
game/main/user/bank.htm
game/main/user/banksend.htm
game/main/user/clanbank.htm
game/main/user/clanbankin.htm
game/main/user/clanbanksend.htm
game/main/user/clanroster.htm
game/main/user/updateacc.htm
index.php
layout/guest/footer.htm
layout/guest/header.htm
layout/main/footer.htm
layout/main/header.htm
Commit message: Security Update 092017

conf/global.php -
          Increment version +1

cron/watchdog.php -
          Remove linking transaction ID's. With clan bank transfers (Hacker 2 Clan or Clan 2 Hacker) the original logic no longer works as there is now two seperate database and ID' are not one after (or below) the other.

game/guest/lgin.htm - 
           Added validation for IP addresses (see game/guest/reg.htm)

game/guest/rebuild.htm
          Alert that the site is down but link off to the bugtracking page on GitHub

game/guest/reg.htm -
           Added ip validation
           Use POST over REQUEST for username, password, and email checks.
            Added username validation and sanitation
            Fixed a known bug that if the user didn't create a bank account first then they did not recieve any money for small jobs done before visiting the bank page for the first time. (Bank registration was an unnecessary complication and should already be handled on account registration.)
               Fixes #4 (lgin.htm too)

game/main/user/bank.htm -
          Added a missing end tag for the Send Cash form.

game/main/user/banksend.htm -
          Added sanitation on extra details message for bank transactions
          Added validation to sent amount
          Added logic to send money from Hacker to Clan
          Fixes #5

game/main/user/clanbank.htm -
          Phased out old button
          Added the radio toggle between Hacker or Clan

game/main/clanbankin.htm -
          Sanitized clan password input 
          Fixes #6

game/main/user/clanbanksend.htm -
          Added sanitation on extra details message for bank transactions
          Added validation to sent amount
          Removed sameid limitation for C 2 H transactions
          Added display for C 2 C coming soon

game/main/user/clanroster.htm - 
          Added logic for clan member status (regular member or council member)

game/main/user/updateacc.htm -
         Added sanitization to bio. Utilized htmlpurifier to allow for minimal html to be added (user can format their bios)
         Note: htmlpurifier sits outside web root (SEE index.php)
         Fixed old redirect location url

index.php -
         Added additional header modification cases
         Require the HTMLPurifier (Seperate download not contained in this repository)

layout/guest/header.htm -
           Removed old starting html logic (no longer needed and handled inside index.php

layout/main/footer.htm -
           Formatting issue

layout/main/header.htm -
           Changed Cryptocurrenty to Crypto-Market (Planned feature)

Vulnerable code:
$GAMEVERSION = "0.1.47"; // Game version
				if($transactionid % 2 == 0){
					$linkedtransid = $transactionid -1;
				}
				else {
					$linkedtransid = $transactionid +1 ;
				};
					$WD_LNK_TRNS = "UPDATE bank_transactions SET linked_transid = '$linkedtransid' WHERE id = '$transactionid'";
					$result = $conn->query($WD_LNK_TRNS);
					$WD_LNK_TRNS = "UPDATE bank_transactions SET linked_transid = '$linkedtransid' WHERE id = '$transactionid'";
					$result = $conn->query($WD_LNK_TRNS);
					$WD_LNK_TRNS = "UPDATE bank_transactions SET linked_transid = '$linkedtransid' WHERE id = '$transactionid'";
					$result = $conn->query($WD_LNK_TRNS);
					$WD_LNK_TRNS = "UPDATE bank_transactions SET linked_transid = '$linkedtransid' WHERE id = '$transactionid'";
					$result = $conn->query($WD_LNK_TRNS);
					$WD_LNK_TRNS = "UPDATE bank_transactions SET linked_transid = '$linkedtransid' WHERE id = '$transactionid'";
					$result = $conn->query($WD_APP);
					$result = $conn->query($WD_LNK_TRNS);					
					$WD_LNK_TRNS = "UPDATE bank_transactions SET linked_transid = '$linkedtransid' WHERE id = '$transactionid'";
					$result = $conn->query($WD_APP);
					$result = $conn->query($WD_LNK_TRNS);		
					$WD_LNK_TRNS = "UPDATE bank_transactions SET linked_transid = '$linkedtransid' WHERE id = '$transactionid'";
					$result = $conn->query($WD_LNK_TRNS);
<h4>LiteCity is currently offline for maintenance </h4>

<h5>Whats to come</h5>

<ul>

	<li>Anti-cheat system</li>
	<li>Live Events</li>
	<li>CLI game program</li>
	<li>Redesigned UI</li>
	<li>... and much more</li>
</div>

if($_REQUEST['username']=='' || $_REQUEST['email']=='' || $_REQUEST['password']=='')
$sql = "INSERT INTO accounts(username,email,password,regip,reghash) VALUES('".$_REQUEST['username']."', '".$_REQUEST['email']."', '$encpass', '$ip', '$reghash')";
$username = $_POST['username'];

Username: $username
$username = $_REQUEST['username'];

$sql9 = "SELECT id FROM accounts WHERE username = '$username';";
}
$details .= $_POST['details']; 
$details2 .= $_POST['details'];
$amount = $_POST['amount'];
echo "Money Transfers to clan banks are unavailable at this time";
</div>
		echo '<input style="width:70px;" type="submit" name="submit" value="Login">';
$sqlSetCBP = "UPDATE clans SET bankpassword = '$password' WHERE id = '$clanid'";
$details .= $_POST['details']; 
$details2 .= $_POST['details'];
$sql = "SELECT cash FROM economy WHERE userid = '$SendUser'";
if ($recvuser == $SendUser) {
echo "<h1>Transaction Failed</h1>";
echo "<br>";
echo "You can't send money to yourself!"; 
echo "<br>";
echo "<br>";
echo "<td><form action='?a=bank' method='POST'><input type='submit' name='submit-btn' value='Back to account summary' /></td></form>";
} else {

$sql = "UPDATE economy SET cash='$newBalUs1' WHERE userid= '$SendUser'";
$sql = "INSERT INTO bank_transactions(userid,recvid,details,amount) VALUES('$SendUser', '$recvuser', '$details2', '$amountnegative')";
}


</div>
$sql = "SELECT id, username FROM accounts WHERE clan = '$clanid'";
    	 echo "<tr><td><a href=?a=profile&id=" . $row["id"]. ">" . $row["username"]. "</td><td>" . $row["email"]. "</td><td>" . $row["role"]. "</td><td>" . $row["lastip"]. "</td><td>" . $row["lastlogindate"]. "</td><td>" . $row["regdate"]. "";
$_safebio = mysqli_real_escape_string($conn, $bio);
$sql = "UPDATE accounts SET bio='$_safebio' WHERE username='$username'";
header("location: /game/main/index.php?a=accountdetails");

<html>
	<head>
		<title>Hacker City | Index</title>
		<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Ek+Mukta">
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
		<link type="text/css" rel="stylesheet" href="/css/style.css">
		<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
		<link type="text/css" rel="stylesheet" href="/css/materialize.css">
		<link type="text/css" rel="stylesheet" href="/css/burn.css">
		<link rel="shortcut icon" href="/usr/share/nginx/html/images/favicon.ico" type="image/x-icon">
		<link rel="icon" href="/usr/share/nginx/html/images/favicon.ico" type="image/x-icon">
		<script src="/js/materialize.js"></script>
		<script src='https://www.google.com/recaptcha/api.js'></script>
		<script async src="/js/spanize.js"></script>
	</head>
	
<body>

<h6>Version <?php echo $GAMEVERSION; ?>
</h6>
            		<li><a href="?a=cryptocurrency">Cryptocurrency</a></li>

Fixed code:
$GAMEVERSION = "0.1.48"; // Game version
					$result = $conn->query($WD_APP);				
					$result = $conn->query($WD_APP);		
		// Check if IP is valid
		if(filter_var($ip, FILTER_VALIDATE_IP)){} // Leave ip as is
		else{$ip = $_SERVER['REMOTE_ADDR'];};		
	// Check if IP is valid
	if(filter_var($ip, FILTER_VALIDATE_IP)){} // Leave ip as is
	else{$ip = $_SERVER['REMOTE_ADDR'];};	
<h4>HackerCity is currently offline for maintenance </h4>
<p>Keep up to date on the latest bugs on our Github!</p>
<a style="font-size:20px;color:cyan;" href="https://github.com/Eleix/openhacker/issues">HackerCity Github Issues Page</a>
</div>
		// Check if IP is valid
		if(filter_var($ip, FILTER_VALIDATE_IP)){} // Leave ip as is
		else{$ip = $_SERVER['REMOTE_ADDR'];};		
	// Check if IP is valid
	if(filter_var($ip, FILTER_VALIDATE_IP)){} // Leave ip as is
	else{$ip = $_SERVER['REMOTE_ADDR'];};	

if($_POST['username']=='' || $_POST['email']=='' || $_POST['password']=='')

// Validate and Sanitize Username

$username = $_POST['username'];
$safeusername = (filter_var($username, FILTER_SANITIZE_STRING));
if(@preg_match(''/^[a-zA-Z0-9]{3,}$/', $safeusername)) { // invalid username
	echo "Your username is not allowed. Allowed values are a-z, A-Z, 0-9 and must be at least 3 characters";
}
else { // valid username

$sql = "INSERT INTO accounts(username,email,password,regip,reghash) VALUES('$safeusername', '$email', '$encpass', '$ip', '$reghash')";
Username: $safeusername
$sql9 = "SELECT id FROM accounts WHERE username = '$safeusername';";
// Create bank account

$sql11 = "INSERT INTO 'economy' ('userid') VALUES ('$userid');"
$result11 = $conn->query($sql11);

};
	echo "</form>";
$amount = $_POST['amount'];
$details = $_POST['details'];

//Sanitize details input

$safedetails = (filter_var($details, FILTER_SANITIZE_STRING));

if($amount < 0 ){ 
	echo "Amount sent cannot be a negative number.";
	echo "<br>";
	echo "Transaction cancelled";

echo "<td><form action='?a=bank' method='POST'><input type='submit' name='submit-btn' value='Back to account summary' /></td></form>";
}
else{
$details .= $safedetails; 
$details2 .= $safedetails;

	

$clanname = $RecUser; //Used in the error message

// Convert clan name to ID
$sqlConv2ID = "SELECT id FROM clans WHERE clanname = '$RecUser'";
$resultConv2ID = $conn->query($sqlConv2ID);
while($rowConvID = $resultConv2ID->fetch_assoc()){
  $RecUser = $rowConvID['id'];
$details = "sent by $SendUserName reason: ";
$details .= $safedetails; 
$details2 = "sent to $clanname reason: ";
$details2 .= $safedetails;
$amount = $_POST['amount'];
$amountnegative = $amount * -1;

//Check to see if RecUser has an account with the bank

$sql = "SELECT clanid FROM claneconomy WHERE clanid = '$RecUser'";
$result = $conn->query($sql);

while($row = $result->fetch_assoc()){
$recvuser = $row['clanid']; // Clan ID return
};

$sql = "SELECT clanid FROM claneconomy WHERE clanid = '$recvuser'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

$sql = "SELECT cash FROM economy WHERE userid = '$SendUser'";
$result = $conn->query($sql);

while($row = $result->fetch_assoc()){
$oldBal = $row['cash'];
}

$newBalUs1 = $oldBal - $amount;

if ($newBalUs1 < 0)
{
   echo "<h1>Transaction Failed</h1>";
   echo "<br>";
   echo "You can't send more money than you actually have.";
   echo "<br>";
   echo "<br>";
   echo "<td><form action='?a=bank' method='POST'><input type='submit' name='submit-btn' value='Back to account summary' /></td></form>";
} else {

$sql = "SELECT cash FROM claneconomy WHERE clanid = '$recvuser'";  //fetch the recieving clan's current bank balance
$result = $conn->query($sql);

while($row = $result->fetch_assoc()){
$oldBalUs2 = $row['cash'];   // export the returned value to a variable
}

$newBalUs2 = $oldBalUs2 + $amount;

$sql = "UPDATE claneconomy SET cash='$newBalUs2' WHERE clanid = '$recvuser'";
$result = $conn->query($sql);

$sql = "UPDATE economy SET cash='$newBalUs1' WHERE userid= '$SendUser'";
$result = $conn->query($sql);

$sql = "INSERT INTO bank_transactions(userid,recvid,details,amount) VALUES('$SendUser', '$recvuser', '$details2', '$amountnegative')";
$result = $conn->query($sql);

$sql = "INSERT INTO clanbank_transactions(clanid,clanrecvid,details,amount,transactionuserid) VALUES('$recvuser', '$recvuser', '$details', '$amount', '$SendUserName')";
$result = $conn->query($sql);

   echo "Transaction Complete";
   echo "<td><form action='?a=bank' method='POST'><input type='submit' name='submit-btn' value='Back to account summary' /></td></form>";

}
} 
else {
	echo "<h1>Transaction Failed</h1>";
	echo "<br>";
	echo "<p>We are sorry. Our database failed to find the clan "; echo $clanname; echo " in our banking database.</p>";
	echo "<p>Your previous transaction has been canceled.</p>";
	echo "<td><form action='?a=bank' method='POST'><input type='submit' name='submit-btn' value='Back to account summary' /></td></form>";

};
};
};
</div>
		echo "<button class='btn waves-effect waves-light' type='submit' name='submit'>Login<i class='material-icons center'></i></button>";

    echo "<input type='radio' value='hacker' id='hacker' name='type'>";
    echo "<label for='hacker'>Hacker</label>";
    echo "<div class='check'><div class='inside'></div></div>";

    echo "<input type='radio' value='clan' id='clan' name='type'>";
    echo "<label for='clan'>Clan</label>";   
    echo "<div class='check'><div class='inside'></div></div>";

	echo "</form>";
$safepassword = mysqli_real_escape_string($conn, $password);
$sqlSetCBP = "UPDATE clans SET bankpassword = '$safepassword' WHERE id = '$clanid'";
	$username = $_SESSION['login_user'];

	$sqlGetClanID = "SELECT clan FROM accounts WHERE username = '$username'";
	$resultGetCID = $conn->query($sqlGetClanID);
	while($rowCID = $resultGetCID->fetch_assoc()) {
		$clanid = $rowCID['clan'];
	};


$type=$_POST['type'];
$amount = $_POST['amount'];
$details = $_POST['details'];

//Sanitize details input

$safedetails = (filter_var($details, FILTER_SANITIZE_STRING));

if($amount < 0 ){ 
	echo "Amount sent cannot be a negative number.";
	echo "<br>";
	echo "Transaction cancelled";

echo "<td><form action='?a=bank' method='POST'><input type='submit' name='submit-btn' value='Back to account summary' /></td></form>";
}
else{

if($type == 'hacker'){
$details .= $safedetails; 
$details2 .= $safedetails;
$sql = "SELECT cash FROM claneconomy WHERE clanid = '$SendUser'";
$sql = "UPDATE claneconomy SET cash='$newBalUs1' WHERE clanid= '$SendUser'";
$sql = "INSERT INTO clanbank_transactions(userid,recvid,details,amount) VALUES('$SendUser', '$recvuser', '$details2', '$amountnegative')";
}
else {
echo "Clan to Clan coming soon";
};
};
</div>
$sql = "SELECT id, username, clancouncil FROM accounts WHERE clan = '$clanid'";

    	 echo "<tr><td><a href=?a=profile&id=" . $row["id"]. ">" . $row["username"]. "</td><td>" . $row["clancouncil"]. "</td><td>" . $row["role"]. "</td><td>" . $row["lastip"]. "</td><td>" . $row["lastlogindate"]. "</td><td>" . $row["regdate"]. "";
// Sanitize bio but allow for a minimal amount of HTML so the user can style their bio thir way.
$safebio = $purifier->purify($_safebio);

$sql = "UPDATE accounts SET bio='$safebio' WHERE username='$username'";
header("location: ?a=accountdetails");
			case "guest": echo "Guest";break;
			case "login": echo "Login";break;
			case "register": echo "Register";break;
require_once '../htmlpurifier/library/HTMLPurifier.auto.php';
$purifier = new HTMLPurifier();

<h6>Version <?php echo $GAMEVERSION; ?></h6>
            		<li><a href="?a=cryptomarket">Crypto-Market</a></li>