SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된...

53
SECUINSIDE CTF Solutions to Challenges

Transcript of SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된...

Page 1: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

SECUINSIDE CTFSolutions to Challenges

Page 2: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

Challenges

• SBBS (Web 250pts)

• ByHuman (Pwnable 300pts)

• TrendyWeb (Web 100pts)

• CGC Challenges

Page 3: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

SBBSWeb :: 250pts

Junsang Cheon [email protected]

Page 4: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

SBBS - Overview

• Two Web Servers (Flask)

• Service Server (Challenges can access)

• Admin Server (Only administrator can access)

• Source code of admin server was provided

• XSS + Server Side Template Injection Vulnerabilities

Page 5: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

SBBS - Structure

ServiceServer

AdminServer

Phantomjs

Database Fetch postWrite/Read post

Read post

Update post IF status is in 400 - 500

Page 6: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

SBBS - VulnerabilityKey Variables

Vulnerable FUnction

Page 7: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

SBBS - Vulnerability

• Make Phantomjs redirect to 404 page using below URLs

• location.href=“/{{ ‘’.__class__ }}”; —> <type ‘str’>

• /{{ ‘’.__class__.__mro__[-1].__subclasses__() }} —> a number of objects

• Challengers can see these responses

Page 8: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

SBBS - Intended Solution

• Use ctypes methods of subclasses

• ctypes.CDLL._func_restype_.from_address(0x7effc7250370)

• 0x7effc7250370 is address of `title_404` variableit can be leaked by using `id` function

• `FLAG` is separated by about 26,000 bytes than `title_404`

Page 9: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

SBBS - Intended Solution• Intended exploitation is similar with Blind SQL Injection

in memory using Server Side Template Injection

Page 10: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

SBBS - Unintended Solution

• Use of specific object has `__builtins__` property

• __builtins__ has `__import__` function which can import any python packages

• ‘’.__class__.__mro__[-1].__subclasses__()[121]()._module.__builtins__[‘__import__’](‘admin.app’).FLAG

• I couldn’t filter challengers’ payloads well…

Page 11: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

ByHumanPwnable :: 300pts

Yonghwi Jin [email protected]

Page 12: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

Concept• 소스코드만 주었음

• 퍼징과 오디팅의 차이가 궁금했음

• 컴파일 환경이 달라도 동작하는 공격코드 요구

Page 13: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

소스코드• 639 줄의 C++ 소스코드

• 바이너리 내의 함수 위치가 랜덤하게 바뀜

• 바이너리가 컴파일 할때마다 다름

Page 14: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

프로그램 동작• 명령을 받아서 연산 수행 후 출력

• 자료형: Number/String/Storage(key-value)

• 명령: 숫자 지원, 변수 선언, 출력, 서브루틴, …

• 선언된 변수를 각각 고유번호로 참조할 수 있음

• 키-값 저장소의 키는 문자열이여야 함

Page 15: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

취약점• typedef std::unique_ptr<Item> ItemPtr;

Page 16: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

취약점• 키-값 저장소의 값 지정 시 (OP_SET)

key가 문자열(STRING)이 아니면 선언된 key, value 둘 다 free 됨

Page 17: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

취약점 요약• C++11 스마트 포인터의 부적절한 사용으로 인한

Use-After-Free

Page 18: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

Exploit• Use-After-Free 후에도 값 참조가 가능함

• 프로그램 흐름 조정

• C++이므로 vftable 조작을 통해 EIP 변경 가능

• 바이너리 및 임의 메모리 유출

• 여러 방법으로 쓸 수 있음

• (ex) String 클래스의 ptr, length 멤버 조작

Page 19: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

TrendyWebWeb :: 100pts

Yonghwi Jin [email protected]

Page 20: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

Concept• wget 1-day를 바로 찾아낼 수 있는가 (혹은 알고 있는가)

Page 21: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

프로그램 동작• PHP로 된 소스코드를 줬음 (46줄)

• parse_url 리턴값의 path가 /avatar.png인 경우에만 파일을 /data/<랜덤id>/ 에 다운로드 가능함

Page 22: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

프로그램 동작2• 다운로드는 wget 커맨드로 아래와 같이 함

• /usr/bin/wget <escaped URL>

• 이후 세션 정보를 /data/<랜덤id>/pickle에 저장 (serialize)

• 스크립트 시작 시 세션 파일이 있으면 로딩함 (unserialize)

Page 23: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

취약점• 의도된 취약점

