/core/filters/base.rb

https://github.com/michaelhidalgo/beef · Ruby · 187 lines · 100 code · 19 blank · 68 comment · 20 complexity · 864ca54559c10c66de21a6291747386d MD5 · raw file

  1. #
  2. # Copyright (c) 2006-2014 Wade Alcorn - wade@bindshell.net
  3. # Browser Exploitation Framework (BeEF) - http://beefproject.com
  4. # See the file 'doc/COPYING' for copying permission
  5. #
  6. module BeEF
  7. module Filters
  8. # Check if the string is not empty and not nil
  9. # @param [String] str String for testing
  10. # @return [Boolean] Whether the string is not empty
  11. def self.is_non_empty_string?(str)
  12. return false if str.nil?
  13. return false if not str.is_a? String
  14. return false if str.empty?
  15. true
  16. end
  17. # Check if only the characters in 'chars' are in 'str'
  18. # @param [String] chars List of characters to match
  19. # @param [String] str String for testing
  20. # @return [Boolean] Whether or not the only characters in str are specified in chars
  21. def self.only?(chars, str)
  22. regex = Regexp.new('[^' + chars + ']')
  23. regex.match(str).nil?
  24. end
  25. # Check if one or more characters in 'chars' are in 'str'
  26. # @param [String] chars List of characters to match
  27. # @param [String] str String for testing
  28. # @return [Boolean] Whether one of the characters exists in the string
  29. def self.exists?(chars, str)
  30. regex = Regexp.new(chars)
  31. not regex.match(str).nil?
  32. end
  33. # Check for null char
  34. # @param [String] str String for testing
  35. # @return [Boolean] If the string has a null character
  36. def self.has_null? (str)
  37. return false if not is_non_empty_string?(str)
  38. exists?('\x00', str)
  39. end
  40. # Check for non-printable char
  41. # @param [String] str String for testing
  42. # @return [Boolean] Whether or not the string has non-printable characters
  43. def self.has_non_printable_char?(str)
  44. return false if not is_non_empty_string?(str)
  45. not only?('[:print:]', str)
  46. end
  47. # Check if num characters only
  48. # @param [String] str String for testing
  49. # @return [Boolean] If the string only contains numbers
  50. def self.nums_only?(str)
  51. return false if not is_non_empty_string?(str)
  52. only?('0-9', str)
  53. end
  54. # Check if valid float
  55. # @param [String] str String for float testing
  56. # @return [Boolean] If the string is a valid float
  57. def self.is_valid_float?(str)
  58. return false if not is_non_empty_string?(str)
  59. return false if not only?('0-9\.', str)
  60. not (str =~ /^[\d]+\.[\d]+$/).nil?
  61. end
  62. # Check if hex characters only
  63. # @param [String] str String for testing
  64. # @return [Boolean] If the string only contains hex characters
  65. def self.hexs_only?(str)
  66. return false if not is_non_empty_string?(str)
  67. only?('0123456789ABCDEFabcdef', str)
  68. end
  69. # Check if first character is a number
  70. # @param [String] String for testing
  71. # @return [Boolean] If the first character of the string is a number
  72. def self.first_char_is_num?(str)
  73. return false if not is_non_empty_string?(str)
  74. not (str =~ /^\d.*/).nil?
  75. end
  76. # Check for space characters: \t\n\r\f
  77. # @param [String] str String for testing
  78. # @return [Boolean] If the string has a whitespace character
  79. def self.has_whitespace_char?(str)
  80. return false if not is_non_empty_string?(str)
  81. exists?('\s', str)
  82. end
  83. # Check for non word characters: a-zA-Z0-9
  84. # @param [String] str String for testing
  85. # @return [Boolean] If the string only has alphanums
  86. def self.alphanums_only?(str)
  87. return false if not is_non_empty_string?(str)
  88. only?("a-zA-Z0-9", str)
  89. end
  90. # @overload self.is_valid_ip?(version, ip)
  91. # Checks if the given string is a valid IP address
  92. # @param [Symbol] version IP version (either <code>:ipv4</code> or <code>:ipv6</code>)
  93. # @param [String] ip string to be tested
  94. # @return [Boolean] true if the string is a valid IP address, otherwise false
  95. #
  96. # @overload self.is_valid_ip?(ip)
  97. # Checks if the given string is either a valid IPv4 or IPv6 address
  98. # @param [String] ip string to be tested
  99. # @return [Boolean] true if the string is a valid IPv4 or IPV6 address, otherwise false
  100. def self.is_valid_ip?(version = :both, ip)
  101. valid = false
  102. if is_non_empty_string?(ip)
  103. valid = case version.inspect.downcase
  104. when /^:ipv4$/
  105. ip =~ /^((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}
  106. (25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])$/x
  107. when /^:ipv6$/
  108. ip =~ /^(([0-9a-f]{1,4}:){7,7}[0-9a-f]{1,4}|
  109. ([0-9a-f]{1,4}:){1,7}:|
  110. ([0-9a-f]{1,4}:){1,6}:[0-9a-f]{1,4}|
  111. ([0-9a-f]{1,4}:){1,5}(:[0-9a-f]{1,4}){1,2}|
  112. ([0-9a-f]{1,4}:){1,4}(:[0-9a-f]{1,4}){1,3}|
  113. ([0-9a-f]{1,4}:){1,3}(:[0-9a-f]{1,4}){1,4}|
  114. ([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,5}|
  115. [0-9a-f]{1,4}:((:[0-9a-f]{1,4}){1,6})|
  116. :((:[0-9a-f]{1,4}){1,7}|:)|
  117. fe80:(:[0-9a-f]{0,4}){0,4}%[0-9a-z]{1,}|
  118. ::(ffff(:0{1,4}){0,1}:){0,1}
  119. ((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}
  120. (25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|
  121. ([0-9a-f]{1,4}:){1,4}:
  122. ((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}
  123. (25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/ix
  124. when /^:both$/
  125. is_valid_ip?(:ipv4, ip) || is_valid_ip?(:ipv6, ip)
  126. end ? true : false
  127. end
  128. valid
  129. end
  130. # Checks if string is a valid domain name
  131. # @param [String] domain string for testing
  132. # @return [Boolean] If the string is a valid domain name
  133. # @note Only validates the string format. It does not check for a valid TLD since ICANN's list of
  134. # TLD's is not static.
  135. def self.is_valid_domain?(domain)
  136. return false unless is_non_empty_string?(domain)
  137. return true if domain =~ /^[0-9a-z-]+(\.[0-9a-z-]+)*(\.[a-z]{2,}).?$/i
  138. false
  139. end
  140. # Check for valid browser details characters
  141. # @param [String] str String for testing
  142. # @return [Boolean] If the string has valid browser details characters
  143. # @note This function passes the \302\256 character which translates to the registered symbol (r)
  144. def self.has_valid_browser_details_chars?(str)
  145. return false if not is_non_empty_string?(str)
  146. not (str =~ /[^\w\d\s()-.,;:_\/!\302\256]/).nil?
  147. end
  148. # Check for valid base details characters
  149. # @param [String] str String for testing
  150. # @return [Boolean] If the string has only valid base characters
  151. # @note This is for basic filtering where possible all specific filters must be implemented
  152. # @note This function passes the \302\256 character which translates to the registered symbol (r)
  153. def self.has_valid_base_chars?(str)
  154. return false if not is_non_empty_string?(str)
  155. (str =~ /[^\302\256[:print:]]/).nil?
  156. end
  157. # Verify the yes and no is valid
  158. # @param [String] str String for testing
  159. # @return [Boolean] If the string is either 'yes' or 'no'
  160. # @todo Confirm this is case insensitive
  161. def self.is_valid_yes_no?(str)
  162. return false if has_non_printable_char?(str)
  163. return false if str !~ /^(Yes|No)$/
  164. return false if str.length > 200
  165. true
  166. end
  167. end
  168. end