OS Command Injection
i will explain the vulnerability and how it happen and some mitigation
Last updated
i will explain the vulnerability and how it happen and some mitigation
Last updated
is a vulnerability that consists of an attacker executing commands on the host (server-OS-system) via a vulnerable application
Shell metacharacters: ( & , && , | , || , ; , \n ,
, $()` )
|
⇒ only second command will show
||
⇒ second command will execute if the first don’t success or get error Second (only if first fails)
**&
⇒ Both (second output generally shown first)**
&&
⇒ execute the second when the first execute successful only if first succeeds
;
⇒ all command will work until one don’t success
commix tool ⇒ give it url - cookie - data
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.
PHP
may use the exec
, system
, shell_exec
, passthru
, or popen
functions to execute commands directly on the back-end server
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 the GET
request is used directly with the touch
command without being sanitized
web application becomes vulnerable to OS command injection
PHP
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".
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.
we don’t have or see code then we try to identify which character can use to bypass
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
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.
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.
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.**
which means removing any non-necessary special characters from the user input
**preg_replace**
to remove any special characters from the user input
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
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