-
Notifications
You must be signed in to change notification settings - Fork 2
/
Enumerate-Mutex.ps1
204 lines (184 loc) · 7.3 KB
/
Enumerate-Mutex.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
# https://web.archive.org/web/20160110122741/http://alienvault-labs-garage.googlecode.com/svn/trunk/EnumerateMutex.cs
$code = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Permissions;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Collections;
namespace EnumerateMutex
{
public class Win
{
[StructLayout(LayoutKind.Sequential)]
public struct UNICODE_STRING : IDisposable
{
public ushort Length;
public ushort MaximumLength;
private IntPtr buffer;
public UNICODE_STRING(string s)
{
Length = (ushort)(s.Length * 2);
MaximumLength = (ushort)(Length + 2);
buffer = Marshal.StringToHGlobalUni(s);
}
public void Dispose()
{
Marshal.FreeHGlobal(buffer);
buffer = IntPtr.Zero;
}
public override string ToString()
{
return Marshal.PtrToStringUni(buffer);
}
}
[StructLayout(LayoutKind.Sequential)]
public struct OBJECT_ATTRIBUTES : IDisposable
{
public int Length;
public IntPtr RootDirectory;
private IntPtr objectName;
public uint Attributes;
public IntPtr SecurityDescriptor;
public IntPtr SecurityQualityOfService;
public OBJECT_ATTRIBUTES(string name, uint attrs)
{
Length = 0;
RootDirectory = IntPtr.Zero;
objectName = IntPtr.Zero;
Attributes = attrs;
SecurityDescriptor = IntPtr.Zero;
SecurityQualityOfService = IntPtr.Zero;
Length = Marshal.SizeOf(this);
ObjectName = new UNICODE_STRING(name);
}
public UNICODE_STRING ObjectName
{
get
{
return (UNICODE_STRING)Marshal.PtrToStructure(
objectName, typeof(UNICODE_STRING));
}
set
{
bool fDeleteOld = objectName != IntPtr.Zero;
if (!fDeleteOld)
objectName = Marshal.AllocHGlobal(Marshal.SizeOf(value));
Marshal.StructureToPtr(value, objectName, fDeleteOld);
}
}
public void Dispose()
{
if (objectName != IntPtr.Zero)
{
Marshal.DestroyStructure(objectName, typeof(UNICODE_STRING));
Marshal.FreeHGlobal(objectName);
objectName = IntPtr.Zero;
}
}
}
/*
* typedef struct _OBJECT_DIRECTORY_INFORMATION {
UNICODE_STRING Name;
UNICODE_STRING TypeName;
} OBJECT_DIRECTORY_INFORMATION, *POBJECT_DIRECTORY_INFORMATION;
*/
[StructLayout(LayoutKind.Sequential)]
public struct OBJECT_DIRECTORY_INFORMATION
{
public UNICODE_STRING Name;
public UNICODE_STRING TypeName;
}
[Flags]
public enum ProcessAccessFlags : uint
{
All = 0x001F0FFF,
Terminate = 0x00000001,
CreateThread = 0x00000002,
VMOperation = 0x00000008,
VMRead = 0x00000010,
VMWrite = 0x00000020,
DupHandle = 0x00000040,
SetInformation = 0x00000200,
QueryInformation = 0x00000400,
Synchronize = 0x00100000
}
/*Windows Functions*/
[DllImport("ntdll.dll")]
public static extern int NtOpenDirectoryObject(
out Microsoft.Win32.SafeHandles.SafeFileHandle DirectoryHandle,
uint DesiredAccess,
ref OBJECT_ATTRIBUTES ObjectAttributes);
[DllImport("ntdll.dll")]
public static extern int NtQueryDirectoryObject(
Microsoft.Win32.SafeHandles.SafeFileHandle DirectoryHandle,
IntPtr Buffer,
int Length,
bool ReturnSingleEntry,
bool RestartScan,
ref uint Context,
out uint ReturnLength);
}
public class Program$id
{
public static void Main()
{
Microsoft.Win32.SafeHandles.SafeFileHandle h;
string dEntry = "\\";
ArrayList entries = new ArrayList();
entries.Add(dEntry);
while (entries.Count != 0)
{
foreach (String entry in entries)
{
var attr = new Win.OBJECT_ATTRIBUTES(entry, 0);
var st = Win.NtOpenDirectoryObject(out h, 1, ref attr);
if (st < 0)
{
h.Dispose();
entries.Remove(entry);
break;
}
var bufsz = 1024;
var buf = Marshal.AllocHGlobal(bufsz);
uint context = 0, len;
while (true)
{
st = Win.NtQueryDirectoryObject(h, buf, bufsz, true, context == 0, ref context, out len);
if (st < 0)
{
entries.Remove(entry);
Marshal.FreeHGlobal(buf);
h.Dispose();
break;
}
var odi = (Win.OBJECT_DIRECTORY_INFORMATION)
Marshal.PtrToStructure(buf, typeof(Win.OBJECT_DIRECTORY_INFORMATION));
if (Convert.ToString(odi.TypeName) == "Mutant")
{
Console.WriteLine("0x{0:X2}:{1,-25}{2}", context, odi.TypeName, odi.Name);
}
if (Convert.ToString(odi.TypeName) == "Directory")
{
if (entry == "\\")
{
entries.Add(entry + Convert.ToString(odi.Name));
}
else
{
entries.Add(entry + "\\" + Convert.ToString(odi.Name));
}
}
}
break;
}
}
}
}
}
"@
Add-Type -TypeDefinition $code -Language CSharp
Invoke-Expression "[EnumerateMutex.Program$id]::Main()"