Advertisement
SERBIANHACKERS

SRBTOOL | Password filter

Apr 14th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.92 KB | None | 0 0
  1. #SRBHACKERS
  2.  
  3. #!/usr/bin/env python
  4.  
  5.  
  6. import argparse
  7. import re
  8. import sys
  9.  
  10. #------------------------------------------------------------------------------
  11. # Function Definitions
  12. #------------------------------------------------------------------------------
  13.  
  14. def parse_word(word, s):
  15.     """Parses the word and counts the number of digits, lowercase letters,
  16.    uppercase letters, and symbols. Returns a dictionary with the results.
  17.    If any character in the word is not in the set of digits, lowercase
  18.    letters, uppercase letters, or symbols it is marked as a bad character.
  19.    Words with bad characters are not output."""
  20.  
  21.     count = {'d': 0, 'l': 0, 'u': 0, 's': 0, 'x':0}
  22.     d = '0123456789'
  23.     l = 'abcdefghijklmnopqrstuvwxyz'
  24.     u = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  25.    
  26.     for c in word:
  27.         if c in d:
  28.             count['d'] += 1
  29.         elif c in l:
  30.             count['l'] += 1
  31.         elif c in u:
  32.             count['u'] += 1
  33.         elif c in s:
  34.             count['s'] += 1
  35.         else:
  36.             count['x'] += 1
  37.  
  38.     return count
  39.  
  40. def parse_requirements(r):
  41.     """Determine which characters are required and the number of them that
  42.    are required."""
  43.    
  44.     req = {'d': 0, 'l': 0, 'u': 0, 's': 0}
  45.     for c in r:
  46.         if c == 'd':
  47.             req['d'] += 1
  48.         elif c == 'l':
  49.             req['l'] += 1
  50.         elif c == 'u':
  51.             req['u'] += 1
  52.         elif c == 's':
  53.             req['s'] += 1
  54.         else:
  55.             continue
  56.  
  57.     return req
  58.            
  59. def complex_pass(count):
  60.     """Windows complexity requires a password to contain three of the four
  61.    groups: digits, lowercase letters, uppercase letters, or symbols."""
  62.  
  63.     if count['d'] and count['l'] and count['u']:
  64.         return True
  65.     elif count['d'] and count['l'] and count['s']:
  66.         return True
  67.     elif count['d'] and count['u'] and count['s']:
  68.         return True
  69.     elif count['l'] and count['u'] and count['s']:
  70.         return True
  71.     else:
  72.         return False
  73.  
  74.  
  75. def meets_requirements(count, r):
  76.     """Does the password have enough of each type of character to meet the
  77.    requirements?"""
  78.    
  79.     if (count['d'] >= r['d'] and count['l'] >= r['l'] and
  80.     count['u'] >= r['u'] and count['s'] >= r['s']):
  81.         return True
  82.     else:
  83.         return False
  84.  
  85.    
  86. #------------------------------------------------------------------------------
  87. # Main Program
  88. #------------------------------------------------------------------------------
  89.  
  90. desc = """Passfilter.py reads a file or stdin and returns words that meet the
  91. defined requirements. For most password policies the set of allowed letters
  92. and numbers is the same. The set of allowed symbols varies widely between
  93. policies. Passfilter.py defines a default set of symbols which can be
  94. overridden using the -s flag.
  95.  
  96. Examples:
  97. Return all words 3 to 10 characters long.
  98.    passfilter.py -f wordlist
  99.  
  100. Return all words 3 to 10 characters long that meet the windows complexity
  101. requirements.
  102.    passfilter.py -w -f wordlist
  103.  
  104. Return all words 5 to 9 characters long that have at least two lowercase
  105. letters and at least one digit.
  106.    passfilter.py -m 5 -x 9 -r lld -f wordlist
  107. """
  108.  
  109. parser = argparse.ArgumentParser(prog="Passfilter.py",
  110.                         formatter_class=argparse.RawDescriptionHelpFormatter,
  111.                         description=desc)
  112. group = parser.add_mutually_exclusive_group()
  113. group.add_argument('-w', action='store_true', default=False,
  114.                     help='Passwords must meet Windows complexity requirements.')
  115. group.add_argument('-r', action='store', default='', metavar='string',
  116.                     help='''String representing the character groups and count
  117.                    required.''')
  118. parser.add_argument('-m', action='store', type=int, default='3', metavar='min',
  119.                     help='Minimum password length. (default: 3)')
  120. parser.add_argument('-x', action='store', type=int, default='10', metavar='max',
  121.                     help='Maximum password length. (default: 10)')
  122. parser.add_argument('-s', action='store', default=''' !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~''',
  123.                     help='''Symbols allowed in the password.
  124. (default:  !"#$%%&'()*+,-./:;<=>?@[\]^_`{|}~)''',
  125.                     metavar='symbols')
  126. parser.add_argument('-f', metavar='wordlist',
  127.                     help='Wordlist to parse (default: stdin).')
  128.  
  129. args = parser.parse_args()
  130.  
  131. # Open the file or stdin
  132. if args.f:
  133.     try:
  134.         wordlist = open(args.f, 'r')
  135.     except IOError:
  136.         print "Could not open file %s" % args.f
  137.         sys.exit()
  138. else:
  139.     wordlist = sys.stdin
  140.  
  141. for line in wordlist:
  142.  
  143.     # Skip blank lines and comments in the word list
  144.     if re.match('^$', line):
  145.         continue
  146.     if re.match('^#.*$', line):
  147.         continue
  148.  
  149.     # Strip the new line character and check the word for length requirements
  150.     word = line.rstrip('\r\n')
  151.     if len(word) < args.m:
  152.         continue
  153.     if len(word) > args.x:
  154.         continue
  155.  
  156.     # Count the occurrance of each type of character.
  157.     count = parse_word(word, args.s)
  158.  
  159.     # If any character did not match the allowed characters, skip the word
  160.     if count['x'] > 0:
  161.         continue
  162.  
  163.     # If requirements were provided then check to see if the word meets the
  164.     # requirements. If it does then keep it, if not, move to the next word.
  165.     if args.r:
  166.         if meets_requirements(count, parse_requirements(args.r)):
  167.             print word
  168.             continue
  169.         else:
  170.             continue
  171.  
  172.     # If we require Windows complexity then check to see if the word meets the
  173.     # windows complexity requirements. If it does then keep it, if not, move to
  174.     # the next word.
  175.     if args.w:
  176.         if complex_pass(count):
  177.             print word
  178.             continue
  179.         else:
  180.             continue
  181.        
  182.     else:
  183.         print word
  184.  
  185. if wordlist is not sys.stdin:
  186.     wordlist.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement