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

#notesArray<Note>

The associated notes.

Returns:

Since:

  • 0.2.0



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

has_many :notes, dependent: :destroy

#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.



107
108
109
# File 'lib/ronin/db/credential.rb', line 107

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:



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/ronin/db/credential.rb', line 192

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:



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/ronin/db/credential.rb', line 166

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.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/ronin/db/credential.rb', line 126

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.



152
153
154
# File 'lib/ronin/db/credential.rb', line 152

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.



232
233
234
# File 'lib/ronin/db/credential.rb', line 232

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.



244
245
246
# File 'lib/ronin/db/credential.rb', line 244

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

#userString

The user the credential belongs to.

Returns:

  • (String)

    The user name.



220
221
222
# File 'lib/ronin/db/credential.rb', line 220

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