Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Create new net_utils.js core module

  • Loading branch information...
commit c92a0dc9306d8eb6ed347b290128b5f88a99ca6d 1 parent 72176bb
@smfreegard smfreegard authored
View
72 docs/Net_Utils.md
@@ -0,0 +1,72 @@
+Net_Utils
+=========
+
+This module provides network utility functions.
+
+Files
+-----
+
+This module depends on the following files to function:
+
+* top-level-tlds
+
+ Contains the list of TLDs valid on the internet.
+ Updates to this list can be found at:
+ http://data.iana.org/TLD/tlds-alpha-by-domain.txt
+
+* two-level-tlds
+
+ Contains the list of 2nd level TLDs.
+ Updates to this list can be found at:
+ http://george.surbl.org/two-level-tlds
+
+* three-level-tlds
+
+ Contains a list of 3rd level TLDs.
+ Updates to this list can be found at:
+ http://www.surbl.org/tld/three-level-tlds
+
+* extra-tlds
+
+ This allows for additional 2nd and 3rd level TLDs to be
+ from a single file. Used for site customizations or
+ for the URIBL hosters.txt that can be updated from:
+ http://rss.uribl.com/hosters/hosters.txt
+
+Usage
+-----
+
+ var net_utils = require('./net_utils');
+
+ // net_utils.top_level_tlds[key]
+ // net_utils.two_level_tlds[key]
+ // net_utils.three_level_tlds[key]
+
+ // Check for a TLD
+ if (net_utils.top_level_tlds['com']) {
+ // true
+ }
+
+ // Split FQDN to host and domain
+ var split = net_utils.split_hostname('host.sub1.sub2.domain.com');
+ // split[0] = 'host.sub1.sub2';
+ // split[1] = 'domain.com';
+
+ // Does all or part of an IP address appear within a string?
+ // This tests for the 1st and 2nd or 3rd and 4th octets of the IP
+ // Ot the entire IP address in hex within the string
+ if (net_utils.is_ip_in_str('11.22.33.44', '3344.rev.hoster.com')) {
+ // true
+ }
+
+ // Convert IPv4 to long
+ var long = net_utils.ip_to_long('11.22.33.44'); // 185999660
+
+ // Convert long to IPv4
+ var ip = net_utils.long_to_ip(185999660); // 11.22.33.44
+
+ // Convert decimal to hex
+ var hex = net_utils.dec_to_hex(20111104); // 132df00
+
+ // Convert hex to decimal
+ var dec = net_utils.hex_to_dec('132df00'); // 20111104
View
59 docs/TLDs.md
@@ -1,59 +0,0 @@
-TLDs
-====
-
-This module provides functions to split a hostname into host
-and domain/TLD parts and for checking to see if a name matches
-on one of the lists.
-
-This is useful for validating input or finding administrative
-boundaries.
-
-Files
------
-
-This module depends on the following files to function:
-
-* top-level-tlds
-
- Contains the list of TLDs valid on the internet.
- Updates to this list can be found at:
- http://data.iana.org/TLD/tlds-alpha-by-domain.txt
-
-* two-level-tlds
-
- Contains the list of 2nd level TLDs.
- Updates to this list can be found at:
- http://george.surbl.org/two-level-tlds
-
-* three-level-tlds
-
- Contains a list of 3rd level TLDs.
- Updates to this list can be found at:
- http://www.surbl.org/tld/three-level-tlds
-
-* extra-tlds
-
- This allows for additional 2nd and 3rd level TLDs to be
- from a single file. Used for site customizations or
- for the URIBL hosters.txt that can be updated from:
- http://rss.uribl.com/hosters/hosters.txt
-
-Usage
------
-
- var tlds = require('./tlds');
-
- // tlds.top_level_tlds[key]
- // tlds.two_level_tlds[key]
- // tlds.three_level_tlds[key]
-
- // Check for a TLD
- if (tlds.top_level_tlds['com']) {
- // true
- }
-
- // Split FQDN to host and domain
- var split = tlds.split_hostname('host.sub1.sub2.domain.com');
- // split[0] = 'host.sub1.sub2';
- // split[1] = 'domain.com';
-
View
60 tlds.js → net_utils.js
@@ -1,6 +1,10 @@
"use strict";
var logger = require('./logger');
var config = require('./config');
+var isIPv4 = require('net').isIPv4;
+
+// Regexp to match private IPv4 ranges
+var re_private_ipv4 = /(?:10|127|169\.254|172\.(?:1[6-9]|2[0-9]|3[01])|192\.168)\..*/;
var top_level_tlds = {};
config.get('top-level-tlds','list').forEach(function (tld) {
@@ -62,3 +66,59 @@ exports.split_hostname = function(host,level) {
return [split.reverse().join('.'), domain];
}
+exports.long_to_ip = function (n) {
+ var d = n%256;
+ for (var i=3; i>0; i--) {
+ n = Math.floor(n/256);
+ d = n%256 + '.' + d;
+ }
+ return d;
+}
+
+exports.dec_to_hex = function (d) {
+ return d.toString(16);
+}
+
+exports.hex_to_dec = function (h) {
+ return parseInt(h, 16);
+}
+
+exports.ip_to_long = function (ip) {
+ if (!isIPv4(ip)) {
+ return false;
+ }
+ else {
+ var d = ip.split('.');
+ return ((((((+d[0])*256)+(+d[1]))*256)+(+d[2]))*256)+(+d[3]);
+ }
+}
+
+exports.is_ip_in_str = function(ip, str) {
+ // Only IPv4 for now
+ if (isIPv4(ip)) {
+ var host_part = (this.split_hostname(str))[0].toLowerCase();
+ var ip_split = ip.split('.');
+ // 3rd and 4th octets
+ if ((host_part.indexOf(ip_split[2]) !== -1) && (host_part.indexOf(ip_split[3]) !== -1)) {
+ return true;
+ }
+ // 1st and 2nd octets
+ if ((host_part.indexOf(ip_split[0]) !== -1) && (host_part.indexOf(ip_split[1]) !== -1)) {
+ return true;
+ }
+ var ip_hex = this.dec_to_hex(this.ip_to_long(ip));
+ // Whole IP in hex
+ if ( (host_part.indexOf(ip_hex[0] + ip_hex[1]) !== -1) &&
+ (host_part.indexOf(ip_hex[2] + ip_hex[3]) !== -1) &&
+ (host_part.indexOf(ip_hex[4] + ip_hex[5]) !== -1) &&
+ (host_part.indexOf(ip_hex[6] + ip_hex[7]) !== -1) )
+ {
+ return true;
+ }
+ }
+ return false;
+}
+
+exports.is_rfc1918 = function (ip) {
+ return (isIPv4(ip) && re_private_ipv4.test(ip));
+}
View
23 plugins/data.uribl.js
@@ -1,11 +1,8 @@
// Look up URLs in SURBL
-var url = require('url');
-var dns = require('dns');
-var tlds = require('./tlds');
-var isIPv4 = require('net').isIPv4;
-
-// Regexp to match private IPv4 ranges
-var re_private_ipv4 = /(?:10|127|169\.254|172\.(?:1[6-9]|2[0-9]|3[01])|192\.168)\..*/;
+var url = require('url');
+var dns = require('dns');
+var isIPv4 = require('net').isIPv4;
+var net_utils = require('./net_utils.js');
// Default regexps to extract the URIs from the message
var numeric_ip = /\w{3,16}:\/+(\S+@)?(\d+|0[xX][0-9A-Fa-f]+)\.(\d+|0[xX][0-9A-Fa-f]+)\.(\d+|0[xX][0-9A-Fa-f]+)\.(\d+|0[xX][0-9A-Fa-f]+)/gi;
@@ -34,13 +31,13 @@ function check_excludes_list(host) {
exports.register = function() {
// Override regexps if top_level_tlds file is present
- if (tlds.top_level_tlds && Object.keys(tlds.top_level_tlds).length) {
+ if (net_utils.top_level_tlds && Object.keys(net_utils.top_level_tlds).length) {
this.logdebug('Building new regexps from TLD file');
var re_schemeless = '((?:www\\.)?[a-zA-Z0-9][a-zA-Z0-9\\-.]+\\.(?:' +
- Object.keys(tlds.top_level_tlds).join('|') + '))(?!\\w)';
+ Object.keys(net_utils.top_level_tlds).join('|') + '))(?!\\w)';
schemeless = new RegExp(re_schemeless, 'gi');
var re_schemed = '(\\w{3,16}:\\/+(?:\\S+@)?([a-zA-Z0-9][a-zA-Z0-9\\-.]+\\.(?:' +
- Object.keys(tlds.top_level_tlds).join('|') + ')))(?!\\w)';
+ Object.keys(net_utils.top_level_tlds).join('|') + ')))(?!\\w)';
schemed = new RegExp(re_schemed, 'gi');
}
}
@@ -78,7 +75,7 @@ exports.do_lookups = function (next, hosts, type) {
var host = hosts[i].toLowerCase();
this.logdebug('(' + type + ') checking: ' + host);
// Make sure we have a valid TLD
- if (!isIPv4(host) && !tlds.top_level_tlds[(host.split('\.').reverse())[0]]) {
+ if (!isIPv4(host) && !net_utils.top_level_tlds[(host.split('\.').reverse())[0]]) {
continue;
}
// Check the exclusion list
@@ -107,13 +104,13 @@ exports.do_lookups = function (next, hosts, type) {
continue;
}
// Skip any private IPs
- if (re_private_ipv4.test(host)) continue;
+ if (net_utils.is_rfc1918(host)) continue;
// Reverse IP for lookup
lookup = host.split(/\./).reverse().join('.');
}
// Handle zones that require host to be stripped to a domain boundary
else if (/^(?:1|true|yes|enabled|on)$/i.test(lists[zone].strip_to_domain)) {
- lookup = (tlds.split_hostname(host, 3))[1];
+ lookup = (net_utils.split_hostname(host, 3))[1];
}
// Anything else..
else {
View
16 plugins/helo.checks.js
@@ -1,6 +1,5 @@
// Check various bits of the HELO string
-var tlds = require('./tlds');
-var utils = require('./utils');
+var net_utils = require('./net_utils');
// Checks to implement:
// - HELO has no "dot"
@@ -9,9 +8,6 @@ var utils = require('./utils');
// - HELO looks dynamic
// - Well known HELOs that must match rdns
-// Regexp to match private IPv4 ranges
-var re_private_ipv4 = /(?:10|127|169\.254|172\.(?:1[6-9]|2[0-9]|3[01])|192\.168)\..*/;
-
exports.register = function () {
var plugin = this;
['helo_no_dot',
@@ -30,7 +26,7 @@ exports.helo_no_dot = function (next, connection, helo) {
if (!config.main.check_no_dot ||
!config.main.require_valid_tld ||
(config.main.skip_private_ip &&
- re_private_ipv4.test(connection.remote_ip)))
+ net_utils.is_rfc1918(connection.remote_ip)))
{
return next();
}
@@ -41,7 +37,7 @@ exports.helo_no_dot = function (next, connection, helo) {
if (config.main.require_valid_tld) {
var tld = (helo.split(/\./).reverse())[0];
- if (!/^\[\d+\.\d+\.\d+\.\d+\]$/.test(helo) && !tlds.top_level_tlds[tld]) {
+ if (!/^\[\d+\.\d+\.\d+\.\d+\]$/.test(helo) && !net_utils.top_level_tlds[tld]) {
return next(DENY, "HELO must have a valid TLD");
}
}
@@ -65,7 +61,7 @@ exports.helo_raw_ip = function (next, connection, helo) {
var config = this.config.get('helo.checks.ini');
if (!config.main.check_raw_ip ||
(config.main.skip_private_ip &&
- re_private_ipv4.test(connection.remote_ip)))
+ net_utils.is_rfc1918(connection.remote_ip)))
{
return next();
}
@@ -80,7 +76,7 @@ exports.helo_is_dynamic = function (next, connection, helo) {
var config = this.config.get('helo.checks.ini');
if (!config.main.check_dynamic ||
(config.main.skip_private_ip &&
- re_private_ipv4.test(connection.remote_ip)))
+ net_utils.is_rfc1918(connection.remote_ip)))
{
return next();
}
@@ -89,7 +85,7 @@ exports.helo_is_dynamic = function (next, connection, helo) {
return next();
}
- (utils.ip_in_str(connection.remote_ip, helo)) ?
+ (utils.is_ip_in_str(connection.remote_ip, helo)) ?
next(DENY, 'HELO is dynamic')
: next();
};
Please sign in to comment.
Something went wrong with that request. Please try again.