-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathProgram.cs
83 lines (68 loc) · 3.25 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Authentication.ExtendedProtection;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Win32;
namespace Create_IpSec_Policies
{
class Program : NativeMethods
{
static readonly Polstore hPolStoreLib = new Polstore();
static void Main(string[] args)
{
if(OpenPolicyStore(out hPolStoreLib.hPolicyStore))
{
Console.WriteLine($"Failed to open policy store with error: {Marshal.GetLastWin32Error()}");
Environment.Exit(0);
}
if (GetFilterData(hPolStoreLib.hPolicyStore, new Guid("{43a24ceb-e7ce-4b40-978f-9e0b6fd90d45}"), out Polstore.Polstructs.IPSEC_FILTER_DATA ipsecFilterData))
Console.WriteLine($"Failed to get filter data with error: {Marshal.GetLastWin32Error()}");
else
{
Console.WriteLine
(
$"Filter Details:\n" +
$"Filter GUID: {ipsecFilterData.filterIdentifier}\n" +
$"Filter Specs Ptr: 0x{ipsecFilterData.filterSpecs}\n" +
$"IPSec Description: {ipsecFilterData.ipsecDescription}\n" +
$"IPSec Name: {ipsecFilterData.ipsecName}\n" +
$"Filter Specs: {ipsecFilterData.numFilterSpecs}\n" +
$"When changed: {ipsecFilterData.whenChanged:x}\n"
);
}
if (DeleteFilterData(hPolStoreLib.hPolicyStore, new Guid("{43a24ceb-e7ce-4b40-978f-9e0b6fd90d45}")))
Console.WriteLine("Failed to delete policy.");
Console.WriteLine("Policy Deleted");
hPolStoreLib.Dispose();
Console.ReadLine();
}
private static bool OpenPolicyStore(out IntPtr hPolicyStore)
{
return Convert.ToBoolean(Polstore.IPSecOpenPolicyStore("", Polstore.TypeOfStore.IPSEC_REGISTRY_PROVIDER, "", out hPolicyStore));
}
private static bool ClosePolicyStore(IntPtr hPolicyStore)
{
return Convert.ToBoolean(Polstore.IPSecClosePolicyStore(hPolicyStore));
}
private static bool DeleteFilterData(IntPtr hPolicyStore, Guid guid)
{
return Convert.ToBoolean(hPolStoreLib.IPSecDeleteFilter(hPolicyStore, guid));
}
private static bool GetFilterData(IntPtr hPolicyStore, Guid guid, out Polstore.Polstructs.IPSEC_FILTER_DATA ipsecFilterData)
{
IntPtr ipsecFilterDataPtr = Marshal.AllocHGlobal(0x8);
ipsecFilterData = new Polstore.Polstructs.IPSEC_FILTER_DATA();
Marshal.StructureToPtr(ipsecFilterData, ipsecFilterDataPtr, false);
if (Convert.ToBoolean(hPolStoreLib.IPSecGetFilter(hPolicyStore, guid, ipsecFilterDataPtr)))
return true;
ipsecFilterData = (Polstore.Polstructs.IPSEC_FILTER_DATA)Marshal.PtrToStructure((IntPtr)Marshal.ReadInt64(ipsecFilterDataPtr), typeof(Polstore.Polstructs.IPSEC_FILTER_DATA));
return false;
}
}
}