Created
April 16, 2017 02:16
-
-
Save picatz/ff1d5b8f1ddc82df5e4a6cee8f48b833 to your computer and use it in GitHub Desktop.
Violent Ruby: Unix Password Cracker Parse Etc File Method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Parse a unix /etc/passwd file into a more mangeable form. | |
# | |
# @example Basic Usage | |
# upc = ViolentRuby::UnixPasswordCracker.new | |
# upc.parse_etc_file(file: 'passwords.txt') | |
# # {"victim" => "HX9LLTdc/jiDE", "root" => "DFNFxgW7C05fo"} | |
# | |
# @example Super Advanced Usage | |
# ViolentRuby::UnixPasswordCracker.new.parse_etc_file(file: 'passwords.txt') do |user, pass| | |
# puts user + ' ' + pass | |
# end | |
# # victim HX9LLTdc/jiDE | |
# # root DFNFxgW7C05fo | |
# | |
# @param args [Hash] The options when parsing the file. | |
# @option args [String] :file The path to an /etc/passwd file. | |
# @option args [Boolean] :users Specify that only users should be returned ( default: +false+ ). | |
# @option args [Boolean] :passwords Specify that only passwords should be returned ( default: +false+ ). | |
# | |
# @return [Hash] | |
def parse_etc_file(args = {}) | |
# Readlines from /etc/passwd file. | |
lines = File.readlines(args[:file]).collect do |line| | |
line unless line.split(':').first.chars.first.include?('#') | |
end | |
# Collect the users and passwords from the lines. | |
users = lines.collect { |x| x.split(':')[0] }.map(&:strip) | |
passwords = lines.collect { |x| x.split(':')[1] }.map(&:strip) | |
# Friendly behavior to return just users or passwords. | |
return users if args[:users] | |
return passwords if args[:passwords] | |
# Zip'm together into a hash. | |
users_passwords = Hash[users.zip(passwords)] | |
# Yield each pair when a block is given, or return all at once. | |
if block_given? | |
users_passwords.each do |user, password| | |
yield user, password | |
end | |
else | |
users_passwords | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment