Last active
April 16, 2017 03:08
-
-
Save picatz/88b44912187556bf817e5b9a2eeebd81 to your computer and use it in GitHub Desktop.
Violent Ruby: Unix Password Cracker Class ( no comments )
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
module ViolentRuby | |
class UnixPasswordCracker | |
attr_accessor :file | |
attr_accessor :dictionary | |
alias etc file | |
def initialize(args = {}) | |
@file = args[:file] if args[:file] | |
@dictionary = args[:dictionary] if args[:dictionary] | |
end | |
def parse_etc_file(args = {}) | |
lines = File.readlines(args[:file]).collect do |line| | |
line unless line.split(':').first.chars.first.include?('#') | |
end | |
users = lines.collect { |x| x.split(':')[0] }.map(&:strip) | |
passwords = lines.collect { |x| x.split(':')[1] }.map(&:strip) | |
return users if args[:users] | |
return passwords if args[:passwords] | |
users_passwords = Hash[users.zip(passwords)] | |
if block_given? | |
users_passwords.each do |user, password| | |
yield user, password | |
end | |
else | |
users_passwords | |
end | |
end | |
def crack_passwords(args = {}) | |
file = args[:file] || @file | |
dict = args[:dictionary] || @dictionary | |
parse_etc_file(file: file) do |user, password| | |
File.readlines(dict).map(&:strip).each do |word| | |
if cracked?(password, word) | |
yield format_result(user, password, word) | |
else | |
yield format_result(user, password) | |
end | |
end | |
end | |
end | |
alias crack crack_passwords | |
alias crack! crack_passwords | |
alias get_crackn crack_passwords | |
alias release_the_kraken crack_passwords | |
def check_password(encrypted_password, plaintext_password, strip = true) | |
plaintext_password.strip! if strip # sometimes passwords have trailing spaces | |
if plaintext_password.crypt(encrypted_password[0, 2]) == encrypted_password | |
true | |
else | |
false | |
end | |
end | |
alias cracked? check_password | |
private | |
def format_result(user, encrypted_pass, plaintext_pass = false) | |
result = {} | |
if plaintext_pass | |
result[:cracked] = true | |
else | |
result[:cracked] = false | |
end | |
result[:username] = user | |
result[:encrypted_password] = encrypted_pass | |
result[:plaintext_password] = plaintext_pass if plaintext_pass | |
result | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment