Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/ronin/fuzzing/core_ext/string.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.generate(*fields) {|string| ... } ⇒ Enumerator

Generate permutations of Strings from a format template.

Examples:

Generate Strings with three alpha chars and one numeric chars:

String.generate([:alpha, 3], :numeric) do |password|
  puts password
end

Generate Strings with two to four alpha chars:

String.generate([:alpha, 2..4]) do |password|
  puts password
end

Generate Strings using alpha and punctuation chars:

String.generate([Chars.alpha + Chars.punctuation, 4]) do |password|
  puts password
end

Generate Strings from a custom char set:

String.generate([['a', 'b', 'c'], 3], [['1', '2', '3'], 3]) do |password|
  puts password
end

Generate Strings containing known Strings:

String.generate("rock", [:numeric, 4]) do |password|
  puts password
end

Generate Strings with ranges of repeating sub-strings:

String.generate(['/AA', (1..100).step(5)]) do |path|
  puts path
end

Parameters:

  • fields (Array(<String,Symbol,Enumerable>, <Integer,Array,Range>))

    The fields which defines the string or character sets which will make up parts of the String. Symbols corresponds to Chars method names.

Yields:

  • (string)

    The given block will be passed each unique String.

Yield Parameters:

  • string (String)

    A newly generated String.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator will be returned.

Raises:

  • (ArgumentError)

    A given character set name was unknown.

  • (TypeError)

    A given string set was not a String, Symbol or Enumerable. A given string set length was not an Integer or Enumerable.



87
88
89
# File 'lib/ronin/fuzzing/core_ext/string.rb', line 87

def self.generate(*fields,&block)
  Ronin::Fuzzing::Template.new(fields).each(&block)
end

Instance Method Details

#fuzz(substitutions = {}) {|fuzz| ... } ⇒ Enumerator

Incrementally fuzzes the String.

Examples:

Replace every e, i, o, u with (, 100 As and a \0:

"the quick brown fox".fuzz(/[eiou]/ => ['(', ('A' * 100), "\0"]) do |str|
  p str
end

"GET /".fuzz('/' => String.generate(['A', 1..100])) do |str|
  p str
end

Replace a Regexp::UNIX_PATH with Ronin::Fuzzing#format_strings:

"GET /downloads/".fuzz(unix_path: :format_strings)

Parameters:

  • substitutions (Hash{Regexp,String,Symbol => Enumerable,Symbol}) (defaults to: {})

    Patterns and their substitutions. Symbols are resolved to Ronin::Support::Text::Patterns constants.

Yields:

  • (fuzz)

    The given block will be passed every fuzzed String.

Yield Parameters:

  • fuzz (String)

    A fuzzed String.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator will be returned.



170
171
172
# File 'lib/ronin/fuzzing/core_ext/string.rb', line 170

def fuzz(substitutions={},&block)
  Ronin::Fuzzing::Fuzzer.new(substitutions).each(self,&block)
end

#mutate(mutations = {}) {|mutant| ... } ⇒ Enumerator

Permutes over every possible mutation of the String.

Examples:

"hello old dog".mutate('e' => ['3'], 'l' => ['1'], 'o' => ['0']) do |str|
  puts str
end

Parameters:

  • mutations (Hash{Regexp,String,Symbol => Enumerable,Symbol}) (defaults to: {})

    The patterns and substitutions to mutate the String with.

Yields:

  • (mutant)

    The given block will be yielded every possible mutant String.

Yield Parameters:

  • mutant (String)

    A mutated String.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator will be returned.

Raises:

  • (TypeError)

    A mutation pattern was not a Regexp, String or Symbol. A mutation substitution was not a Symbol or Enumerable.



200
201
202
# File 'lib/ronin/fuzzing/core_ext/string.rb', line 200

def mutate(mutations={},&block)
  Ronin::Fuzzing::Mutator.new(mutations).each(self,&block)
end

#repeating(lengths) {|repeated| ... } ⇒ Enumerator

Repeats the String.

Examples:

'A'.repeating(100)
# => "AAAAAAAAAAAAA..."

Generates 100 upto 700 As, increasing by 100 at a time:

'A'.repeating((100..700).step(100)) do |str|
  # ...
end

Generates 128, 1024, 65536 As:

'A'.repeating([128, 1024, 65536]) do |str|
  # ...
end

Parameters:

  • lengths (Enumerable<Integer>, Integer)

    The number of times to repeat the String.

Yields:

  • (repeated)

    The given block will be passed every repeated String.

Yield Parameters:

  • repeated (String)

    A repeated version of the String.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator will be returned.

Raises:

  • (TypeError)

    n must either be Enumerable or an Integer.



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ronin/fuzzing/core_ext/string.rb', line 125

def repeating(lengths,&block)
  case lengths
  when Integer
    # if lengths is an Integer, simply multiply the String and return
    repeated = (self * lengths)

    yield repeated if block_given?
    return repeated
  else
    return Ronin::Fuzzing::Repeater.new(lengths).each(self,&block)
  end
end