|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +import sys |
| 4 | +import socket |
| 5 | +import argparse |
| 6 | +import threading |
| 7 | +import subprocess |
| 8 | + |
| 9 | + |
| 10 | +class TargetServer(object): |
| 11 | + |
| 12 | + def __init__(self, port): |
| 13 | + self.port = port |
| 14 | + self.host = socket.gethostname() |
| 15 | + self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 16 | + self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 17 | + self.server.bind(("0.0.0.0", int(self.port))) |
| 18 | + self.server.listen(10) |
| 19 | + |
| 20 | + def run(self): |
| 21 | + while 1: |
| 22 | + client_socket, client_addr = self.server.accept() |
| 23 | + client_thread = threading.Thread(target=self.client_handler, |
| 24 | + args=(client_socket,)) |
| 25 | + client_thread.start() |
| 26 | + |
| 27 | + def client_handler(self, client_socket): |
| 28 | + client_socket.sendall("<@ %s $ >" % self.host) |
| 29 | + while 1: |
| 30 | + try: |
| 31 | + cmd_buffer = client_socket.recv(1024) |
| 32 | + response = self.run_command(cmd_buffer) |
| 33 | + if len(response) == 0: |
| 34 | + response = "[Successful!]\n" |
| 35 | + client_socket.sendall(response) |
| 36 | + except Exception as e: |
| 37 | + # print e |
| 38 | + break |
| 39 | + |
| 40 | + def run_command(self, command): |
| 41 | + command = command.strip() |
| 42 | + try: |
| 43 | + output = subprocess.check_output(command, stderr=subprocess.STDOUT, |
| 44 | + shell=True) |
| 45 | + except: |
| 46 | + output = '[*]Failed to execute command ! \n' |
| 47 | + |
| 48 | + return output |
| 49 | + |
| 50 | + |
| 51 | +class Client(object): |
| 52 | + |
| 53 | + def __init__(self, host, port): |
| 54 | + self.host = host |
| 55 | + self.port = port |
| 56 | + self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 57 | + self.client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 58 | + |
| 59 | + def run(self): |
| 60 | + try: |
| 61 | + self.client.connect((self.host, int(self.port))) |
| 62 | + header = self.client.recv(4096) |
| 63 | + command = raw_input(header) |
| 64 | + if command == "exit" or command == "quit": |
| 65 | + self.clien.close() |
| 66 | + sys.exit(0) |
| 67 | + self.client.sendall(command) |
| 68 | + while 1: |
| 69 | + recv_len = 1 |
| 70 | + response = "" |
| 71 | + |
| 72 | + while recv_len: |
| 73 | + data = self.client.recv(4096) |
| 74 | + recv_len = len(data) |
| 75 | + response += data |
| 76 | + if recv_len < 4096: |
| 77 | + break |
| 78 | + |
| 79 | + print(response) |
| 80 | + |
| 81 | + command = raw_input(header) |
| 82 | + if command == "exit" or command == "quit": |
| 83 | + self.client.close() |
| 84 | + break |
| 85 | + self.client.sendall(command) |
| 86 | + |
| 87 | + except: |
| 88 | + print("[*] Exception Failed ! \n") |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == '__main__': |
| 92 | + parser = argparse.ArgumentParser(description="NetCat Shell") |
| 93 | + parser.add_argument("-s", "--server", help="Target Server", |
| 94 | + action="store_true") |
| 95 | + parser.add_argument("-c", "--client", help="Client", action="store_true") |
| 96 | + parser.add_argument("--host", help="target host IP", action="store", |
| 97 | + default="127.0.0.1") |
| 98 | + parser.add_argument("-p", "--port", help="target host port", action="store", |
| 99 | + type=int) |
| 100 | + args = parser.parse_args() |
| 101 | + port = args.port |
| 102 | + if args.server: |
| 103 | + s = TargetServer(port) |
| 104 | + s.run() |
| 105 | + if args.client: |
| 106 | + host = args.host |
| 107 | + c = Client(host, port) |
| 108 | + c.run() |
| 109 | + |
0 commit comments