Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid Get-IcingaRandomChars to start with a special char #699

Closed
AlexMilotin opened this issue Mar 21, 2024 · 1 comment · Fixed by #703
Closed

Avoid Get-IcingaRandomChars to start with a special char #699

AlexMilotin opened this issue Mar 21, 2024 · 1 comment · Fixed by #703
Assignees
Labels
Bug There is an issue present
Milestone

Comments

@AlexMilotin
Copy link

We are currently in an ongoing action to configure a Password Rotation process for icinga local account..
The reason why we need to do this is because in our environment InfoSec does not allow local accounts with Password Never Expire flag enabled.

While working on the solution, re-purposing the already existing icinga powershell framework functions, i've noticed that on some servers this is failing. The reason is that the random char string generated for the new password is sometimes starting with "/" or "&"
image

I would suggest either having those 2 char excluded or have the function defined such that it would always starts with a letter of number.

This should be enough to avoid '/' and '&' from the string

function Get-RandomChars() {
    param (
        [int]$Count = 10,
        [string]$Symbols = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+=-{}[];:,.<>/?'
    )

    $RandomChars = ''

    if ([string]::IsNullOrEmpty($Symbols)) {
        return $RandomChars
    }

    do {
        $RandomChars = ''
        while ($Count -gt 0) {
            [int]$SymbolLength = $Symbols.Length
            $RandomValue = Get-Random -Minimum 0 -Maximum ($SymbolLength - 1)
            $RandomChars += $Symbols[$RandomValue]
            $Count -= 1
        }
    } until ($RandomChars[0] -ne '/' -and $RandomChars[0] -ne '&')

    return $RandomChars
}

This would ensure that the first char is always a lLetter or Number

function Get-RandomChars2() {
    param (
        [int]$Count = 10,
        [string]$Symbols = 'abcdefghiklmnoprstuvwxyzABCDEFGHKLMNOPRSTUVWXYZ1234567890!§$%&/()=?}][{@#*+'
    )

    $RandomChars = ''

    if ([string]::IsNullOrEmpty($Symbols)) {
        return $RandomChars
    }

    # Split the symbols into letters/numbers and special characters
    $LettersNumbers = [regex]::Matches($Symbols, '[a-zA-Z0-9]') | ForEach-Object { $_.Value }
    $SpecialChars = [regex]::Matches($Symbols, '[^a-zA-Z0-9]') | ForEach-Object { $_.Value }

    # Generate the first character (letter or number)
    $RandomValue = Get-Random -Minimum 0 -Maximum ($LettersNumbers.Count - 1)
    $RandomChars += $LettersNumbers[$RandomValue]
    $Count -= 1

    # Generate the remaining characters
    while ($Count -gt 0) {
        [int]$SymbolLength = $Symbols.Length
        $RandomValue = Get-Random -Minimum 0 -Maximum ($SymbolLength - 1)
        $RandomChars += $Symbols[$RandomValue]
        $Count -= 1
    }

    return $RandomChars
}
@LordHepipud LordHepipud self-assigned this Mar 25, 2024
@LordHepipud LordHepipud added this to the v1.12.0 milestone Mar 25, 2024
@LordHepipud LordHepipud added the Bug There is an issue present label Mar 25, 2024
@LordHepipud
Copy link
Collaborator

Thank you for the report. This is fixed with v1.12.0.
To update the user, simply use

New-IcingaWindowsUser;

This command will update the password with every call and has now a built in retry count of 10, in case the password fails for some reason. The two ambiguous characters were removed from the random generator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug There is an issue present
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants