I'm learning how to do pentesting with a book. One of the exercises uses this script:
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()
I looked in the Ubuntu documentation to find out how to create the keys for the server, so I used these commands in the terminal:
mkdir ~/.ssh
chmod 700 ~/.ssh
ssh-keygen -t rsa
I then proceeded to start the ssh server with service ssh start
Next, I run the script. It doesn't return my hashed root password like the books says it should. Rather, I get an OpenSSH popup requesting a root password, and the following output:
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', 'root@localhost']
searcher: <pexpect.searcher_re object at 0x7f2ca63ca850>
buffer (last 100 chars): "\r\nPermission denied, please try again.\r\r\nroot@localhost's password: "
before (last 100 chars): "\r\nPermission denied, please try again.\r\r\nroot@localhost'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
I'm going to be honest, I'm still pretty much a newbie at python, although I've been playing with it for several weeks now. I apologize if this is a silly question. And yes, my password is just "g".
You're getting an error when trying to SSH into the machine as root@localhost. Your error is found in the output of the command
buffer (last 100 chars): "\r\nPermission denied, please try again.\r\r\nroot@localhost's password: "
before (last 100 chars): "\r\nPermission denied, please try again.\r\r\nroot@localhost's password: "
This is the typical error you'll get when you try to log into a machine with the wrong username/password combo.
From a terminal, try SSH'ing into the box directly to see if 1) root ssh is allowed, and 2) if the username/password combination is in fact correct.
In the future, paramiko
is an SSH library for Python which can log into machines, run commands and read/write files over SFTP. Obviously, this is just learning from a book, but consider writing real stuff with paramiko.
Here's what the same example looks like in 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'