Tuesday, June 7, 2011

PowerShell Create-ComplexPassword v1

NEW!:  http://thepip3r.blogspot.com/2012/02/powershell-create-complexpassword-v2.html


Description: Generates complex passwords based off of the criteria given. Able to create passwords of any length and any complexity given the following standard options:

  • length
  • lowercase
  • uppercase
  • numbers
  • special characters

Acknowledgements: Depending on the complexity given, with this being a true random password generation, a potential exists to generate a large number (thousands of iterations) of internal bad passwords before presenting one back that matches. As an example, if you specified a 30 character password where 29 had to be numbers, the possible iterations before you found a match could be in the millions or more.


Future: I'm currently working on a much more efficient version that will simply generate the random values requested and then shuffle the resulting string. The result will be much less random, but much more efficient.


Logic Overview:

  • Determine supplied options
  • Build Regular Expression based off of those options
  • Generate a random string of characters based off the $PasswordLength and compare it to the generated Regular Expression to see if it meets the remaining complexity requirements supplied.


Example Syntax:

Create-ComplexPassword -PasswordLength 30 -LowerAlphas 5 -UpperAlphas 5 -Numbers 10 -SpecialCharacters 10

Create-ComplexPassword -PasswordLength 16 -LowerAlphas 2 -UpperAlphas 2 -Numbers 2 -SpecialCharacters 2

Create-ComplexPassword -PasswordLength 16 -SpecialCharacters 16



Script:
Function Create-ComplexPassword {
 
Param (
        [parameter()] [ValidateNotNullOrEmpty()] [int] [ValidateScript({$_ -gt 0})] $PasswordLength = 8,
        [parameter()] [ValidateNotNullOrEmpty()] [int] [ValidateScript({$_ -gt 0})] $LowerAlphas = 1,
        [parameter()] [ValidateNotNullOrEmpty()] [int] [ValidateScript({$_ -gt 0})] $UpperAlphas = 1,
        [parameter()] [ValidateNotNullOrEmpty()] [int] [ValidateScript({$_ -gt 0})] $Numbers = 1,
        [parameter()] [ValidateNotNullOrEmpty()] [int] [ValidateScript({$_ -gt 0})] $SpecialCharacters = 1
    )
  
    If (($LowerAlphas + $UpperAlphas + $Numbers + $SpecialCharacters) -gt $PasswordLength) { 
        Throw "The specified sum of the number of upper, lower, numeric, and special characters cannot be greater than the desired length of the password."
    }
    
    # Build the regular expression for pattern matching based on the supplied requirements
    $match = "^"
    
    # Build match for number of required lower alphas
    $match += "(?="
    For ($i=1; $i -le $LowerAlphas; $i++) {
        $match += ".*[a-z]"
    }
    $match += ")"
    
    # Build match for number of required upper alphas
    $match += "(?="
    For ($i=1; $i -le $UpperAlphas; $i++) {
        $match += ".*[A-Z]"
    }
    $match += ")"
    
    # Build match for number of required numbers
    $match += "(?="
    For ($i=1; $i -le $Numbers; $i++) {
        $match += ".*[0-9]"
    }
    $match += ")"
    
    
    # Build match for number of required special characters
    $match += "(?="
    For ($i=1; $i -le $SpecialCharacters; $i++) {
        $match += ".*[^a-zA-Z0-9]"
    }
    $match += ")"
    
    # Build the ending match of the statement to include anything else
    $match += ".*$"

    # Creates random object with seed to guarantee uniqueness
 $objRnd = New-Object System.Random((Get-Date).Millisecond); 

    $iterations = 0

    Do {
        $strPassword = $Null
     For ($i = 0; $i -lt $PasswordLength; $i++) {
       $NextChar = $objRnd.next(33,126)
                $strPassword += ([char]$nextChar)
     }
        $iterations++
    } Until (($strPassword -match $match) -eq $True)

 $strPassword
}



1 comment:

  1. $service = new-webserviceproxy
    http://dimmiunapassword.com/wcf/pwdpassword.svc?wsdl

    $service.getpwd(12, $true) #get a strong password of length 12

    try it!

    ReplyDelete