-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHostsManager.clsCoreFunctions.cs
322 lines (303 loc) · 14.1 KB
/
HostsManager.clsCoreFunctions.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using HostsManager.Settings;
using System.Security.Principal;
using System.Net;
using System.Security.AccessControl;
using System.Windows.Forms;
using System.Diagnostics;
using System.ServiceProcess;
using System.Net.NetworkInformation;
using System.Management;
using System.IO;
namespace HostsManager
{
public static class clsCoreFunctions
{
//The update process
public static void updateHostsFile(object f)
{
bool err = false;
Thread start = null;
try
{
/*
clsUtilitys.Dialogs.dlgOptions o = new clsUtilitys.Dialogs.dlgOptions();
o.frm = (frmCore)f;
o.txt = "Updating hosts file...";
start = new Thread(new ParameterizedThreadStart(clsUtilitys.Dialogs.showDialog));
start.Start(o);*/
/* if (clsSettingsData.urls.Count == 0 && clsSettingsData.UseCustomBlacklist && !clsSettingsData.isHidden)
{
frmDialog d = new frmDialog();
d.action = "Your personal hosts file is empty.";
d.showButton = true;
d.ShowDialog();
err = true;
}*/
if(true)
{
//Get name of admin group
SecurityIdentifier id = new SecurityIdentifier("S-1-5-32-544");
string adminGroupName = id.Translate(typeof(NTAccount)).Value;
if (!clsSettingsData.isHidden && !err)
{
clsUtilitys.Dialogs.dlgOptions o1 = new clsUtilitys.Dialogs.dlgOptions();
o1.frm = (frmCore)f;
o1.txt = "Updating hosts file(s)...";
start = new Thread(new ParameterizedThreadStart(clsUtilitys.Dialogs.showDialog));
start.Start(o1);
}
String fileText = "";
//Read hosts files from web
if (clsSettingsData.UseHostsFileBlacklist)
{
fileText += clsUtilitys.Downloader.DownloadString("https://hosts-file.net/download/hosts.txt");
fileText += clsUtilitys.Downloader.DownloadString("https://hosts-file.net/hphosts-partial.txt");
}
if (clsSettingsData.UseStevensBlacklist)
{
fileText += clsUtilitys.Downloader.DownloadString(
"https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts");
if (clsSettingsData.showFakeNews)
fileText += clsUtilitys.Downloader.DownloadString(
"https://raw.githubusercontent.com/StevenBlack/hosts/master/extensions/fakenews/hosts");
if (clsSettingsData.showGambling)
fileText += clsUtilitys.Downloader.DownloadString(
"https://raw.githubusercontent.com/StevenBlack/hosts/master/extensions/gambling/hosts");
if (clsSettingsData.showPorn)
{
fileText += clsUtilitys.Downloader.DownloadString(
"https://raw.githubusercontent.com/StevenBlack/hosts/master/extensions/porn/clefspeare13/hosts");
fileText += clsUtilitys.Downloader.DownloadString(
"https://raw.githubusercontent.com/StevenBlack/hosts/master/extensions/porn/sinfonietta/hosts");
}
if (clsSettingsData.showSocial)
fileText += clsUtilitys.Downloader.DownloadString(
"https://raw.githubusercontent.com/StevenBlack/hosts/master/extensions/social/hosts");
}
if (clsSettingsData.UseCustomBlacklist)
{
foreach (String u in clsSettingsData.urls)
fileText += "\r\n" + clsUtilitys.Downloader.DownloadString(u);
}
if (!clsSettingsData.isHidden)
{
if (start != null)
{
clsUtilitys.Dialogs.closeDialog();
start.Abort();
}
clsUtilitys.Dialogs.dlgOptions o2 = new clsUtilitys.Dialogs.dlgOptions();
o2.frm = (frmCore)f;
o2.txt = "Replacing IP's";
start = new Thread(new ParameterizedThreadStart(clsUtilitys.Dialogs.showDialog));
start.Start(o2);
}
//IP Overwrite
if (clsSettingsData.replaceMethod == clsSettingsData.mIPReplaceMethod.KEEP_LOCALHOST)
clsSettingsData.ipTo = clsSettingsData.ipFrom;
else if (clsSettingsData.replaceMethod == clsSettingsData.mIPReplaceMethod.SET_CUSTOM)
{
clsSettingsData.ipTo = clsSettingsData.replaceIP;
}
else
{
clsSettingsData.ipTo = clsBrandingData.DefaultBlockPage;
clsSettingsData.ipTo = Dns.GetHostAddresses(clsSettingsData.ipTo)[0].ToString();
}
if (clsSettingsData.replaceMethod != clsSettingsData.mIPReplaceMethod.KEEP_LOCALHOST)
{
fileText = fileText.Replace("0.0.0.0", clsSettingsData.ipTo);
fileText = fileText.Replace("127.0.0.1", clsSettingsData.ipTo);
fileText = fileText.Replace("::1", clsSettingsData.ipTo);
fileText = fileText.Replace("0\t", clsSettingsData.ipTo + "\t");
}
foreach (String host in clsSettingsData.addHosts)
fileText += "\r\n" + clsSettingsData.ipTo + " " + host;
//CR/LF detection
fileText = fileText.Replace("\r", "");
fileText = fileText.Replace("\n", "\r\n");
//Write temp hosts file
System.IO.File.Delete(Path.GetTempPath()+"\\hosts.tmp");
System.IO.File.WriteAllText(Path.GetTempPath()+"\\hosts.tmp", fileText);
//Set permissions of hosts file to be writable by group admins
FileSecurity fs = clsUtilitys.HostsFile.setHostsFilePermissions();
//Copy hosts file to real hosts file
System.IO.File.Copy(Path.GetTempPath()+"\\hosts.tmp",
Environment.GetEnvironmentVariable("windir") + "\\system32\\drivers\\etc\\hosts", true);
System.IO.File.Delete(Path.GetTempPath()+"\\hosts.tmp");
//Reset permissions
clsUtilitys.HostsFile.resetHostsFilePermissions(fs);
}
if (start != null)
{
clsUtilitys.Dialogs.closeDialog();
start.Abort();
}
if (!clsSettingsData.isHidden && !err)
{
clsUtilitys.Dialogs.dlgOptions o1 = new clsUtilitys.Dialogs.dlgOptions();
o1.frm = (frmCore)f;
o1.txt = "Hosts file updated.";
o1.okbutton = true;
start = new Thread(new ParameterizedThreadStart(clsUtilitys.Dialogs.showDialog));
start.Start(o1);
}
clsUtilitys.executeNoWindow("ipconfig.exe", "/flushdns");
}
catch (Exception ex) //Hosts file update
{
if (start != null)
{
clsUtilitys.Dialogs.closeDialog();
start.Abort();
}
//generate error string
String add = "";
if (!clsSettingsData.isHidden)
if (clsUtilitys.Info.isAntivir())
{
frmNotifyAntivirus fr = new frmNotifyAntivirus();
fr.ShowDialog();
}
if (!clsSettingsData.isHidden)
MessageBox.Show("Error: " + add + ex.Message);
}
}
//Create Task for auto update
public static void doAutoUpdate()
{
//prepare ProcessStartInfo
ProcessStartInfo psi = new ProcessStartInfo();
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.CreateNoWindow = true;
psi.FileName = "schtasks.exe";
String name = clsBrandingData.COMPANY + " " + clsBrandingData.PRODUCT;
if (clsSettingsData.autoUpdate)
{
//Create task using schtasks.exe
//FileSecurity fs=setHostsFilePermissions(); !!! 080417DH - muss evtl wieder reingemacht werden.
String Arguments = "/Create /tn \""+name+"\" /tr \"" +
System.Reflection.Assembly.GetEntryAssembly().Location +
" /auto\" /sc DAILY /RL HIGHEST /F";
Process p = clsUtilitys.executeNoWindow("schtasks.exe", Arguments);
}
else
{
//Delete task using schtasks.exe
clsUtilitys.executeNoWindow("schtasks.exe", "/Delete /tn "+name+" /F");
}
}
public static void disableDNSService(frmCore frm)
{
System.Threading.Thread start = null;
bool err = false;
clsUtilitys.Dialogs.dlgOptions o = new clsUtilitys.Dialogs.dlgOptions();
o.frm = (frmCore)frm;
o.txt = "Disabling DNS-Client Service...";
start = new Thread(new ParameterizedThreadStart(clsUtilitys.Dialogs.showDialog));
start.Start(o);
ServiceController _ServiceController = new ServiceController("dnscache");
if (!_ServiceController.ServiceHandle.IsInvalid)
{
try
{
_ServiceController.Stop();
_ServiceController.WaitForStatus(ServiceControllerStatus.Stopped,
TimeSpan.FromMilliseconds(10000));
}
catch (Exception)
{
err = true;
}
try
{
if (!err)
{
Process p = clsUtilitys.executeNoWindow("sc.exe", "config dnscache start=disabled");
p.WaitForExit();
clsSettingsData.DNServiceDisabled = true;
frm.setServiceButtonText("Enable DNS Service");
}
}
catch (Exception)
{
err = true;
}
}
if (start != null)
start.Abort();
if (!err)
{
clsUtilitys.Dialogs.dlgOptions o1 = new clsUtilitys.Dialogs.dlgOptions();
o1.frm = (frmCore)frm;
o1.txt = "DNS-Client service disabled.";
o1.okbutton = true;
start = new Thread(new ParameterizedThreadStart(clsUtilitys.Dialogs.showDialog));
start.Start(o1);
}
else
{
clsUtilitys.Dialogs.dlgOptions o1 = new clsUtilitys.Dialogs.dlgOptions();
o1.frm = (frmCore)frm;
o1.txt = "Erro disabling DNS-Client service. Please retry..";
o1.okbutton = true;
start = new Thread(new ParameterizedThreadStart(clsUtilitys.Dialogs.showDialog));
start.Start(o1);
}
}
public static void setDNS(String DNS)
{
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
if ((nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet) || (nic.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)) //&& (nic.OperationalStatus == OperationalStatus.Up))
{
ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();
foreach (ManagementObject objMO in objMOC)
{
if ((bool)objMO["IPEnabled"])
{
try
{
ManagementBaseObject newDNS =
objMO.GetMethodParameters("SetDNSServerSearchOrder");
newDNS["DNSServerSearchOrder"] = DNS.Split(',');
ManagementBaseObject setDNS =
objMO.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);
}
catch (Exception)
{
throw;
}
}
}
}
}
}
public static String[] getBackups()
{
String[] f = Directory.GetFiles((Environment.GetEnvironmentVariable("windir") + "\\system32\\drivers\\etc\\"));
String[] r = new String[f.Length];
int count = 0;
for (int i = 0; i < f.Length; i++)
{
if (f[i] != null)
{
if (f[i].Contains(clsBrandingData.PRODUCT + ".bak"))
{
r[count] = f[i];
count++;
}
}
}
return r;
}
}
}