Last active
August 29, 2015 14:20
-
-
Save coxley/dc5a6a1a8896ed08cd17 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Description: Attempts to login as root with each password in the provided | |
password list. If successful, runs ``uname -a`` and prints to | |
stdout | |
Usage: | |
waruname.py <hosts_file> <password_list> | |
Options: | |
hosts_file Newline delimeted host list in URI form. | |
e.g, ssh://127.0.0.1, telnet://127.0.0.1 | |
password_list Newline delimited list of passwords to attempt | |
""" | |
from __future__ import print_function | |
import sys | |
import Exscript.protocols | |
from Exscript.util.interact import Account | |
from Exscript.util.file import get_hosts_from_file | |
from Exscript.protocols.Exception import (LoginFailure, ProtocolException, | |
TimeoutException, | |
InvalidCommandException) | |
from paramiko.ssh_exception import SSHException | |
# from Exscript.util.start import start | |
def run(host, conn): | |
'''Runs commands on hosts and logs''' | |
try: # Try command and log if doesn't support uname | |
conn.execute('uname -a') | |
result = conn.response.splitlines()[1] | |
log = "%s: %s\n" % (host.address, result) | |
print(log) | |
except InvalidCommandException as e: | |
print('WARNING: %s - command failed: %s' % (host.address, e), | |
file=sys.stderr) | |
except Exception as e: | |
print('WARNING: %s - error sending command: %s' % (host.address, e), | |
file=sys.stderr) | |
try: # Try to send exit. Not all platforms support. Force close otherwise. | |
conn.send('exit\r') | |
except InvalidCommandException as e: | |
print('WARNING: %s - command failed: %s' % (host.address, e), | |
file=sys.stderr) | |
except Exception as e: | |
print('WARNING: %s - error sending exit: %s' % (host.address, e), | |
file=sys.stderr) | |
conn.close() | |
def connect(accounts, host, callback): | |
'''Sets up connection, attempting each password''' | |
for account in accounts: | |
try: | |
conn = Exscript.protocols.connect(host) | |
except SSHException: | |
print('WARNING: %s failed - SSHError' % host.address, | |
file=sys.stderr) | |
return | |
except Exception as e: | |
print('WARNING: %s failed - Error: %s' % (host.address, e), | |
file=sys.stderr) | |
return | |
try: | |
conn.login(account) | |
break | |
except SSHException: | |
print('WARNING: %s failed - SSHError' % host.address, | |
file=sys.stderr) | |
return | |
except LoginFailure: | |
continue | |
except TimeoutException: | |
print('WARNING: %s failed - Timeout' % host.address, | |
file=sys.stderr) | |
return | |
except ProtocolException: | |
print('WARNING: %s failed - Protocol Error' % host.address, | |
file=sys.stderr) | |
return | |
except KeyboardInterrupt: | |
sys.exit(1) | |
except Exception as e: | |
print('WARNING: %s failed - Error: %s' % (host.address, e), | |
file=sys.stderr) | |
return | |
if conn.is_protocol_authenticated(): | |
callback(host, conn) | |
else: | |
print('WARNING: %s failed - Bad Logins' % host.address, | |
file=sys.stderr) | |
def main(): | |
'''Opens files provide via argv''' | |
hosts = get_hosts_from_file(sys.argv[1]) | |
with open(sys.argv[2]) as f: | |
passwords = [i.strip() for i in f.readlines()] | |
accounts = [Account('root', pw) for pw in passwords] | |
for host in hosts: | |
connect(accounts, host, run) | |
if __name__ == '__main__': | |
if len(sys.argv) <= 1 or '-h' in sys.argv: | |
sys.exit(__doc__) | |
else: | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment