Repository navigation
Expand file tree
/
Copy pathPersistence.cs
More file actions
376 lines (342 loc) · 17.2 KB
/
Copy pathPersistence.cs
File metadata and controls
376 lines (342 loc) · 17.2 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
using System.Runtime.InteropServices;
using System.Text.Json;
using Microsoft.Win32;
namespace Argus;
// -------------------------------------------------------------------------------------------------
// "Programs that keep coming back".
//
// Some analytics programs are restarted by a Windows service, a scheduled task or a startup entry. Closing them again and
// again is pointless; the clean fix is to disable what restarts them. This file finds those starters and can disable and
// restore them.
//
// Safety:
// * never offered for core Windows programs (the same rails as everywhere else)
// * nothing is deleted: a service is set to Disabled, a task is switched off, a startup entry is un-ticked
// * every change is written to a list (disabled-by-argus.json) so it can be restored from the Rules page
// * services and system tasks need administrator rights; without them the change is refused and nothing is touched
// -------------------------------------------------------------------------------------------------
internal enum PersistKind { Service, ScheduledTask, StartupEntry }
internal sealed class PersistenceSource
{
public PersistKind Kind { get; set; }
public string Name { get; set; } = ""; // service key name / full task path / startup entry name
public string Display { get; set; } = "";
public string Detail { get; set; } = ""; // how it starts, in plain words
public bool NeedsAdmin { get; set; }
public StartupSource StartupSrc { get; set; } // only for startup entries
public string KindLabel => Kind switch { PersistKind.Service => "Windows service", PersistKind.ScheduledTask => "scheduled task", _ => "startup entry" };
}
internal sealed class DisabledRecord
{
public PersistKind Kind { get; set; }
public string Name { get; set; } = "";
public string Display { get; set; } = "";
public int PreviousStart { get; set; } // services: the start type before it was disabled (2 automatic, 3 manual, ...)
public StartupSource StartupSrc { get; set; }
public string Exe { get; set; } = "";
public DateTime WhenUtc { get; set; }
}
/// <summary>Reads and changes a service's start type. A seam so tests never touch a real service.</summary>
internal interface IServiceControl
{
int? GetStart(string name);
/// <summary>"ok", or a short human-readable reason it was not changed.</summary>
string SetStart(string name, int start);
}
internal sealed class ScmServiceControl : IServiceControl
{
private const uint SC_MANAGER_CONNECT = 1, SERVICE_CHANGE_CONFIG = 2, SERVICE_NO_CHANGE = 0xFFFFFFFF;
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern IntPtr OpenSCManager(string? machine, string? database, uint access);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern IntPtr OpenService(IntPtr scm, string name, uint access);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool ChangeServiceConfig(IntPtr svc, uint serviceType, uint startType, uint errorControl, string? binaryPath, string? loadOrderGroup,
IntPtr tagId, string? dependencies, string? account, string? password, string? displayName);
[DllImport("advapi32.dll")] private static extern bool CloseServiceHandle(IntPtr handle);
public int? GetStart(string name)
{
try
{
using var k = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\" + name);
return k?.GetValue("Start") is int s ? s : null;
}
catch { return null; }
}
public string SetStart(string name, int start)
{
IntPtr scm = OpenSCManager(null, null, SC_MANAGER_CONNECT);
if (scm == IntPtr.Zero) return Describe(Marshal.GetLastWin32Error());
try
{
IntPtr svc = OpenService(scm, name, SERVICE_CHANGE_CONFIG);
if (svc == IntPtr.Zero) return Describe(Marshal.GetLastWin32Error());
try
{
return ChangeServiceConfig(svc, SERVICE_NO_CHANGE, (uint)start, SERVICE_NO_CHANGE, null, null, IntPtr.Zero, null, null, null, null)
? "ok" : Describe(Marshal.GetLastWin32Error());
}
finally { CloseServiceHandle(svc); }
}
finally { CloseServiceHandle(scm); }
}
private static string Describe(int error) => error switch
{
5 => "needs administrator rights (use Rules > Administrator mode)",
1060 => "that service no longer exists",
_ => $"Windows refused (error {error})",
};
}
internal static class Persistence
{
/// <summary>Replaced by tests so they never change a real service.</summary>
internal static IServiceControl Services = new ScmServiceControl();
private static string RecordsPath => Path.Combine(Config.Dir, "disabled-by-argus.json");
private static readonly object Lock = new();
// ---- finding the starters ------------------------------------------------------------------------------------------
/// <summary>Every service, scheduled task and startup entry that starts this program. Empty for core Windows programs.</summary>
public static List<PersistenceSource> Find(string? exePath)
{
var list = new List<PersistenceSource>();
if (string.IsNullOrWhiteSpace(exePath)) return list;
string name = Path.GetFileNameWithoutExtension(exePath);
if (Safety.IsNeverAuto(name, 0, exePath)) return list; // never the plumbing of core Windows
try { list.AddRange(FindServices(exePath, ReadServices())); } catch { }
try { list.AddRange(FindTasks(exePath)); } catch { }
try { list.AddRange(FindStartup(exePath)); } catch { }
return list;
}
/// <summary>
/// A "keeps coming back" finding for one program, listing everything that starts it. Null when nothing does (or it is core Windows).
/// Used by the watcher after repeated closes and by the "What restarts it?" button. Nothing is changed.
/// </summary>
public static Finding? FindingFor(string name, string? path, string reasonPrefix = "")
{
var sources = Find(path);
if (sources.Count == 0) return null;
return new Finding
{
Key = "respawn:" + name, Category = Category.Respawn, ProcName = name, Path = path, Sources = sources,
Reason = reasonPrefix + "Restarted by: " + string.Join("; ", sources.Select(s => $"{s.KindLabel} \"{s.Display}\"")),
};
}
internal static bool SameExe(string? commandOrPath, string target)
{
if (string.IsNullOrWhiteSpace(commandOrPath)) return false;
string? exe = Perf.ExePath(commandOrPath.Trim());
if (exe == null) return false;
try { return Path.GetFullPath(exe.Trim('"')).Equals(Path.GetFullPath(target), StringComparison.OrdinalIgnoreCase); }
catch { return false; }
}
private static IEnumerable<(string Name, string? Display, string? ImagePath, int Start)> ReadServices()
{
using var root = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services");
if (root == null) yield break;
foreach (var name in root.GetSubKeyNames())
{
string? image = null, display = null; int start = 3;
try
{
using var k = root.OpenSubKey(name);
if (k == null) continue;
image = k.GetValue("ImagePath") as string;
display = k.GetValue("DisplayName") as string;
if (k.GetValue("Start") is int s) start = s;
}
catch { continue; }
yield return (name, display, image, start);
}
}
/// <summary>Pure matching, so it can be tested with made-up services.</summary>
internal static List<PersistenceSource> FindServices(string exe, IEnumerable<(string Name, string? Display, string? ImagePath, int Start)> services)
{
var found = new List<PersistenceSource>();
foreach (var (name, display, image, start) in services)
{
if (start == 4 || string.IsNullOrWhiteSpace(image)) continue; // already disabled, or no program
if (!SameExe(Environment.ExpandEnvironmentVariables(image), exe)) continue;
string how = start switch { 0 or 1 => "starts when Windows boots", 2 => "starts automatically", _ => "starts on demand" };
found.Add(new PersistenceSource { Kind = PersistKind.Service, Name = name, Display = string.IsNullOrWhiteSpace(display) || display.StartsWith('@') ? name : display, Detail = how, NeedsAdmin = true });
}
return found;
}
private static List<PersistenceSource> FindTasks(string exe)
{
var found = new List<PersistenceSource>();
var type = Type.GetTypeFromProgID("Schedule.Service");
if (type == null) return found;
dynamic svc = Activator.CreateInstance(type)!;
svc.Connect();
void Walk(dynamic folder)
{
try
{
foreach (dynamic task in folder.GetTasks(1)) // 1 = include hidden tasks
{
try
{
if (!(bool)task.Enabled) continue;
foreach (dynamic action in task.Definition.Actions)
{
if ((int)action.Type != 0) continue; // 0 = "run a program"
string? p = action.Path as string;
if (p != null && SameExe(Environment.ExpandEnvironmentVariables(p), exe))
{
found.Add(new PersistenceSource { Kind = PersistKind.ScheduledTask, Name = (string)task.Path, Display = (string)task.Name, Detail = "runs on a schedule or event", NeedsAdmin = TaskNeedsAdmin(task.Definition.Principal) });
break;
}
}
}
catch { /* a task we cannot read */ }
}
}
catch { }
try { foreach (dynamic sub in folder.GetFolders(0)) Walk(sub); } catch { }
}
Walk(svc.GetFolder("\\"));
return found;
}
/// <summary>A task owned by another account (SYSTEM, a service account, a group) or set to run with highest privileges cannot be changed without administrator rights.</summary>
private static bool TaskNeedsAdmin(dynamic principal)
{
try
{
if ((int)principal.RunLevel == 1) return true; // TASK_RUNLEVEL_HIGHEST
if (!string.IsNullOrEmpty(principal.GroupId as string)) return true;
string user = (principal.UserId as string) ?? "";
if (user.Length == 0) return false;
return !(user.Equals(Environment.UserName, StringComparison.OrdinalIgnoreCase)
|| user.EndsWith("\\" + Environment.UserName, StringComparison.OrdinalIgnoreCase));
}
catch { return true; } // unsure: assume it needs it, the result tells the truth
}
private static List<PersistenceSource> FindStartup(string exe) =>
Perf.StartupItems().Where(i => i.Enabled && SameExe(i.Command, exe))
.Select(i => new PersistenceSource { Kind = PersistKind.StartupEntry, Name = i.Name, Display = i.Name, Detail = "starts when you sign in", NeedsAdmin = i.NeedsAdmin, StartupSrc = i.Source })
.ToList();
// ---- disabling and restoring ---------------------------------------------------------------------------------------
/// <summary>Disables one starter. Returns "disabled", or the reason nothing was changed. Everything disabled is written to the undo list.</summary>
public static string Disable(PersistenceSource s, string exe = "")
{
try
{
switch (s.Kind)
{
case PersistKind.Service:
{
int? current = Services.GetStart(s.Name);
if (current == null) return "could not read the service";
if (current == 4) return "already disabled";
string r = Services.SetStart(s.Name, 4);
if (r != "ok") return r;
AddRecord(new DisabledRecord { Kind = s.Kind, Name = s.Name, Display = s.Display, PreviousStart = current.Value, Exe = exe });
return "disabled";
}
case PersistKind.ScheduledTask:
{
string r = SetTaskEnabled(s.Name, false);
if (r != "ok") return r;
AddRecord(new DisabledRecord { Kind = s.Kind, Name = s.Name, Display = s.Display, Exe = exe });
return "disabled";
}
default:
{
var item = Perf.StartupItems().FirstOrDefault(i => i.Name == s.Name && i.Source == s.StartupSrc);
if (item == null) return "no longer there";
string r = Perf.SetStartupEnabled(item, false);
if (r != "disabled") return r;
AddRecord(new DisabledRecord { Kind = s.Kind, Name = s.Name, Display = s.Display, StartupSrc = s.StartupSrc, Exe = exe });
return "disabled";
}
}
}
catch (Exception ex) { return "failed: " + ex.Message; }
}
/// <summary>Puts one disabled starter back the way it was. Returns "restored" or the reason it was not.</summary>
public static string Restore(DisabledRecord r)
{
try
{
string result;
switch (r.Kind)
{
case PersistKind.Service:
result = Services.SetStart(r.Name, r.PreviousStart is >= 0 and <= 3 ? r.PreviousStart : 3);
if (result != "ok") return result;
break;
case PersistKind.ScheduledTask:
result = SetTaskEnabled(r.Name, true);
if (result != "ok") return result;
break;
default:
var item = Perf.StartupItems().FirstOrDefault(i => i.Name == r.Name && i.Source == r.StartupSrc);
if (item != null)
{
string s = Perf.SetStartupEnabled(item, true);
if (s != "enabled") return s;
}
break;
}
RemoveRecord(r);
return "restored";
}
catch (Exception ex) { return "failed: " + ex.Message; }
}
private static string SetTaskEnabled(string fullPath, bool enabled)
{
try
{
var type = Type.GetTypeFromProgID("Schedule.Service");
if (type == null) return "Task Scheduler is not available";
dynamic svc = Activator.CreateInstance(type)!;
svc.Connect();
int cut = fullPath.LastIndexOf('\\');
string folder = cut <= 0 ? "\\" : fullPath[..cut];
string name = fullPath[(cut + 1)..];
dynamic task = svc.GetFolder(folder).GetTask(name);
task.Enabled = enabled;
return "ok";
}
catch (COMException ex) when ((uint)ex.HResult == 0x80070005) { return "needs administrator rights (use Rules > Administrator mode)"; }
catch (UnauthorizedAccessException) { return "needs administrator rights (use Rules > Administrator mode)"; }
catch (COMException ex) when ((uint)ex.HResult == 0x80070002) { return "that task no longer exists"; }
catch (FileNotFoundException) { return "that task no longer exists"; } // .NET turns ERROR_FILE_NOT_FOUND into this
catch (Exception ex) { return "failed: " + ex.Message; }
}
// ---- the undo list ----------------------------------------------------------------------------------------------------
public static List<DisabledRecord> Records()
{
lock (Lock)
{
try { return File.Exists(RecordsPath) ? JsonSerializer.Deserialize<List<DisabledRecord>>(File.ReadAllText(RecordsPath)) ?? new() : new(); }
catch { return new(); }
}
}
private static void Save(List<DisabledRecord> list)
{
Directory.CreateDirectory(Config.Dir);
File.WriteAllText(RecordsPath, JsonSerializer.Serialize(list));
}
private static void AddRecord(DisabledRecord r)
{
r.WhenUtc = DateTime.UtcNow;
lock (Lock)
{
var list = Records();
list.RemoveAll(x => x.Kind == r.Kind && x.Name == r.Name);
list.Add(r);
Save(list);
}
Log.Write($"Disabled {r.Display} ({new PersistenceSource { Kind = r.Kind }.KindLabel}). It can be restored from Rules > Disabled by Argus.");
}
private static void RemoveRecord(DisabledRecord r)
{
lock (Lock)
{
var list = Records();
list.RemoveAll(x => x.Kind == r.Kind && x.Name == r.Name);
Save(list);
}
Log.Write($"Restored {r.Display}.");
}
}