Contact Us!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/rex/proto/ms_nrtp/client.rb
Views: 18569
1
class Rex::Proto::MsNrtp::Client
2
3
require 'rex/stopwatch'
4
require 'rex/proto/ms_nrtp/ms_nrtp_message'
5
6
include Rex::Proto::MsNrtp
7
8
# @return [String] The MS-NRTP server host.
9
attr_reader :host
10
11
# @return [Integer] The S-NRTP server port.
12
attr_reader :port
13
14
# @return [String] The server resource component of the URI string.
15
attr_reader :resource
16
17
# @return [Boolean] Whether or not SSL is used for the connection.
18
attr_reader :ssl
19
20
# @return [Rex::Socket::Comm] An optional, explicit object to use for creating the connection.
21
attr_reader :comm
22
23
# @!attribute timeout
24
# @return [Integer] The communication timeout in seconds.
25
attr_accessor :timeout
26
27
# @param [String] host The MS-NRTP server host.
28
# @param [Integer,NilClass] port The MS-NRTP server port or nil for automatic based on ssl.
29
# @param [Boolean] ssl Whether or not SSL is used for the connection.
30
# @param [String] ssl_version The SSL version to use.
31
# @param [Rex::Socket::Comm] comm An optional, explicit object to use for creating the connection.
32
# @param [Integer] timeout The communication timeout in seconds.
33
def initialize(host, port, resource, context: {}, ssl: false, ssl_version: nil, comm: nil, timeout: 10)
34
@host = host
35
@port = port
36
@resource = resource
37
@context = context
38
@ssl = ssl
39
@ssl_version = ssl_version
40
@comm = comm
41
@timeout = timeout
42
end
43
44
# Establish the connection to the remote server.
45
#
46
# @param [Integer] t An explicit timeout to use for the connection otherwise the default will be used.
47
# @return [NilClass]
48
def connect(t = -1)
49
timeout = (t.nil? or t == -1) ? @timeout : t
50
51
@conn = Rex::Socket::Tcp.create(
52
'PeerHost' => @host,
53
'PeerPort' => @port.to_i,
54
'Context' => @context,
55
'SSL' => @ssl,
56
'SSLVersion' => @ssl_version,
57
'Timeout' => timeout,
58
'Comm' => @comm
59
)
60
61
nil
62
end
63
64
# Close the connection to the remote server.
65
#
66
# @return [NilClass]
67
def close
68
if @conn && !@conn.closed?
69
@conn.shutdown
70
@conn.close
71
end
72
73
@conn = nil
74
end
75
76
def recv
77
remaining = @timeout
78
message, elapsed_time = Rex::Stopwatch.elapsed_time do
79
::Timeout.timeout(remaining) do
80
MsNrtpMessage.read(@conn)
81
end
82
end
83
return nil unless message.operation_type == Enums::OperationTypeEnum[:Reply] && message.content_length?
84
85
remaining -= elapsed_time
86
body = ''
87
while body.length < message.content_length
88
chunk, elapsed_time = Rex::Stopwatch.elapsed_time do
89
@conn.read(message.content_length - body.length, remaining)
90
end
91
remaining -= elapsed_time
92
body << chunk
93
end
94
95
body
96
end
97
98
def send(data, content_type)
99
message = MsNrtpMessage.new(
100
content_length: data.length,
101
headers: [
102
{ token: MsNrtpHeader::MsNrtpHeaderUri::TOKEN, header: { uri_value: "tcp://#{Rex::Socket.to_authority(@host, @port)}/#{@resource}" } },
103
{ token: MsNrtpHeader::MsNrtpHeaderContentType::TOKEN, header: { content_type_value: content_type } },
104
{ token: MsNrtpHeader::MsNrtpHeaderEnd::TOKEN }
105
]
106
)
107
@conn.put(message.to_binary_s + data)
108
end
109
110
def send_recv(data, content_type)
111
send(data, content_type)
112
recv
113
end
114
115
def send_binary(serialized_stream)
116
send(serialized_stream.to_binary_s, 'application/octet-stream'.encode('UTF-8'))
117
end
118
end
119
120