CTF/복기

[DUCTF] co2 풀이

copyc4t 2025. 1. 18. 00:59

[2024 DownUnderCTF]

Date : held on July 5 to 7, 2024

CTF 때 못풀고 아쉬워서 묵혀놨다가, 드디어 정신차리고 CTF 복기

 

요약 : 주석을 보고 python merge에서 발생하는 함수의 취약점 이용 Class pollution(python's prototype pollution)

{"__class__":{"__init__":{"__globals__":{"flag":"true"}}}}

 


1. 분석

input 요소가 존재하는 endpoint

 

/register

/login

/create_post

/feedback

/create_post 와 /feedback

  • 입력 가능한 요소 중, /feedback에서 주석을 줌
  • merge 함수를 사용하여 두 객체를 결합하겠다 선언
@app.route("/save_feedback", methods=["POST"])
def save_feedback():
    data = json.loads(request.data)
    feedback = Feedback()
    # Because we want to dynamically grab the data and save it attributes we can merge it
    #and it *should* create those attribs for the object.
    #이 주석은 data에서 가져온 정보를 feedback 객체의 속성으로 동적으로 저장하기 위해
    #merge 함수를 사용하여 두 객체를 결합할 것이라는 내용을 설명합니다. 
    #즉, data의 내용이 feedback 객체의 속성으로 변환되어 저장될 것이라는 의미입니다.
    merge(data, feedback)
    save_feedback_to_disk(feedback)
    return jsonify({"success": "true"}), 200

@app.route("/get_flag")
@login_required
def get_flag():
    if flag == "true":
        return "DUCTF{NOT_THE_REAL_FLAG}"
    else:
        return "Nope"

 

→ python merge 함수 취약점 검색 

Prototye-Pollution 취약점을 이용하는건지 알아봐야 함

 

사실상 글 하나 빼고 모든 글이 prototype을 이용한다는 것과, 심지어 CodeGate CTF 저 게시글에서 나온 Class Pollution을 따라했으면 그대로 플래그를 얻을 수 있다.

2. 정리

간단한 CTF에서 주석은 힌트일 가능성이 있다.

없었더라도, 로그인, 회원가입 빼면 feedback과 post 둘 중 하나였다는 것,

json을 사용한다는 것,

merge함수가 직접 만들어졌다는 것에서 생각을 깊이 파고들었어야 하지 않았을까.

 

3. Prototype Pollution (PP)

- 발생 위치에 따라 종류가 다양하다.

1. Prototye Pollution (Javascript, Json)

JS에서 PP가 발생하면 Object의 Prototype을 수정할 수 있다.

이 때 DOM에 관여하여 XSS 등의 chain exploit이 가능해진다.

Object.__proto__

Object.constructor.prototype

ex) { "constructor": { "prototype": { "polluted": true } } }

 

 

2. Python Prototype Polltion (Class Pollution)

ex) __class__.__init__.__globals__.flag:true

- Reference

- co2 writeup & Class Pollution

https://one3147.tistory.com/83

 

https://book.hacktricks.wiki/en/generic-methodologies-and-resources/python/class-pollution-pythons-prototype-pollution.html

https://github.com/DownUnderCTF/Challenges_2024_Public/blob/main/web/co2/solve/WRITEUP.md

 

- Prototye Pollution

https://keyme2003.tistory.com/entry/Prototype-Pollution