Wednesday, March 22, 2023
HomeColdFusionDamaging An IPv4 Address Array Up Into CIDR Varies In Lucee CFML...

Damaging An IPv4 Address Array Up Into CIDR Varies In Lucee CFML

A few days ago, this blog site was being assaulted by a harmful star in Australia. I determined 38 special IP addresses that were all jabbing as well as pushing the application, searching for weak points. Luckily, this created absolutely no problems for the website itself or its site visitors. Nevertheless, in the warmth of the minute, as I was including these IPs to Cloudflare’s Internet Application Firewall program (WAF) guidelines, I understood that my understanding of just how IP addresses job was fairly doing not have. I required to produce CIDR arrays for the WAF; however, had not been certain just how to do that. Thus, I wished to take a minute as well as experiment with IP addresses, taking a provided array as well as damaging it up right into the tightest feasible CIDR varies in Lucee CFML.

For this expedition, I’m mosting likely to utilize the Commons IP Mathematics Java collection developed by Yannis Gonianakis I have actually utilized this collection in the past to see if a provided IP address drops within a CIDR array; as well as, it’s fairly hassle-free! In addition to that, I’m mosting likely to utilize Lucee CFML’s capability to dynamically tons container documents on the fly in createObject() calls

I do not truly recognize networking methods whatsoever So, I’m not mosting likely to attempt as well as discuss just how IP addresses operate in any kind of information – that’s finest entrusted to individuals that recognize what the hell they’re really speaking about. However, for this expedition, I’ll state that a CIDR (Egalitarian Inter-Domain Routing) array is an IP address symbols that represents an adjoining series of IPs that can be stood for making use of a variety of right-hand little bits within an IP address.

The variety of little bits “secured down” is specified by the lower in the CIDR array. For instance, in this CIDR array: 31

… the “31” at the end implies that the left 31 littles the IP address are “secured down”, ie can not be altered. Thus, every one of the IP addresses in this array are stood for by a solitary vibrant little bit at the end:

00001010.00000000.00000000.0000001 *

That last little bit can either be a 1 or a 0, which implies that this CIDR array can just stands for 2 special IP addresses:

  • 00001010.00000000.00000000.00000010 – finishing in .2
  • 00001010.00000000.00000000.00000011 – finishing in .3

The less “secured down” little bits in the CIDR array, the much more vibrant little bits there at the end, the bigger the variety of IPs that can be stood for by a solitary array. My objective in this experiment is to locate CIDR arrays that stand for a provided collection of IPs without producing arrays that include even more IPs than we desire

To do this, I’m mosting likely to sequentially go across from the most affordable IP to the greatest IP as well as check out the “usual prefix” (ie, the variety of “secured down” little bits) in between each set of brother or sister addresses. If both brother or sisters share an usual prefix – as well as if that prefix does not enable IP addresses outside our wanted array – after that both brother or sister addresses can be component of the exact same CIDR array. Or else, both brother or sister addresses need to be divided as well as stood for by 2 various CIDR arrays.

Below’s my formula for doing this in ColdFusion making use of the Commons-Ip-Math. Now, I’ll delay all description to the remarks in the code:

<< cfscript>>.


// Some IP energy courses that will certainly assist us compute our arrays.
Ipv4 = javaNew( "com.github.jgonian.ipmath.Ipv4" );
Ipv4Range = javaNew( "com.github.jgonian.ipmath.Ipv4Range" );.

// Envision that we intend to produce CIDR (Egalitarian Inter-Domain Routing) varies that.
// include the provided min/max IP addresses. If we attempted to produce a "solitary array".
// to rule them all, we  would certainly wind up needing to produce a variety bigger than we wished to.
// (which would certainly consist of IPs outside this min/max). Thus, we need to damage this.
// array up right into a collection of tighter arrays that consist of just the IPs we have below.
minIpAddress = Ipv4.of( "" );.
maxIpAddress = Ipv4.of( "" );.

// CIDR varies job by securing down a provided variety of "prefix little bits" in the IP address.
// The IPs in a provided array are after that stood for by the extensive mix of the.
// staying "suffix little bits". To produce "limited" arrays (ie, varies that just consist of the.
// IPs that we desire as well as absolutely nothing additional) we have produce sections in which the "most affordable".
// as well as the "greatest" IPs for a provided prefix autumn within the series of IPs that we desire.
// to target. To compute this, I'm mosting likely to begin at the most affordable IP as well as incrementally.
// action in the direction of the greatest IP, examining to see if the sub-slice of IPs suit a.
// limited array. If they do, they can be component of the exact same CIDR array; however, if they.
// do not, I need to divide the sub-slice even more, producing 2 tighter arrays.
section = {
from: minIpAddress,.
to: minIpAddress.
// Monitor all the sections that enter into our array.
arrays = [ segment ];.

// Maintain tipping up in the IP array while our existing section's TO IP is much less than the.
// optimal IP in our wanted array.
while (isIpLessThan(, maxIpAddress)) {

following =;.

// To see if the "following" IP suits the existing section, we need to obtain the usual.
// prefix of both IPs (ie, the variety of locked-down "prefix little bits" in the IP.
// address that incorporates both IPs); as well as, we need to ensure that stated prefix.
// does not include any kind of IPs OUTSIDE the existing section.
commonPrefix = segment.from.getCommonPrefixLength( following );.
minPrefixIpAddress = next.lowerBoundForPrefix( commonPrefix );.
maxPrefixIpAddress = next.upperBoundForPrefix( commonPrefix );.

// If the lower-bound IP incorporated by the usual prefix is smaller sized than our.
// existing section; or, if the upper-bound IP incorporated by the usual prefix is.
// bigger than our general IP-range; after that, we require to divide right into a brand-new section.
// with a tighter prefix.
if (.
isIpLessThan( minPrefixIpAddress, segment.from)||
isIpGreaterThan( maxPrefixIpAddress, maxIpAddress ).
) {

section = {
from: following,.
to: following.
ranges.append( section );.

// If the lower/upper-bound IPs drop within our general IP-range, than we can simply.
// expand the existing section to consist of the provided IP and after that relocate onto the following.
// IP in the array.
} else { = following;.



// -------------------------------------------------------------------------------//.
// -------------------------------------------------------------------------------//.

// Now, we have actually taken our min/man IP-range as well as divided it up right into a collection of CIDR.
// arrays that stand for just the IPs that we appreciate.
// KEEP IN MIND: I have actually ascertained this making use of the CIDR IP-Address energies supplied by.
for (section in arrays) {

segmentRange = Ipv4Range
. from( segment.from )
. to( ).

resemble( segmentRange.toStringInCidrNotation() );.
resemble(" ... which has ..." );.
resemble( segmentRange.toStringInRangeNotationWithSpaces() );.
resemble( "<< br/>>" );.


// -------------------------------------------------------------------------------//.
// -------------------------------------------------------------------------------//.

/ **.
* I figure out if the very first Commons-Ip-Math address is smaller sized than the 2nd one.
public boolean feature isIpLessThan(.
called for any kind of a,.
called for any kind of b.
) {

return( a.compareTo( b) < < 0 );.


/ **.
* I figure out if the very first Commons-Ip-Math address is above the 2nd one.
public boolean feature isIpGreaterThan(.
called for any kind of a,.
called for any kind of b.
) {

return( a.compareTo( b) > > 0 );.


/ **.
* I produce the Commons-Ip-Math course meaning with the provided name.
public any kind of feature javaNew( called for string className) {

var jarPaths = [
			expandPath( "/vendor/commons-ip-math-1.32.jar" )

return( createObject( "java", className, jarPaths) );.



This code actions from approximately, damaging the array down right into "limited" CIDR arrays. And also, when we run this ColdFusion code, we obtain the list below outcome: 32 ... which has ... 30 ... which has ... 29 ... which has ... 28 ... which has ... 27 ... which has ... 29 ... which has ... 32 ... which has ...

As you can see, the series of IPs can not be stood for by one huge CIDR array Rather, we need to damage it up right into a number of smaller sized CIDR arrays that include the restricted collection of IPs we are targeting as well as absolutely nothing even more

ASIDE: As I was servicing this ColdFusion formula, I was making use of the IP Address Overview CIDR devices to both comprehend the restraints of the trouble as well as to verify that my outcome matched their outcome. Much version was required to obtain this right!

This article utilizes IPv4 addresses; however, I think that a formula for IPv6 addresses would certainly function similarly, just with a greatly bigger array of feasible addresses. With any luck, I really did not obtain any one of this also incorrect, practically. Like I stated previously, I do not truly comprehend anything regarding networking. However, child actions similar to this are gradually filling out the spaces.

Wish to utilize code from this article?
Have a look at the certificate


Most Popular

Recent Comments