πOS Command Injection
i will explain the vulnerability and how it happen and some mitigation
is a vulnerability that consists of an attacker executing commands on the host
(server-OS-system) via a vulnerable application
Untitled
π Shell metacharacters: ( & , && , | , || , ; , \n ,
, $()` )
|
β only second command will show||
β second command will execute if the first donβt success or get errorSecond (only if first fails)
**&
β Both (second output generally shown first)**&&
β execute the second when the first execute successfulonly if first succeeds
;
β all command will work until one donβt success
#Both Unix and Windows supported
ls||id; ls ||id; ls|| id; ls || id # Execute both
ls|id; ls |id; ls| id; ls | id # Execute both (using a pipe)
ls&&id; ls &&id; ls&& id; ls && id # Execute 2ΒΊ if 1ΒΊ finish ok
ls&id; ls &id; ls& id; ls & id # Execute both but you can only see the output of the 2ΒΊ
ls %0A id # %0A Execute both (RECOMMENDED)
#Only unix supported
`ls` # ``
$(ls) # $()
ls; id # ; Chain commands
ls${LS_COLORS:10:1}${IFS}id # Might be useful
#Not executed but may be interesting
> /var/www/html/out.txt #Try to redirect the output to a file
< /etc/passwd #Try to send some input to the command
Tools For Exploit :
commix tool β give it url - cookie - data
Types of Command Injection
In-band Command Injection :
Consists of an attacker executing commands on the host operating system via a vulnerable application and
receiving the response of the command in the application.
Blind Command Injection :
Consists of an attacker executing commands on the host operating system via a vulnerable application that
does not return the output from the command within its HTTP response
Blind vulnerabilities can still be exploited, but different techniques are required.
πVuln PHP Code :
PHP
may use theexec
,system
,shell_exec
,passthru
, orpopen
functions to execute commands directly on the back-end server<?php if (isset($_GET['filename'])) { system("touch /tmp/" . $_GET['filename'] . ".pdf"); } ?>
web application has a functionality that allows users to create a new
.pdf
document that gets created in the/tmp
the user input from the
filename
parameter in theGET
request is used directly with thetouch
command without being sanitizedweb application becomes vulnerable to OS command injection
πbasic command blacklist filter in PHP
PHP
$blacklist = ['whoami', 'cat', ...SNIP...];
foreach ($blacklist as $word) {
if (strpos('$_POST['ip']', $word) !== false) {
echo "Invalid input";
}
}
The code you provided will check if the input in the $_POST['ip']
variable contains any of the words in the blacklist
array. If it does, the code will echo "Invalid input".
πBlacklisted Characters bypass :
backend use function to block all special characters - If any character in the string we sent matches a character in the blacklist, our request is denied.
$blacklist = ['&', '|', ';', ...SNIP...]; foreach ($blacklist as $character) { if (strpos($_POST['ip'], $character) !== false) { echo "Invalid input"; } }
we donβt have or see code then we try to identify which character can use to bypass
Impact of Command Injection Attacks :
Unauthorized access to the application and host operating system.
Confidentiality β Command injection can be used to view sensitive information.
Integrity β Command injection can be used to alter content in the application.
Availability β Command injection can be used to delete content in the application.
Remote code execution on the operating system
πBlack-Box Testing
Map the application : Identify all instances where the web application appears to interacting with operating system
Fuzz the application. β’ Shell metacharacters: (
& , && , | , || , ; , \n ,
, $()` )For in-band command injection :
analyze the response of the application to determine if itβs vulnerable.
For blind command injection, you need to get creative.
Trigger a
**time delay using the ping or sleep**
command.Output the response of the command in the web root and retrieve the file directly using a browser.
Open an out-of-band channel back to a server you control.
π White-Box Testing
Map all input vectors in the application.
Review source code to determine if any of the input vectors are added as parameters to functions that execute system commands
Once a vulnerability is identified, test it to confirm that it is exploitable.
Prevent
1. Input Validation :
we should always validate and then sanitize the user input
Input validation is done to ensure it matches the expected format for the input, such that the request is denied if it does not match
**filter_var
: built-in PHP function used for validating and filtering data.**if (filter_var($_GET['ip'], FILTER_VALIDATE_IP)) { // call function } else { // deny request }
2. Input Sanitization : always performed after input validation.
which means removing any non-necessary special characters from the user input
**preg_replace**
to remove any special characters from the user input
$ip = preg_replace('/[^A-Za-z0-9.]/', '', $_GET['ip']);
echo "Sanitized IP: " . $ip;
if we input ip like 192.168.1.15#koko
Function will remove #koko and the output will be
**Sanitized IP: 192.168.0.1**
another code with JS
var ip = ip.replace(/[^A-Za-z0-9.]/g, '');
Prevent :
Validating against a whitelist of permitted values.
utilizing blacklisted characters and words on the back-end to detect injection
Validating that the input is a number.
Validating that the input contains only alphanumeric characters, no other syntax or whitespace
Last updated