DefangIPAddresses.mjs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. this.checks = {
  24. input: {
  25. regex: [
  26. {
  27. match: "^\\s*(([0-9]{1,3}\\.){3}[0-9]{1,3}|([0-9a-f]{4}:){7}[0-9a-f]{4})\\s*$",
  28. flags: "i",
  29. args: [],
  30. }
  31. ]
  32. },
  33. output: {
  34. regex: [
  35. {
  36. match: "^\\s*(([0-9]{1,3}\\[\\.\\]){3}[0-9]{1,3}|([0-9a-f]{4}\\[\\:\\]){7}[0-9a-f]{4})\\s*$",
  37. flags: "i",
  38. shouldMatch: true,
  39. args: []
  40. }
  41. ]
  42. }
  43. };
  44. }
  45. /**
  46. * @param {string} input
  47. * @param {Object[]} args
  48. * @returns {string}
  49. */
  50. run(input, args) {
  51. input = input.replace(IPV4_REGEX, x => {
  52. return x.replace(/\./g, "[.]");
  53. });
  54. input = input.replace(IPV6_REGEX, x => {
  55. return x.replace(/:/g, "[:]");
  56. });
  57. return input;
  58. }
  59. }
  60. export default DefangIPAddresses;
  61. /**
  62. * IPV4 regular expression
  63. */
  64. 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");
  65. /**
  66. * IPV6 regular expression
  67. */
  68. 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");