Skip to content

Instantly share code, notes, and snippets.

@Dump-GUY
Created April 4, 2024 02:29
Show Gist options
  • Save Dump-GUY/905012a32c00d1fc94408431e2a1386a to your computer and use it in GitHub Desktop.
Save Dump-GUY/905012a32c00d1fc94408431e2a1386a to your computer and use it in GitHub Desktop.
PowerShell IEX Hooking via Harmony Library
# Harmony Reference: https://github.com/pardeike/Harmony
using assembly '.\net48\0Harmony.dll'
using namespace HarmonyLib
class HooK
{
static [bool] PreFix_IEX($scriptText)
{
[Console]::WriteLine("Original IEX Command: '$scriptText'")
return $true
}
}
function Hook-IEX()
{
[Harmony]$Global:harmony = [Harmony]::new("IEXPatch")
$target = [AccessTools]::Method("System.Management.Automation.CommandInvocationIntrinsics:NewScriptBlock")
$patch = ([HooK]).GetMethod("PreFix_IEX")
$Global:harmony.Patch($target, [HarmonyMethod]::new($patch)) | Out-Null
}
function UnHook-IEX()
{
$Global:harmony.UnpatchAll()
}
@Dump-GUY
Copy link
Author

Dump-GUY commented Apr 4, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment