Skip to content

Commit

Permalink
Make ServiceCredential and WebCredential join models (closes #16,#17
Browse files Browse the repository at this point in the history
).
  • Loading branch information
postmodern committed Oct 11, 2022
1 parent 0b230d2 commit 962d17e
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 47 deletions.
19 changes: 5 additions & 14 deletions db/migrate/0024_create_ronin_credentials_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,16 @@ def change
foreign_key: {
to_table: :ronin_user_names
}
t.references :password, null: false,
foreign_key: {
to_table: :ronin_passwords
}

t.references :open_port, null: true,
foreign_key: {
to_table: :ronin_open_ports
}
t.references :email_address, null: true,
foreign_key: {
to_table: :ronin_email_addresses
}
t.references :url, null: true,
foreign_key: {
to_table: :ronin_urls
}
t.references :password, null: false,
foreign_key: {
to_table: :ronin_passwords
}

t.index [:user_name_id, :password_id, :open_port_id, :email_address_id, :url_id], unique: true,
t.index [:user_name_id, :password_id, :email_address_id], unique: true,
name: :index_ronin_credentials_table_unique
end
end
Expand Down
41 changes: 41 additions & 0 deletions db/migrate/0034_create_ronin_service_credentials_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#
# ronin-db-activerecord - ActiveRecord backend for the Ronin Database.
#
# Copyright (c) 2022 Hal Brodigan (postmodern.mod3 at gmail.com)
#
# This file is part of ronin-db-activerecord.
#
# ronin-db-activerecord is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ronin-db-activerecord is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with ronin-db-activerecord. If not, see <https://www.gnu.org/licenses/>.
#

class CreateRoninServiceCredentialsTable < ActiveRecord::Migration[7.0]

def change
create_table :ronin_service_credentials, if_not_exists: true do |t|
t.references :credential, null: false,
foreign_key: {
to_table: :ronin_credentials
}

t.references :open_port, null: false,
foreign_key: {
to_table: :ronin_open_ports
}

t.index [:credential_id, :open_port_id], unique: true,
name: :index_ronin_service_credentials_table_unique
end
end

end
41 changes: 41 additions & 0 deletions db/migrate/0035_create_ronin_web_credentials_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#
# ronin-db-activerecord - ActiveRecord backend for the Ronin Database.
#
# Copyright (c) 2022 Hal Brodigan (postmodern.mod3 at gmail.com)
#
# This file is part of ronin-db-activerecord.
#
# ronin-db-activerecord is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ronin-db-activerecord is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with ronin-db-activerecord. If not, see <https://www.gnu.org/licenses/>.
#

class CreateRoninWebCredentialsTable < ActiveRecord::Migration[7.0]

def change
create_table :ronin_web_credentials, if_not_exists: true do |t|
t.references :credential, null: false,
foreign_key: {
to_table: :ronin_credentials
}

t.references :url, null: false,
foreign_key: {
to_table: :ronin_urls
}

t.index [:credential_id, :url_id], unique: true,
name: :index_ronin_web_credentials_table_unique
end
end

end
26 changes: 26 additions & 0 deletions lib/ronin/db/credential.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#

require 'ronin/db/model'
require 'ronin/db/model/importable'

require 'active_record'

Expand All @@ -29,6 +30,7 @@ module DB
class Credential < ActiveRecord::Base

include Model
include Model::Importable

# @!attribute [rw] id
# Primary key of the credential.
Expand Down Expand Up @@ -58,6 +60,30 @@ class Credential < ActiveRecord::Base
# @return [Password]
belongs_to :password, required: true

# @!attribute [rw] service_credentials
# The service credentials.
#
# @return [Array<ServiceCredential>]
has_many :service_credentials, dependent: :destroy

# @!attribute [rw] open_ports
# The open ports that accept this credential pair.
#
# @return [Array<OpenPort>]
has_many :open_ports, through: :service_credentials

# @!attribute [rw] web_credentials
# The Web credentials.
#
# @return [Array<WebCredential>]
has_many :web_credentials, dependent: :destroy

# @!attribute [rw] urls
# The URLs that accept this credential pair.
#
# @return [Array<URL>]
has_many :urls, through: :web_credentials

#
# Searches for all credentials for a specific user.
#
Expand Down
19 changes: 12 additions & 7 deletions lib/ronin/db/open_port.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@ class OpenPort < ActiveRecord::Base
# @return [Software]
belongs_to :software, optional: true

# @!attribute [rw] service_credentials
# Credentials used by the service running on the port
#
# @return [Array<ServiceCredential>]
has_many :service_credentials, dependent: :destroy

# @!attribute [rw] ssl
# Specifies whether the service requires SSL.
#
Expand All @@ -81,6 +75,18 @@ class OpenPort < ActiveRecord::Base
# @return [Time]
attribute :created_at, :time

# @!attribute [rw] service_credentials
# Credentials used by the service running on the port
#
# @return [Array<ServiceCredential>]
has_many :service_credentials, dependent: :destroy

# @!attribute [rw] credentials
# The credentials that will work with this open port.
#
# @return [Array<Credential>]
has_many :credentials, through: :service_credentials

#
# The IP Address of the open port.
#
Expand Down Expand Up @@ -139,4 +145,3 @@ def to_s
require 'ronin/db/port'
require 'ronin/db/service'
require 'ronin/db/service_credential'
require 'ronin/db/port'
30 changes: 23 additions & 7 deletions lib/ronin/db/service_credential.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,35 @@
# along with ronin-db-activerecord. If not, see <https://www.gnu.org/licenses/>.
#

require 'ronin/db/credential'
require 'ronin/db/model'

require 'active_record'

module Ronin
module DB
#
# Represents Credentials used to access a TCP/UDP {Service}.
#
class ServiceCredential < Credential
class ServiceCredential < ActiveRecord::Base

include Model

# @!attribute [rw] id
# Primary key of the service credential.
#
# @return [Integer]
attribute :id, :integer

# @!attribute [rw] credential
#
# @return [Credential]
belongs_to :credential

# @!attribute [rw] open_port
# The open port the credential belongs to.
#
# @return [OpenPort, nil]
belongs_to :open_port, optional: true
# @return [OpenPort]
belongs_to :open_port

#
# Converts the service credential to a String.
Expand All @@ -39,11 +54,12 @@ class ServiceCredential < Credential
# The service credential string.
#
def to_s
if self.open_port then "#{super} (#{self.open_port})"
else super
end
"#{self.credential} (#{self.open_port})"
end

end
end
end

require 'ronin/db/credential'
require 'ronin/db/open_port'
14 changes: 10 additions & 4 deletions lib/ronin/db/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ class URL < ActiveRecord::Base
# @return [String, nil]
attribute :fragment, :string

# @!attribute [r] created_at
# Defines the created_at timestamp
#
# @return [Time]
attribute :created_at, :time

# @!attribute [rw] query_params
# The query params of the URL.
#
Expand All @@ -103,11 +109,11 @@ class URL < ActiveRecord::Base
# @return [Array<WebCredential>]
has_many :web_credentials, dependent: :destroy

# @!attribute [r] created_at
# Defines the created_at timestamp
# @!attribute [rw] credentials
# The credentials that will work with this URL.
#
# @return [Time]
attribute :created_at, :time
# @return [Array<Credentials>]
has_many :credentials, through: :web_credentials

#
# Searches for all URLs using HTTP.
Expand Down
24 changes: 19 additions & 5 deletions lib/ronin/db/web_credential.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,29 @@
# along with ronin-db-activerecord. If not, see <https://www.gnu.org/licenses/>.
#

require 'ronin/db/credential'
require 'ronin/db/model'

require 'active_record'

module Ronin
module DB
#
# Represents Credentials used to access websites at specified {URL}s.
#
class WebCredential < Credential
class WebCredential < ActiveRecord::Base

include Model

# @!attribute [rw] id
# Primary key of the service credential.
#
# @return [Integer]
attribute :id, :integer

# @!attribute [rw] credential
#
# @return [Credential]
belongs_to :credential

# @!attribute [rw] url
# The URL the credential can be used with.
Expand All @@ -42,13 +57,12 @@ class WebCredential < Credential
# @api public
#
def to_s
if self.url then "#{super} (#{self.url})"
else super
end
"#{self.credential} (#{self.url})"
end

end
end
end

require 'ronin/db/credential'
require 'ronin/db/url'
16 changes: 11 additions & 5 deletions spec/service_credential_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@
require 'ronin/db/service_credential'

describe Ronin::DB::ServiceCredential do
it "must use the 'ronin_credentials' table" do
expect(described_class.table_name).to eq('ronin_credentials')
it "must use the 'ronin_service_credentials' table" do
expect(described_class.table_name).to eq('ronin_service_credentials')
end

let(:user) { 'admin' }
let(:user_name) { Ronin::DB::UserName.new(name: user) }
let(:password) { Ronin::DB::Password.new(plain_text: 'secret') }
let(:credential) do
Ronin::DB::Credential.new(
user_name: user_name,
password: password
)
end

let(:port_number) { 22 }
let(:ip_address) { Ronin::DB::IPAddress.new(address: '1.2.3.4') }
let(:port) { Ronin::DB::Port.new(number: port_number) }
Expand All @@ -21,9 +28,8 @@

subject do
described_class.new(
user_name: user_name,
password: password,
open_port: open_port
credential: credential,
open_port: open_port
)
end

Expand Down
15 changes: 10 additions & 5 deletions spec/web_credential_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
require 'ronin/db/web_credential'

describe Ronin::DB::WebCredential do
it "must use the 'ronin_credentials' table" do
expect(described_class.table_name).to eq('ronin_credentials')
it "must use the 'ronin_web_credentials' table" do
expect(described_class.table_name).to eq('ronin_web_credentials')
end

let(:scheme) { 'https' }
Expand Down Expand Up @@ -48,12 +48,17 @@

let(:user_name) { Ronin::DB::UserName.new(name: 'admin') }
let(:password) { Ronin::DB::Password.new(plain_text: 'secret') }
let(:credential) do
Ronin::DB::Credential.new(
user_name: user_name,
password: password
)
end

subject do
described_class.new(
user_name: user_name,
password: password,
url: url
credential: credential,
url: url
)
end

Expand Down

0 comments on commit 962d17e

Please sign in to comment.