前言
- 上周编写了minio客户端和dbeaver数据库管理客户端的凭证提取模块,这两个客户端都是跨平台的,而且模块只涉及了读文件操作,所以社区回复是否可以支持多操作系统,和多会话类型。在Christophe De La Fuente的帮助下完成了两个模块的多平台支持。
模块适配
- 主要功能都在这几个文件上,常用的文件操作和进程操作。
lib/msf/core/post/common.rb
lib/msf/core/post/file.rb
lib/msf/core/post/process.rb
- 先看一下通用模块,也就是common.rb这个文件。
函数名 |
功能描述 |
clear_screen |
清屏,在模块编写中没啥作用 |
rhost |
获取目标IP地址 |
rport |
获取目标端口 |
peer |
获取IP地址和端口组合 |
函数名 |
功能描述 |
cmd_exec |
执行命令 |
cmd_exec_get_pid |
执行命令返回pid,只支持meterpreter |
get_env |
获取环境变量 |
get_envs |
获取多个环境变量,以字典的形式返回 |
command_exists |
判断命令是否存在 |
cmd_exec_with_result |
执行命令读取结果 |
函数名 |
功能描述 |
cd |
改变会话当前目录 |
pwd |
返回会话当前目录 |
dir |
获取指定目录下的文件和目录 |
mkdir |
创建目录 |
directory |
判断指定路径是否为目录 |
expand_path |
拼接存在环境变量的字符串为完整路径 |
file |
判断指定路径是否为文件 |
setuid |
判断指定路径是否为setuid文件 |
executable |
判断指定路径是否为可执行文件,不支持window系统 |
writable |
判断指定路径是否为可写行文件,支持powershell,不支持shell会话 |
immutable |
判断指定路径是否为不可变文件,不支持window系统 |
readable |
判断指定路径是否为可读行文件,支持powershell,不支持shell会话 |
exist |
判断指定路径是否存在,文件和文件夹都行 |
attributes |
获取指定路径的属性 |
file_local_write |
写数据到本地文件 |
file_remote_digestmd5 |
获取指定路径的md5 |
file_remote_digestsha1 |
获取指定路径的sha1 |
file_remote_digestsha2 |
获取指定路径的sha2 |
read_file |
读指定路径的文件 |
write_file |
写文件 |
append_file |
在指定文件后面追加数据 |
upload_file |
上传文件,原理是读本地文件再调用上面的写文件 |
upload_and_chmodx |
上传完文件再设置属性 |
chmod |
设置属性,不支持window |
exploit_data |
二进制读取exploit目录下的文件 |
exploit_source |
读取exploit源码文件 |
rm_f |
删除文件 |
rm_rf |
删除文件夹 |
rename_file |
重命名文件 |
copy_file |
复制文件 |
函数名 |
功能描述 |
pidof |
回去进程pid |
has_pid |
判断是否存在指定pid |
kill_process |
杀死指定进程 |
代码编写
- 在适配多平台时需要修改模块描述信息,在加载模块时可以让不同会话识别是否可用,如果会话和平台不在描述信息上会报警告信息。
def initialize(info = {})
super(
update_info(
info,
'Name' => 'Gather Dbeaver Passwords',
'Description' => %q{
This module will determine if Dbeaver is installed on the target system and, if it is, it will try to
dump all saved session information from the target. The passwords for these saved sessions will then be decrypted
where possible.
},
'License' => MSF_LICENSE,
'References' => [
[ 'URL', '<https://blog.kali-team.cn/Metasploit-dbeaver-9f42e26241c94ba785dce5f1e69697aa>' ]
],
'Author' => ['Kali-Team <kali-team[at]qq.com>'],
'Platform' => [ 'linux', 'win', 'osx', 'unix'],
'SessionTypes' => [ 'meterpreter', 'shell', 'powershell' ],
'Notes' => {
'Stability' => [],
'Reliability' => [],
'SideEffects' => []
}
)
)
register_options(
[
OptString.new('XML_FILE_PATH', [ false, 'Specifies the .dbeaver-data-sources.xml file path for Dbeaver']),
OptString.new('JSON_DIR_PATH', [ false, 'Specifies the json directory path for Dbeaver']),
]
)
end
- 主要修改
Platform
这个字段和SessionTypes
这个字段,当然模块名就不要留什么window字样了,会让别人有只能在window系统上运行的错觉。
- 习惯了window系统下的meterpreter会话模块编写,上来就是UserProfiles遍历一遍全部用户,根据环境变量和注册表来获取凭证的文件路径,现在只能通过自己去读环境变量,自己拼接路径了,稍微麻烦一点。
- 在shell会话中,meterpreter的session类无法使用
总结
- 如果遇到编写跨平台的后渗透模块时,有只涉及到文件读取操作,算法对系统没有什么依赖时可以尝试编写支持多平台的模块,这样更加方便别人使用。
参考
https://github.com/rapid7/metasploit-framework/pull/17341
https://github.com/rapid7/metasploit-framework/pull/17337