Skip to content

Commit 6557a84

Browse files
David MaloneyDavid Maloney
authored andcommitted
add resource command dispatcher
move resource script related commands into their own command dispatcher
1 parent 2008dcb commit 6557a84

File tree

3 files changed

+139
-98
lines changed

3 files changed

+139
-98
lines changed

lib/msf/ui/console/command_dispatcher/core.rb

Lines changed: 1 addition & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
require 'msf/ui/console/command_dispatcher/auxiliary'
1818
require 'msf/ui/console/command_dispatcher/post'
1919
require 'msf/ui/console/command_dispatcher/jobs'
20+
require 'msf/ui/console/command_dispatcher/resource'
2021
require 'msf/util/document_generator'
2122

2223
module Msf
@@ -120,8 +121,6 @@ def commands
120121
"pushm" => "Pushes the active or list of modules onto the module stack",
121122
"previous" => "Sets the previously loaded module as the current module",
122123
"quit" => "Exit the console",
123-
"resource" => "Run the commands stored in a file",
124-
"makerc" => "Save commands entered since start to a file",
125124
"reload_all" => "Reloads all modules from all defined module paths",
126125
"route" => "Route traffic through a session",
127126
"save" => "Saves the active datastores",
@@ -222,101 +221,6 @@ def cmd_reload_all(*args)
222221
cmd_banner()
223222
end
224223

225-
def cmd_resource_help
226-
print_line "Usage: resource path1 [path2 ...]"
227-
print_line
228-
print_line "Run the commands stored in the supplied files. Resource files may also contain"
229-
print_line "ruby code between <ruby></ruby> tags."
230-
print_line
231-
print_line "See also: makerc"
232-
print_line
233-
end
234-
235-
def cmd_resource(*args)
236-
if args.empty?
237-
cmd_resource_help
238-
return false
239-
end
240-
241-
args.each do |res|
242-
good_res = nil
243-
if ::File.exist?(res)
244-
good_res = res
245-
elsif
246-
# let's check to see if it's in the scripts/resource dir (like when tab completed)
247-
[
248-
::Msf::Config.script_directory + ::File::SEPARATOR + "resource",
249-
::Msf::Config.user_script_directory + ::File::SEPARATOR + "resource"
250-
].each do |dir|
251-
res_path = dir + ::File::SEPARATOR + res
252-
if ::File.exist?(res_path)
253-
good_res = res_path
254-
break
255-
end
256-
end
257-
end
258-
if good_res
259-
driver.load_resource(good_res)
260-
else
261-
print_error("#{res} is not a valid resource file")
262-
next
263-
end
264-
end
265-
end
266-
267-
#
268-
# Tab completion for the resource command
269-
#
270-
# @param str [String] the string currently being typed before tab was hit
271-
# @param words [Array<String>] the previously completed words on the command line. words is always
272-
# at least 1 when tab completion has reached this stage since the command itself has been completed
273-
274-
def cmd_resource_tabs(str, words)
275-
tabs = []
276-
#return tabs if words.length > 1
277-
if ( str and str =~ /^#{Regexp.escape(File::SEPARATOR)}/ )
278-
# then you are probably specifying a full path so let's just use normal file completion
279-
return tab_complete_filenames(str,words)
280-
elsif (not words[1] or not words[1].match(/^\//))
281-
# then let's start tab completion in the scripts/resource directories
282-
begin
283-
[
284-
::Msf::Config.script_directory + File::SEPARATOR + "resource",
285-
::Msf::Config.user_script_directory + File::SEPARATOR + "resource",
286-
"."
287-
].each do |dir|
288-
next if not ::File.exist? dir
289-
tabs += ::Dir.new(dir).find_all { |e|
290-
path = dir + File::SEPARATOR + e
291-
::File.file?(path) and File.readable?(path)
292-
}
293-
end
294-
rescue Exception
295-
end
296-
else
297-
tabs += tab_complete_filenames(str,words)
298-
end
299-
return tabs
300-
end
301-
302-
def cmd_makerc_help
303-
print_line "Usage: makerc <output rc file>"
304-
print_line
305-
print_line "Save the commands executed since startup to the specified file."
306-
print_line
307-
end
308-
309-
#
310-
# Saves commands executed since the ui started to the specified msfrc file
311-
#
312-
def cmd_makerc(*args)
313-
if args.empty?
314-
cmd_makerc_help
315-
return false
316-
end
317-
driver.save_recent_history(args[0])
318-
end
319-
320224
def cmd_back_help
321225
print_line "Usage: back"
322226
print_line
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# -*- coding: binary -*-
2+
3+
#
4+
# Rex
5+
#
6+
7+
require 'rex/ui/text/output/buffer/stdout'
8+
9+
10+
module Msf
11+
module Ui
12+
module Console
13+
module CommandDispatcher
14+
15+
#
16+
# {CommandDispatcher} for commands related to background jobs in Metasploit Framework.
17+
#
18+
class Resource
19+
20+
include Msf::Ui::Console::CommandDispatcher
21+
22+
23+
def commands
24+
{
25+
"resource" => "Run the commands stored in a file",
26+
"makerc" => "Save commands entered since start to a file",
27+
}
28+
end
29+
30+
#
31+
# Returns the name of the command dispatcher.
32+
#
33+
def name
34+
"Resource Scripts"
35+
end
36+
37+
def cmd_resource_help
38+
print_line "Usage: resource path1 [path2 ...]"
39+
print_line
40+
print_line "Run the commands stored in the supplied files. Resource files may also contain"
41+
print_line "ruby code between <ruby></ruby> tags."
42+
print_line
43+
print_line "See also: makerc"
44+
print_line
45+
end
46+
47+
def cmd_resource(*args)
48+
if args.empty?
49+
cmd_resource_help
50+
return false
51+
end
52+
53+
args.each do |res|
54+
good_res = nil
55+
if ::File.exist?(res)
56+
good_res = res
57+
elsif
58+
# let's check to see if it's in the scripts/resource dir (like when tab completed)
59+
[
60+
::Msf::Config.script_directory + ::File::SEPARATOR + "resource",
61+
::Msf::Config.user_script_directory + ::File::SEPARATOR + "resource"
62+
].each do |dir|
63+
res_path = dir + ::File::SEPARATOR + res
64+
if ::File.exist?(res_path)
65+
good_res = res_path
66+
break
67+
end
68+
end
69+
end
70+
if good_res
71+
driver.load_resource(good_res)
72+
else
73+
print_error("#{res} is not a valid resource file")
74+
next
75+
end
76+
end
77+
end
78+
79+
#
80+
# Tab completion for the resource command
81+
#
82+
# @param str [String] the string currently being typed before tab was hit
83+
# @param words [Array<String>] the previously completed words on the command line. words is always
84+
# at least 1 when tab completion has reached this stage since the command itself has been completed
85+
86+
def cmd_resource_tabs(str, words)
87+
tabs = []
88+
#return tabs if words.length > 1
89+
if ( str and str =~ /^#{Regexp.escape(File::SEPARATOR)}/ )
90+
# then you are probably specifying a full path so let's just use normal file completion
91+
return tab_complete_filenames(str,words)
92+
elsif (not words[1] or not words[1].match(/^\//))
93+
# then let's start tab completion in the scripts/resource directories
94+
begin
95+
[
96+
::Msf::Config.script_directory + File::SEPARATOR + "resource",
97+
::Msf::Config.user_script_directory + File::SEPARATOR + "resource",
98+
"."
99+
].each do |dir|
100+
next if not ::File.exist? dir
101+
tabs += ::Dir.new(dir).find_all { |e|
102+
path = dir + File::SEPARATOR + e
103+
::File.file?(path) and File.readable?(path)
104+
}
105+
end
106+
rescue Exception
107+
end
108+
else
109+
tabs += tab_complete_filenames(str,words)
110+
end
111+
return tabs
112+
end
113+
114+
def cmd_makerc_help
115+
print_line "Usage: makerc <output rc file>"
116+
print_line
117+
print_line "Save the commands executed since startup to the specified file."
118+
print_line
119+
end
120+
121+
#
122+
# Saves commands executed since the ui started to the specified msfrc file
123+
#
124+
def cmd_makerc(*args)
125+
if args.empty?
126+
cmd_makerc_help
127+
return false
128+
end
129+
driver.save_recent_history(args[0])
130+
end
131+
end
132+
133+
end
134+
end
135+
end
136+
end

lib/msf/ui/console/driver.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class Driver < Msf::Ui::Driver
3030
# Console Command Dispatchers to be loaded after the Core dispatcher.
3131
#
3232
CommandDispatchers = [
33-
CommandDispatcher::Jobs
33+
CommandDispatcher::Jobs,
34+
CommandDispatcher::Resource
3435
]
3536

3637
#

0 commit comments

Comments
 (0)