2019-05-11 20:24:39 +01:00
/**
* @author h345983745
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/
2019-11-06 06:01:52 -07:00
import Operation from "../Operation.mjs" ;
2019-05-11 20:24:39 +01:00
/**
2019-08-13 14:23:41 +01:00
* Defang IP Addresses operation
2019-05-11 20:24:39 +01:00
*/
2019-08-13 14:23:41 +01:00
class DefangIPAddresses extends Operation {
2019-05-11 20:24:39 +01:00
/**
2019-08-13 14:23:41 +01:00
* DefangIPAddresses constructor
2019-05-11 20:24:39 +01:00
*/
constructor () {
super ();
2019-08-13 14:23:41 +01:00
this . name = "Defang IP Addresses" ;
2019-05-11 20:24:39 +01:00
this . module = "Default" ;
2019-08-13 14:23:41 +01:00
this . description = "Takes a IPv4 or IPv6 address and 'Defangs' it, meaning the IP becomes invalid, removing the risk of accidentally utilising it as an IP address." ;
2019-05-11 20:24:39 +01:00
this . infoURL = "https://isc.sans.edu/forums/diary/Defang+all+the+things/22744/" ;
this . inputType = "string" ;
this . outputType = "string" ;
2019-06-29 01:12:50 +01:00
this . args = [];
2020-03-24 11:06:37 +00:00
this . checks = [
{
pattern : "^\\s*(([0-9]{1,3}\\.){3}[0-9]{1,3}|([0-9a-f]{4}:){7}[0-9a-f]{4})\\s*$" ,
flags : "i" ,
args : [],
output : {
pattern : "^\\s*(([0-9]{1,3}\\[\\.\\]){3}[0-9]{1,3}|([0-9a-f]{4}\\[\\:\\]){7}[0-9a-f]{4})\\s*$" ,
flags : "i"
}
2020-02-25 11:27:03 +00:00
}
2020-03-24 11:06:37 +00:00
];
2019-05-11 20:24:39 +01:00
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run ( input , args ) {
2019-06-29 01:12:50 +01:00
input = input . replace ( IPV4_REGEX , x => {
return x . replace ( /\./g , "[.]" );
});
input = input . replace ( IPV6_REGEX , x => {
return x . replace ( /:/g , "[:]" );
});
2019-05-11 20:24:39 +01:00
return input ;
}
}
2019-08-13 14:23:41 +01:00
export default DefangIPAddresses ;
2019-05-11 20:24:39 +01:00
/**
* IPV4 regular expression
*/
const IPV4_REGEX = new RegExp ( "(?:(?:\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d\\d|\\d)(?:\\/\\d{1,2})?" , "g" );
/**
* IPV6 regular expression
*/
const IPV6_REGEX = new RegExp ( "((?=.*::)(?!.*::.+::)(::)?([\\dA-Fa-f]{1,4}:(:|\\b)|){5}|([\\dA-Fa-f]{1,4}:){6})((([\\dA-Fa-f]{1,4}((?!\\3)::|:\\b|(?![\\dA-Fa-f])))|(?!\\2\\3)){2}|(((2[0-4]|1\\d|[1-9])?\\d|25[0-5])\\.?\\b){4})" , "g" );