Skip to content

Instantly share code, notes, and snippets.

@booth-f
Created December 1, 2020 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save booth-f/1d1a6b9eefc1464d6236b39dd1df7626 to your computer and use it in GitHub Desktop.
Save booth-f/1d1a6b9eefc1464d6236b39dd1df7626 to your computer and use it in GitHub Desktop.
EFF-Based Password Generator
#!/usr/bin/ruby
# Generate (a) password(s) based on the EFF Wordlists
# Command Arguments
# ./genPass.rb (Wordlist) [Number of passwords] [Output method]
require 'csv'
# Wordlist
if ARGV[0].nil?
# Return error
puts "ERR: No wordlist defined... exiting"
exit
else
wordlist = ARGV[0].to_s
if(File.exists?(wordlist))
# Wordlist was found, continue on
else
# Wordlist file doesn't exist, error
puts "Specified wordlist does not exist, please check the file path and try again"
exit
end
end
# Number of Passwords
if ARGV[1].nil?
# Default generate 1 password
numberOfPasswords = 1
else
# Convert value to an integer
ARGV[1] = ARGV[1].chomp.to_i
# Check if the converted value is a positive integer
if ARGV[1] > 0
# Set the number of passwords to generate
numberOfPasswords = ARGV[1].to_i
else
puts "Requested number of passwords cannot be zero, please choose a positive integer value and try again."
exit
end
end
# Output Method
if ARGV[2].nil?
# Export passwords to the screen by default
exportType = "screen"
else
# Convert whatever the user specified to full lowercase, otherwise the following check will fail in annoying ways
ARGV[2] = ARGV[2].downcase
# Check if we got a valid output method
if ARGV[2].include?("screen") || ARGV[2].include?("csv")
exportType = ARGV[2].to_s
else
puts "Please specify a valid output method. The available methods are CSV and Screen"
exit
end
end
# Populate our numbers and symbols in memory and initialize an empty words array
words = Array.new
numbers = [0,1,2,3,4,5,6,7,8,9]
symbols = ["!", "@", "#", "$", "%", "^", "&", "*", "+", "="]
# Take the wordlist as input
File.open(wordlist).each do |line|
word = line.split(" ")
# If the current word is less than X number of characters, append it to the list of potential words
if word[1].size < 7
words.append(word[1])
else
# Do nothing, the word is too long for us
end
end
def generatePassword(words, numbers, symbols)
# Select 2 random words from the array
selectedWords = words.sample(2) # Modify if you wish to adjust the number of words randomly selected from the list
# Select 2 random numbers
selectedNumber = numbers.sample(2) # Modify if you wish to adjust the number of numbers included in the password
# Select a random symbol
selectedSymbol = symbols.sample(1) # Modify if you wish to adjust the number of symbols included in the password
# Create blank password variable
password = ""
# Combine the words and append them to the password variable
selectedWords.each do |w|
# If we have the first word in the list, make the first character capitalized
if w == selectedWords[0]
w = w.capitalize
end
# Append the word to our password
password << w.strip.to_s
end
# Append a random number to the password
password << selectedNumber.join
# Append a random symbol to the password
password << selectedSymbol.join
# Return our generated password
return password
end
# Create a blank password array to store all our generated passwords
passwords = []
i = 0
# Keep generating passwords until we hit the requested amount
while i < numberOfPasswords
passwords << generatePassword(words, numbers, symbols)
i += 1
end
if (exportType == "screen")
# Output to the screen
for password in passwords
puts password
end
elsif (exportType == "csv")
# Output to csv file
CSV.open("Generated_Passwords.csv", "w") do |csv|
csv << ["Passwords"]
passwords.each do |password|
csv << Array(password)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment