Skip to content

Instantly share code, notes, and snippets.

@CJHarmath
Last active August 2, 2019 18:54
Show Gist options
  • Save CJHarmath/afde4a3ce2159d2f9fbf0bbcfed9d501 to your computer and use it in GitHub Desktop.
Save CJHarmath/afde4a3ce2159d2f9fbf0bbcfed9d501 to your computer and use it in GitHub Desktop.

Revisions

  1. CJHarmath revised this gist Aug 2, 2019. 1 changed file with 124 additions and 0 deletions.
    124 changes: 124 additions & 0 deletions Get-PasswordComplexity.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,124 @@
    function Get-PasswordComplexity {
    <#
    .SYNOPSIS
    Testing if a given password is complex
    .DESCRIPTION
    Based on the given SecureString or Credential the function tests if the password used is complex enough.
    The complexity is calculated based on the number of character classes use in the password.
    The classes are lower case letter, upper case letters, numbers and special characters.
    Each class gets a complexity point and the password must include at least 3 classes.
    The length requirement defaults to 10, but can be overriden.
    Based on http://tompaps.blogspot.com/2018/01/verify-password-complexity-os.html
    With a few security enhancements in mind and using advanced function.
    .PARAMETER SecurePassword
    The password to be tested as a SecureString
    .PARAMETER Credential
    The credential object which password is going to be tested
    .PARAMETER MinPasswordLength
    Optional. Defaults to 10. The minimum length requirement for accepted passwords.
    .EXAMPLE
    $secureString = Read-Host -AsSecureString -Prompt "enter password"
    enter password: ***********
    > Test-PasswordComplexity -SecurePassword $secureString
    > Test-PasswordComplexity -SecurePassword $secureString
    IsValid Length Complexity ComplexityScore
    ------- ------ ---------- ---------------
    True length - OK complex 4
    .EXAMPLE
    $secureStringWeak = Read-Host -AsSecureString -Prompt "enter password"
    enter password: ******
    > Test-PasswordComplexity -SecurePassword $secureStringWeak
    IsValid Length Complexity ComplexityScore
    ------- ------ ---------- ---------------
    False length < 10 NOT complex 1
    .LINK
    http://tompaps.blogspot.com/2018/01/verify-password-complexity-os.html
    #>
    [CmdletBinding(
    DefaultParameterSetName = 'SecurePassword'
    )]
    [OutputType('PasswordComplexity')]
    param (
    [Parameter(Mandatory, ParameterSetName = 'SecurePassword')]
    [SecureString]
    $SecurePassword,

    [Parameter(Mandatory, ParameterSetName = 'Credential')]
    [PSCredential]
    $Credential,

    [Parameter()]
    [int]
    $MinPasswordLength = 10
    )
    try {
    if ($PSCmdlet.ParameterSetName -eq 'SecurePassword') {
    $passwordString = (New-Object PSCredential "user",$SecurePassword).GetNetworkCredential().Password
    } else {
    $passwordString = $Credential.GetNetworkCredential().Password
    }

    $valid = $true
    # checking the minimal length
    if($passwordString.length -lt $MinPasswordLength){
    $passwordLength = "length < {0}" -f $MinPasswordLength
    $valid = $false
    } else {
    $passwordLength = "length - OK"
    }

    $pwComplexity = 0
    # lowercase
    if($passwordString -cmatch "[a-z]"){
    $pwComplexity++
    }

    # uppercase
    if($passwordString -cmatch "[A-Z]"){
    $pwComplexity++
    }

    # digits
    if($passwordString -cmatch "[0-9]"){
    $pwComplexity++
    }


    # special character (not alphabetic characters or numbers)
    if($passwordString -cmatch "[^a-zA-Z0-9]"){
    $pwComplexity++
    }

    # if 3 of the criterias
    if($pwComplexity -ge 3){
    $complexity = "complex"
    }
    else{
    $complexity = "NOT complex"
    $valid = $false
    }

    [PSCustomObject]@{
    IsValid = $valid
    Length = $passwordLength
    Complexity = $complexity
    ComplexityScore = $pwComplexity
    PSTypeName = 'PasswordComplexity'
    }
    } catch {
    Write-Error -ErrorRecord $_
    }
    }
  2. CJHarmath revised this gist Aug 2, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Test-PasswordComplexity.ps1
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    function Test-PasswordComplexity {
    function Get-PasswordComplexity {
    <#
    .SYNOPSIS
    Testing if a given password is complex
  3. CJHarmath revised this gist Aug 2, 2019. No changes.
  4. CJHarmath revised this gist Aug 2, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Test-PasswordComplexity.ps1
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@ function Test-PasswordComplexity {
    <#
    .SYNOPSIS
    Testing if a given password is complex
    .DESCRIPTION
    Based on the given SecureString or Credential the function tests if the password used is complex enough.
    The complexity is calculated based on the number of character classes use in the password.
  5. CJHarmath revised this gist Aug 2, 2019. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion Test-PasswordComplexity.ps1
    Original file line number Diff line number Diff line change
    @@ -42,8 +42,9 @@ function Test-PasswordComplexity {
    IsValid Length Complexity ComplexityScore
    ------- ------ ---------- ---------------
    False length < 10 NOT complex 1
    .LINK
    http://tompaps.blogspot.com/2018/01/verify-password-complexity-os.html
    #>
    [CmdletBinding(
    DefaultParameterSetName = 'SecurePassword'
  6. CJHarmath revised this gist Aug 2, 2019. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions Test-PasswordComplexity.ps1
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,8 @@ function Test-PasswordComplexity {
    .PARAMETER MinPasswordLength
    Optional. Defaults to 10. The minimum length requirement for accepted passwords.
    .EXAMPLE SecureString input
    .EXAMPLE
    $secureString = Read-Host -AsSecureString -Prompt "enter password"
    enter password: ***********
    > Test-PasswordComplexity -SecurePassword $secureString
    @@ -32,14 +33,17 @@ function Test-PasswordComplexity {
    ------- ------ ---------- ---------------
    True length - OK complex 4
    .EXAMPLE weak password
    .EXAMPLE
    $secureStringWeak = Read-Host -AsSecureString -Prompt "enter password"
    enter password: ******
    > Test-PasswordComplexity -SecurePassword $secureStringWeak
    IsValid Length Complexity ComplexityScore
    ------- ------ ---------- ---------------
    False length < 10 NOT complex 1
    .LINK
    #>
    [CmdletBinding(
    DefaultParameterSetName = 'SecurePassword'
  7. CJHarmath revised this gist Aug 2, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Test-PasswordComplexity.ps1
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ function Test-PasswordComplexity {
    .PARAMETER MinPasswordLength
    Optional. Defaults to 10. The minimum length requirement for accepted passwords.
    .EXAMPLE
    .EXAMPLE SecureString input
    $secureString = Read-Host -AsSecureString -Prompt "enter password"
    enter password: ***********
    > Test-PasswordComplexity -SecurePassword $secureString
    @@ -32,7 +32,7 @@ function Test-PasswordComplexity {
    ------- ------ ---------- ---------------
    True length - OK complex 4
    .EXAMPLE
    .EXAMPLE weak password
    $secureStringWeak = Read-Host -AsSecureString -Prompt "enter password"
    enter password: ******
    > Test-PasswordComplexity -SecurePassword $secureStringWeak
  8. CJHarmath revised this gist Aug 2, 2019. 1 changed file with 17 additions and 1 deletion.
    18 changes: 17 additions & 1 deletion Test-PasswordComplexity.ps1
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,23 @@ function Test-PasswordComplexity {
    Optional. Defaults to 10. The minimum length requirement for accepted passwords.
    .EXAMPLE
    $secureString = Read-Host -AsSecureString -Prompt "enter password"
    enter password: ***********
    > Test-PasswordComplexity -SecurePassword $secureString
    > Test-PasswordComplexity -SecurePassword $secureString
    IsValid Length Complexity ComplexityScore
    ------- ------ ---------- ---------------
    True length - OK complex 4
    .EXAMPLE
    $secureStringWeak = Read-Host -AsSecureString -Prompt "enter password"
    enter password: ******
    > Test-PasswordComplexity -SecurePassword $secureStringWeak
    IsValid Length Complexity ComplexityScore
    ------- ------ ---------- ---------------
    False length < 10 NOT complex 1
    #>
    [CmdletBinding(
    DefaultParameterSetName = 'SecurePassword'
  9. CJHarmath revised this gist Aug 2, 2019. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion Test-PasswordComplexity.ps1
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,9 @@ function Test-PasswordComplexity {
    .PARAMETER MinPasswordLength
    Optional. Defaults to 10. The minimum length requirement for accepted passwords.
    .EXAMPLE
    #>
    [CmdletBinding(
    DefaultParameterSetName = 'SecurePassword'
  10. CJHarmath revised this gist Aug 2, 2019. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions Test-PasswordComplexity.ps1
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,16 @@ function Test-PasswordComplexity {
    Based on http://tompaps.blogspot.com/2018/01/verify-password-complexity-os.html
    With a few security enhancements in mind and using advanced function.
    .PARAMETER SecurePassword
    The password to be tested as a SecureString
    .PARAMETER Credential
    The credential object which password is going to be tested
    .PARAMETER MinPasswordLength
    Optional. Defaults to 10. The minimum length requirement for accepted passwords.
    #>
    [CmdletBinding(
    DefaultParameterSetName = 'SecurePassword'
  11. CJHarmath revised this gist Aug 2, 2019. No changes.
  12. CJHarmath revised this gist Aug 2, 2019. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions Test-PasswordComplexity.ps1
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,9 @@ function Test-PasswordComplexity {
    Each class gets a complexity point and the password must include at least 3 classes.
    The length requirement defaults to 10, but can be overriden.
    Based on http://tompaps.blogspot.com/2018/01/verify-password-complexity-os.html
    With a few security enhancements in mind and using advanced function.
    #>
    [CmdletBinding(
    DefaultParameterSetName = 'SecurePassword'
  13. CJHarmath created this gist Aug 2, 2019.
    87 changes: 87 additions & 0 deletions Test-PasswordComplexity.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,87 @@
    function Test-PasswordComplexity {
    <#
    .SYNOPSIS
    Testing if a given password is complex
    .DESCRIPTION
    Based on the given SecureString or Credential the function tests if the password used is complex enough.
    The complexity is calculated based on the number of character classes use in the password.
    The classes are lower case letter, upper case letters, numbers and special characters.
    Each class gets a complexity point and the password must include at least 3 classes.
    The length requirement defaults to 10, but can be overriden.
    #>
    [CmdletBinding(
    DefaultParameterSetName = 'SecurePassword'
    )]
    [OutputType('PasswordComplexity')]
    param (
    [Parameter(Mandatory, ParameterSetName = 'SecurePassword')]
    [SecureString]
    $SecurePassword,

    [Parameter(Mandatory, ParameterSetName = 'Credential')]
    [PSCredential]
    $Credential,

    [Parameter()]
    [int]
    $MinPasswordLength = 10
    )
    try {
    if ($PSCmdlet.ParameterSetName -eq 'SecurePassword') {
    $passwordString = (New-Object PSCredential "user",$SecurePassword).GetNetworkCredential().Password
    } else {
    $passwordString = $Credential.GetNetworkCredential().Password
    }

    $valid = $true
    # checking the minimal length
    if($passwordString.length -lt $MinPasswordLength){
    $passwordLength = "length < {0}" -f $MinPasswordLength
    $valid = $false
    } else {
    $passwordLength = "length - OK"
    }

    $pwComplexity = 0
    # lowercase
    if($passwordString -cmatch "[a-z]"){
    $pwComplexity++
    }

    # uppercase
    if($passwordString -cmatch "[A-Z]"){
    $pwComplexity++
    }

    # digits
    if($passwordString -cmatch "[0-9]"){
    $pwComplexity++
    }


    # special character (not alphabetic characters or numbers)
    if($passwordString -cmatch "[^a-zA-Z0-9]"){
    $pwComplexity++
    }

    # if 3 of the criterias
    if($pwComplexity -ge 3){
    $complexity = "complex"
    }
    else{
    $complexity = "NOT complex"
    $valid = $false
    }

    [PSCustomObject]@{
    IsValid = $valid
    Length = $passwordLength
    Complexity = $complexity
    ComplexityScore = $pwComplexity
    PSTypeName = 'PasswordComplexity'
    }
    } catch {
    Write-Error -ErrorRecord $_
    }
    }