<# FPS:Containers:DataResources:Private:Put-KeePass:ACTIVE
#>
function Put-KeePass {
<#
.DESCRIPTION
Module: DataResources
Public CmdLet: Put-KeePass.ps1
Dependencies: NA
This function performs a change of the KeePass directory
password.
Called when update may be needed. The Get-KeePass object is stored in the image
specified within the CI GUID.
.EXAMPLE
if (Push-NeededFor Put-KeePass)
{ . (Get-ActiveFileNm -PS1
Put-KeePass) }
$Testing = $True # Leaves the scratch files
if ($Testing) {
Put-KeePass
-PWD "FoundationalOps" -Testing
} else {
Put-KeePass
-PWD "FoundationalOps"
}
-or-
# This will set the script in an image other than the
image dictated by the GUID
$OverrideImageNum = '0143' # Manually zero fill to 4 digits
Put-KeePass -PWD "FoundationalOps"
.CC
[2024.11.19 JD] Total re-write. Use of imagery used to
store the passkey within an image. Dual factor with Source being the constant
source.
[2024.11.08 JD] Logging added due to the importance of
the utility.
[2024.10.19 JD] Original code release
.NOTES
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)] [string]$Pwd,
[Parameter(Mandatory=$false)] [switch]$Testing
)
#region Initialization
$overrideSrc, $overrideSnippet = '','' #
Initialize override variables
. (Get-ActiveFileNm -PS1 Initialize-DataResources-Locations)
. (Get-ActiveFileNm -PS1 Load-DataResources-Dependencies)
$Sev, $Module, $Element = "Info", "DataResources", "Get-KeePass"
$Buffer = "KP
Default DB Entry Modified by "+$global:pro_CI.SvcAcct
Update-Log -Sev $Sev -Module $Module -Element $Element -Buffer $Buffer -LogFile (Mount-Path($drcLoc_Logs))
if ($OverrideImageNum -gt '') {
$ImageNum = $OverrideImage
} else {
$SpGuid = $global:pro_CI.GUID.Split('-')
$ImageNum = $SpGuid[3]
}
$SourceImageJpg = $drcLoc_SourceImagePath+'\'+$ImageNum+'-3000x2000.jpg'
$SourceImagePng = $drcLoc_SourceImagePath+'\'+$ImageNum+'-3000x2000.png'
$RuntimeImage = $drcLoc_RuntimeImagePath+'\'+$ImageNum+'-3000x2000.png'
$ScrImage = $drcLoc_ScratchPath+'\'+$ImageNum+'-3000x2000.png'
$ScrFile = $drcLoc_ScratchPath+'\Get-KeePass.ps1'
if ((Test-Path $SourceImageJpg) -and ($SourceImagePng)) {
# Performed upon entry so the last version will be in
scratch for testing
if ($PSBoundParameters.Testing -eq $true) {
if (Test-Path $ScrImage) {
Remove-Item $ScrImage }
if (Test-Path $ScrFile) {
Remove-Item $ScrFile }
}
# The source image will always be fresh - runtime could
get mucked up over time, so bring a fresh image down before writing to it.
new-item $ScrFile -type file | Out-Null
add-content $ScrFile ('function Get-KeePass { ')
add-content $ScrFile ('[CmdletBinding()]')
add-content $ScrFile ('param(')
add-content $ScrFile (')')
add-content $ScrFile ('$overrideSrc, $overrideSnippet = "",""')
add-content $ScrFile ('. (Get-ActiveFileNm
-PS1 Initialize-DataResources-Locations)')
add-content $ScrFile ('. (Get-ActiveFileNm
-PS1 Load-DataResources-Dependencies)')
add-content $ScrFile ('$Sev, $Module, $Element =
"Info", "DataResources",
"Get-KeePass"')
add-content $ScrFile ('$Buffer = "KP Default DB Entry Used
by "+$global:pro_CI.SvcAcct')
add-content $ScrFile ('Update-Log -Sev $Sev -Module $Module
-Element $Element -Buffer $Buffer -LogFile
(Mount-Path($drcLoc_Logs))')
add-content $ScrFile ('$encryptedPasswordFromFile,
$encryptedKeyFromFile, $secureDecryptedPassword=
"","",""')
add-content $ScrFile ('$BSTR1, $BSTR2 =
"",""')
$str = [string](Get-ItemProperty -Path $SourceImageJpg | select lastwritetime)
$key = (($str.substring(19,16)).replace(" ","*")).replace("/",".")
$secureKey = ConvertTo-SecureString $key -AsPlainText -Force
$encryptedKey = ConvertFrom-SecureString $secureKey -Key (1..16)
add-content $ScrFile ('$encryptedKey =
"'+$encryptedKey+'"')
$securePassword = ConvertTo-SecureString $Pwd -AsPlainText -Force
$encryptedPassword = ConvertFrom-SecureString $SecurePassword -SecureKey $SecureKey
add-content $ScrFile ('$encryptedPassword
= "'+$encryptedPassword+'"')
add-content $ScrFile ('$secureDecryptedKey
= ConvertTo-SecureString $encryptedKey
-Key (1..16)')
add-content $ScrFile ('$secureDecryptedPassword
= ConvertTo-SecureString $encryptedPassword
-SecureKey $secureDecryptedKey')
add-content $ScrFile ('$BSTR1 = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secureDecryptedPassword)')
add-content $ScrFile ('$textPassword =
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR1)')
add-content $ScrFile ('$BSTR2 = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secureDecryptedKey)')
add-content $ScrFile ('$key = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR2)')
add-content $ScrFile ('# Write-Host "Key: $key"')
add-content $ScrFile ('# Write-Host "Text Password: $textPassword"')
add-content $ScrFile ('$textPassword')
add-content $ScrFile ('# Write-Host "Encrypted Password: $encryptedPassword"')
add-content $ScrFile ('# Write-Host "Encrypted Key: $encryptedKey"')
add-content $ScrFile ('}')
# The source image will always be fresh - runtime could
get mucked up over time, so bring a fresh image down before writing to it.
PSImage -Script $ScrFile -Out $ScrImage -Image $SourceImagePng
if (Test-Path $RuntimeImage) {
Remove-Item $RuntimeImage }
Copy-Item $ScrImage $RuntimeImage
if (!($PSBoundParameters.Testing -eq $true)) {
Remove-Item $ScrImage
Remove-Item $ScrFile
}
} else { # If image exists
Write-Host "Source file does not exist"
Write-Host ("Source JPG: $SourceImageJpg")
Write-Host ("Source PNG: $SourceImagePng")
}
}