DefangIPAddresses.mjs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * @author h345983745
  3. * @copyright Crown Copyright 2019
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation.mjs";
  7. /**
  8. * Defang IP Addresses operation
  9. */
  10. class DefangIPAddresses extends Operation {
  11. /**
  12. * DefangIPAddresses constructor
  13. */
  14. constructor() {
  15. super();
  16. this.name = "Defang IP Addresses";
  17. this.module = "Default";
  18. 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.";
  19. this.infoURL = "https://isc.sans.edu/forums/diary/Defang+all+the+things/22744/";
  20. this.inputType = "string";
  21. this.outputType = "string";
  22. this.args = [];
  23. }
  24. /**
  25. * @param {string} input
  26. * @param {Object[]} args
  27. * @returns {string}
  28. */
  29. run(input, args) {
  30. input = input.replace(IPV4_REGEX, x => {
  31. return x.replace(/\./g, "[.]");
  32. });
  33. input = input.replace(IPV6_REGEX, x => {
  34. return x.replace(/:/g, "[:]");
  35. });
  36. return input;
  37. }
  38. }
  39. export default DefangIPAddresses;
  40. /**
  41. * IPV4 regular expression
  42. */
  43. 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");
  44. /**
  45. * IPV6 regular expression
  46. */
  47. 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");