Command injection is the abuse of an application’s behavior to execute commands on the operating system with the same privileges as the user who is running the application on the server.

It is also called as Remote Code Execution.

<?php
$songs = "/var/www/html/songs"

if(isset $_GET["title"]){
		$title = $_GET ["title"];
		$command = " grep $title /var/www/html/songtitle.txt";
		$search = exec($cnd);
		if ($search == ""){
				$return = "<p> The requested song </p><p> $title does </p><b> not</b><p>exist!</p>;
		} else {
				$return = "<p> The requested song </p><p> $title does </p><b>exist!</b>;
				}
				
				echo $return;
		}
		?>

In this the title of the song is an input to the program from the user, the program then uses the title and search the file using grep. Here the input from the user can be abused to execute their own commands.

Shell operators ; , & and && will combine two or more system commands or execute them both.

Remediating Command Injection

Vulnerable function in PHP

Proper checks should be done before passing it on to these functions.

Input sanitization is great way to prevent command injection.

<?php
if(!filter_input(INPUT_GET, "number", FILTER_VALIDATE_NUMBER)){
}

Bypassing Filters:

Application will use numerous filtering and sanitizing across a user’s input, but we can bypass them by giving the payload in hexadecimal. Filters might catch quotation marks ; but when passed as hexa decimal they may go through.