Skip to content

Commit d5b6f50

Browse files
author
Kevin Gut
committed
Allow to block only IPv4 or IPv6
1 parent e98b8c7 commit d5b6f50

4 files changed

Lines changed: 92 additions & 14 deletions

File tree

AnyBlock/CIDR.cs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
11
using System;
22
using System.Net;
3+
using System.Net.Sockets;
34

45
namespace WinAPI.NET
56
{
7+
/// <summary>
8+
/// IP version specifier
9+
/// </summary>
10+
[Flags]
11+
public enum IPVersion : int
12+
{
13+
/// <summary>
14+
/// No or invalid value
15+
/// </summary>
16+
None = 0,
17+
/// <summary>
18+
/// IPv4
19+
/// </summary>
20+
V4 = 1,
21+
/// <summary>
22+
/// IPv6
23+
/// </summary>
24+
V6 = 2,
25+
/// <summary>
26+
/// IPv4 or IPv6
27+
/// </summary>
28+
Any = V4 | V6
29+
}
30+
631
/// <summary>
732
/// Handles CIDR notation of IPv4 and IPv6 addresses
833
/// </summary>
@@ -60,6 +85,17 @@ public IPAddress MaskIP
6085
public bool FixAddress
6186
{ get; set; }
6287

88+
/// <summary>
89+
/// Gets the Type of address in use
90+
/// </summary>
91+
public IPVersion Type
92+
{
93+
get
94+
{
95+
return Address.AddressFamily == AddressFamily.InterNetwork ? IPVersion.V4 : IPVersion.V6;
96+
}
97+
}
98+
6399
/// <summary>
64100
/// Initializes a new Range in CIDR Notation
65101
/// </summary>
@@ -86,10 +122,14 @@ public CIDR(string combinedNotation, bool fixAddress)
86122
{
87123
throw new FormatException("CombinedNotation doesn't has a valid IP Address");
88124
}
125+
if (tempAddr.AddressFamily != AddressFamily.InterNetwork && tempAddr.AddressFamily != AddressFamily.InterNetworkV6)
126+
{
127+
throw new FormatException("Supplied address is not IPv4 or IPv6");
128+
}
89129
//If no CIDR delimiter is provided, assume fully closed mask
90130
if (Parts.Length == 1)
91131
{
92-
tempMask = tempAddr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6 ? 128 : 32;
132+
tempMask = tempAddr.AddressFamily == AddressFamily.InterNetworkV6 ? 128 : 32;
93133
}
94134
else
95135
{
@@ -104,7 +144,7 @@ public CIDR(string combinedNotation, bool fixAddress)
104144
throw new ArgumentOutOfRangeException(nameof(combinedNotation), "CIDR Mask is outside of Bounds");
105145
}
106146
//Mask must not be bigger than IPv4=32 or IPv6=128
107-
if (tempMask > (tempAddr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6 ? 128 : 32))
147+
if (tempMask > (tempAddr.AddressFamily == AddressFamily.InterNetworkV6 ? 128 : 32))
108148
{
109149
throw new ArgumentOutOfRangeException(nameof(combinedNotation), "CIDR Mask is outside of Bounds");
110150
}
@@ -124,7 +164,7 @@ public void SetMask(int cidrMask)
124164
throw new ArgumentOutOfRangeException(nameof(cidrMask), "CIDR Mask is outside of Bounds");
125165
}
126166
//Mask must not be bigger than IPv4=32 or IPv6=128
127-
if (cidrMask > (Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6 ? 128 : 32))
167+
if (cidrMask > (Address.AddressFamily == AddressFamily.InterNetworkV6 ? 128 : 32))
128168
{
129169
throw new ArgumentOutOfRangeException(nameof(cidrMask), "CIDR Mask is outside of Bounds");
130170
}
@@ -253,7 +293,7 @@ private void ComputeMask(IPAddress ipAddr, int cidrMask)
253293
MaskBits = bitmask;
254294
Mask = cidrMask;
255295
//Set addresses and keep scope if applicable
256-
if (ipAddr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
296+
if (ipAddr.AddressFamily == AddressFamily.InterNetwork)
257297
{
258298
Address = new IPAddress(bytes);
259299
AddressLow = new IPAddress(low);

AnyBlock/Firewall.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private static INetFwPolicy2 GetPolicy()
5858
/// <summary>
5959
/// Removes all rules with the current prefix
6060
/// </summary>
61-
public static void ClearRules()
61+
public static int ClearRules()
6262
{
6363
var Policy = GetPolicy();
6464
var Rules = Policy.Rules
@@ -70,6 +70,7 @@ public static void ClearRules()
7070
{
7171
Policy.Rules.Remove(Rule);
7272
}
73+
return Rules.Length;
7374
}
7475

7576
/// <summary>

AnyBlock/Program.cs

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static int Main(string[] args)
9393
switch (args[0].ToLower())
9494
{
9595
case "/apply":
96-
return ApplyRules();
96+
return ApplyRules(IPVersion.Any);
9797
case "/clear":
9898
return ClearRules();
9999
case "/config":
@@ -106,6 +106,9 @@ static int Main(string[] args)
106106
{
107107
switch (args[0].ToLower())
108108
{
109+
case "/apply":
110+
IPVersion v = ParseIPVersion(args.Skip(1).FirstOrDefault());
111+
return ApplyRules(v);
109112
case "/remove":
110113
return RemoveCacheItem(args.Skip(1));
111114
case "/add":
@@ -119,6 +122,32 @@ static int Main(string[] args)
119122
return ERR.ARGS;
120123
}
121124

125+
/// <summary>
126+
/// Parses the string "v4" or "v6" into an <see cref="IPVersion"/>
127+
/// </summary>
128+
/// <param name="Arg">IP version string</param>
129+
/// <returns>
130+
/// <see cref="IPVersion"/> value.
131+
/// <see cref="IPVersion.Any"/> if no argument supplied or empty.
132+
/// <see cref="IPVersion.None"/> if argument supplied and invalid.
133+
/// </returns>
134+
private static IPVersion ParseIPVersion(string Arg)
135+
{
136+
if (string.IsNullOrEmpty(Arg))
137+
{
138+
return IPVersion.Any;
139+
}
140+
if (Arg.ToLower() == "v4")
141+
{
142+
return IPVersion.V4;
143+
}
144+
if (Arg.ToLower() == "v6")
145+
{
146+
return IPVersion.V6;
147+
}
148+
return IPVersion.None;
149+
}
150+
122151
/// <summary>
123152
/// Clears firewall rules
124153
/// </summary>
@@ -133,7 +162,7 @@ private static int ClearRules()
133162
/// <summary>
134163
/// Applies firewall rules
135164
/// </summary>
136-
private static int ApplyRules()
165+
private static int ApplyRules(IPVersion Version)
137166
{
138167
Log("Applying firewall Rules...");
139168
try
@@ -142,14 +171,19 @@ private static int ApplyRules()
142171
.Select(m => new RangeSet()
143172
{
144173
Direction = m.Direction,
145-
Ranges = Cache.GetAddresses(m).Select(n => new CIDR(n, true)).ToArray()
174+
Ranges = Cache
175+
.GetAddresses(m)
176+
.Select(n => new CIDR(n, true))
177+
.Where(n => Version.HasFlag(n.Type))
178+
.ToArray()
146179
})
180+
.Where(m => m.Ranges.Length > 0)
147181
.ToArray();
148182
Debug("Clearing existing firewall rules...");
149-
Firewall.ClearRules();
183+
Debug("Removed {0} rules...", Firewall.ClearRules());
150184
Debug("Adding new rules...");
151185
Firewall.BlockRanges(FWRanges);
152-
Log("Blocked {0} ranges", FWRanges.SelectMany(m => m.Ranges).Count());
186+
Log("Blocked {0} ranges in {1} rules", FWRanges.SelectMany(m => m.Ranges).Count(), FWRanges.Length);
153187
}
154188
catch (Exception ex)
155189
{
@@ -382,7 +416,7 @@ private static bool GetCache()
382416

383417
private static void ShowHelp()
384418
{
385-
Console.Error.WriteLine(@"AnyBlock.exe [/v] [/clear | /config | /add dir name | /remove name | /apply | /list | /export <format>]
419+
Console.Error.WriteLine(@"AnyBlock.exe [/v] [/clear | /config | /add dir name | /remove name | /apply [v{4|6}]| /list | /export <format>]
386420
Blocks IP ranges in the Windows firewall
387421
388422
Shows a graphical configuration window if no arguments are specified.
@@ -424,11 +458,14 @@ To remove TOR exit nodes you would use the arguments /remove tor tor Exit
424458
You can only remove one entry at a time.
425459
To remove all entries, simply delete the 'settings.json' file
426460
427-
/apply
461+
/apply [v{4|6}]
428462
Applying the List will remove all blocked IPs that are no longer in the
429463
current list of addresses.
430464
To get most out of this command, schedule this as a task to be run every
431465
24 hours.
466+
You can optionally specify to only add IPv4 or IPv6 addresses.
467+
This will reduce the number of rules drastically
468+
if you're only reachable via one protocol.
432469
433470
/clear
434471
Removes all rules from the firewall without deleting them from the settings.

AnyBlock/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
// You can specify all the values or you can default the Build and Revision Numbers
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("2.1.0.0")]
35-
[assembly: AssemblyFileVersion("2.1.0.0")]
34+
[assembly: AssemblyVersion("2.2.0.0")]
35+
[assembly: AssemblyFileVersion("2.2.0.0")]

0 commit comments

Comments
 (0)