Class: Ronin::Wordlist

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/ronin/wordlist.rb

Overview

An Enumerable class for iterating over wordlist files or lists of words.

Since:

  • 0.4.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#map_hash

Constructor Details

#initialize(wordlist, mutations = {}) {|wordlist| ... } ⇒ Wordlist

Initializes the wordlist.

Examples:

Use a file wordlist

wordlist = Wordlist.new('passwords.txt')

Use a range of Strings

wordlist = Wordlist.new('aaaa'..'zzzz')

Specify mutation rules

wordlist = Wordlist.new('passwords.txt', /e/ => ['E', '3'])

Yields:

  • (wordlist)

    The given block will be passed the new wordlist.

Yield Parameters:

  • wordlist (Wordlist)

    The new wordlist object.

Raises:

  • (TypeError)

    The list was not a path to a wordlist file, nor a list of words.

See Also:

Since:

  • 0.4.0



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ronin/wordlist.rb', line 80

def initialize(wordlist,mutations={})
  case wordlist
  when String
    @path  = wordlist
    @words = nil
  when Enumerable
    @path  = nil
    @words = wordlist
  else
    raise(TypeError,"wordlist must be a path or Enumerable")
  end

  @mutations = mutations

  yield self if block_given?
end

Instance Attribute Details

#mutationsObject (readonly)

Mutation rules to apply to every word in the list

Since:

  • 0.4.0



47
48
49
# File 'lib/ronin/wordlist.rb', line 47

def mutations
  @mutations
end

#pathObject (readonly)

The path to the wordlist file

Since:

  • 0.5.0



39
40
41
# File 'lib/ronin/wordlist.rb', line 39

def path
  @path
end

#wordsObject (readonly)

The words for the list

Since:

  • 0.5.0



44
45
46
# File 'lib/ronin/wordlist.rb', line 44

def words
  @words
end

Class Method Details

.build(text, mutations = {}) ⇒ Wordlist

Builds a new wordlist from the text.

Since:

  • 0.4.0



141
142
143
# File 'lib/ronin/wordlist.rb', line 141

def self.build(text,mutations={})
  new(parse(text),mutations)
end

.create(path, text, mutations = {}) ⇒ Wordlist

Creates a new wordlist file.

Since:

  • 0.5.0



162
163
164
165
166
# File 'lib/ronin/wordlist.rb', line 162

def self.create(path,text,mutations={})
  wordlist = build(text,mutations)

  return wordlist.save(path)
end

.parse(text) {|word| ... } ⇒ SortedSet

Parses the text into a unique Set of words.

Yields:

  • (word)

    If a block is given, it will be passed every unique word, the first time it is seen.

Yield Parameters:

  • word (String)

    A unique word from the text.

Since:

  • 0.4.0



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ronin/wordlist.rb', line 113

def self.parse(text)
  words_seen = SortedSet[]

  text.each_line do |line|
    line.scan(Regexp::WORD) do |word|
      if block_given?
        yield word unless words_seen.include?(word)
      end

      words_seen << word
    end
  end

  return words_seen
end

Instance Method Details

#each {|word| ... } ⇒ Enumerator

Iterates over each word, and each mutation, from the list.

Yields:

  • (word)

    The given block will be passed each word.

Yield Parameters:

  • word (String)

    A word from the list.

Since:

  • 0.4.0



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/ronin/wordlist.rb', line 225

def each(&block)
  return enum_for(__method__) unless block

  mutator = unless @mutations.empty?
              Fuzzing::Mutator.new(@mutations)
            end

  each_word do |word|
    yield word

    if mutator
      # perform additional mutations
      mutator.each(word,&block)
    end
  end
end

#each_n_words(n) {|words| ... } ⇒ Enumerator

Iterates over every n words.

Yields:

  • (words)

    The given block will be passed every combination of n words.

Yield Parameters:

  • The (String)

    combination of n words.

Since:

  • 0.4.0



259
260
261
# File 'lib/ronin/wordlist.rb', line 259

def each_n_words(n,&block)
  Fuzzing::Template[[each, n]].each(&block)
end

#each_word {|word| ... } ⇒ Enumerator

Iterates over each word in the list.

Yields:

  • (word)

    The given block will be passed each word.

Yield Parameters:

  • word (String)

    A word from the list.

Raises:

Since:

  • 0.4.0



197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/ronin/wordlist.rb', line 197

def each_word(&block)
  return enum_for(__method__) unless block

  if @path
    File.open(@path) do |file|
      file.each_line do |line|
        yield line.chomp
      end
    end
  elsif @words
    @words.each(&block)
  end
end

#listString, Enumerable

The wordlist file or list of words.

Since:

  • 0.4.0



176
177
178
# File 'lib/ronin/wordlist.rb', line 176

def list
  @path || @words
end

#save(path) ⇒ Wordlist

Saves the words to a new file.

See Also:

Since:

  • 0.5.0



278
279
280
281
282
283
284
# File 'lib/ronin/wordlist.rb', line 278

def save(path)
  File.open(path,'w') do |file|
    each { |word| file.puts word }
  end

  return self
end