/usr/share/honeyd/scripts/kuang2.pl is in honeyd-common 1.5c-8ubuntu1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 | #! /usr/bin/perl -T
#-*-Perl-*-
#
# $Id: kuang2.pl,v 1.1 2004/12/31 18:54:22 provos Exp $
#
# kuang2.pl -- Honeyd module that emulates the backdoor installed by the
# Kuang2 virus.
#
# Copyright (c) 2003, 2004 Klaus Steding-Jessen
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# - Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
######################################################################
=head1 NAME
kuang2.pl -- Honeyd module that emulates the backdoor installed by the Kuang2 virus.
=head1 SYNOPSIS
kuang2.pl [-Vh] [-d] [-f file]
=head1 DESCRIPTION
Start kuang2.pl as a Honeyd service.
The options are as follows:
=over 6
=item -V
Display version number and exit.
=item -h
Display this help and exit.
=item -d
debug mode.
=item -f file
configuration file.
=back
=head1 SEE ALSO
Honeyd.
=head1 AUTHOR
Klaus Steding-Jessen <jessen@nic.br>
=head1 AVAILABILITY
The latest version of kuang2.pl is available from
http://www.honeynet.org.br/tools/
=cut
######################################################################
(my $program_name = $0) =~ s@.*/@@;
use strict;
use warnings;
use Getopt::Std;
use Fcntl qw(:flock);
use POSIX qw(strftime);
use File::Path;
use File::Basename;
unless (eval "use Digest::SHA1; 1") {
die "$program_name: Please install Digest::SHA1\n";
}
######################################################################
my %option = ();
getopts('Vhdf:n', \%option);
my $version='0.2';
# unbuffered output.
$| = 1;
# set PATH.
$ENV{'PATH'} = '/bin:/usr/bin:/usr/sbin';
######################################################################
# configuration defaults -- can be changed via configuration file.
# parameter, regex value and default value.
my %checksum = ();
my %conf = (
"logdir" => {
regex => '[\w\.-\/]+',
value => '/var/kuang2' },
"timeout" => {
regex => '\d+',
value => 30 },
"num_drives" => {
regex => '\d+',
value => 1 },
"computer_name" => {
regex => '[\w\s]+',
value => 'MY COMPUTER' },
"max_upload_size" => {
regex => '\d+',
value => 15728640 },
);
######################################################################
# kuang2 constants.
my $K2_BUFFER_SIZE = 1024;
my $K2_HELO = 0x324B4F59;
my $K2_ERROR = 0x52525245;
my $K2_DONE = 0x454E4F44;
my $K2_QUIT = 0x54495551;
my $K2_DELETE_FILE = 0x464C4544;
my $K2_RUN_FILE = 0x464E5552;
my $K2_FOLDER_INFO = 0x464E4946;
my $K2_DOWNLOAD_FILE = 0x464E5744;
my $K2_UPLOAD_FILE = 0x46445055;
my $K2_UPLOAD_FILE_2 = 0x0000687f;
my $K2_UPLOAD_FILE_3 = 0x00007620;
my $K2_UPLOAD_FILE_4 = 0x00004820;
######################################################################
# display usage if requested.
show_usage() if ($option{'h'});
# display version if requested.
show_version() if ($option{'V'});
# parse config options from the configuration file, if requested.
if (defined($option{'f'})) {
my ($confref) = process_config_file($option{'f'}, \%conf);
%conf = %{$confref};
}
# logfile.
my $logfile = $conf{'logdir'}{'value'} . '/' . 'logfile';
######################################################################
# get srchost, dsthost, srcport and dstport from the environment
# variables set by honeyd.
my $srchost = '127.0.0.1';
if (defined($ENV{'HONEYD_IP_SRC'}) &&
$ENV{'HONEYD_IP_SRC'} =~ /^(\d+\.\d+\.\d+\.\d+)$/) {
$srchost = $1;
}
my $dsthost = '127.0.0.1';
if (defined($ENV{'HONEYD_IP_DST'}) &&
$ENV{'HONEYD_IP_DST'} =~ /^(\d+\.\d+\.\d+\.\d+)$/) {
$dsthost = $1;
}
my $srcport = 0;
if (defined($ENV{'HONEYD_SRC_PORT'}) &&
$ENV{'HONEYD_SRC_PORT'} =~ /^(\d+)$/) {
$srcport = $1;
}
my $dstport = 0;
if (defined($ENV{'HONEYD_DST_PORT'}) &&
$ENV{'HONEYD_DST_PORT'} =~ /^(\d+)$/) {
$dstport = $1;
}
######################################################################
# main.
# install SIGALRM handler.
$SIG{ALRM} = sub {
my $msg = sprintf("timed out after %d seconds", $conf{'timeout'}{'value'});
logentry($logfile, $msg);
die("$program_name: $msg\n");
exit 0;
};
alarm $conf{'timeout'}{'value'};
logentry($logfile, sprintf("connection from %s:%d to %s:%d",
$srchost, $srcport, $dsthost, $dstport));
# send the initial K2_HELO to the client.
my $name_size = $K2_BUFFER_SIZE - 8;
my $nwrite = syswrite(STDOUT, pack("IIZ$name_size", $K2_HELO,
$conf{'num_drives'}{'value'},
$conf{'computer_name'}{'value'}));
logentry($logfile, "DEBUG: K2_HELO sent to client")
if ($option{'d'});
# read stdin from honeyd -- loop reading commands from the client.
my $nread;
while ($nread = sysread(STDIN, my $buffer, $K2_BUFFER_SIZE)) {
# debug.
#logentry($logfile, "DEBUG: $nread byte(s) read") if ($option{'d'});
my $cmd = (unpack("I*", $buffer));
if (defined($cmd)) {
#logentry($logfile, sprintf("DEBUG: cmd: 0x%02X", $cmd))
# if ($option{'d'});
} else {
logentry($logfile, sprintf("data: %s", data2hex($buffer)));
next;
}
# process commands sent from the kuang2 client.
if ($cmd == $K2_UPLOAD_FILE) {
k2_upload_file($buffer, 0);
} elsif ($cmd == $K2_DOWNLOAD_FILE) {
k2_download_file($buffer);
} elsif ($cmd == $K2_QUIT) {
logentry($logfile, "K2_QUIT received, exiting");
exit 0;
} elsif ($cmd == $K2_DELETE_FILE) {
k2_delete_file($buffer);
} elsif ($cmd == $K2_RUN_FILE) {
k2_run_file($buffer);
} elsif ($cmd == $K2_FOLDER_INFO) {
k2_folder_info($buffer);
} elsif (($cmd == $K2_UPLOAD_FILE_2) ||
($cmd == $K2_UPLOAD_FILE_3) ||
($cmd == $K2_UPLOAD_FILE_4)) {
# different upload procotol.
k2_upload_file($buffer, -1);
} else {
# unknown command.
logentry($logfile, sprintf("unknown command: %s", data2hex($buffer)));
my $nwrite = syswrite(STDOUT, pack("I", $K2_ERROR));
logentry($logfile, "DEBUG: K2_ERROR sent to client") if ($option{'d'});
}
alarm $conf{'timeout'}{'value'};
}
logentry($logfile, "ERROR: sysread: $!") if (!defined($nread));
alarm 0;
logentry($logfile, "exiting");
exit 0;
######################################################################
# handle upload file requests.
sub k2_upload_file {
my ($buffer, $flag) = @_;
my ($cmd, $filesize, $filename);
if ($flag == 0) {
($cmd, $filesize, $filename) = unpack("IIZ*", $buffer);
} else {
$filesize = -1;
($cmd, $filename) = unpack("IZ*", $buffer);
}
# check if unpack() succeeded.
if (!defined($cmd) || !defined($filesize) || !defined($filename)) {
my $msg = "unpack() error";
logentry($logfile, $msg);
die("$program_name: $msg\n");
}
# untaint filename.
if ($filename =~ /^([\w\\\/\.:]+)$/) {
$filename = $1;
}
if ($flag == 0) {
# we only accept files >0 and <= MAX_UPLOAD_FILESIZE bytes long.
if (!(($filesize > 0) &&
($filesize <= $conf{'max_upload_size'}{'value'}))) {
my $nwrite = syswrite(STDOUT, pack("I", $K2_ERROR));
logentry($logfile, "DEBUG: K2_ERROR sent to client")
if ($option{'d'});
my $msg = sprintf("illegal file size: %ld byte(s)", $filesize);
logentry($logfile, $msg);
die("$program_name: $msg\n");
}
}
if ($flag == 0) {
logentry($logfile,
sprintf("cmd received: K2_UPLOAD_FILE, file: %s, size: %d byte(s)",
quotemeta($filename), $filesize));
} else {
logentry($logfile,
sprintf("cmd received: K2_UPLOAD_FILE (ALT), file: %s",
quotemeta($filename)));
}
# ack the K2_UPLOAD_FILE request.
my $nwrite = syswrite(STDOUT, pack("I", $K2_DONE));
logentry($logfile, "DEBUG: K2_DONE sent to client") if ($option{'d'});
# keep only the basename component and also remove the drive part
# (c:\\, d:\\, etc) on MSDOS/Win filenames.
fileparse_set_fstype("MSWin32");
$filename = basename($filename);
# create directory hierarchy (adapted from smtp.pl).
my $srchostdir = $srchost;
$srchostdir =~ s/\./\//g;
my $upload_dir = "$conf{'logdir'}{'value'}/$srchostdir/$srcport";
if (! -d "$upload_dir" ) {
eval { mkpath($upload_dir) };
if ($@) {
logentry($logfile, "ERROR: $upload_dir: $@");
die("$program_name: $upload_dir: $@");
}
}
my $upload_file = "$upload_dir/$filename";
if (!defined(open(UPLOAD_FILE, ">$upload_file"))) {
logentry($logfile, "ERROR: $upload_file: $!");
die("$program_name: $upload_file: $!\n");
}
# read the file.
my $uploaded = 0;
my $nread = 0;
do {
$nread = sysread(STDIN, my $buffer, $K2_BUFFER_SIZE);
#logentry($logfile, "DEBUG: $nread byte(s) read") if ($option{'d'});
my $nwrite = syswrite(UPLOAD_FILE, $buffer, $nread);
$uploaded += $nwrite;
if ($uploaded > $conf{'max_upload_size'}{'value'}) {
my $msg = sprintf("ERROR: upload limit exceeded: %ld byte(s)",
$uploaded);
logentry($logfile, $msg);
die("$program_name: $msg\n");
}
alarm $conf{'timeout'}{'value'};
} while (($uploaded != $filesize) && $nread);
close(UPLOAD_FILE);
# if filesize is known, check if it matches uploaded size.
if (($flag == 0) && ($uploaded != $filesize)) {
logentry($logfile,
sprintf("sizes don't match, filesize: %ld, uploaded: %ld",
$filesize, $uploaded));
}
# check for empty uploaded files.
if (-z $upload_file) {
my $msg = sprintf("ERROR: %s is empty", $upload_file);
logentry($logfile, $msg);
die("$program_name: $msg\n");
}
# determine the SHA-1 of uploaded file.
if (!defined(open(UPLOAD_FILE, "$upload_file"))) {
logentry($logfile, "ERROR: $upload_file: $!");
die("$program_name: $upload_file: $!\n");
}
my $ctx = Digest::SHA1->new;
$ctx->addfile(*UPLOAD_FILE);
my $digest = $ctx->hexdigest;
close(UPLOAD_FILE);
logentry($logfile,
sprintf("file uploaded to %s, size: %d byte(s), SHA-1: %s",
$upload_file, -s $upload_file, $digest));
# we're done.
$nwrite = syswrite(STDOUT, pack("I", $K2_DONE));
logentry($logfile, "DEBUG: K2_DONE sent to client") if ($option{'d'});
return 0;
}
######################################################################
# handle run file requests.
sub k2_run_file {
my ($buffer) = @_;
my ($cmd, $filename) = unpack("IZ*", $buffer);
# check if unpack() succeeded.
if (!defined($cmd) || !defined($filename)) {
my $msg = "unpack() error";
logentry($logfile, $msg);
die("$program_name: $msg\n");
}
# untaint filename.
if ($filename =~ /^([\w\\\/\.:]+)$/) {
$filename = $1;
}
logentry($logfile,
sprintf("cmd received: K2_RUN_FILE, file: %s",
quotemeta($filename)));
# we're done.
$nwrite = syswrite(STDOUT, pack("I", $K2_DONE));
logentry($logfile, "DEBUG: K2_DONE sent to client") if ($option{'d'});
return 0;
}
######################################################################
# handle delete file requests.
sub k2_delete_file {
my ($buffer) = @_;
my ($cmd, $filename) = unpack("IZ*", $buffer);
# check if unpack() succeeded.
if (!defined($cmd) || !defined($filename)) {
my $msg = "unpack() error";
logentry($logfile, $msg);
die("$program_name: $msg\n");
}
# untaint filename.
if ($filename =~ /^([\w\\\/\.:]+)$/) {
$filename = $1;
}
logentry($logfile,
sprintf("cmd received: K2_DELETE_FILE, file: %s",
quotemeta($filename)));
# return an error to the client.
$nwrite = syswrite(STDOUT, pack("I", $K2_ERROR));
logentry($logfile, "DEBUG: K2_ERROR sent to client") if ($option{'d'});
return 0;
}
######################################################################
# handle download file requests.
sub k2_download_file {
my ($buffer) = @_;
my ($cmd, $filename) = unpack("IZ*", $buffer);
# check if unpack() succeeded.
if (!defined($cmd) || !defined($filename)) {
my $msg = "unpack() error";
logentry($logfile, $msg);
die("$program_name: $msg\n");
}
# untaint filename.
if ($filename =~ /^([\w\\\/\.:]+)$/) {
$filename = $1;
}
logentry($logfile,
sprintf("cmd received: K2_DOWNLOAD_FILE (init), file: %s",
quotemeta($filename)));
# return an error to the client.
$nwrite = syswrite(STDOUT, pack("I", $K2_ERROR));
logentry($logfile, "DEBUG: K2_ERROR sent to client") if ($option{'d'});
return 0;
}
######################################################################
# handle folfer info requests.
sub k2_folder_info {
my ($buffer) = @_;
my ($cmd, $filename) = unpack("IZ*", $buffer);
# check if unpack() succeeded.
if (!defined($cmd) || !defined($filename)) {
my $msg = "unpack() error";
logentry($logfile, $msg);
die("$program_name: $msg\n");
}
# untaint filename.
if ($filename =~ /^([\w\\\/\.:]+)$/) {
$filename = $1;
}
logentry($logfile,
sprintf("cmd received: K2_FOLDER_INFO, start: %s",
quotemeta($filename)));
# return an error to the client.
$nwrite = syswrite(STDOUT, pack("I", $K2_ERROR));
logentry($logfile, "DEBUG: K2_ERROR sent to client") if ($option{'d'});
return 0;
}
######################################################################
# create a log entry.
sub logentry {
my ($logfile, $msg) = @_;
my $datum = strftime "%F %T %z", localtime;
my $pid = $$;
open(LOG, ">>$logfile") ||
die "$program_name: $logfile: $!\n";
flock(LOG, LOCK_EX);
seek(LOG, 0, 2);
printf LOG ("%s: %s[%d]: %s\n", $datum, $program_name, $pid, $msg);
flock(LOG, LOCK_UN);
close(LOG);
}
######################################################################
# return a hexa representation of data.
sub data2hex {
my ($data) = @_;
my $hex = unpack("H*", $data);
if (defined($hex)) {
$hex =~ s/../0x$& /g;
$hex =~ s/ $//g;
} else {
$hex = "";
}
return $hex;
}
######################################################################
# process the configuration file. Exits in case of error.
sub process_config_file {
my ($filename, $confref) = @_;
my %conf = %{$confref};
# filename: words, numbers, '-', '/' and '.' are ok.
if ($filename =~ /^([\w\.\-\/]+)$/) {
$filename = $1;
} else {
$filename = quotemeta($filename);
die("$program_name: invalid filename: $filename\n");
}
open(CONF, "$filename") ||
die ("$program_name: can't open $filename: $!\n");
while (my $line = <CONF>) {
chomp($line);
next if ($line =~ /^$|^\s*$|^\s*#/); # skip comments and empty lines.
foreach my $key (keys %conf) {
if ($line =~ /^$key\s*[=:]\s*($conf{$key}{'regex'})\s*$/) {
$conf{$key}{'value'} = $1;
last;
} elsif ($line =~ /^$key\s*/) {
die("$program_name: invalid parameter: $line\n");
}
}
}
close(CONF);
return \%conf;
}
######################################################################
# print program usage and exit.
sub show_usage {
print <<EOF;
Usage: $program_name [-Vh] [-d] [-f file]
-h display this help and exit.
-V display version number and exit.
-d debug.
-f file configuration file.
EOF
exit 0;
}
######################################################################
# print program version and exit.
sub show_version {
printf("%s %s\n", $program_name, $version);
exit 0;
}
######################################################################
# kuang2.pl ends here.
|