PageRenderTime 61ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/ronin/extensions/regexp.rb

https://bitbucket.org/postmodern/ronin-support
Ruby | 112 lines | 45 code | 26 blank | 41 comment | 0 complexity | 5d05427e28e9acf040d8d778889bc0d0 MD5 | raw file
  1. #
  2. # Copyright (c) 2006-2012 Hal Brodigan (postmodern.mod3 at gmail.com)
  3. #
  4. # This file is part of Ronin Support.
  5. #
  6. # Ronin Support is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Lesser General Public License as published
  8. # by the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Ronin Support is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Lesser General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Lesser General Public License
  17. # along with Ronin Support. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. require 'ronin/extensions/resolv'
  20. class Regexp
  21. # Regular expression for finding a decimal octet (0 - 255)
  22. OCTET = /25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9]/
  23. # Regular expression for finding MAC addresses in text
  24. MAC = /[0-9a-fA-F]{2}(?::[0-9a-fA-F]{2}){5}/
  25. # A regular expression for matching IPv4 Addresses.
  26. IPv4 = /#{OCTET}(?:\.#{OCTET}){3}(?:\/\d{1,2})?/
  27. # A regular expression for matching IPv6 Addresses.
  28. IPv6 = union(
  29. /(?:[0-9a-f]{1,4}:){6}#{IPv4}/,
  30. /(?:[0-9a-f]{1,4}:){5}[0-9a-f]{1,4}:#{IPv4}/,
  31. /(?:[0-9a-f]{1,4}:){5}:[0-9a-f]{1,4}:#{IPv4}/,
  32. /(?:[0-9a-f]{1,4}:){1,1}(?::[0-9a-f]{1,4}){1,4}:#{IPv4}/,
  33. /(?:[0-9a-f]{1,4}:){1,2}(?::[0-9a-f]{1,4}){1,3}:#{IPv4}/,
  34. /(?:[0-9a-f]{1,4}:){1,3}(?::[0-9a-f]{1,4}){1,2}:#{IPv4}/,
  35. /(?:[0-9a-f]{1,4}:){1,4}(?::[0-9a-f]{1,4}){1,1}:#{IPv4}/,
  36. /:(?::[0-9a-f]{1,4}){1,5}:#{IPv4}/,
  37. /(?:(?:[0-9a-f]{1,4}:){1,5}|:):#{IPv4}/,
  38. /(?:[0-9a-f]{1,4}:){1,1}(?::[0-9a-f]{1,4}){1,6}(?:\/\d{1,3})?/,
  39. /(?:[0-9a-f]{1,4}:){1,2}(?::[0-9a-f]{1,4}){1,5}(?:\/\d{1,3})?/,
  40. /(?:[0-9a-f]{1,4}:){1,3}(?::[0-9a-f]{1,4}){1,4}(?:\/\d{1,3})?/,
  41. /(?:[0-9a-f]{1,4}:){1,4}(?::[0-9a-f]{1,4}){1,3}(?:\/\d{1,3})?/,
  42. /(?:[0-9a-f]{1,4}:){1,5}(?::[0-9a-f]{1,4}){1,2}(?:\/\d{1,3})?/,
  43. /(?:[0-9a-f]{1,4}:){1,6}(?::[0-9a-f]{1,4}){1,1}(?:\/\d{1,3})?/,
  44. /[0-9a-f]{1,4}(?::[0-9a-f]{1,4}){7}(?:\/\d{1,3})?/,
  45. /:(?::[0-9a-f]{1,4}){1,7}(?:\/\d{1,3})?/,
  46. /(?:(?:[0-9a-f]{1,4}:){1,7}|:):(?:\/\d{1,3})?/
  47. )
  48. # A regular expression for matching IP Addresses.
  49. IP = /#{IPv4}|#{IPv6}/
  50. # Regular expression used to find host-names in text
  51. HOST_NAME = /(?:[a-zA-Z0-9]+(?:[_-][a-zA-Z0-9]+)*\.)+(?:#{union(Resolv::TLDS)})/i
  52. # Regular expression to match a word in the username of an email address
  53. USER_NAME = /[A-Za-z](?:[A-Za-z0-9]*[\._-])*[A-Za-z0-9]+/
  54. # Regular expression to find email addresses in text
  55. EMAIL_ADDR = /#{USER_NAME}\@#{HOST_NAME}/
  56. # Regular expression to find deliminators in text
  57. DELIM = /[;&\n\r]/
  58. # Regular expression to find identifier in text
  59. IDENTIFIER = /[_]*[a-zA-Z]+[a-zA-Z0-9_-]*/
  60. # Regular expression to find File extensions in text
  61. FILE_EXT = /(?:\.[A-Za-z0-9]+)+/
  62. # Regular expression to find File names in text
  63. FILE_NAME = /(?:[^\/\\\. ]|\\[\/\\ ])+/
  64. # Regular expression to find Files in text
  65. FILE = /#{FILE_NAME}(?:#{FILE_EXT})?/
  66. # Regular expression to find Directory names in text
  67. DIRECTORY = /(?:\.\.|\.|#{FILE})/
  68. # Regular expression to find local UNIX Paths in text
  69. RELATIVE_UNIX_PATH = /(?:#{DIRECTORY}\/)+#{DIRECTORY}\/?/
  70. # Regular expression to find absolute UNIX Paths in text
  71. ABSOLUTE_UNIX_PATH = /(?:\/#{FILE})+\/?/
  72. # Regular expression to find UNIX Paths in text
  73. UNIX_PATH = /#{ABSOLUTE_UNIX_PATH}|#{RELATIVE_UNIX_PATH}/
  74. # Regular expression to find local Windows Paths in text
  75. RELATIVE_WINDOWS_PATH = /(?:#{DIRECTORY}\\)+#{DIRECTORY}\\?/
  76. # Regular expression to find absolute Windows Paths in text
  77. ABSOLUTE_WINDOWS_PATH = /[A-Za-z]:(?:\\#{FILE})+\\?/
  78. # Regular expression to find Windows Paths in text
  79. WINDOWS_PATH = /#{ABSOLUTE_WINDOWS_PATH}|#{RELATIVE_WINDOWS_PATH}/
  80. # Regular expression to find local Paths in text
  81. RELATIVE_PATH = /#{RELATIVE_UNIX_PATH}|#{RELATIVE_WINDOWS_PATH}/
  82. # Regular expression to find absolute Paths in text
  83. ABSOLUTE_PATH = /#{ABSOLUTE_UNIX_PATH}|#{ABSOLUTE_WINDOWS_PATH}/
  84. # Regular expression to find Paths in text
  85. PATH = /#{UNIX_PATH}|#{WINDOWS_PATH}/
  86. end