记一次powershell在内网中的分析与利用
字数 1025 2025-08-24 16:48:07
PowerShell在内网渗透中的分析与利用
前言
PowerShell作为Windows内置的强大脚本语言,结合Metasploit框架(Meterpreter)可以成为内网渗透的理想工具。本文将详细介绍如何利用PowerShell进行内网信息收集、扫描和渗透。
基础环境搭建
建立Meterpreter会话
首先在受害者系统上建立Meterpreter有效负载,然后加载PowerShell扩展:
load powershell
加载后可使用四个主要命令:
powershell_execute: 执行PowerShell语句powershell_import: 导入本地PowerShell脚本在远程执行powershell_shell: 启动交互式PowerShell外壳powershell_session_remove: 移除PowerShell会话
内网信息收集技术
ARP枚举
使用Get-NetNeighbor cmdlet发现本地网络中的活动主机:
powershell_execute 'Get-NetNeighbor | Where-Object -Property State -NE "Unreachable" | Select-Object -Property IPAddress'
Ping扫描
使用Test-Connection进行ping扫描:
powershell_execute '1..254 | foreach {"192.168.171.$($_): $(Test-Connection -TimeoutSeconds 1 -Count 1 -ComputerName 192.168.171.$($_) -Quiet)"}'
端口扫描技术
基础TCP扫描
powershell_execute 'Test-NetConnection -ComputerName 192.168.171.21 -Port 80 | Select-Object -Property RemotePort,TcpTestSucceeded'
高效TCP扫描
直接使用TcpClient .NET类:
1..1024 | foreach {echo ((New-Object Net.Sockets.TcpClient).Connect("192.168.171.21",$_)) "Port $_ is open!"} 2>$null
优化端口扫描脚本
Function Test-CommonTCPPorts {
Param($address, $timeout=1000)
$ports = @(21,22,23,25,53,80,81,110,111,113,135,139,143,179,199,443,445,465,514,548,554,587,993,995,1025,1026,1720,1723,2000,3306,3389,5060,5900,6001,8000,8080,8443,8888,10000,32768)
ForEach($port in $ports) {
$socket = New-Object System.Net.Sockets.TcpClient
try {
$result = $socket.BeginConnect($address, $port, $null, $null)
if(!$result.AsyncWaitHandle.WaitOne($timeout, $False)) {
throw [System.Exception]::new("Connection Timeout")
}
"$port - open"
} catch {
"$port - closed"
} finally {
$socket.Close()
}
}
}
使用示例:
powershell_execute 'Test-CommonTCPPorts 192.168.171.21 500'
SMB共享枚举
使用Get-WmiObject枚举SMB共享:
powershell_execute 'Get-WmiObject -Class win32_share'
Nishang框架集成
Nishang是一个基于PowerShell的开源渗透测试和后渗透框架,包含多种实用脚本。
获取Nishang
git clone https://github.com/samratashok/nishang.git
使用Nishang脚本
导入并执行信息收集脚本:
powershell_import nishang/Gather/Get-Information.ps1
powershell_execute Get-Information
高级利用技术
内存中执行脚本
通过Meterpreter的powershell_import功能,可以直接在内存中加载和执行PowerShell脚本,无需将文件写入磁盘:
powershell_import /path/to/script.ps1
powershell_execute FunctionName
绕过防御技术
PowerShell脚本可以通过以下方式绕过安全防御:
- 内存执行不落盘
- 编码混淆技术
- 合法命令滥用
- 进程注入
总结
PowerShell在内网渗透中具有以下优势:
- Windows系统内置,无需额外安装
- 强大的系统访问能力
- 内存执行特性
- 丰富的.NET类库支持
- 与Metasploit框架无缝集成
在实际渗透测试中,应结合具体环境选择合适的PowerShell技术,并注意:
- 扫描时设置合理超时时间
- 优先使用内存执行技术
- 结合多种信息收集方法
- 善用Nishang等成熟框架
- 注意操作隐蔽性
通过灵活运用PowerShell,可以高效完成内网渗透的各个阶段任务。