Metasploit Resource 脚本简要指南 (Creating Metasploit Resource Script )

Metasploit Resource脚本是MSF框架提供的非常方便的用于组织自动化任务的工具。简单来说,resource脚本目前可以支持两种类型的指令组合,分别是MSF命令和嵌入式Ruby代码。
以下分别就两种使用方法进行演示。

这里以 multi/handler 这个用于反弹shell监听器的模块来演示。

使用makerc命令制作metasploit命令行resource脚本

执行msfconsole启动MSF控制台以后,执行如下操作:

1
2
3
4
5
6
7
8
9
10
msf5 > use multi/handler
msf5 exploit(multi/handler) > set payload windows/meterpreter/reverse_tcp
payload => windows/meterpreter/reverse_tcp
msf5 exploit(multi/handler) > set lhost 0.0.0.0
lhost => 0.0.0.0
msf5 exploit(multi/handler) > set lport 4444
lport => 4444
msf5 exploit(multi/handler) > makerc /root/test.rc
[*] Saving last 4 commands to /root/test.rc ...
msf5 exploit(multi/handler) >

将会在/root/目录下生成test.rc文件,其中记录着刚刚输入的所有命令。下次需要执行同样操作的时候,只需要运行msfconsole -r /root/test.rc即可载入和执行这次的命令记录。

使用run_single函数编写基于嵌入式Ruby的resource脚本

除了直接编写指令代码以外,Resource脚本还可以支持嵌入式Ruby脚本,只需要在.rc文件中使用<ruby></ruby>标签引入Ruby代码即可。

这里将上面用于启动handler模块的脚本稍微改良一下,变成一个易于使用的rc文件,代码如下:

handle.rc:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

<ruby>

def init_args

args = {}

if ARGV.join('') =~ /^help$/i

args[:help] = true

return args

end

args[:payload] = ARGV.shift
args[:host] = ARGV.shift
args[:port] = ARGV.shift

return args

end


# resource process entry

begin

args = init_args

if args[:help]

print_line("Usage:\nmsfconsole handler.rc PAYLOAD HOST PORT")

return

end

#run_single("workspace handler")
run_single("use multi/handler")
run_single("set payload #{args[:payload]}")
run_single("set lhost #{args[:host]}")
run_single("set lport #{args[:port]}")

rescue ArgumentError => e

print_error("Invalid argument: #{e.message}")

return

rescue RuntimeError => e

print_error(e.message)

return

rescue ::Exception => e

raise e

end

</ruby>

将上面的代码保存为handle.rc,启动MSF控制台时,执行:

1
msfconsole -r handler.rc windows/meterpreter/reverse_tcp 0.0.0.0 4444

即可快速地配置好配备相应payload的handler监听器。

其中Ruby代码中的run_single就是用于执行MSF框架指令的方法。

MSF自带了很多方便实用的脚本,位于MSF框架目录/scripts/resource/目录下,可以参考这些脚本的写法,写出更加方便的脚本。

参考链接:

https://metasploit.help.rapid7.com/docs/resource-scripts

Ruby元编程技术1-对象模型(Ruby Meta Programming - Object Models) Debian创建.desktop文件(Create .desktop file in Debian/Gnome)
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×