Station X Python for Cyber Security SSH自动登录脚本出错,一直在试图解决这个问题,我们的约50小时,帮助我。

问题描述 投票:0回答:1

正如问题中所说的,这是在线python编程课程的一部分,用于网络安全目的。我已经按照代码,一遍又一遍地查看我自己注意到的任何错误,或者我的代码和视频课程之间的任何差异。我已经尝试了多种不同的东西来修复它。我已经尝试过实现child.delaybeforesend=None,因为根据pexpect库的文档,它没有工作。任何帮助都将是非常感激的。我是一个中级程序员,绝不是专家,但我正在努力,在我弄清楚这个问题之前,我不能继续这个课程。

#!/usr/bin/python

import pexpect

PROMPT = ['# ', '>>> ', '> ', '/$ ']

def connect(user, host, password):
    ssh_newkey = 'Are you sure you want to continue connecting'
    connStr = 'ssh ' + user + '@' + host
    child = pexpect.spawn(connStr)
    ret = child.expect([pexpect.TIMEOUT, ssh_newkey,'[P|p]assword: '])
    if ret == 0:
            print '[-] Error Connecting'
            return
    if ret == 1:
            child.sendline('yes')
            ret = child.expect([pexpect.TIMEOUT,'[P|p]assword: '])
            if ret == 0:
                    print '[-] Error Connecting'
                    return
    child.sendline(password)
    child.expect(PROMPT)
    return child

def send_command(child,command):
    child.sendline(command)
    child.expect(PROMPT)
    print chlild.before


def main():
    host = raw_input('Enter Target: ')
    user = raw_input('Enter SSH Username: ')
    password = raw_input('Enter SSH Password: ')
    child = connect(user,host,password)
    send_command(child, 'cat etc/shadow | grep root;ps')

main()

它给我的错误如下。

Enter Target: 192.168.1.83
Enter SSH Username: msfadmin
Enter SSH Password: msfadmin
Traceback (most recent call last):
  File "./sshlogin.py", line 37, in <module>
main()
  File "./sshlogin.py", line 35, in main
child = connect(user,host,password)
  File "./sshlogin.py", line 22, in connect
child.expect(PROMPT)
  File "/usr/lib/python2.7/dist-packages/pexpect/spawnbase.py", line 341, in expect
timeout, searchwindowsize, async_)
  File "/usr/lib/python2.7/dist-packages/pexpect/spawnbase.py", line 369, in expect_list
return exp.expect_loop(timeout)
  File "/usr/lib/python2.7/dist-packages/pexpect/expect.py", line 119, in expect_loop
return self.timeout(e)
  File "/usr/lib/python2.7/dist-packages/pexpect/expect.py", line 82, in timeout
raise TIMEOUT(msg)
pexpect.exceptions.TIMEOUT: Timeout exceeded.
<pexpect.pty_spawn.spawn object at 0x7f123bfca410>
command: /usr/bin/ssh
args: ['/usr/bin/ssh', '[email protected]']
buffer (last 100 chars): 'com/\r\nNo mail.\r\nLast login: Mon Jun  8 04:48:20 2020 from     192.168.1.69\r\r\nmsfadmin@metasploitable:~$ '
before (last 100 chars): 'com/\r\nNo mail.\r\nLast login: Mon Jun  8 04:48:20 2020 from    192.168.1.69\r\r\nmsfadmin@metasploitable:~$ '
after: <class 'pexpect.exceptions.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 2432
child_fd: 5
closed: False
timeout: 30
delimiter: <class 'pexpect.exceptions.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
searcher: searcher_re:
0: re.compile('# ')
1: re.compile('>>> ')
2: re.compile('> ')
3: re.compile('/$ ')

我想弄清楚事情的来龙去脉,但我已经浪费了足够长的时间,没有继续下去,我已经尝试了大约10个不同的建议,我在网上看到的,并搜索了pexpect,这似乎是一个问题,但没有任何工作,我不确定它是否是一个错误,或者是一些远远超出我的专业知识,或者是一个缺少的逗号,我的大脑拒绝识别。

python security ssh pexpect kali-linux
1个回答
0
投票
PROMPT = ['# ', '>>> ', '> ', '/$ ']
...
child.expect(PROMPT)
...
before (last 100 chars): '...\nmsfadmin@metasploitable:~$ '

我不是python程序员。但你的代码似乎是在等待一个提示,但它并没有等待得到的具体提示字符串。你得到的是提示,

msfadmin@metasploitable:~$ 

但它并不包含任何代码正在等待的字符串。

在提示列表中添加"~$"可能会解决你的问题。

PROMPT = ['# ', '>>> ', '> ', '/$ ', '~$ ']
                                     ^^^^^--added
© www.soinside.com 2019 - 2024. All rights reserved.