Class: Ronin::Wordlists::WordlistDir

Inherits:
Object
  • Object
show all
Defined in:
lib/ronin/wordlists/wordlist_dir.rb

Overview

Represents a directory of wordlists.

Example

wordlist_dir = Wordlists::WordlistDir.new('/path/to/wordlists')
wordlist_dir.find('passwords.txt')
# => "/path/to/wordlists/passwords.txt"
wordlist_dir.find('passwords')
# => "/path/to/wordlists/passwords.txt"
wordlist_dir.open('passwords.txt')
# => #<Wordlist::File:...>
wordlist_dir.open('passwords')
# => #<Wordlist::File:...>

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ WordlistDir

Initializes the wordlist directory.

Parameters:

  • path (String)

    The path to the wordlist directory.



59
60
61
# File 'lib/ronin/wordlists/wordlist_dir.rb', line 59

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathString (readonly)

The path to the wordlist directory.

Returns:

  • (String)


51
52
53
# File 'lib/ronin/wordlists/wordlist_dir.rb', line 51

def path
  @path
end

Instance Method Details

#delete(name) ⇒ Object

Deletes a wordlist file from the wordlist directory.

Parameters:

  • name (String)

    The wordlist name to delete.

Raises:

  • (ArgumentError)

    No wordlist with the given name.



182
183
184
185
186
187
188
189
190
# File 'lib/ronin/wordlists/wordlist_dir.rb', line 182

def delete(name)
  if (path = find(name))
    File.unlink(path)

    return path
  else
    raise(WordlistNotFound,"unknown wordlist: #{name.inspect}")
  end
end

#download(url) ⇒ WordlistFile, WordlistRepo

Downloads a wordlist from the given URL into the wordlist directory.

Parameters:

  • url (String, URI::HTTP)

    The URL of the wordlist to downloaded.

Returns:

Raises:

  • (DownloadFailed)

    The download of the wordlist file or repository failed.



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/ronin/wordlists/wordlist_dir.rb', line 160

def download(url)
  uri = URI(url)

  wordlist_class = if uri.scheme == 'git' || uri.path.end_with?('.git')
                     WordlistRepo
                   else
                     WordlistFile
                   end

  FileUtils.mkdir_p(@path)
  return wordlist_class.download(url,@path)
end

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

Enumerates over every wordlist in the wordlist directory.

Yields:

  • (path)

    The given block will be passed each path to each wordlist.

Yield Parameters:

  • path (String)

    A path to a wordlist within the wordlist directory.

Returns:

  • (Enumerator)

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



75
76
77
78
79
# File 'lib/ronin/wordlists/wordlist_dir.rb', line 75

def each(&block)
  return enum_for unless block

  Dir.glob(File.join(@path,'**','*.{txt,gz,bz2,xz}'),&block)
end

#find(name) ⇒ String?

Looks up a wordlist within the wordlist directory.

Examples:

wordlist_dir.find('passwords.txt')
# => "/path/to/wordlists/passwords.txt"
wordlist_dir.find('passwords')
# => "/path/to/wordlists/passwords.txt"

Parameters:

  • name (String)

    The wordlist file name.

Returns:

  • (String, nil)

    The path to the wordlist or nil if the wordlist could not be found.



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ronin/wordlists/wordlist_dir.rb', line 96

def find(name)
  path = File.join(@path,name)

  # check for an exact filename match first
  if File.file?(path)
    path
  else
    # fallback to search for the wordlist file by name
    Dir.glob(File.join(@path,'**',"#{name}.{txt,gz,bz2,xz}")).first
  end
end

#list(name = '*') ⇒ Array<String>

Lists the wordlists in the wordlist directory.

Parameters:

  • name (String) (defaults to: '*')

    Optional file name to search for.

Returns:

  • (Array<String>)

    The wordlist files within the wordlist directory.



117
118
119
# File 'lib/ronin/wordlists/wordlist_dir.rb', line 117

def list(name='*')
  Dir.glob("{**/}#{name}.{txt,gz,bz2,xz}", base: @path)
end

#open(name) ⇒ Wordlist::File

Opens a wordlist from the wordlist directory.

Examples:

wordlist_dir.open('passwords.txt')
# => #<Wordlist::File:...>
wordlist_dir.open('passwords')
# => #<Wordlist::File:...>

Parameters:

  • name (String)

    The wordlist file name.

Returns:

  • (Wordlist::File)

    The opened wordlist file.

Raises:



139
140
141
142
143
144
145
# File 'lib/ronin/wordlists/wordlist_dir.rb', line 139

def open(name)
  if (path = find(name))
    Wordlist.open(path)
  else
    raise(WordlistNotFound,"wordlist not found: #{name.inspect}")
  end
end