利用Paramiko模块实现SSH客户端
1 import paramiko 2 import sys 3 import ipaddress 4 import optparse 5 6 7 class SSHClient: 8 def __init__(self) -> None: 9 self.target, self.port, self.username, self.password = self.get_params() 10 self.banner() 11 12 def banner(self): 13 banner= """ 14 ************************************************** 15 16 **********SSHClient by Jason Wong***************** 17 18 ************************************************** 19 20 21 """ 22 print(banner) 23 24 def get_params(self): 25 parser = optparse.OptionParser('Usage: < Program > -t target IP address -p port -u username -P password') 26 parser.add_option('-t', '--target', dest='target', type='string', help='Specify target IP address') 27 parser.add_option('-p', '--port', dest='port', type='int', help='Specify port to connect') 28 parser.add_option('-u', '--username', dest='username', type='string', help='Speicfy username') 29 parser.add_option('-P', '--password', dest='password', type='string', help='Specify password') 30 options, args = parser.parse_args() 31 if options.target is None or options.username is None or options.password is None: 32 print(parser.usage) 33 sys.exit() 34 try: 35 ipaddress.ip_address(options.target) 36 except: 37 print("[-] Input right IP address") 38 sys.exit(0) 39 if options.port is None: 40 options.port = 22 41 return options.target, options.port, options.username, options.password 42 43 def execute_command(self, sshclient, command): 44 stdin, stdout,stderr = sshclient.exec_command(command) 45 return stdout, stderr 46 47 def help(self): 48 help_info = """ 49 q quit program 50 command send command to target for execution 51 """ 52 print(help_info) 53 def run(self): 54 try: 55 sshclient = paramiko.SSHClient() 56 sshclient.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 57 sshclient.connect(hostname=self.target, port=self.port,username=self.username, password=self.password) 58 while True: 59 command = input('%s$ ' % self.target) 60 if command == 'q': 61 break 62 elif command == 'help': 63 self.help() 64 else: 65 stdout, stderr = self.execute_command(sshclient, command) 66 if stdout: 67 print(stdout.read().decode('utf-8')) 68 if stderr: 69 print(stderr.read().decode('utf-8')) 70 sshclient.close() 71 72 except paramiko.AuthenticationException: 73 print("[-] Failed to authentiate to the target") 74 sys.exit(0) 75 76 except Exception as e: 77 print(e) 78 sys.exit(0) 79 80 if __name__ == '__main__': 81 sshclient = SSHClient() 82 sshclient.run()
运行效果如下:
(root💀kali)-[~/Desktop/Hack_Project/web_pentesting] └─# python ssh_client.py -t 192.168.140.144 -u msfadmin -P msfadmin ************************************************** **********SSHClient by Jason Wong***************** ************************************************** 192.168.140.144$ ls 1GB.zip 5MB.zip 5MB.zip.1 index.html sogou.exe vulnerable 192.168.140.144$ ifconfig eth0 Link encap:Ethernet HWaddr 00:0c:29:91:f0:6e inet addr:192.168.140.144 Bcast:192.168.140.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe91:f06e/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:160 errors:0 dropped:0 overruns:0 frame:0 TX packets:185 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:19967 (19.4 KB) TX bytes:26631 (26.0 KB) Interrupt:19 Base address:0x2000 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:114 errors:0 dropped:0 overruns:0 frame:0 TX packets:114 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:29797 (29.0 KB) TX bytes:29797 (29.0 KB) 192.168.140.144$
STRIVE FOR PROGRESS,NOT FOR PERFECTION