• wget은 원래 맨 마지막 / 뒤의 파일명으로 파일을 다운받게 되어있으며 HTTP 리다이렉트 후에도 첫번째 파일명으로 다운로드됨

• FTP는 안 그랬음 (CVE-2016-4971)

• 따라서 임의의 파일명으로 파일을 다운로드 할 수 있음

Page 24: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

취약점• 의도된 풀이 (1-day + race condition)

• 이미지 다운로드 후 해당 폴더의 pickle에 현재 세션의 데이터를 저장함

• 이미지 다운로드 중 파일명을 조작하여 pickle로 해당 데이터를 조작한 후 세션 기록 전에 한번 더 같은 세션으로 요청을 보내서 임의의 데이터를 unserialize 하게 유도함

• unserialize 후 공격할 수 있는 클래스는 제공함

Page 25: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

취약점• 의도치 않은 풀이 1 (1-day)

• pickle 파일 외에도 .php 확장자의 파일을 다운로드해서 풀 수 있었음 (php 실행 불가능하게 설정해야했음)

Page 26: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

취약점• 의도치 않은 풀이 2 (parse_url bypass)

• path는 ? 뒤의 문자를 검사 안함

• wget은 ? 뒤의 문자까지 경로에 포함시켜서 파일을 다운받음

• /avatar.png?.php로 하면 path는 /avatar.png 이고 query가 .php가 됨

• php 파일 업로드 후 실행

Page 27: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

CGC ChallengesCGC

Hwiwon Lee [email protected]

Page 28: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

What is CGC?

• Fully automated computer security challenge by DARPA

• https://www.cybergrandchallenge.com/

• https://github.com/cybergrandchallenge

Page 29: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

In DEFCON 2016 Quals

https://legitbs.net/statdump_2016/challenges.html

Page 30: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

What is DECREE?

• DARPA’s Cyber Grand Challenge uses a new computing environment, the DARPA Experimental Cyber Research Evaluation Environment.

• It has some properties that make it excellent for binary reverse engineering, patching, and exploitation contests.

• http://repo.cybergrandchallenge.com/boxes/

Page 31: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

How to make CGC Binary?

• In DECREE,

Page 32: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

How to make CGC Binary?• And make,

http://cgc-docs.legitbs.net/cgc-release-documentation/walk-throughs/building-a-cb/

Page 33: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

CGC CTF?

• Like DEFCON 2016 Quals, it needs the following.

• DECREE or Docker(with compiled cgc binaries and testing tools)

• Codes with some vulnerabilities

• Wrappers that recv&send PoV|CB&Result(or FLAG)

Page 34: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

DECREE or Docker

• I decided to use DECREE as testing server

• Port forwarding rule must be set

• VBoxManage controlvm run_crs_##_## natpf1 cykor1,tcp,0.0.0.0,31338,,31338

• python wrapper.py 31338 (crs@virtualbox)

Page 35: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

Codes with some vulns

• Patching point must be included

Page 36: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

Codes with some vulns

• And make,

Page 37: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

Wrappers

• 2 types

• For PoV (XML or CGC Binary)

• For Patched Binary

Page 38: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

Wrappers

• For PoV

• Support 2 types (XML or CGC Binary)

• Get PoV and then TEST

• cb-test —negotiate —xml pov_filename —directory path —failure_ok —should_core —cb cb

Page 39: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

Wrappers

• For Patched Binary

• Get patched binary

• Do some test (CGC type, Poller, …)

• cb-test —negotiate —xml pov_filename —directory path —cb cb

Page 40: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

Wrappers

Page 41: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

CYKOR_00001 & patch

• Stack based buffer overflow

• Type1 PoV (Control EIP and a general purpose register)

Page 42: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

CYKOR_00001

Page 43: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

CYKOR_00001

Page 44: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

CYKOR_00001 PoV

Page 45: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

CYKOR_00001 PoV

Page 46: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

CYKOR_00001 Patch

Page 47: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

CYKOR_00002 & patch

• Null pointer dereference

• Just crash (like CQE)

Page 48: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

CYKOR_00002

Page 49: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

CYKOR_00002

Page 50: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

CYKOR_00002 PoV

Page 51: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

CYKOR_00002 PoV

Page 52: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

CYKOR_00002 Patch

Page 53: SECUINSIDE CTFsecuinside.com/archive/2016/2016-2-7.pdf · 2016-07-13 · 취약점 •의도된 풀이 (1-day + race condition) • 이미지 다운로드 후 해당 폴더의 pickle에

Thank you