r/activedirectory • u/poolmanjim Moderator | Microsoft MVP • Mar 21 '25
Help Thoughts on storing user creds encrypted using certificate private key for a automated backup script
Sorry for the long post, it's a lot to cover, so bear with me.
TL;DR - Do you see any security concerns that I have not addressed with storing user credentials for a script using certificate private keys to encrypt the secure string to generate a "password hash" of sorts?
If you didn't already know I've been (still am) working on a "Not-So-Enterprise AD Backup Solution/Script/Process". I'm currently in the last mile of the planning and development of the initial release.
My question is do you think the process I will soon detail is as secure as possible. Basically am I missing something before I waste a boat load of time on fitting it in.
The backup process requirements (at least as far as this conversation is concerned).
- Cannot be AD-joined. This is for restoring AD after-all.
- As few dependencies as possible. No additional modules, scripts, apps, etc. if we can help it.
- Cheap. I don't want this to be an expensive thing for people to deploy.
What's happening is an off-domain archive server (ARCHIVE01) is reaching out to the DCs who are running Windows Server Backup to a local volume. This archive server will copy the backup files to the archive server. In this design the DC itself does not have access to the archive server. The archive server can read the shares on the DC but cannot write them.
For this to work, the domain requires a service account (SvcArchive) that has read permissions on the DC backup directories. The archive server maps to the shared Backup folders that can only be read by the SvcArchive user. I need to store the creds for the SvcArchive account in a way that can be non-interactively and programmatically retrieved. I'm also going to have multi-domain support so imagine several of these service accounts.
I'm storing all the config data as JSON files so, naturally, I want to include the credentials there.
The Process
To solve this, the credentials will be initially manfully input via PowerShell, here's an example, but not in plain-text of course.
ConvertTo-SecureString -String "Password01!" -AsPlainText -Force # Yes, I know this is bad. It's just an example for here.
The challenge is that the secure string could be exported to CliXml but that is user-bound. Meaning to have this for SYSTEM, is a challenge.
I know that you can specify a key for the SecureString so you get something that looks like this.
$PasswordSS = ConvertTo-SecureString -String "Password01!" -AsPlainText -Force
$PasswordEnc = ConvertFrom-SecureString -SecureString $PasswordSS -Key $Key -ErrorAction Stop
If you didn't see it, the challenge now is I have traded plain-text passwords for plain-text keys. Well here's where my question takes shape: what if I used certificates?
Here's the detail
- I generate a self-signed certificate that has an exportable key. Self signed because no PKI. This is off domain (don't worry a version of this will have PKI support).
- Using PowerShell I extract the private key from this.
$Certificate = (Get-ChildItem -Path "Cert:\LocalMachine\My" | Where-Object { $_.FriendlyName -eq $BackupCertificateFriendlyName })($Certificate.PrivateKey).Key.Export([System.Security.Cryptography.CngKeyBlobFormat]::Pkcs8PrivateBlob)
- I generate a hash of that key. This is done because
ConvertFrom-SecureString -Keyhas size limitations. SHA512 fits right into one of them.$Sha256 = [System.Security.Cryptography.SHA256]::Create()$Sha256HashBlob = $Sha256.ComputeHash( $KeyBytes )ConvertFrom-SecureString -SecureString $SecureString -Key $Sha256HashBlob -ErrorAction Stop
- I can take the output from
ConvertFrom-SecureString -Keyand toss that into the JSON file and decrypt it on demand. - When I need to decrypt the JSON credential later, I can just read the private key again and all is well.
Address the questions you're probably going to have
- Why not use a vaulting solution (CyberArk, Azure Vault, etc.)?
- Answer: Dependencies. I am assuming ALL the corporate infrastructure has burned down and ins compromised. Thus another solution, is a risk.
- Rebuttal: I do intend to include some support for this later, but that is down the road.
- Why not use Windows Credential Manager?
- Answer: Have you tried doing that in PowerShell? Even with the module it is kind of a joke. Also, it ultimately still requires a key to be stored in plain text.
- Why not use PKI?
- Answer: Dependencies again. PKI is burnt down or compromised. Self-signed is all we have.
- Don't all administrators have read access to Private Keys on machine certs?
- Answer: Yes. Access to the box is going to be heavily restricted.
- Why didn't you do [insert thing here] security to protect the archive server?
- Answer: I probably did. I just didn't enumerate the entire architecutre here. I'm still writing it all down.
- Why not use Azure Backup?
- Answer: Didn't say I wouldn't. But again, everything is compromised in the design.
- Why not use [insert enterprise product for backups here]?
- Answer: Not everyone has budget for Semperis, Quest, Veeam, Rubrik, etc. Even places that should, don't always have it. This is fully intended to be a plan B.
- Windows Backup sucks. Why are you using it?
- Answer: It's free. It's first party.
In conclusion, do you see any glaring holes in this design that I didn't address? All ideas are welcome. I really want to make sure I'm doing the best I can with a very rigid set of requirements.
•
u/AutoModerator Mar 21 '25
Welcome to /r/ActiveDirectory! Please read the following information.
If you are looking for more resources on learning and building AD, see the following sticky for resources, recommendations, and guides!
When asking questions make sure you provide enough information. Posts with inadequate details may be removed without warning.
Make sure to sanitize any private information, posts with too much personal or environment information will be removed. See Rule 6.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
3
u/[deleted] Mar 22 '25
I can’t possibly weigh in on it all but still a couple suggestions…
You may even be able to use certificate based authentication against your backup source. It would certainly a lot less convoluted and a good bit more secure. You’d have to put that certificate into a physical safe but you would need to do that with any emergency credentials.
2
u/dcdiagfix Mar 22 '25
…. Watching with interest ;)
2
u/Lanky_Common8148 Mar 22 '25
When I've HAD to do things like this in the past I've used a key file encrypted using ConvertTo-Secure String via a script running as a scheduled task under the machines system context. Once it's created the key is retrievable only via the system account on the machine so another scheduled task is able to read the blob from the file and read the plaintext password. Obviously if I can clone the machine then we're toast. The one thing I'd point out though is that online warm backups, however they're stored are usually toast during any malware outbreak. I've seen plenty of off domain solutions that still get infected and all that happens is you spend additional time recovering the recovery solution first. We spent months testing various solutions against native windows backup and we honestly found that it was quicker and even more crucially had strong vendor support. A lot of the enterprise grade solutions simply weren't reliable or faster and were a faff to use and a nightmare to get support on. So please, supportability is the critical element. If this is FOSS then brilliant but users need to understand how to use it and it's recovery from its own loss needs to be fast and baked into the solution. All that said though, I will follow this with interest. AD is severely lacking a decent 3rd party backup/recovery solution and if you pull this off it'll be absolutely epic
2
u/Borgquite Mar 23 '25
If you’re just wanting to secure some strings using DPAPI in a way that only Administrators and SYSTEM can read, you might find it simpler to copy the [DpapiNgUtil]::ProtectBase64 and [DpapiNgUtil]::UnprotectBase64 function usage in these scripts rather than trying to (?ab?)use the DPAPI protections of certificate private keys you’ve done here:
https://github.com/Azure/ArcEnabledServersGroupPolicy/
2
u/PowerShellGenius Mar 28 '25
You are storing copies of the AD database on this archive server. Those contain the krbtgt secret. How are you protecting those?
The backups themselves, and the password to the service account that can download them from the DC, are both equally tier 0 assets.
The service account's only power is to retrieve those backups from the DC, and it's possession of those backups that allows you to take over the domain. They are equivalent to physical access to a non-BitLocker writeable DC.
If the backups are encrypted on your archive server, encrypt the password too.
If the backups are protected only by NTFS permissions and/or restrictions on who can access the Archive server - then it sounds bad to say it out loud, but I can't see how that password in a .txt file (protected the same way as the AD backups) is any worse. Its only power is to get those backups.
IN ANY CASE- your archive server is tier 0, no less than a DC, if it is handling backups of writeable DCs.
2
u/jg0x00 Mar 22 '25 edited Mar 22 '25
Use export-clixml and import-clixml
Data is encrypted. The only security context that can get the string is THAT user on THAT computer.
I see from other comments you do not like this because you need to act as system, and cannot use PSexce - So make a task that has to be run manually; have it run as system
Have the script read a text file and set the user and pass from info in the text file. Then when you need to change creds, you populate the text file, run the task as system, it sets the creds in the clixml, and then deletes the text file.
-1
u/xxdcmast Mar 22 '25
Why not just psexec -i -s -d PowerShell.exe then store your
ConvertTo-SecureString -String "Password01!" -AsPlainText -force
Or however you plan to save the creds.
0
u/[deleted] Mar 22 '25
[deleted] — view deleted comment