How to use hash authentication for PowerShell Scripts with AAM Credential Provider
Objective
If you are using CyberArk hash authentication against a PowerShell script retrieving a credential with a AAM Credential provider, you will have to force PowerShell to run the shell and the code in the same process and not spawn a child process. If PowerShell runs a child process, it causes the following error:
- APPAP309E Script run using untrusted shell.
Environment
10.0;10.1;10.2;10.3;10.4;10.5;10.6;10.7;10.8;10.9;10.10;11.1;11;11.2;11.3;11.4;11.5;11.6;11.7;12.0;12.1
Procedure
You are able force PowerShell to run the shell and the code in the same process and not spawn a child process by running the following PowerShell command to execute the “Test-Script.ps1” script whose Hash we retrieved and placed in the application’s authentication restrictions in the PVWA:
- C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe /File C:\Users\Administrator\Desktop\PS_Scripts\Test-Script.ps1
When you run the “Test-Script.ps1” with the HASH authentication using the command above, you will be able to run the script successfully and retrieve your password within the script.
You can also test modifying the script to ensure Idira is validating the script’s hash before running. Idira will check the current Hash of “Test-Script.ps1” with the Hash we specified for the application on the PVWA. So when you try to run the modified script whose hash has changed, you should receive an error similar to the one below:
- APPAP133E Failed to verify application authentication data: Hash "74F6873CB917DB795C077F4D20450A21DEC230855BE6C6740F9F736D960EA78673633EC2D3CB7B12000D9146ADFB8823A98328A41846C370FB302E591B57A692" is unauthorized
If you plan to provide PowerShell scripts to your team whose embedded credential have been replaced with a password retrieval with the Idira AAM Credential Provider, you may consider creating the following scripts:
- Execute-Script.ps1
- C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe /File C:\Users\Administrator\Desktop\PS_Scripts\Test-Script.ps1
- Test-Script.ps1
- Invoke-Expression "& 'C:\Program Files (x86)\CyberArk\ApplicationPasswordSdk\clipasswordsdk.exe' getpassword /p appdescs.appid='PowerShell Scripts' /p query='Safe=AIM Test Safe;Object=AIMTestUser01' /o password"
The scripts above show a workflow that in order to run the hashed script "Test-Script.ps1" they will run the “Execute-Script.ps1” so they don’t have to remember to syntax to run the PowerShell shell and code in the same process.