2015-01-06 6 views
0

나는 펜 스텟 핑을하는 법을 배우고있다. 서버에 대한 키를 만드는 방법을 알아파이썬과 Pexpect를 사용하여 로컬 호스트 해쉬 된 패스워드를 돌려 준다

import pexpect 

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

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

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 main(): 
    host = 'localhost' 
    user = 'root'  import pexpect 

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

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

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 main(): 
    host = 'localhost' 
    user = 'root' 
    password = 'g' 
    child = connect(user, host, password) 
    send_command(child, 'cat /etc/shadow | grep root') 

if __name__ == '__main__': 
    main() 
    password = 'g' 
    child = connect(user, host, password) 
    send_command(child, 'cat /etc/shadow | grep root') 

if __name__ == '__main__': 
    main() 

내가 우분투 문서에보고, 그래서 터미널에서 다음 명령을 사용 : 운동 중 하나는이 스크립트를 사용

mkdir ~/.ssh 
chmod 700 ~/.ssh 
ssh-keygen -t rsa 

I 그런 다음 service ssh start

으로 ssh 서버를 시작합니다. 다음으로 스크립트를 실행합니다. 책에서 말하는 것처럼 해쉬 된 루트 암호를 반환하지 않습니다. 오히려, 나는 루트 암호를 요청하는 OpenSSH의 팝업을 얻고, 다음과 같은 출력 :

Traceback (most recent call last): 
    File "local.py", line 38, in <module> 
    main() 
    File "local.py", line 34, in main 
    child = connect(user, host, password) 
    File "local.py", line 27, in connect 
    child.expect(PROMPT) 
    File "/usr/lib/python2.7/dist-packages/pexpect/__init__.py", line 1418, in expect 
    timeout, searchwindowsize) 
    File "/usr/lib/python2.7/dist-packages/pexpect/__init__.py", line 1433, in expect_list 
    timeout, searchwindowsize) 
    File "/usr/lib/python2.7/dist-packages/pexpect/__init__.py", line 1535, in expect_loop 
    raise TIMEOUT(str(err) + '\n' + str(self)) 
pexpect.TIMEOUT: Timeout exceeded. 
<pexpect.spawn object at 0x7f2ca63ca7d0> 
version: 3.2 
command: /usr/bin/ssh 
args: ['/usr/bin/ssh', '[email protected]'] 
searcher: <pexpect.searcher_re object at 0x7f2ca63ca850> 
buffer (last 100 chars): "\r\nPermission denied, please try again.\r\r\[email protected]'s password: " 
before (last 100 chars): "\r\nPermission denied, please try again.\r\r\[email protected]'s password: " 
after: <class 'pexpect.TIMEOUT'> 
match: None 
match_index: None 
exitstatus: None 
flag_eof: False 
pid: 4562 
child_fd: 3 
closed: False 
timeout: 30 
delimiter: <class 'pexpect.EOF'> 
logfile: None 
logfile_read: None 
logfile_send: None 
maxread: 2000 
ignorecase: False 
searchwindowsize: None 
delaybeforesend: 0.05 
delayafterclose: 0.1 
delayafterterminate: 0.1 

솔직히거야, 난 여전히 파이썬에서 꽤 많이 안돼서 내가 함께 놀았 던 있지만, 지금 몇 주 동안. 이것이 바보 같은 질문이라면 사과드립니다. 그리고 네, 암호는 단지 "g"입니다.

답변

0

root @ localhost로 시스템에 SSH를 시도 할 때 오류가 발생합니다. 귀하의 오류가 명령

buffer (last 100 chars): "\r\nPermission denied, please try again.\r\r\[email protected]'s password: " 
before (last 100 chars): "\r\nPermission denied, please try again.\r\r\[email protected]'s password: " 

이 잘못된 사용자 이름/암호 콤보가있는 시스템에 로그인 할 때 당신이 얻을 것이다 전형적인 오류가의 출력에서 ​​발견된다.

터미널에서 직접 SSH를 실행하여 1) root ssh가 허용되는지 확인하고 2) 사용자 이름/암호 조합이 실제로 올바른지 확인하십시오. 향후


, paramiko는 기계로 실행 명령을 기록 및 읽기/SFTP를 통해 파일을 쓸 수 있습니다 파이썬에 대한 SSH 라이브러리입니다. 분명히 이것은 책에서 배우는 것일 뿐이지 만 paramiko와 함께 실제 물건을 쓰는 것을 고려하십시오. 여기

같은 예 paramiko에서의 모습입니다 :

import paramiko 

client = paramiko.SSHClient() 
client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
try: 
    client.connect('localhost', username='root', password='g') 
    stdin, stdout, stderr = client.exec_command('/bin/cat /etc/shadow') 
    # Now, you can read from stdout (if the command succeeded), or stderr (if it failed) 
    shadow_file_contents = stdout.readlines() 
    if shadow_file_contents: 
     print '/etc/shadow: {0}'.format(''.join(line for line in shadow_file_contents if 'root' in line)) 
    else: # No contents in the file. Show the user why... 
     print 'errors: {0}'.format(''.join(stderr.readlines())) 
except (paramiko.BadAuthenticationType) as why: # Invalid un/pw 
    print 'Unable to login using given username/password to this host'