CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/rex/post/meterpreter/ui/console/command_dispatcher.rb
Views: 1904
1
# -*- coding: binary -*-
2
3
module Rex
4
module Post
5
module Meterpreter
6
module Ui
7
8
###
9
#
10
# Base class for all command dispatchers within the meterpreter console user
11
# interface.
12
#
13
###
14
module Console::CommandDispatcher
15
16
include Msf::Ui::Console::CommandDispatcher::Session
17
18
#
19
# The hash of file names to class names after a module has already been
20
# loaded once on the client side.
21
#
22
@@file_hash = {}
23
24
#
25
# Checks the file name to hash association to see if the module being
26
# requested has already been loaded once.
27
#
28
def self.check_hash(name)
29
@@file_hash[name]
30
end
31
32
#
33
# Sets the file path to class name association for future reference.
34
#
35
def self.set_hash(name, klass)
36
@@file_hash[name] = klass
37
end
38
39
def initialize(shell)
40
@msf_loaded = nil
41
@filtered_commands = []
42
super
43
end
44
45
#
46
# Returns the meterpreter client context.
47
#
48
def client
49
shell.client
50
end
51
52
# A meterpreter session *is* a client but for the smb session it *has* a (ruby smb) client
53
# adding this here for parity with the smb session
54
def session
55
shell.client
56
end
57
58
#
59
# Returns the commands that meet the requirements
60
#
61
def filter_commands(all, reqs)
62
all.delete_if do |cmd, _desc|
63
if reqs[cmd]&.any? { |req| !client.commands.include?(req) }
64
@filtered_commands << cmd
65
true
66
end
67
end
68
end
69
70
def unknown_command(cmd, line)
71
if @filtered_commands.include?(cmd)
72
print_error("The \"#{cmd}\" command is not supported by this Meterpreter type (#{client.session_type})")
73
return :handled
74
end
75
76
super
77
end
78
79
#
80
# Return the subdir of the `documentation/` directory that should be used
81
# to find usage documentation
82
#
83
def docs_dir
84
File.join(super, 'meterpreter')
85
end
86
87
#
88
# Returns true if the client has a framework object.
89
#
90
# Used for firing framework session events
91
#
92
def msf_loaded?
93
return @msf_loaded unless @msf_loaded.nil?
94
# if we get here we must not have initialized yet
95
96
@msf_loaded = !!(client.framework)
97
@msf_loaded
98
end
99
100
#
101
# Log that an error occurred.
102
#
103
def log_error(msg)
104
print_error(msg)
105
106
elog(msg, 'meterpreter')
107
108
dlog("Call stack:\n#{$@.join("\n")}", 'meterpreter')
109
end
110
111
end
112
113
end
114
end
115
end
116
end
117
118