-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHostsManager.clsUtilitys.cs
326 lines (293 loc) · 12.5 KB
/
HostsManager.clsUtilitys.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
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management;
using System.Net;
using System.Net.NetworkInformation;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
namespace HostsManager
{
public static class clsUtilitys
{
public static bool isADMember()
{
String pcname = Environment.GetEnvironmentVariable("computername");
String domainname = Environment.GetEnvironmentVariable("logonserver");
if (("\\\\" + pcname.ToLower()).Equals(domainname.ToLower()))
return false;
else
return true;
}
public static class HostsFile
{
//Set Permissions of hosts file to be writable by group admins
public static FileSecurity setHostsFilePermissions()
{
FileSecurity fs = null;
FileSecurity fsold = null;
try
{
SecurityIdentifier id = new SecurityIdentifier("S-1-5-32-544");
string adminGroupName = id.Translate(typeof(NTAccount)).Value;
fs = System.IO.File.GetAccessControl(Environment.GetEnvironmentVariable("windir") +
"\\system32\\drivers\\etc\\hosts");
fsold = fs;
fs.AddAccessRule(new FileSystemAccessRule(adminGroupName, FileSystemRights.FullControl,
AccessControlType.Allow));
fs.RemoveAccessRule(new FileSystemAccessRule(adminGroupName, FileSystemRights.Write,
AccessControlType.Deny));
System.IO.File.SetAccessControl(
Environment.GetEnvironmentVariable("windir") + "\\system32\\drivers\\etc\\hosts", fs);
}
catch (Exception) { }
return fsold;
}
//Restore hosts file permissions
public static void resetHostsFilePermissions(FileSecurity fs)
{
try
{
System.IO.File.SetAccessControl(
Environment.GetEnvironmentVariable("windir") + "\\system32\\drivers\\etc\\hosts", fs);
}
catch (Exception) { }
}
}
public static class Dialogs
{
public static DialogResult showYesNoDialog(String text)
{
frmDialog d = new frmDialog();
d.action = text;
d.yesNoButtons = true;
return d.ShowDialog();
}
public class dlgOptions
{
public String txt = "";
public HostsManager.frmCore frm = null;
public int width = 279;
public int height = 72;
public bool okbutton = false;
public Thread thrd = null;
}
public static frmDialog dlg;
public delegate void deleg(clsUtilitys.Dialogs.dlgOptions o);
public delegate void delegThrd(clsUtilitys.Dialogs.dlgOptions o);
public static void subThrd(object d)
{
try
{
dlg = new frmDialog();
dlg.action = ((dlgOptions)d).txt;
dlg.showCancel = true;
if (((dlgOptions)d).frm.InvokeRequired)
((dlgOptions)d).frm.Invoke(new delegThrd(subThrd), new object[] { d });
else
if (dlg.ShowDialog(((dlgOptions)d).frm) == DialogResult.Cancel)
((dlgOptions)d).thrd.Abort();
}
catch (ThreadAbortException ex) {
Exception e = ex;
}
}
public static void showDialogThrd(object d)
{
System.Threading.Thread t = new Thread(new ParameterizedThreadStart(subThrd));
t.Start(d);
}
public static void showDialog(object d)
{
try
{
dlg = new frmDialog();
dlg.action = ((dlgOptions)d).txt;
dlg.showButton = ((dlgOptions)d).okbutton;
if (((dlgOptions)d).frm.InvokeRequired)
((dlgOptions)d).frm.Invoke(new deleg(showDialog),new object[]{d});
else
dlg.ShowDialog(((dlgOptions)d).frm);
}
catch (ThreadAbortException) { }
}
public delegate void closedlgdeleg();
public static void closeDialog()
{
if (dlg.InvokeRequired)
dlg.Invoke(new closedlgdeleg(closeDialog));
else
dlg.Close();
}
}
public static string[] Resolver(string DnsName)
{
System.Net.IPHostEntry IHE = System.Net.Dns.Resolve(DnsName);
string[] ret = new string[IHE.AddressList.Length];
for (int i = 0; i < IHE.AddressList.Length; i++)
ret[i] = IHE.AddressList[i].ToString();
return ret;
}
public static class SSLCertificates
{
//Search for FireFox Profile.
//080417DH - give credits
public static string ReadFirefoxProfile()
{
string apppath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string mozilla = System.IO.Path.Combine(apppath, "Mozilla");
bool exist = System.IO.Directory.Exists(mozilla);
if (exist)
{
string firefox = System.IO.Path.Combine(mozilla, "firefox");
if (System.IO.Directory.Exists(firefox))
{
string prof_file = System.IO.Path.Combine(firefox, "profiles.ini");
bool file_exist = System.IO.File.Exists(prof_file);
if (file_exist)
{
StreamReader rdr = new StreamReader(prof_file);
string resp = rdr.ReadToEnd();
string[] lines = resp.Split(new string[] {
"\r\n"
}, StringSplitOptions.None);
if (lines.Length > 1)
{
string location = lines.First(x => x.Contains("Path="))
.Split(new string[] {
"="
}, StringSplitOptions.None)[1];
foreach (String line in lines)
{
if (line.Contains("Path="))
{
String profpath = line.Split(new string[] {
"="
}, StringSplitOptions.None)[1];
profpath = System.IO.Path.Combine(firefox, profpath).Replace("/", "\\");
importCert(profpath);
}
}
string prof_dir = System.IO.Path.Combine(firefox, location);
return prof_dir.Replace("/", "\\");
}
}
}
}
return "";
}
private static void importCert(String profPath)
{
//Get Firefox profile path
//String profPath = ReadFirefoxProfile();
String currpath = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
currpath = currpath.Substring(0, currpath.LastIndexOf("/"));
currpath = currpath.Replace("file:///", "").Replace("/", "\\");
//Import certificate to FF
executeNoWindow(currpath + "\\certutil\\certutil_moz.exe",
"-A -n \"Testcert\" -t \"TCu,Cuw,Tuw\" -i " + currpath + "\\cert.pem -d \"" + profPath + "\"");
//Import certificate to Win
executeNoWindow("certutil.exe", "-addstore \"Root\" " + currpath + "\\cert.pem");
executeNoWindow("certutil.exe", "-addstore \"CA\" " + currpath + "\\cert.pem");
}
}
public static class Info
{
//Check for antivir
public static bool isAntivir()
{
//Check porcesses for process names of antivirs
if (Process.GetProcessesByName("avgnt").Length > 0 || Process.GetProcessesByName("inststub").Length > 0 ||
Process.GetProcessesByName("uiStub").Length > 0 || Process.GetProcessesByName("KLAgent").Length > 0 ||
Process.GetProcessesByName("vsserv").Length > 0 || Process.GetProcessesByName("VisthAux").Length > 0 ||
Process.GetProcessesByName("avastui").Length > 0)
return true;
else
return false;
}
public static String getCurrentDNSServer()
{
NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface networkInterface in networkInterfaces)
{
if (networkInterface.OperationalStatus == OperationalStatus.Up)
{
IPInterfaceProperties ipProperties = networkInterface.GetIPProperties();
IPAddressCollection dnsAddresses = ipProperties.DnsAddresses;
foreach (IPAddress dnsAdress in dnsAddresses)
{
return dnsAdress.ToString();
}
}
}
return "";
}
}
public static class Downloader
{
public static String DownloadString(String url)
{
String ret = "";
WebClient wc = new WebClient();
try
{
ret = wc.DownloadString(url);
}
catch (Exception) { }
return ret;
}
}
public static class Services
{
public static bool isServiceActive()
{
string path = "Win32_Service.Name='dnscache'";
ManagementPath p = new ManagementPath(path);
ManagementObject ManagementObj = new ManagementObject(p);
return (ManagementObj["StartMode"].ToString().Equals("Auto"));
}
public static void changeServiceStartMode(string hostname, string serviceName, string startMode)
{
try
{
ManagementObject classInstance =
new ManagementObject(@"\\" + hostname + @"\root\cimv2",
"Win32_Service.Name='" + serviceName + "'",
null);
// Obtain in-parameters for the method
ManagementBaseObject inParams =
classInstance.GetMethodParameters("ChangeStartMode");
// Add the input parameters.
inParams["StartMode"] = startMode;
// Execute the method and obtain the return values.
ManagementBaseObject outParams =
classInstance.InvokeMethod("ChangeStartMode", inParams, null);
}
catch (ManagementException) { }
}
}
//Start external process with hidden window
public static Process executeNoWindow(String cmd, String param)
{
try
{
ProcessStartInfo pi = new ProcessStartInfo(cmd, param);
pi.CreateNoWindow = true;
pi.UseShellExecute = false;
pi.RedirectStandardOutput = true;
pi.RedirectStandardError = true;
pi.RedirectStandardInput = true;
pi.WindowStyle = ProcessWindowStyle.Hidden;
return Process.Start(pi);
}
catch (Exception) { }
return null;
}
}
}