-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathCheckSafeMode.cs
164 lines (137 loc) · 5.19 KB
/
CheckSafeMode.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Management;
namespace CheckSafeMode
{
class Program
{
internal const int SM_CLEANBOOT = 67;
[DllImport("user32.dll")]
internal static extern int GetSystemMetrics(int smIndex);
static void Main(string[] args)
{
var IsSafeMode = GetSystemMetrics(SM_CLEANBOOT);
bool safeModeActive = Convert.ToBoolean(IsSafeMode);
if (safeModeActive)
{
ManagementObjectSearcher wmiData = new ManagementObjectSearcher(@"root\SecurityCenter2", "SELECT * FROM AntiVirusProduct");
ManagementObjectCollection data = wmiData.Get();
foreach (ManagementObject virusChecker in data)
{
var AvName = virusChecker["displayName"];
var xstate = virusChecker["productState"];
var f = Convert.ToInt32(xstate);
var zz = f.ToString("X").PadLeft(6, '0');
var StatusOfAV = "";
var y = zz.Substring(2, 2);
switch (y)
{
case "00":
StatusOfAV = "OFF";
break;
case "01":
StatusOfAV = "Exipired";
break;
case "10":
StatusOfAV = "ON";
break;
case "11":
StatusOfAV = "Snoozed";
break;
default:
StatusOfAV = "Unknown";
break;
}
string text = String.Format("In SafeBoot Mode = {0} AVInstalled = {1} Status = {2} ", Convert.ToBoolean(IsSafeMode).ToString(), AvName.ToString(), StatusOfAV);
System.IO.File.WriteAllText(@"C:\Users\Public\SafeBoot.txt", text);
BcdStoreAccessor b = new BcdStoreAccessor();
b.RemoveSafeboot();
Shutdown.Restart();
}
}
//some stuff came from here
//https://gallery.technet.microsoft.com/scriptcenter/Get-the-status-of-4b748f25
}
public static bool AntivirusInstalled()
{
string wmipathstr = @"\\" + Environment.MachineName + @"\root\SecurityCenter2";
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmipathstr, "SELECT * FROM AntivirusProduct");
ManagementObjectCollection instances = searcher.Get();
return instances.Count > 0;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return false;
}
// https://stackoverflow.com/questions/25295117/use-c-sharp-bcd-wmi-provider-to-safeboot-windows?noredirect=1
public class BcdStoreAccessor
{
public const int BcdOSLoaderInteger_SafeBoot = 0x25000080;
public enum BcdLibrary_SafeBoot
{
SafemodeMinimal = 0,
SafemodeNetwork = 1,
SafemodeDsRepair = 2
}
private ConnectionOptions connectionOptions;
private ManagementScope managementScope;
private ManagementPath managementPath;
public BcdStoreAccessor()
{
connectionOptions = new ConnectionOptions();
connectionOptions.Impersonation = ImpersonationLevel.Impersonate;
connectionOptions.EnablePrivileges = true;
managementScope = new ManagementScope("root\\WMI", connectionOptions);
managementPath = new ManagementPath("root\\WMI:BcdObject.Id=\"{fa926493-6f1c-4193-a414-58f0b2456d1e}\",StoreFilePath=\"\"");
}
public void SetSafeboot()
{
ManagementObject currentBootloader = new ManagementObject(managementScope, managementPath, null);
currentBootloader.InvokeMethod("SetIntegerElement", new object[] { BcdOSLoaderInteger_SafeBoot, BcdLibrary_SafeBoot.SafemodeMinimal });
}
public void RemoveSafeboot()
{
ManagementObject currentBootloader = new ManagementObject(managementScope, managementPath, null);
currentBootloader.InvokeMethod("DeleteElement", new object[] { BcdOSLoaderInteger_SafeBoot });
}
}
public class Shutdown
{
public static void Restart()
{
StartShutDown("-f -r -t 5");
}
/// <summary>
/// Log off.
/// </summary>
public static void LogOff()
{
StartShutDown("-l");
}
/// <summary>
/// Shutting Down Windows
/// </summary>
public static void Shut()
{
StartShutDown("-f -s -t 5");
}
private static void StartShutDown(string param)
{
ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = "cmd";
proc.WindowStyle = ProcessWindowStyle.Hidden;
proc.Arguments = "/C shutdown " + param;
Process.Start(proc);
}
}
}
}