-
-
Save p0w3rsh3ll/28e083113133401a6e3c74bc5cbdf575 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Requires -Modules PSScriptAnalyzer | |
Function Test-ObfuscationHunter { | |
[CmdletBinding(DefaultParameterSetName='File')] | |
Param ( | |
[Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName,ParameterSetName = 'File',Position = 0)] | |
[ValidateNotNullOrEmpty()] | |
[Alias('FilePath', 'FileName')] | |
[string[]]$Path = ('.\*.ps1', '.\*.psm1'), | |
[Parameter(ValueFromPipelineByPropertyName,ParameterSetName = 'Code',Position=0)] | |
[ValidateNotNull()] | |
[Alias('Script', 'ScriptBlock')] | |
[string[]]$Code, | |
[Parameter(Position=1)] | |
[string]$ObfuscationHunterModuleFilePath= | |
"$((Get-Module -Name ObfuscationHunter -ListAvailable | | |
Select-Object -First 1 -ExpandProperty Path) -replace 'psd1','psm1')" | |
) | |
Begin { | |
try { | |
$HT = @{ | |
CustomizedRulePath = (Resolve-Path -Path $ObfuscationHunterModuleFilePath -ErrorAction Stop) | |
ExcludeRule = 'PS*' | |
} | |
} catch { | |
Write-Warning -Message "Failed because $($_.Exception.Message)" | |
} | |
} | |
Process { | |
if ($HT) { | |
try { | |
if ($PsBoundParameters['Code']) { | |
PSScriptAnalyzer\Invoke-ScriptAnalyzer -ScriptDefinition ` | |
"$(([System.Management.Automation.Language.Parser]::ParseInput($Code, [ref]$null, [ref]$null)).Extent)" @HT | |
} else { | |
Get-ChildItem -Path $Path -ErrorAction SilentlyContinue | | |
ForEach-Object { | |
PSScriptAnalyzer\Invoke-ScriptAnalyzer -Path $_.FullName @HT | |
} | |
} | |
} catch { | |
Write-Warning -Message "Failed because $($_.Exception.Message)" | |
} | |
} | |
} | |
End {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment