Skip to content

Commit d215043

Browse files
Enhance IP range detection with comprehensive private IP range support
1 parent b5d9d67 commit d215043

File tree

2 files changed

+54
-29
lines changed

2 files changed

+54
-29
lines changed

Aikido.Zen.Core/Helpers/IPHelper.cs

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,59 @@
22
using System.Linq;
33
using System.Collections.Generic;
44
using System;
5+
using Aikido.Zen.Core.Models.Ip;
56

67
namespace Aikido.Zen.Core.Helpers
78
{
89
public class IPHelper
910
{
11+
private static readonly IPRange _privateIpRanges;
12+
13+
static IPHelper()
14+
{
15+
_privateIpRanges = new IPRange();
16+
17+
// IPv4 private ranges
18+
var ipv4Ranges = new[]
19+
{
20+
"0.0.0.0/8",
21+
"10.0.0.0/8",
22+
"100.64.0.0/10",
23+
"127.0.0.0/8",
24+
"169.254.0.0/16",
25+
"172.16.0.0/12",
26+
"192.0.0.0/24",
27+
"192.0.2.0/24",
28+
"192.31.196.0/24",
29+
"192.52.193.0/24",
30+
"192.88.99.0/24",
31+
"192.168.0.0/16",
32+
"192.175.48.0/24",
33+
"198.18.0.0/15",
34+
"198.51.100.0/24",
35+
"203.0.113.0/24",
36+
"240.0.0.0/4",
37+
"224.0.0.0/4",
38+
"255.255.255.255/32"
39+
};
40+
41+
// IPv6 private ranges
42+
var ipv6Ranges = new[]
43+
{
44+
"::/128", // Unspecified address
45+
"::1/128", // Loopback address
46+
"fc00::/7", // Unique local address (ULA)
47+
"fe80::/10", // Link-local address (LLA)
48+
"::ffff:127.0.0.1/128", // IPv4-mapped address
49+
"::ffff:0:0/96" // IPv4-mapped addresses
50+
};
51+
52+
foreach (var range in ipv4Ranges.Concat(ipv6Ranges))
53+
{
54+
_privateIpRanges.InsertRange(range);
55+
}
56+
}
57+
1058
public static string Server
1159
{
1260
get
@@ -141,39 +189,14 @@ private static long IMask(int s)
141189

142190
/// <summary>
143191
/// Checks if an IP address is a private IP address.
144-
/// Supports both IPv4 and IPv6 address formats.
192+
/// Supports both IPv4 and IPv6 address formats using a comprehensive list of private IP ranges.
193+
/// https://github.com/AikidoSec/firewall-node/blob/02f25f1e2566c84e695b9f4b7d1723138485654d/library/vulnerabilities/ssrf/isPrivateIP.ts
145194
/// </summary>
146195
/// <param name="ip">The IP address to check.</param>
147196
/// <returns>True if the IP address is private, false otherwise.</returns>
148197
private static bool IsPrivateIPAddress(IPAddress ip)
149198
{
150-
byte[] ipBytes = ip.GetAddressBytes();
151-
152-
// Check if IPv4 private address ranges
153-
if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
154-
{
155-
// 10.0.0.0/8
156-
// 172.16.0.0/12
157-
// 192.168.0.0/16
158-
if (ipBytes[0] == 10 ||
159-
(ipBytes[0] == 172 && ipBytes[1] >= 16 && ipBytes[1] <= 31) ||
160-
(ipBytes[0] == 192 && ipBytes[1] == 168))
161-
{
162-
return true;
163-
}
164-
}
165-
166-
// Check if IPv6 private address ranges
167-
if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
168-
{
169-
// Unique local addresses (fc00::/7)
170-
if ((ipBytes[0] & 0xFE) == 0xFC)
171-
{
172-
return true;
173-
}
174-
}
175-
176-
return false;
199+
return _privateIpRanges.IsIpInRange(ip.ToString());
177200
}
178201
}
179202
}

Aikido.Zen.DotNetCore/DependencyInjection.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ internal ZenFirewallBuilder(IServiceCollection services)
2626
}
2727

2828
/// <summary>
29-
/// Configures a custom HttpClient to be used by the Zen API clients
29+
/// Configures a custom HttpClient to be used by the Zen API clients, helpful for testing
3030
/// </summary>
3131
/// <param name="httpClient">The HttpClient instance to use</param>
3232
/// <returns>The builder instance for method chaining</returns>
@@ -38,6 +38,7 @@ public ZenFirewallBuilder UseHttpClient(HttpClient httpClient)
3838

3939
internal void ConfigureServices()
4040
{
41+
// if we have a custom httpclient, use it
4142
if (_httpClient != null)
4243
{
4344
_services.AddTransient<IReportingAPIClient>(provider =>
@@ -49,6 +50,7 @@ internal void ConfigureServices()
4950
return new RuntimeAPIClient(_httpClient);
5051
});
5152
}
53+
// otherwise, use the default httpclient
5254
else
5355
{
5456
_services.AddTransient<IReportingAPIClient>(provider =>

0 commit comments

Comments
 (0)