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/net/winrm/connection.rb
Views: 11779
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
require 'winrm'
7
require 'net/winrm/stdin_shell'
8
require 'net/winrm/rex_http_transport'
9
10
module Net
11
module MsfWinRM
12
# Connection to a WinRM service, using Rex sockets
13
class RexWinRMConnection < WinRM::Connection
14
# Factory class to create a shell of the appropriate type.
15
# Subclassed to be able to provide a StdinShell
16
class ShellFactory < WinRM::Shells::ShellFactory
17
def create_shell(shell_type, shell_opts = {})
18
args = [
19
@connection_opts,
20
@transport,
21
@logger,
22
shell_opts
23
]
24
return StdinShell.new(*args) if shell_type == :stdin
25
26
super(shell_type, shell_opts)
27
end
28
end
29
30
# Creates a WinRM transport, subclassed to support Rex sockets
31
class TransportFactory < WinRM::HTTP::TransportFactory
32
def create_transport(connection_opts)
33
raise NotImplementedError unless connection_opts[:transport] == :rexhttp
34
35
super
36
end
37
38
private
39
40
def init_rexhttp_transport(opts)
41
RexHttpTransport.new(opts)
42
end
43
end
44
45
# Provide an adapter for logging WinRM module messages to the MSF log
46
class WinRMProxyLogger
47
def error(msg)
48
elog(msg, 'winrm')
49
end
50
51
def warn(msg)
52
wlog(msg, 'winrm')
53
end
54
55
def info(msg)
56
ilog(msg, 'winrm')
57
end
58
59
def debug(msg)
60
dlog(msg, 'winrm')
61
end
62
end
63
64
def shell_factory
65
@shell_factory ||= ShellFactory.new(@connection_opts, transport, logger)
66
end
67
68
def transport
69
@transport ||= begin
70
transport_factory = TransportFactory.new
71
transport_factory.create_transport(@connection_opts)
72
end
73
end
74
75
def configure_logger
76
@logger = WinRMProxyLogger.new
77
end
78
end
79
end
80
end
81
82