CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/msf/ui/console/command_dispatcher/resource.rb
Views: 11787
1
# -*- coding: binary -*-
2
3
#
4
# Rex
5
#
6
7
8
9
module Msf
10
module Ui
11
module Console
12
module CommandDispatcher
13
14
#
15
# {CommandDispatcher} for commands related to background jobs in Metasploit Framework.
16
#
17
class Resource
18
19
include Msf::Ui::Console::CommandDispatcher
20
21
22
def commands
23
{
24
"resource" => "Run the commands stored in a file",
25
"makerc" => "Save commands entered since start to a file",
26
}
27
end
28
29
#
30
# Returns the name of the command dispatcher.
31
#
32
def name
33
"Resource Script"
34
end
35
36
def cmd_resource_help
37
print_line "Usage: resource path1 [path2 ...]"
38
print_line
39
print_line "Run the commands stored in the supplied files (- for stdin)."
40
print_line "Resource files may also contain ERB or Ruby code between <ruby></ruby> tags."
41
print_line
42
print_line "See also: makerc"
43
print_line
44
end
45
46
def cmd_resource(*args)
47
if args.empty?
48
cmd_resource_help
49
return false
50
end
51
52
args.each do |res|
53
res_expand = ::File.expand_path(res)
54
good_res = nil
55
if res == '-'
56
good_res = res
57
elsif ::File.file?(res_expand) && File.readable?(res_expand)
58
good_res = res_expand
59
else
60
# let's check to see if it's in the scripts/resource dir (like when tab completed)
61
[
62
::Msf::Config.script_directory + ::File::SEPARATOR + 'resource',
63
::Msf::Config.user_script_directory + ::File::SEPARATOR + 'resource'
64
].each do |dir|
65
res_path = dir + ::File::SEPARATOR + res
66
if ::File.file?(res_path) && File.readable?(res_path)
67
good_res = res_path
68
break
69
end
70
end
71
end
72
if good_res
73
driver.load_resource(good_res)
74
else
75
print_error("#{res} is not a valid resource file")
76
next
77
end
78
end
79
end
80
81
#
82
# Tab completion for the resource command
83
#
84
# @param str [String] the string currently being typed before tab was hit
85
# @param words [Array<String>] the previously completed words on the command line. words is always
86
# at least 1 when tab completion has reached this stage since the command itself has been completed
87
88
def cmd_resource_tabs(str, words)
89
tabs = []
90
#return tabs if words.length > 1
91
if !str.nil? && (str.start_with?('~') || str =~ /^#{Regexp.escape(File::SEPARATOR)}/)
92
# then you are probably specifying a full path so let's just use normal file completion
93
return tab_complete_filenames(str, words)
94
elsif (not words[1] or not words[1].match(/^\//))
95
# then let's start tab completion in the scripts/resource directories
96
begin
97
[
98
::Msf::Config.script_directory + File::SEPARATOR + "resource",
99
::Msf::Config.user_script_directory + File::SEPARATOR + "resource",
100
'.'
101
].each do |dir|
102
next unless ::File.exist?(dir)
103
tabs += ::Dir.new(dir).find_all { |e|
104
path = dir + File::SEPARATOR + e
105
::File.file?(path) && File.readable?(path)
106
}
107
end
108
rescue
109
end
110
else
111
tabs += tab_complete_filenames(str,words)
112
end
113
return tabs
114
end
115
116
def cmd_makerc_help
117
print_line "Usage: makerc <output rc file>"
118
print_line
119
print_line "Save the commands executed since startup to the specified file."
120
print_line
121
end
122
123
def cmd_makerc_tabs(str, words)
124
tab_complete_filenames(str, words)
125
end
126
127
#
128
# Saves commands executed since the ui started to the specified msfrc file
129
#
130
def cmd_makerc(*args)
131
if args.empty? || args.include?('-h')
132
cmd_makerc_help
133
return false
134
end
135
driver.save_recent_history(args[0])
136
end
137
end
138
139
end
140
end
141
end
142
end
143
144