-
Notifications
You must be signed in to change notification settings - Fork 14
/
Firewall.cs
129 lines (116 loc) · 4.31 KB
/
Firewall.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//Firewall API
using NATUPNPLib;
using NETCONLib;
using NetFwTypeLib;
using System.Collections;
using System.Runtime.InteropServices;
namespace AntiVirus_Project
{
class Firewall
{
private const string CLSID_FIREWALL_MANAGER = "{304CE942-6E39-40D8-943A-B913C40C9CD4}";
private INetFwMgr manager = null;
public bool firewallOn = false;
private static NetFwTypeLib.INetFwMgr GetFirewallManager()
{
Type objectType = Type.GetTypeFromCLSID(new Guid(CLSID_FIREWALL_MANAGER));
return Activator.CreateInstance(objectType) as NetFwTypeLib.INetFwMgr;
}
public bool IsFirewallInstalled
{
get
{
if (manager != null &&
manager.LocalPolicy != null &&
manager.LocalPolicy.CurrentProfile != null)
return true;
else
return false;
}
}
public Firewall()
{
manager = GetFirewallManager();
if (manager != null)
firewallOn = manager.LocalPolicy.CurrentProfile.FirewallEnabled;
else
firewallOn = false;
}
public bool FirewallStatus()
{
return manager.LocalPolicy.CurrentProfile.FirewallEnabled && IsFirewallInstalled;
}
public void FirewallStart(bool flag)
{
if (IsFirewallInstalled)
{
if (flag)
manager.LocalPolicy.CurrentProfile.FirewallEnabled = true;
else
{
// there will be an exception if we try to turnoff windows firewall directly via API
ConsoleSetups con = new ConsoleSetups();
con.RunExternalExe("netsh.exe", "Firewall set opmode disable");
}
}
}
public bool HasAuthorization(string applicationFullPath)
{
foreach (string appName in GetAuthorizedAppPaths())
{
// Paths on windows file systems are not case sensitive.
if (appName.ToLower() == applicationFullPath.ToLower())
return true;
}
// Failed to locate the given app.
return false;
}
public ICollection GetAuthorizedAppPaths()
{
if (IsFirewallInstalled && manager!=null)
{
ArrayList list = new ArrayList();
// Collect the paths of all authorized applications
foreach (INetFwAuthorizedApplication app in manager.LocalPolicy.CurrentProfile.AuthorizedApplications)
list.Add(app.ProcessImageFileName);
return list;
}
else return null;
}
public void GrantAuthorization(string applicationFullPath, string appName)
{
if(!HasAuthorization(applicationFullPath))
{
Type authAppType = Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication", false);
INetFwAuthorizedApplication appInfo = null;
if (authAppType != null)
{
try
{
appInfo = (INetFwAuthorizedApplication)Activator.CreateInstance(authAppType);
}
// In all other circumstances, appInfo is null.
catch { appInfo = null; }
}
if(appInfo!=null)
{
appInfo.Name = appName;
appInfo.ProcessImageFileName = applicationFullPath;
manager.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(appInfo);
}
}
}
public void RemoveAuthorization(string applicationFullPath)
{
if (HasAuthorization(applicationFullPath))
{
// Remove Authorization for this application
manager.LocalPolicy.CurrentProfile.AuthorizedApplications.Remove(applicationFullPath);
}
}
}
}