나는 펜 스텟 핑을하는 법을 배우고있다. 서버에 대한 키를 만드는 방법을 알아파이썬과 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"입니다.