<드림핵 command-injection>
https://dreamhack.io/wargame/challenges/768
command-injection-chatgpt
특정 Host에 ping 패킷을 보내는 서비스입니다. Command Injection을 통해 플래그를 획득하세요. 플래그는 flag.py에 있습니다. chatGPT와 함께 풀어보세요! Reference Introduction of Webhacking
dreamhack.io


제공되는 웹 사이트에 들어가면 ping 통신을 하기 위한 input 박스가 있다.
정상적으로 작동되는지 확인하기 위해 google.com 주소를 넣으면

핑 명령어가 출력된 것을 확인할 수 있다. 핑 명령어는 오류를 내면 코드를 확인할 수 있어서 특정 값을 넣어서 일부러 오류 코드를 확인한다.

ping -c 3 [input]
ping -c 3 [input], 명령어; 명령어 형식으로 명령어를 실행할 수 있다.


ls 명령어가 정상적으로 입력되는 것을 확인할 수 있다. flag.py 파일을 확인할 수 있다. cat 명령어를 통해 flag.py 파일의 내용을 읽는다. ;cat flag.py를 입력하면 플래그 값을 확인할 수 있다.


<드림핵 command-injection-1>
https://dreamhack.io/wargame/challenges/44
command-injection-1
특정 Host에 ping 패킷을 보내는 서비스입니다. Command Injection을 통해 플래그를 획득하세요. 플래그는 flag.py에 있습니다. Reference Introduction of Webhacking
dreamhack.io

command injection 취약점이란 시스템 명령어를 쿼리문에 주입하여 취약한 변수를 통해 서버 운영체제에 접근하는 공격이다.

ip 8.8.8.8로 입력해봤다.

#!/usr/bin/env python3
import subprocess
from flask import Flask, request, render_template, redirect
from flag import FLAG
APP = Flask(__name__)
@APP.route('/')
def index():
return render_template('index.html')
@APP.route('/ping', methods=['GET', 'POST'])
def ping():
if request.method == 'POST':
host = request.form.get('host')
cmd = f'ping -c 3 "{host}"'
try:
output = subprocess.check_output(['/bin/sh', '-c', cmd], timeout=5)
return render_template('ping_result.html', data=output.decode('utf-8'))
except subprocess.TimeoutExpired:
return render_template('ping_result.html', data='Timeout !')
except subprocess.CalledProcessError:
return render_template('ping_result.html', data=f'an error occurred while executing the command. -> {cmd}')
return render_template('ping.html')
if __name__ == '__main__':
APP.run(host='0.0.0.0', port=8000)
우선 화면만 봐선 잘 모르겠어서 소스 코드를 확인했다.
cmd = f'ping -c 3 "{host}"'
윗 문제처럼 ping -c 3 명령어를 통해 해결하는 것 같다.
/bin/sh -c ping -c 3 "8.8.8.8"
"{host}"부분이 반영되기 때문에 리눅스 다중명령어를 사용한다.
1) ; : 하나의 라인에 주어진 명령어를 성공, 실패와 관련 없이 모두 실행한다.
- 예시) ping 8.8.8.8; ls; ps
2) && : 앞에서부터 순차적으로 실행하되, 명령 실행에 실패할 경우 뒤 명령은 실행하지 않는다.
- 예시) ping 8.8.8.8 && ls && ps
3) || : 앞에서부터 순차적으로 실행하되, 명령 실행에 성공할 경우 뒤 명령은 실행하지 않는다.
- 예시) ping 8.8.8.8 || ls || ps
4) | : 앞의 명령어 처리결과를 뒤 명령어로 전달하여 뒤 명령어를 실행한다.
- 예시) ping 8.8.8.8 | ls
-> ping 명령어 결과로 ls 명령어를 수행하는데 이렇게 되면 실제로 모두 수행되지만 화면에는 ls 결과만 출력된다.
/bin/sh -c ping "8.8.8.8"; ls "./" 그런데 이렇게 입력하려고 하니 형식을 맞춰줘야 한다는 문구가 떴다. 검색해보니 버프스위트를 이용해 우회하면 된다고 한다.


flag.py 파일이 있는 것을 확인할 수 있고, cat flag.py 명령어를 통해 내용을 읽어준다.

플래그 값을 확인할 수 있다.
<webhacking.kr 44번>
Challenge 44
webhacking.kr:10005


들어가자마자 view-source를 누르니 이러한 코드가 나왔다. 소스 코드를 확인하니 주어진 양식을 이용해서 ls를 실행하고, ls를 execute하라고 나온다. id는 다섯 자리고 post 방식으로 받는다. system("echo 'hello! {$id}'");에서 ls를 실행하기 위해 텍스트박스에 ';'ls를 입력하여 전송한다.


flag_29cbb98dafb4e471117fec409148e9386753569e
이 값을 주소창에 넣어주면

플래그 값이 나오는 것을 확인할 수 있다.
FLAG{y2u.be/sW3RT0tF020}
<드림핵 Command injection advanced>
https://dreamhack.io/wargame/challenges/413
Command Injection Advanced
Description Exercise: Command Injection Advanced에서 실습하는 문제입니다. 문제 수정 내역 2023.07.27 Dockerfile 제공
dreamhack.io

문제 화면이고 url을 입력하면 flag 값이 출력되는 것 같다. url에 그냥 문제 url을 바로 제출해봤는데 캐시 파일이 나왔다. url이 해싱되어 파일명으로 쓰이고 캐시 디렉토리에 저장되었다는 것으로 보인다.

이것 말고는 확인할 수 있는게 없어서 우선 코드를 확인했다.
<html>
<head></head>
<link rel="stylesheet" href="/static/bulma.min.css" />
<body>
<div class="container card">
<div class="card-content">
<h1 class="title">Online Curl Request</h1>
<?php
if(isset($_GET['url'])){
$url = $_GET['url'];
if(strpos($url, 'http') !== 0 ){
die('http only !');
}else{
$result = shell_exec('curl '. escapeshellcmd($_GET['url']));
$cache_file = './cache/'.md5($url);
file_put_contents($cache_file, $result);
echo "<p>cache file: <a href='{$cache_file}'>{$cache_file}</a></p>";
echo '<pre>'. htmlentities($result) .'</pre>';
return;
}
}else{
?>
<form>
<div class="field">
<label class="label">URL</label>
<input class="input" type="text" placeholder="url" name="url" required>
</div>
<div class="control">
<input class="button is-success" type="submit" value="submit">
</div>
</form>
<?php
}
?>
</div>
</div>
</body>
</html>
-
- url 파라미터의 시작이 'http' 이어야 한다.
- 'http' 로 시작하는 파라미터 값을 escapeshellcmd 함수로 필터링 한다.
- 이후 /cache/.md5(url) 경로에 저장한다.
- escapeshellcmd 함수는 '-' , '/' 등의 문자들을 필터링 하지 못하는 점을 이용한다.curl 명령어의 -o 옵션을 이용해 웹쉘을 파일을 업로드하는 경로에 저장하게 한뒤 실행시켜 FLAG 를 획득한다. 개인 웹서버가 없기 때문에 깃허브를 이용한다.
다른 사람이 미리 만들어 깃허브에 올려둔 웹쉘의 링크를 입력했다.

전송하면 curl명령이 정상적으로 이루어졌음을 알 수 있다.
주소 뒤에 /cache/exploit.php를 입력하면 아래 화면이 뜬다. 명령어로 ls를 입력했더니 제대로 출력된다.

다 출력되는 것을 보아 플래그 파일도 될 것 같아 명령어에 /flag라고 입력했더니 flag 값이 나왔다.
