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