Skip to content

Commit 3e27fd3

Browse files
committed
Land rapid7#8517, CommandDispatcher::Common
Also fixes jobs -i.
2 parents a052ee4 + 5969245 commit 3e27fd3

File tree

7 files changed

+156
-183
lines changed

7 files changed

+156
-183
lines changed

lib/msf/ui/console/command_dispatcher.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: binary -*-
2-
2+
require 'msf/ui/console/command_dispatcher/common'
33
module Msf
44
module Ui
55
module Console
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# -*- coding: binary -*-
2+
3+
require 'rexml/document'
4+
require 'rex/parser/nmap_xml'
5+
require 'msf/core/db_export'
6+
7+
module Msf
8+
module Ui
9+
module Console
10+
module CommandDispatcher
11+
12+
# These are functions that are used in two or more command dispatchers.
13+
14+
module Common
15+
16+
# Parse +arg+ into a {Rex::Socket::RangeWalker} and append the result into +host_ranges+
17+
#
18+
# @note This modifies +host_ranges+ in place
19+
#
20+
# @param arg [String] The thing to turn into a RangeWalker
21+
# @param host_ranges [Array] The array of ranges to append
22+
# @param required [Boolean] Whether an empty +arg+ should be an error
23+
# @return [Boolean] true if parsing was successful or false otherwise
24+
def arg_host_range(arg, host_ranges, required=false)
25+
if (!arg and required)
26+
print_error("Missing required host argument")
27+
return false
28+
end
29+
begin
30+
rw = Rex::Socket::RangeWalker.new(arg)
31+
rescue
32+
print_error("Invalid host parameter, #{arg}.")
33+
return false
34+
end
35+
36+
if rw.valid?
37+
host_ranges << rw
38+
else
39+
print_error("Invalid host parameter, #{arg}.")
40+
return false
41+
end
42+
return true
43+
end
44+
45+
#
46+
# Parse +arg+ into an array of ports and append the result into +port_ranges+
47+
#
48+
# Returns true if parsing was successful or nil otherwise.
49+
#
50+
# NOTE: This modifies +port_ranges+
51+
#
52+
def arg_port_range(arg, port_ranges, required=false)
53+
if (!arg and required)
54+
print_error("Argument required for -p")
55+
return
56+
end
57+
begin
58+
port_ranges << Rex::Socket.portspec_to_portlist(arg)
59+
rescue
60+
print_error("Invalid port parameter, #{arg}.")
61+
return
62+
end
63+
return true
64+
end
65+
66+
#
67+
# Set RHOSTS in the +active_module+'s (or global if none) datastore from an array of addresses
68+
#
69+
# This stores all the addresses to a temporary file and utilizes the
70+
# <pre>file:/tmp/filename</pre> syntax to confer the addrs. +rhosts+
71+
# should be an Array. NOTE: the temporary file is *not* deleted
72+
# automatically.
73+
#
74+
def set_rhosts_from_addrs(rhosts)
75+
if rhosts.empty?
76+
print_status("The list is empty, cowardly refusing to set RHOSTS")
77+
return
78+
end
79+
if active_module
80+
mydatastore = active_module.datastore
81+
else
82+
# if there is no module in use set the list to the global variable
83+
mydatastore = self.framework.datastore
84+
end
85+
86+
if rhosts.length > 5
87+
# Lots of hosts makes 'show options' wrap which is difficult to
88+
# read, store to a temp file
89+
rhosts_file = Rex::Quickfile.new("msf-db-rhosts-")
90+
mydatastore['RHOSTS'] = 'file:'+rhosts_file.path
91+
# create the output file and assign it to the RHOSTS variable
92+
rhosts_file.write(rhosts.join("\n")+"\n")
93+
rhosts_file.close
94+
else
95+
# For short lists, just set it directly
96+
mydatastore['RHOSTS'] = rhosts.join(" ")
97+
end
98+
99+
print_line "RHOSTS => #{mydatastore['RHOSTS']}"
100+
print_line
101+
end
102+
103+
def show_options(mod) # :nodoc:
104+
mod_opt = Serializer::ReadableText.dump_options(mod, ' ')
105+
print("\nModule options (#{mod.fullname}):\n\n#{mod_opt}\n") if (mod_opt and mod_opt.length > 0)
106+
107+
# If it's an exploit and a payload is defined, create it and
108+
# display the payload's options
109+
if (mod.exploit? and mod.datastore['PAYLOAD'])
110+
p = framework.payloads.create(mod.datastore['PAYLOAD'])
111+
112+
if (!p)
113+
print_error("Invalid payload defined: #{mod.datastore['PAYLOAD']}\n")
114+
return
115+
end
116+
117+
p.share_datastore(mod.datastore)
118+
119+
if (p)
120+
p_opt = Serializer::ReadableText.dump_options(p, ' ')
121+
print("\nPayload options (#{mod.datastore['PAYLOAD']}):\n\n#{p_opt}\n") if (p_opt and p_opt.length > 0)
122+
end
123+
end
124+
125+
# Print the selected target
126+
if (mod.exploit? and mod.target)
127+
mod_targ = Serializer::ReadableText.dump_exploit_target(mod, ' ')
128+
print("\nExploit target:\n\n#{mod_targ}\n") if (mod_targ and mod_targ.length > 0)
129+
end
130+
131+
# Print the selected action
132+
if mod.kind_of?(Msf::Module::HasActions) && mod.action
133+
mod_action = Serializer::ReadableText.dump_module_action(mod, ' ')
134+
print("\n#{mod.type.capitalize} action:\n\n#{mod_action}\n") if (mod_action and mod_action.length > 0)
135+
end
136+
137+
# Uncomment this line if u want target like msf2 format
138+
#print("\nTarget: #{mod.target.name}\n\n")
139+
end
140+
141+
142+
end
143+
144+
end
145+
end
146+
end
147+
end

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

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
require 'rexml/document'
44
require 'rex/parser/nmap_xml'
55
require 'msf/core/db_export'
6-
require 'msf/ui/console/command_dispatcher/db_common'
76

87
module Msf
98
module Ui
@@ -15,7 +14,7 @@ class Creds
1514

1615
include Msf::Ui::Console::CommandDispatcher
1716
include Metasploit::Credential::Creation
18-
include Msf::Ui::Console::CommandDispatcher::DbCommon
17+
include Msf::Ui::Console::CommandDispatcher::Common
1918

2019
#
2120
# The dispatcher's name.
@@ -53,39 +52,6 @@ def active?
5352
true
5453
end
5554

56-
#
57-
# Miscellaneous option helpers
58-
#
59-
60-
# Parse +arg+ into a {Rex::Socket::RangeWalker} and append the result into +host_ranges+
61-
#
62-
# @note This modifies +host_ranges+ in place
63-
#
64-
# @param arg [String] The thing to turn into a RangeWalker
65-
# @param host_ranges [Array] The array of ranges to append
66-
# @param required [Boolean] Whether an empty +arg+ should be an error
67-
# @return [Boolean] true if parsing was successful or false otherwise
68-
def arg_host_range(arg, host_ranges, required=false)
69-
if (!arg and required)
70-
print_error("Missing required host argument")
71-
return false
72-
end
73-
begin
74-
rw = Rex::Socket::RangeWalker.new(arg)
75-
rescue
76-
print_error("Invalid host parameter, #{arg}.")
77-
return false
78-
end
79-
80-
if rw.valid?
81-
host_ranges << rw
82-
else
83-
print_error("Invalid host parameter, #{arg}.")
84-
return false
85-
end
86-
return true
87-
end
88-
8955
#
9056
# Can return return active or all, on a certain host or range, on a
9157
# certain port or range, and/or on a service name.
@@ -118,6 +84,9 @@ def cmd_creds(*args)
11884
# TODO: this needs to be cleaned up to use the new syntax
11985
#
12086
def cmd_creds_help
87+
require 'pry'
88+
binding.pry
89+
12190
print_line
12291
print_line "With no sub-command, list credentials. If an address range is"
12392
print_line "given, show only credentials with logins on hosts within that"

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

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
require 'rexml/document'
44
require 'rex/parser/nmap_xml'
55
require 'msf/core/db_export'
6-
require 'msf/ui/console/command_dispatcher/db_common'
76

87
module Msf
98
module Ui
@@ -15,7 +14,7 @@ class Db
1514
require 'tempfile'
1615

1716
include Msf::Ui::Console::CommandDispatcher
18-
include Msf::Ui::Console::CommandDispatcher::DbCommon
17+
include Msf::Ui::Console::CommandDispatcher::Common
1918

2019
#
2120
# The dispatcher's name.
@@ -1809,55 +1808,6 @@ def db_parse_db_uri_postgresql(path)
18091808
# Miscellaneous option helpers
18101809
#
18111810

1812-
# Parse +arg+ into a {Rex::Socket::RangeWalker} and append the result into +host_ranges+
1813-
#
1814-
# @note This modifies +host_ranges+ in place
1815-
#
1816-
# @param arg [String] The thing to turn into a RangeWalker
1817-
# @param host_ranges [Array] The array of ranges to append
1818-
# @param required [Boolean] Whether an empty +arg+ should be an error
1819-
# @return [Boolean] true if parsing was successful or false otherwise
1820-
def arg_host_range(arg, host_ranges, required=false)
1821-
if (!arg and required)
1822-
print_error("Missing required host argument")
1823-
return false
1824-
end
1825-
begin
1826-
rw = Rex::Socket::RangeWalker.new(arg)
1827-
rescue
1828-
print_error("Invalid host parameter, #{arg}.")
1829-
return false
1830-
end
1831-
1832-
if rw.valid?
1833-
host_ranges << rw
1834-
else
1835-
print_error("Invalid host parameter, #{arg}.")
1836-
return false
1837-
end
1838-
return true
1839-
end
1840-
1841-
#
1842-
# Parse +arg+ into an array of ports and append the result into +port_ranges+
1843-
#
1844-
# Returns true if parsing was successful or nil otherwise.
1845-
#
1846-
# NOTE: This modifies +port_ranges+
1847-
#
1848-
def arg_port_range(arg, port_ranges, required=false)
1849-
if (!arg and required)
1850-
print_error("Argument required for -p")
1851-
return
1852-
end
1853-
begin
1854-
port_ranges << Rex::Socket.portspec_to_portlist(arg)
1855-
rescue
1856-
print_error("Invalid port parameter, #{arg}.")
1857-
return
1858-
end
1859-
return true
1860-
end
18611811

18621812
#
18631813
# Takes +host_ranges+, an Array of RangeWalkers, and chunks it up into

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

Lines changed: 0 additions & 57 deletions
This file was deleted.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module CommandDispatcher
1616
#
1717
class Jobs
1818
include Msf::Ui::Console::CommandDispatcher
19+
include Msf::Ui::Console::CommandDispatcher::Common
1920

2021
@@handler_opts = Rex::Parser::Arguments.new(
2122
"-h" => [ false, "Help Banner"],
@@ -164,7 +165,7 @@ def cmd_jobs(*args)
164165
job = framework.jobs[job_id.to_s]
165166
mod = job.ctx[0]
166167

167-
output = '\n'
168+
output = "\n"
168169
output += "Name: #{mod.name}"
169170
output += ", started at #{job.start_time}" if job.start_time
170171
print_line(output)

0 commit comments

Comments
 (0)