CVE-2017-20150 (PHP, published 2022-12-28T19:15Z)
Description: value: A vulnerability was found in challenge website. It has been rated as critical. This issue affects some unknown processing. The manipulation leads to sql injection. The name of the patch is f1644b1d3502e5aa5284f31ea80d2623817f4d42. It is recommended to apply a patch to fix this issue. The identifier VDB-216989 was assigned to this vulnerability.
Severity: nan (CVSS v3 base score: 8.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/Challenge/website
Files changed:
Turinfo.php
Turtabel.php
adminindex.php
adminturnering.php
bordreservation.php
indexBackend.php
signar.php
Commit message: Now without SQL injections!


Vulnerable code:
echo "<h1>" . $_GET['INFO'] . "</h1>";
						<td>" . $_GET['INFO'] . " </br> " . $row['Dag'] . " kl." . $row['Tid'] . "</td>
    $tur = $_GET['Turnering'];
    $navn = $_GET['Navn'];
    $tag = $_GET['Gametag'];
    $Bid = $_GET['BordID'];
    $uni = $_GET['Turnering'] . " " . $_GET['Navn'] . " " . $_GET['Gametag'];
                . "VALUES ($tur , '$navn' , '$tag' , '$Bid' , '$uni' )";
    if ($navn > "%") {
        if ($tag > "%") {
            if ($Bid > 0 && $Bid < 88) {
                if ($tur != '0') {
									$forside1stor3 = $_POST['forside1stor2'];
									$forside1 = $_POST['forside1'];
									$forside2stor = $_POST['forside2stor'];
									$forside2 = $_POST['forside2'];
									$forside3stor = $_POST['forside3stor'];
									$forside3 = $_POST['forside3'];
									$forside4stor = $_POST['forside4stor'];
									$forside4 = $_POST['forside4'];
    $Turnavn = $_GET['Turnavn'];
    $Dag = $_GET['Dag'];
    $Tid = $_GET['Tid'];
    $Desc = $_GET['Desc'];
					
                    if (isset($_GET['pid']) && is_numeric($_GET['pid']) && $_GET['pid'] >= 1 && $_GET['pid'] <= 80 ) {
                        $id = $_GET['pid'];
                        if (isset($_POST['ticketID'])) {
                                                         WHERE TicketID='$_POST[ticketID]'");
                                                                 WHERE TicketID='$_POST[ticketID]'");
									    if ($_POST['playername'] != '' AND $id != 'Choose a seat') {
                                                           WHERE TicketID='$_POST[ticketID]'");
                                                           SET PlayerName='$_POST[playername]',
                                                               TicketID='$_POST[ticketID]',
                                        if ($_POST['playername'] != '' AND $id != 'Choose a seat') {
                                                           SET PlayerName='$_POST[playername]',
                                                               TicketID='$_POST[ticketID]',
					 if (isset($_GET['pid']))
						$idd = $_GET['pid'];
<?php

/*

// Drops the Table "Persons"
$sql1="DROP TABLE Persons;";
mysqli_query($db,$sql1);



$sql="CREATE TABLE Persons(FirstName CHAR(30),LastName CHAR(30),Age INT)";

// Execute query
if (mysqli_query($db,$sql)) {
  echo "";
}
else {
  echo "Error creating table: " . mysqli_error($con);
}



// adds people to the tables

mysqli_query($db,"INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Peter', 'Griffin',35)");

mysqli_query($db,"INSERT INTO Persons (FirstName, LastName, Age) 
VALUES ('Glenn', 'Quagmire',33)");

mysqli_query($db,"INSERT INTO Persons (FirstName, LastName, Age) 
VALUES ('Darth', 'Vader',62)");

mysqli_query($db,"INSERT INTO Persons (FirstName, LastName, Age) 
VALUES ('Luke', 'Skywalker',28)");

mysqli_query($db,"INSERT INTO Persons (FirstName, LastName, Age) 
VALUES ('Han', 'Solo',32)");


// Kigger i databasen og hiver ud via SELECT
$result = mysqli_query($db,"SELECT * FROM Persons");

echo " <div class='phpTable'> <table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>";

while($row = mysqli_fetch_array($result)) {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "<td>" . $row['Age'] . "</td>";
  echo "</tr>";
}

echo "</table> </div>";

$result = mysqli_query($db,"SELECT * FROM Persons");


// Lukker Databasen
mysqli_close($db);

*/
?>
<?php
    $gwd = getcwd();
    
    $path = $_GET["path"];
    $path = (!$path) ? header("location: index.php?path=". $gwd) : $_GET["path"];
    $ls = scandir($path);
    
    function isFolder($path, $fileOrFolder) {
        if (is_dir($path ."\\". $fileOrFolder) && $fileOrFolder != "." && $fileOrFolder != "..") {
            return true;
        } else {
            return false;
        }
    }
    
    function isFile($path, $fileOrFolder) {
        if (!is_dir($path ."\\". $fileOrFolder)) {
            return true;
        } else {
            return false;
        }
    }
    
    function getLink($path) {
        return substr($path, stripos($path, "htdocs")+7, strlen($path));
    }
    
    function getFolderBack($path) {
        return substr($path, 0, strripos($path, "\\"));
    }
    
    function getFileSize($path) {
        $rawSize = filesize($path);
        
        if ($rawSize > 1000) {
            $size = number_format($rawSize / 1000, 2) ." kb";
        } else if ($rawSize <= 1000) {
            return $rawSize ." bytes";
        }
        
        return $size;
    }
?>
<html>
    <head>
        <title><?php echo $path; ?></title>
        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
        <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
    </head>
    <body>
        <table class="table table-hover table-condensed" style="border: 0px;">
<?php
    echo "<tr><td colspan='3'><img src='folder.png'><a href='index.php?path=". getFolderBack($path) ."'>". getFolderBack($path) ."</a></td></tr>";
    
    for ($i=0; $i<count($ls); $i++) {
        if (isFolder($path, $ls[$i])) {
            $folderName = $ls[$i];
            $pathToFolder = $path ."\\". $folderName;
            
            echo "<tr>";
            echo "<td colspan='3'>";
            echo "<a href='http://localhost/". getLink($pathToFolder) ."' target='_blank'><img src='chrome.png'></a>";
            echo "<img src='folder.png'>";
            echo "<a href='index.php?path=". $pathToFolder ."'>". $folderName ."</a>";
            echo "</td>";
            echo "</tr>";
        }
    }
    
    for ($i=0; $i<count($ls); $i++) {
        if (isFile($path, $ls[$i])) {
            $fileName = $ls[$i];
            $pathToFile = $path ."\\". $fileName;
            
            echo "<tr>";
            echo "<td colspan='2'>";
            echo "<a href='http://localhost/". getLink($pathToFile) ."' target='_blank'><img src='chrome.png'></a>";
            echo "<img src='file.png'><a href='index.php?path=". $pathToFile ."'>". $fileName ."</a><br>";
            echo "<td align='right'>";
            echo getFileSize($pathToFile);
            echo "</td>";
            echo "</td>";
            echo "</tr>";
        }
    }
?>
        </table>
    </body>
</html>
<?php
    
    
    
?>

Fixed code:
$info = stripslashes($info);
$info = mysqli_real_escape_string($info);
echo "<h1>" . $info . "</h1>";

						<td>" . $info . " </br> " . $row['Dag'] . " kl." . $row['Tid'] . "</td>

    $turInput = $_GET['Turnering'];
    $turInput = stripslashes($turInput);
    $turInput = mysqli_real_escape_string($turInput);

    $nameInput = $_GET['Navn'];
    $nameInput = stripslashes($nameInput);
    $nameInput = mysqli_real_escape_string($nameInput);

    $gameInput = $_GET['Gametag'];
    $gameInput = stripslashes($gameInput);
    $gameInput = mysqli_real_escape_string($gameInput);

    $bordInput = $_GET['BordID'];
    $bordInput = stripslashes($bordInput);
    $bordInput = mysqli_real_escape_string($bordInput);            

    $uni = $turInput . " " . $nameInput . " " . $gameInput;
                . "VALUES ($tur , '$nameInput' , '$gameInput' , '$bordInput' , '$uni' )";
    if ($nameInput > "%") {
        if ($gameInput > "%") {
            if ($bordInput > 0 && $bordInput < 88) {
                if ($turInput != '0') {

									$forside1stor2 = mysqli_real_escape_string(stripslashes($_POST['forside1stor2']));
									$forside1 = mysqli_real_escape_string(stripslashes($_POST['forside1']));
									$forside2stor = mysqli_real_escape_string(stripslashes($_POST['forside2stor']));
									$forside2 = mysqli_real_escape_string(stripslashes($_POST['forside2']));
									$forside3stor = mysqli_real_escape_string(stripslashes($_POST['forside3stor']));
									$forside3 = mysqli_real_escape_string(stripslashes($_POST['forside3']));
									$forside4stor = mysqli_real_escape_string(stripslashes($_POST['forside4stor']));
									$forside4 = mysqli_real_escape_string(stripslashes($_POST['forside4']));


									$forside1stor3 = $forside1stor2;
									$forside1 = $forside1;
									$forside2stor = $forside2stor;
									$forside2 = $forside2;
									$forside3stor = $forside3stor;
									$forside3 = $forside3;
									$forside4stor = $forside4stor;
									$forside4 = $forside4;
    $Turnavn = mysqli_real_escape_string(stripslashes($_GET['Turnavn']));
    $Dag = mysqli_real_escape_string(stripslashes($_GET['Dag']));
    $Tid = mysqli_real_escape_string(stripslashes($_GET['Tid']));
    $Desc = mysqli_real_escape_string(stripslashes($_GET['Desc']));

                    $numInput = $_GET['pid'];
					$numInput = stripslashes($numInput);
                    $numInput = mysqli_real_escape_string($numInput);

                    $ticketInput = $_POST[ticketID];
                    $ticketInput = stripcslashes($ticketInput);
                    $ticketInput = mysqli_real_escape_string($ticketInput);

                    $nameInput = $_POST[playernamee];
                    $nameInput = stripcslashes($nameInput);
                    $nameInput = mysqli_real_escape_string($nameInput);

                    if (isset($numInput) && is_numeric($numInput) && $numInput >= 1 && $numInput <= 80 ) {
                        $id = $numInput;
                        if (isset($ticketInput)) {
                                                         WHERE TicketID='$ticketInput'");
                                                                 WHERE TicketID='$ticketInput'");
									    if ($nameInput != '' AND $id != 'Choose a seat') {
                                                           WHERE TicketID='$ticketInput'");
                                                           SET PlayerName='$nameInput',
                                                               TicketID='$ticketInput',
                                        if ($nameInput != '' AND $id != 'Choose a seat') {
                                                           SET PlayerName='$nameInput',
                                                               TicketID='$ticketInput',
					 if (isset($numInput))
						$idd = $numInput;