1
+ function Get-ProcessMiniDump {
2
+ <#
3
+ . SYNOPSIS
4
+ Create process dump using Dbghelp::MiniDumpWriteDump.
5
+
6
+ . DESCRIPTION
7
+ Author: Ruben Boonen (@FuzzySec)
8
+ License: BSD 3-Clause
9
+ Required Dependencies: None
10
+ Optional Dependencies: None
11
+
12
+ . PARAMETER ProcID
13
+ PID for the target process.
14
+
15
+ . PARAMETER Path
16
+ Dump outfile path.
17
+
18
+ . EXAMPLE
19
+ C:\PS> Get-ProcessMiniDump -ProcID 1234 -Path C:\Some\File\Path.out
20
+ #>
21
+
22
+ [cmdletbinding ()]
23
+ param (
24
+ [Parameter (Mandatory = $True )]
25
+ [Int ]$ProcID ,
26
+ [Parameter (Mandatory = $True )]
27
+ [String ]$Path
28
+ )
29
+
30
+ Add-Type - TypeDefinition @"
31
+ using System;
32
+ using System.Diagnostics;
33
+ using System.Runtime.InteropServices;
34
+ using System.Security.Principal;
35
+
36
+ public class GetProcessMiniDump
37
+ {
38
+ [DllImport("Dbghelp.dll")]
39
+ public static extern bool MiniDumpWriteDump(
40
+ IntPtr hProcess,
41
+ uint ProcessId,
42
+ IntPtr hFile,
43
+ int DumpType,
44
+ IntPtr ExceptionParam,
45
+ IntPtr UserStreamParam,
46
+ IntPtr CallbackParam);
47
+ }
48
+ "@
49
+
50
+ # Check PID
51
+ $IsValidProc = (Get-Process | Select - Expand Id) -Contains $ProcID
52
+ if (! $IsValidProc ) {
53
+ Write-Verbose " [!] The specified PID does not exist!"
54
+ $false
55
+ Return
56
+ }
57
+
58
+ # Guesstimate if elevated privs are required
59
+ $WhoAmI = [Environment ]::UserName
60
+ Write-Verbose " [?] Running as: $WhoAmI "
61
+ $TargetPIDUser = (Get-WmiObject Win32_Process - Filter " ProcessId = $ProcID " ).GetOwner().User
62
+ if ($WhoAmI -ne $TargetPIDUser ) {
63
+ Write-Verbose " [?] Administrator privileges required"
64
+ $IsAdmin = ([Security.Principal.WindowsPrincipal ][Security.Principal.WindowsIdentity ]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole ]' Administrator' )
65
+ if (! $IsAdmin ) {
66
+ Write-Verbose " [!] Administrator privileges not held!"
67
+ $false
68
+ Return
69
+ } else {
70
+ Write-Verbose " [>] Administrator privileges held"
71
+ }
72
+ }
73
+
74
+ # Get handle for minidump outfile
75
+ try {
76
+ $FileStreamObject = [System.IO.File ]::Create($Path )
77
+ } catch {
78
+ $ExceptionMsg = $_.Exception.Message
79
+ Write-Verbose " [!] $ExceptionMsg "
80
+ $false
81
+ Return
82
+ }
83
+
84
+ # Full Process Dump
85
+ # -----
86
+ # MiniDumpIgnoreInaccessibleMemory = 0x00020000
87
+ # MiniDumpWithDataSegs = 0x00000001
88
+ # MiniDumpWithFullMemory = 0x00000002
89
+ # MiniDumpWithFullMemoryInfo = 0x00000800
90
+ # MiniDumpWithHandleData = 0x00000004
91
+ # MiniDumpWithProcessThreadData = 0x00000100
92
+ # MiniDumpWithThreadInfo = 0x00001000
93
+ # MiniDumpWithTokenInformation = 0x00040000
94
+ # => 0x00061907
95
+ # -----
96
+ $hProc = (Get-Process - Id $ProcID ).Handle
97
+ $IsDumped = [GetProcessMiniDump ]::MiniDumpWriteDump($hProc , $ProcID , $FileStreamObject.Handle , 0x00061907 , [IntPtr ]::Zero, [IntPtr ]::Zero, [IntPtr ]::Zero)
98
+ $FileStreamObject.Close ()
99
+ if (! $IsDumped ) {
100
+ Write-Verbose " [!] Process dump failed!"
101
+ Remove-Item $FileStreamObject.Name
102
+ $false
103
+ } else {
104
+ Write-Verbose " [>] Process dump success!"
105
+ $true
106
+ }
107
+ }
0 commit comments