Class: Ronin::DB::Credential

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Model, Model::Importable
Defined in:
lib/ronin/db/credential.rb

Overview

Represents Credentials used to access services or websites.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Model::Importable

included

Methods included from Model

included

Instance Attribute Details

#email_addressEmailAddress?

The optional email address associated with the Credential

Returns:



54
# File 'lib/ronin/db/credential.rb', line 54

belongs_to :email_address, optional: true

#idInteger

Primary key of the credential.

Returns:

  • (Integer)


40
# File 'lib/ronin/db/credential.rb', line 40

attribute :id, :integer

#open_portsArray<OpenPort>

The open ports that accept this credential pair.

Returns:



74
# File 'lib/ronin/db/credential.rb', line 74

has_many :open_ports, through: :service_credentials

#passwordPassword

Password of the credential.

Returns:



62
# File 'lib/ronin/db/credential.rb', line 62

belongs_to :password, required: true

#service_credentialsArray<ServiceCredential>

The service credentials.

Returns:



68
# File 'lib/ronin/db/credential.rb', line 68

has_many :service_credentials, dependent: :destroy

#urlsArray<URL>

The URLs that accept this credential pair.

Returns:



86
# File 'lib/ronin/db/credential.rb', line 86

has_many :urls, through: :web_credentials

#user_nameUserName?

User name of the credential.

Returns:



46
# File 'lib/ronin/db/credential.rb', line 46

belongs_to :user_name, optional: true

#web_credentialsArray<WebCredential>

The Web credentials.

Returns:



80
# File 'lib/ronin/db/credential.rb', line 80

has_many :web_credentials, dependent: :destroy

Class Method Details

.for_user(name) ⇒ Array<Credential>

Searches for all credentials for a specific user.

Parameters:

  • name (String)

    The name of the user.

Returns:

  • (Array<Credential>)

    The credentials for the user.



99
100
101
# File 'lib/ronin/db/credential.rb', line 99

def self.for_user(name)
  joins(:user_name).where(user_name: {name: name})
end

.import(cred) ⇒ Credential

Imports the given credential.

Parameters:

  • cred (String)

    The credential String (ex: user:password or user@example.com:password).

Returns:



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/ronin/db/credential.rb', line 184

def self.import(cred)
  unless cred.include?(':')
    raise(ArgumentError,"credential must be of the form user:password or email:password: #{cred.inspect}")
  end

  user_or_email, password = cred.split(':',2)

  if user_or_email.include?('@')
    create(
      email_address: EmailAddress.find_or_import(user_or_email),
      password:      Password.find_or_import(password)
    )
  else
    create(
      user_name: UserName.find_or_import(user_or_email),
      password:  Password.find_or_import(password)
    )
  end
end

.lookup(cred) ⇒ Credential?

Looks up the given credential.

Parameters:

  • cred (String)

    The credential String (ex: user:password or user@example.com:password).

Returns:



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/ronin/db/credential.rb', line 158

def self.lookup(cred)
  unless cred.include?(':')
    raise(ArgumentError,"credential must be of the form user:password or email:password: #{cred.inspect}")
  end

  user_or_email, password = cred.split(':',2)

  query = if user_or_email.include?('@')
            with_email_address(user_or_email)
          else
            for_user(user_or_email)
          end
  query.with_password(password)
  return query.first
end

.with_email_address(email) ⇒ Array<WebCredential>

Searches all web credentials that are associated with an email address.

Parameters:

  • email (String)

    The email address to search for.

Returns:

  • (Array<WebCredential>)

    The web credentials associated with the email address.

Raises:

  • (ArgumentError)

    The given email address was not a valid email address.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/ronin/db/credential.rb', line 118

def self.with_email_address(email)
  unless email.include?('@')
    raise(ArgumentError,"invalid email address #{email.inspect}")
  end

  user, domain = email.split('@',2)

  return joins(email_address: [:user_name, :host_name]).where(
    email_address: {
      ronin_user_names: {name: user},
      ronin_host_names: {name: domain}
    }
  )
end

.with_password(password) ⇒ Array<Credential>

Searches for all credentials with a common password.

Parameters:

  • password (String)

    The password to search for.

Returns:

  • (Array<Credential>)

    The credentials with the common password.



144
145
146
# File 'lib/ronin/db/credential.rb', line 144

def self.with_password(password)
  joins(:password).where(password: {plain_text: password})
end

Instance Method Details

#plain_textString

The clear-text password of the credential.

Returns:

  • (String)

    The clear-text password.



224
225
226
# File 'lib/ronin/db/credential.rb', line 224

def plain_text
  self.password.plain_text if self.password
end

#to_sString

Converts the credentials to a String.

Returns:

  • (String)

    The user name and the password.



236
237
238
# File 'lib/ronin/db/credential.rb', line 236

def to_s
  "#{self.user_name}:#{self.password}"
end

#userString

The user the credential belongs to.

Returns:

  • (String)

    The user name.



212
213
214
# File 'lib/ronin/db/credential.rb', line 212

def user
  self.user_name.name if self.user_name
end