Do you know you can encrypt a PowerShell script and run the encrypted script?

#PowerShellTrick #Count1


Use case: You want to keep the script on a shared path so that it's accessible for running but don't want others to see the code inside. Another benefit, this ties the script to one machine, one user and even though someone can bypass PowerShell executionpolicy, they cannot bypass this and no modification in script would be possible unless you have access to machine and the user credential

?

# It would encrypt the script content using Windows Data Protection API using your user account and machine name. Means it can be decrypted ONLY on the same machine and by the same user account

?

# It would decrypt and run the script ONLY on the same machine and with the same user logged in, through which it was encrypted.

Function Export-EncryptedScript {
[cmdletbinding()]
param (
[parameter(Mandatory=$true, ValueFromPipeline=$True)][validatescript({Test-Path $_})][string]$ScriptPath,
[parameter(Mandatory=$true, ValueFromPipeline=$True)][string]$ExportPath
)

[string]$Code = Get-content -Path $ScriptPath
$SecureCode = ConvertTo-SecureString $Code -AsPlainText -Force
$null = ConvertFrom-SecureString -SecureString $SecureCode | Out-File -FilePath $ExportPath
}
        
Function Run-EncryptedScript {

[cmdletbinding()]
param (
[parameter(Mandatory=$true, ValueFromPipeline=$True)][validatescript({Test-Path $_})][string]$ScriptPath,
[parameter(Mandatory=$false, ValueFromPipeline=$True)][switch]$ShowOnly
) 

[string]$scriptCode = Get-Content $ScriptPath
$decryptedCode = $scriptCode | ConvertTo-SecureString
$Code = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($decryptedCode)) 

if($ShowOnly){ 
$Code -split "\t" 
} else { 
Invoke-Expression $Code 
}
}        

?

If you are looking to run it with a service account, then just right click PowerShell, run as that user then perform the encryption so that when you call decryption in the same service account context then it can work.

?

PS: If required then the code can be updated so that it doesn't make use of WDPA but of a key file storing 32-byte key. That way it would be portable and can be run from any machine or user.

MP van Sijll

ICT—Co?rdinator / Azure Systeembeheerder

1 年

It's only useful when you're allowed to make an encrypted powershell script that only you can read or execute in/on a company's network that allows you to do that. Unfortunately....., I could not think of such situation. As a matter of fact, it would give them a reason to fire my position as system administrator.

回复

要查看或添加评论,请登录

Nitish Kumar的更多文章

  • Creating a Chart image in PowerShell

    Creating a Chart image in PowerShell

    As many of you know that PowerShell can tap into potential of .Net methods easily available with Microsoft Windows…

    2 条评论
  • PS script for horizontal tabbed HTML Report

    PS script for horizontal tabbed HTML Report

    It's just another version of the earlier PS script, to create the report with horizontal tabs this time. Likely in…

  • Tabbed HTML output by PS script

    Tabbed HTML output by PS script

    Just a weekend musing. Tried to create a function which if given array input with columns Title and content, then it…

  • Getting license report for O365 with real names

    Getting license report for O365 with real names

    I am sure most of you don't need my help in getting License report from O365 via PowerShell, I actually covered that in…

  • PS Script to get meeting details via graph

    PS Script to get meeting details via graph

    #PowerShell #MicrosoftGraph #Scripting I had posted about a simple script to get your meeting related details in…

  • Getting status of your Teams meeting invite (MS graph)

    Getting status of your Teams meeting invite (MS graph)

    Just a small code to get a list of your meeting invitees and their responses to your meeting along with their…

  • Balloon notification with PowerShell

    Balloon notification with PowerShell

    What if you running a script which takes long time and covers various tasks, it can quickly get boring, and you don't…

  • How to handle credentials in PowerShell for unattended tasks?

    How to handle credentials in PowerShell for unattended tasks?

    Hard-coding credentials in script is considered cardinal sin and been the source of countless breaches. No matter, big…

  • Microsoft Defender for Identity

    Microsoft Defender for Identity

    Why defender for identity? Security perimeter isn't physical or network anymore, but identity becomes another end, in a…

  • How to start with learning PowerShell?

    How to start with learning PowerShell?

    Learning a new language always looks messy, be it the case that you never knew programming or be it the case that you…

社区洞察

其他会员也浏览了