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/rex/post/smb/ui/console/command_dispatcher.rb
Views: 11766
1
# -*- coding: binary -*-
2
3
require 'English'
4
require 'rex/ui/text/dispatcher_shell'
5
6
module Rex
7
module Post
8
module SMB
9
module Ui
10
###
11
#
12
# Base class for all command dispatchers within the SMB console user
13
# interface.
14
#
15
###
16
module Console::CommandDispatcher
17
include Msf::Ui::Console::CommandDispatcher::Session
18
19
#
20
# Initializes an instance of the core command set using the supplied session and client
21
# for interactivity.
22
#
23
# @param [Rex::Post::SMB::Ui::Console] console
24
def initialize(console)
25
super
26
@msf_loaded = nil
27
@filtered_commands = []
28
end
29
30
#
31
# Returns the smb client context.
32
#
33
# @return [RubySMB::Client]
34
def client
35
console = shell
36
console.client
37
end
38
39
#
40
# Returns the smb simple client.
41
#
42
# @return [Rex::Proto::SMB::SimpleClient]
43
def simple_client
44
shell.simple_client
45
end
46
47
#
48
# Returns the smb session context.
49
#
50
# @return [Msf::Sessions::SMB]
51
def session
52
console = shell
53
console.session
54
end
55
56
#
57
# Returns the active share
58
#
59
# @return [RubySMB::SMB2::Tree]
60
def active_share
61
console = shell
62
console.active_share
63
end
64
65
#
66
# Returns the commands that meet the requirements
67
#
68
def filter_commands(all, reqs)
69
all.delete_if do |cmd, _desc|
70
if reqs[cmd]&.any? { |req| !client.commands.include?(req) }
71
@filtered_commands << cmd
72
true
73
end
74
end
75
end
76
77
def unknown_command(cmd, line)
78
if @filtered_commands.include?(cmd)
79
print_error("The \"#{cmd}\" command is not supported by this session type (#{session.session_type})")
80
return :handled
81
end
82
83
super
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, 'smb_session')
92
end
93
94
#
95
# Returns true if the client has a framework object.
96
#
97
# Used for firing framework session events
98
#
99
def msf_loaded?
100
return @msf_loaded unless @msf_loaded.nil?
101
102
# if we get here we must not have initialized yet
103
104
@msf_loaded = !session.framework.nil?
105
@msf_loaded
106
end
107
108
#
109
# Log that an error occurred.
110
#
111
def log_error(msg)
112
print_error(msg)
113
114
elog(msg, 'smb')
115
116
dlog("Call stack:\n#{$ERROR_POSITION.join("\n")}", 'smb')
117
end
118
end
119
end
120
end
121
end
122
end
123
124