Class: ViolentRuby::UnixPasswordCracker

Inherits:
Object
  • Object
show all
Defined in:
lib/violent_ruby/unix_password_cracker/unix_password_cracker.rb

Overview

Unix Password Cracker provides a friendly interface to crack unix passwords. Because all hackers totes do this.

Create a new Unix Password Cracker

In order for the password cracker to work, we’re going to need a dictionary, and an /etc/passwd file we want to crack.

Examples:

Basic Usage

config = { file: "/etc/passwd", dictionry: "dictionary.txt" }
upc = ViolentRuby::UnixPasswordCracker.new(config)
upc.crack! 

Author:

  • Kent ‘picat’ Gruber

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ UnixPasswordCracker

Create a new Unix Password Cracker.

Parameters:

  • args (Hash) (defaults to: {})

    The options to create a new Unix Password Cracker.

  • args (String) (defaults to: {})

    :file The path to an /etc/passwd file.

  • args (String) (defaults to: {})

    :dictionary The path to a dictionry of passwords.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/violent_ruby/unix_password_cracker/unix_password_cracker.rb', line 25

def initialize(args = {})
  @file       = false
  @dictionary = false
  if args[:file] && File.readable?(args[:file])
    @file        = args[:file]
    @credentials = parse_etc_file(file: args[:file])
  end
  return unless args[:dictionary]
  return unless File.readable?(args[:dictionary])
  @dictionary = args[:dictionary]
end

Instance Attribute Details

#dictionaryObject



18
19
20
# File 'lib/violent_ruby/unix_password_cracker/unix_password_cracker.rb', line 18

def dictionary
  @dictionary
end

#fileObject



16
17
18
# File 'lib/violent_ruby/unix_password_cracker/unix_password_cracker.rb', line 16

def file
  @file
end

Instance Method Details

#check_password(encrypted_password, word) ⇒ Boolean Also known as: cracked?

Check if a given encrypted password matches a given plaintext word when the same crytographic operation is performed on it.

Parameters:

  • encrypted_password (String)

    The encrypted password to check against.

  • word (String)

    The plaintext password to check against.

Returns:

  • (Boolean)


94
95
96
97
98
99
100
# File 'lib/violent_ruby/unix_password_cracker/unix_password_cracker.rb', line 94

def check_password(encrypted_password, word)
  if word.strip.crypt(encrypted_password[0, 2]) == encrypted_password
    true
  else
    false
  end
end

#crack_passwords(args = {}) ⇒ Array<Hash> Also known as: crack!, get_crackn, release_the_kraken

Crack unix passwords.

Parameters:

  • args (Hash) (defaults to: {})

    The options when crack’n some passwords.

  • args (String) (defaults to: {})

    :file The path to an /etc/passwd file.

  • args (String) (defaults to: {})

    :dictionary The path to a dictionry of passwords.

Returns:

  • (Array<Hash>)


70
71
72
73
74
75
76
77
78
79
80
# File 'lib/violent_ruby/unix_password_cracker/unix_password_cracker.rb', line 70

def crack_passwords(args = {})
  file = args[:file]       || @file
  dict = args[:dictionary] || @dictionary
  results = []
  parse_etc_file(file: file) do |user, password|
    File.readlines(dict).map(&:strip).each do |word|
      results << format_result(user, password, word) if cracked?(password, word)
    end
  end
  results
end

#parse_etc_file(args = {}) ⇒ Hash

Parse a unix /etc/passwd file into a more mangeable form.

Parameters:

  • args (Hash) (defaults to: {})

    The options when parsing the file.

  • args (String) (defaults to: {})

    :file The path to an /etc/passwd file.

  • args (Boolean) (defaults to: {})

    :users Specify that only users should be returned ( default: false ).

  • args (Boolean) (defaults to: {})

    :passwords Specify that only passwords should be returned ( default: false ).

Returns:

  • (Hash)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/violent_ruby/unix_password_cracker/unix_password_cracker.rb', line 44

def parse_etc_file(args = {})
  raise 'No /etc/passwd file given.' unless args[:file]
  raise "File #{args[:file]} not readable!" unless File.readable?(args[:file])
  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)
  return users if args[:users]
  passwords = lines.collect { |x| x.split(':')[1] }.map(&:strip)
  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