Module: Msf::Exploit::Xmpp

Defined in:
lib/msf/core/exploit/xmpp.rb

Overview

XMPP protocol helpers shared by remote exploit modules.

Defined Under Namespace

Classes: XmppError

Constant Summary collapse

XMPP_FEATURE_REGISTER =
'http://jabber.org/features/iq-register'
XMPP_IQ_REGISTER =
'jabber:iq:register'
XMPP_STARTTLS =
'urn:ietf:params:xml:ns:xmpp-tls'
XMPP_ERROR_CONDITIONS =
%w[bad-request conflict feature-not-implemented forbidden not-acceptable not-authorized service-unavailable].freeze
XMPP_RESPONSE_LIMIT =
131_072

Instance Method Summary collapse

Instance Method Details

#xmpp_complete_elements(response, name) ⇒ Object



76
77
78
79
# File 'lib/msf/core/exploit/xmpp.rb', line 76

def xmpp_complete_elements(response, name)
  escaped_name = Regexp.escape(name)
  response.scan(%r{<#{escaped_name}(?![A-Za-z0-9_.:-])(?:"[^"]*"|'[^']*'|[^'">/])*(?:/\s*>|>.*?</#{escaped_name}\s*>)}im)
end

#xmpp_connection ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/msf/core/exploit/xmpp.rb', line 152

def xmpp_connection
  nsock = Rex::Socket::Tcp.create(
    'PeerHost' => rhost,
    'PeerPort' => datastore['XMPPPORT'],
    'Proxies' => datastore['Proxies'],
    'Timeout' => xmpp_timeout,
    'Comm' => datastore['Comm'],
    'Context' => {
      'Msf' => framework,
      'MsfExploit' => self
    }
  )
  add_socket(nsock)
  nsock
end

#xmpp_disconnect(nsock) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/msf/core/exploit/xmpp.rb', line 168

def xmpp_disconnect(nsock)
  nsock.shutdown
rescue Rex::ConnectionError, IOError, SystemCallError, OpenSSL::SSL::SSLError
  nil
ensure
  begin
    nsock.close
  rescue Rex::ConnectionError, IOError, SystemCallError, OpenSSL::SSL::SSLError
    nil
  ensure
    remove_socket(nsock)
  end
end

#xmpp_domain ⇒ Object



25
26
27
# File 'lib/msf/core/exploit/xmpp.rb', line 25

def xmpp_domain
  datastore['XMPPDOMAIN']
end

#xmpp_element_attribute(element, name) ⇒ Object



125
126
127
128
129
130
# File 'lib/msf/core/exploit/xmpp.rb', line 125

def xmpp_element_attribute(element, name)
  root = xmpp_xml_root(element)
  return unless root&.name == 'iq'

  root[name]
end

#xmpp_error_condition(response) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/msf/core/exploit/xmpp.rb', line 144

def xmpp_error_condition(response)
  root = xmpp_xml_root(response)
  return unless root

  error = root.xpath('.//*[local-name()="error"]').first
  error&.element_children&.find { |child| XMPP_ERROR_CONDITIONS.include?(child.name) }&.name
end

#xmpp_feature_probe ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/msf/core/exploit/xmpp.rb', line 182

def xmpp_feature_probe
  nsock = xmpp_connection
  features = xmpp_stream_features(nsock)

  if xmpp_starttls_advertised?(features)
    xmpp_upgrade_to_tls(nsock)
    features = xmpp_stream_features(nsock)
  end

  features
ensure
  xmpp_disconnect(nsock) if nsock
end

#xmpp_iq_response(nsock, id) ⇒ Object



120
121
122
123
# File 'lib/msf/core/exploit/xmpp.rb', line 120

def xmpp_iq_response(nsock, id)
  response = xmpp_read_until(nsock, "XMPP IQ response #{id}") { |data| xmpp_iq_stanza(data, id) }
  xmpp_iq_stanza(response, id)
end

#xmpp_iq_stanza(response, id) ⇒ Object



132
133
134
135
136
# File 'lib/msf/core/exploit/xmpp.rb', line 132

def xmpp_iq_stanza(response, id)
  xmpp_complete_elements(response, 'iq').find do |stanza|
    xmpp_element_attribute(stanza, 'id') == id
  end
end

#xmpp_iq_type(response, id) ⇒ Object



138
139
140
141
142
# File 'lib/msf/core/exploit/xmpp.rb', line 138

def xmpp_iq_type(response, id)
  return unless xmpp_element_attribute(response, 'id') == id

  xmpp_element_attribute(response, 'type')
end

#xmpp_read_until(nsock, description) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/msf/core/exploit/xmpp.rb', line 34

def xmpp_read_until(nsock, description)
  response = String.new.b

  Timeout.timeout(xmpp_timeout) do
    loop do
      chunk = nsock.get_once(8192, 1)
      next unless chunk

      response << chunk
      raise XmppError, "#{description} exceeded #{XMPP_RESPONSE_LIMIT} bytes" if response.bytesize > XMPP_RESPONSE_LIMIT

      return response if yield response
    end
  end
rescue Timeout::Error
  raise XmppError, "Timed out waiting for #{description}"
end

#xmpp_registration_advertised?(features) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
# File 'lib/msf/core/exploit/xmpp.rb', line 62

def xmpp_registration_advertised?(features)
  xmpp_complete_elements(features, 'register').any? do |element|
    root = xmpp_xml_root(element)
    root&.name == 'register' && root.namespace&.href == XMPP_FEATURE_REGISTER
  end
end

#xmpp_starttls_advertised?(features) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
# File 'lib/msf/core/exploit/xmpp.rb', line 69

def xmpp_starttls_advertised?(features)
  xmpp_complete_elements(features, 'starttls').any? do |element|
    root = xmpp_xml_root(element)
    root&.name == 'starttls' && root.namespace&.href == XMPP_STARTTLS
  end
end

#xmpp_stream_features(nsock) ⇒ Object

Raises:



52
53
54
55
56
57
58
59
60
# File 'lib/msf/core/exploit/xmpp.rb', line 52

def xmpp_stream_features(nsock)
  nsock.put(xmpp_stream_open)
  response = xmpp_read_until(nsock, 'XMPP stream features') do |data|
    data.match?(%r{(?:</stream:features\s*>|<stream:features\b[^>]*/\s*>)}i) || data.include?('<stream:error')
  end
  raise XmppError, 'The server returned an XMPP stream error; verify XMPPDOMAIN' if response.include?('<stream:error')

  response
end

#xmpp_stream_open ⇒ Object



29
30
31
32
# File 'lib/msf/core/exploit/xmpp.rb', line 29

def xmpp_stream_open
  domain = CGI.escapeHTML(xmpp_domain)
  "<stream:stream to='#{domain}' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>"
end

#xmpp_timeout ⇒ Object

Raises:



18
19
20
21
22
23
# File 'lib/msf/core/exploit/xmpp.rb', line 18

def xmpp_timeout
  timeout = datastore['XMPPTimeout']
  raise XmppError, 'XMPPTimeout must be greater than zero' unless timeout&.positive?

  timeout
end

#xmpp_tls_response(response) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/msf/core/exploit/xmpp.rb', line 90

def xmpp_tls_response(response)
  elements = xmpp_complete_elements(response, 'proceed') + xmpp_complete_elements(response, 'failure')
  elements.select! do |element|
    root = xmpp_xml_root(element)
    %w[proceed failure].include?(root&.name) && root.namespace&.href == XMPP_STARTTLS
  end
  elements.min_by { |element| response.index(element) }
end

#xmpp_upgrade_to_tls(nsock) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/msf/core/exploit/xmpp.rb', line 99

def xmpp_upgrade_to_tls(nsock)
  nsock.put("<starttls xmlns='#{XMPP_STARTTLS}'/>")
  response = xmpp_read_until(nsock, 'XMPP STARTTLS response') do |data|
    xmpp_tls_response(data)
  end
  tls_response = xmpp_tls_response(response)
  raise XmppError, 'The server rejected XMPP STARTTLS' unless xmpp_xml_root(tls_response)&.name == 'proceed'

  context = OpenSSL::SSL::SSLContext.new
  context.verify_mode = OpenSSL::SSL::VERIFY_NONE
  tls_socket = OpenSSL::SSL::SSLSocket.new(nsock, context)
  tls_socket.hostname = xmpp_domain if tls_socket.respond_to?(:hostname=)
  Timeout.timeout(xmpp_timeout) { tls_socket.connect }

  nsock.extend(Rex::Socket::SslTcp)
  nsock.sslctx = context
  nsock.sslsock = tls_socket
rescue OpenSSL::SSL::SSLError, Timeout::Error => e
  raise XmppError, "XMPP STARTTLS negotiation failed: #{e.message}"
end

#xmpp_xml_root(element) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/msf/core/exploit/xmpp.rb', line 81

def xmpp_xml_root(element)
  return unless element

  fragment = Nokogiri::XML::DocumentFragment.parse(element) { |config| config.strict.nonet }
  fragment.element_children.first
rescue Nokogiri::XML::SyntaxError
  nil
end