Skip to content

Commit 854ef78

Browse files
authored
Fix: Improved security for random character generator (#814)
Fixes random chars function to truly generate unpredictable character sequences and to replace `Get-Random` which is not entirely secure
1 parent 440a046 commit 854ef78

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

doc/100-General/10-Changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
1515

1616
[Issues and PRs](https://github.com/Icinga/icinga-powershell-framework/milestone/42)
1717

18+
### Bugfixes
19+
20+
* [#814](https://github.com/Icinga/icinga-powershell-framework/pull/814) Fixes random chars function to truly generate unpredictable character sequences and to replace `Get-Random` which is not entirely secure
1821
* [#815](https://github.com/Icinga/icinga-powershell-framework/pull/815) Fixes a possible crash for `Test-IcingaAddTypeExist`, causing the Icinga for Windows installation to fail when third party components are checked which are malfunctioning
1922

2023
## 1.13.3 (2025-05-08)

lib/core/windows/Get-IcingaRandomChars.psm1

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,28 @@ function Get-IcingaRandomChars()
1111
return $RandomChars;
1212
}
1313

14-
while ($Count -gt 0) {
14+
[int]$SymbolLength = $Symbols.Length;
15+
$CryptoProvider = New-Object System.Security.Cryptography.RNGCryptoServiceProvider;
16+
$ByteValue = New-Object Byte[] 4;
17+
$maxValid = [uint32]::MaxValue - ([uint32]::MaxValue % $SymbolLength);
1518

16-
[int]$SymbolLength = $Symbols.Length;
17-
$RandomValue = Get-Random -Minimum 0 -Maximum ($SymbolLength - 1);
18-
$RandomChars += $Symbols[$RandomValue];
19-
$Count -= 1;
19+
for ($index = 0; $index -lt $Count; $index++) {
20+
do {
21+
# Generate random bytes
22+
$CryptoProvider.GetBytes($ByteValue);
23+
$RandomNumber = [BitConverter]::ToUInt32($ByteValue, 0);
24+
# Ensure the random number is within the valid range to avoid maximum security
25+
} while ($RandomNumber -ge $maxValid);
26+
27+
# Calculate the index for the symbol array
28+
$randomIndex = $RandomNumber % $SymbolLength;
29+
$RandomChars += $Symbols[$randomIndex];
2030
}
2131

32+
# Clean up
33+
$CryptoProvider.Dispose();
34+
$CryptoProvider = $null;
35+
$ByteValue = $null;
36+
2237
return $RandomChars;
2338
}

0 commit comments

Comments
 (0)