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.rb
Views: 11783
1
# -*- coding: binary -*-
2
3
4
module Msf
5
module Ui
6
module Console
7
8
###
9
#
10
# The common command dispatcher base class that is shared for component-specific
11
# command dispatching.
12
#
13
###
14
module CommandDispatcher
15
16
include Rex::Ui::Text::DispatcherShell::CommandDispatcher
17
18
#
19
# Initializes a command dispatcher instance.
20
#
21
def initialize(driver)
22
super
23
24
self.driver = driver
25
self.driver.on_command_proc = Proc.new { |command| framework.events.on_ui_command(command) }
26
end
27
28
#
29
# Returns the framework instance associated with this command dispatcher.
30
#
31
def framework
32
return driver.framework
33
end
34
35
#
36
# Returns the active module if one has been selected, otherwise nil is
37
# returned.
38
#
39
def active_module
40
driver.active_module
41
end
42
43
#
44
# Sets the active module for this driver instance.
45
#
46
def active_module=(mod)
47
driver.active_module = mod
48
end
49
50
#
51
# Returns the active session if one has been selected, otherwise nil is
52
# returned.
53
#
54
def active_session
55
driver.active_session
56
end
57
58
#
59
# Sets the active session for this driver instance.
60
#
61
def active_session=(mod)
62
driver.active_session = mod
63
end
64
65
#
66
# Logs an error message to the screen and the log file. The callstack is
67
# also printed.
68
#
69
def log_error(err)
70
print_error(err)
71
72
wlog(err)
73
74
# If it's a syntax error, log the call stack that it originated from.
75
dlog("Call stack:\n#{$@.join("\n")}", 'core', LEV_1)
76
end
77
78
#
79
# Load the configuration required for this CommandDispatcher, configuring
80
# any internal state as required.
81
#
82
def load_config(_path = nil)
83
# noop
84
end
85
86
#
87
# Return the subdir of the `documentation/` directory that should be used
88
# to find usage documentation
89
#
90
def docs_dir
91
File.join(super, 'msfconsole')
92
end
93
94
#
95
# Generate an array of job or session IDs from a given range String.
96
# Always returns an Array unless an incorrect input is given.
97
# In that case, the result will always be nil, even if only one argument is incorrect.
98
#
99
# @param id_list [String] Range or list description such as 1-5 or 1,3,5 etc
100
# @return [Array<String>, nil] Representing the range
101
def build_range_array(id_list)
102
item_list = []
103
unless id_list.blank?
104
temp_list = id_list.split(',')
105
temp_list.each do |ele|
106
return if ele.count('-') > 1 # Eg. 'sessions -u -1-,5', incorrect syntax
107
return if ele.last == '-' # Last item of array is a '-', resulting in an incomplete range
108
return if ele.first == '.' || ele.last == '.' #Eg. 'sessions -u .1..' or 'sessions -u ..
109
return unless ele =~ (/^\d+((\.\.|-)\d+)?$/) || ele =~ (/^-?\d+$/) # Not a number or range
110
111
# Check if the item is negative, as this will not always be a range
112
if ele =~ (/^-?\d+$/) && ele.to_i < 0 # if ele is a single negative number
113
item_list.push(ele.to_i)
114
elsif ele.include? '-'
115
temp_array = (ele.split("-").inject { |s, e| s.to_i..e.to_i }).to_a
116
item_list.concat(temp_array)
117
elsif ele.include? '..'
118
temp_array = (ele.split("..").inject { |s, e| s.to_i..e.to_i }).to_a
119
item_list.concat(temp_array)
120
else
121
item_list.push(ele.to_i)
122
end
123
end
124
end
125
126
item_list.uniq.sort
127
end
128
129
#
130
# Remove lines with specific substring
131
#
132
# @param text [String] Block of text to search over
133
# @param to_match [String] String that when found, causes the whole line to
134
# be removed, including trailing "\n" if present
135
# @return [String] Text sans lines containing to_match
136
#
137
def remove_lines(text, to_match)
138
to_match = Regexp.escape(to_match)
139
text.gsub(/^.*(#{to_match}).*(#{Regexp.escape $/})?/, '')
140
end
141
142
#
143
# The driver that this command dispatcher is associated with.
144
#
145
attr_accessor :driver
146
147
end
148
end end end
149
150