Skip to content

Commit

Permalink
Use software_version instead of just version for consistency (closes
Browse files Browse the repository at this point in the history
 #148).
  • Loading branch information
postmodern committed Dec 10, 2024
1 parent d29dc3a commit 72213ec
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 43 deletions.
21 changes: 14 additions & 7 deletions lib/ronin/exploits/mixins/has_targets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,12 @@ def targets
# @option kwargs [Symbol, nil] :software
# The software name of the target.
#
# @option kwargs [String, nil] :version
# @option kwargs [String, nil] :software_version
# The software version of the target.
#
# @option kwargs [String, nil] :version
# Alias for `software_version:`.
#
# @example
# target arch: :x86_64, os: :linux, software: 'Apache' do |t|
# t.foo = 0x123456
Expand Down Expand Up @@ -172,15 +175,15 @@ def perform_validate
# @param [Symbol] software
# The targeted software.
#
# @param [Symbol] version
# @param [Symbol] software_version
# The targeted software version.
#
# @raise [NoMatchingTarget]
# No matching target could be found.
#
# @api semipublic
#
def select_target(arch: nil, os: nil, os_version: nil, software: nil, version: nil)
def select_target(arch: nil, os: nil, os_version: nil, software: nil, software_version: nil)
targets = self.class.targets.lazy

if arch
Expand All @@ -199,9 +202,9 @@ def select_target(arch: nil, os: nil, os_version: nil, software: nil, version: n
targets = targets.select { |target| target.software == software }
end

if version
if software_version
targets = targets.select do |target|
target.version == version
target.software_version == software_version
end
end

Expand Down Expand Up @@ -289,9 +292,13 @@ def software
#
# @api public
#
def version
target.version if target
# @since 1.2.0
#
def software_version
target.software_version if target
end

alias version software_version
end
end
end
Expand Down
26 changes: 17 additions & 9 deletions lib/ronin/exploits/target.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ class Target < OpenStruct
# The target's software version.
#
# @return [String, nil]
attr_reader :version
#
# @since 1.2.0
attr_reader :software_version

alias version software_version

#
# Creates a new ExploitTarget object
Expand All @@ -71,20 +75,24 @@ class Target < OpenStruct
# @param [String, nil] software
# The software name of the target (ex: `"Apache"`).
#
# @param [String, nil] version
# @param [String, nil] software_version
# The software version of the target (ex: `"2.4.54"`).
#
# @param [String, nil] version
# Alias for `software_version:`.
#
# @yield [target]
# If a block is given, it will be passed the new target object.
#
# @yieldparam [Target] target
# The newly created target object.
#
def initialize(arch: nil,
os: nil,
os_version: nil,
software: nil,
version: nil,
def initialize(arch: nil,
os: nil,
os_version: nil,
software: nil,
software_version: nil,
version: nil,
**params)
super(**params)

Expand All @@ -93,8 +101,8 @@ def initialize(arch: nil,
@os = os
@os_version = os_version

@software = software
@version = version
@software = software
@software_version = software_version || version

yield self if block_given?
end
Expand Down
46 changes: 23 additions & 23 deletions spec/mixins/has_targets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ class ExampleExploit < Ronin::Exploits::Exploit
os: :linux,
os_version: '5.18.1',
software: 'Apache',
version: '2.4.53',
software_version: '2.4.53',
foo: 1

target arch: :arm,
os: :macos,
os_version: '10.13',
software: 'nginx',
version: '1.22.0',
software_version: '1.22.0',
foo: 2
end
end
Expand Down Expand Up @@ -289,37 +289,37 @@ class ExampleExploit < Ronin::Exploits::Exploit
end
end

context "when given the version: keyword argument" do
let(:version) { '1.22.0' }
context "when given the software_version: keyword argument" do
let(:software_version) { '1.22.0' }

it "must find the target in .targets with the matching #version" do
subject.select_target(version: version)
it "must find the target in .targets with the matching #software_version" do
subject.select_target(software_version: software_version)

expect(subject.target.version).to eq(version)
expect(subject.target.software_version).to eq(software_version)
end
end

context "when given multiple keyword arguments" do
let(:arch) { :arm }
let(:os) { :macos }
let(:os_version) { '10.13' }
let(:software) { 'nginx' }
let(:version) { '1.22.0' }
let(:arch) { :arm }
let(:os) { :macos }
let(:os_version) { '10.13' }
let(:software) { 'nginx' }
let(:software_version) { '1.22.0' }

it "must find the target in .targets which matches all given values" do
subject.select_target(
arch: arch,
os: os,
os_version: os_version,
software: software,
version: version
software_version: software_version
)

expect(subject.target.arch).to eq(arch)
expect(subject.target.os).to eq(os)
expect(subject.target.os_version).to eq(os_version)
expect(subject.target.software).to eq(software)
expect(subject.target.version).to eq(version)
expect(subject.target.arch).to eq(arch)
expect(subject.target.os).to eq(os)
expect(subject.target.os_version).to eq(os_version)
expect(subject.target.software).to eq(software)
expect(subject.target.software_version).to eq(software_version)
end
end

Expand Down Expand Up @@ -412,22 +412,22 @@ class ExampleExploit < Ronin::Exploits::Exploit
end
end

describe "#version" do
describe "#software_version" do
context "when a target has been set" do
subject do
test_class.new(target: {version: '1.22.0'})
test_class.new(target: {software_version: '1.22.0'})
end

it "must return the #target's #version" do
expect(subject.version).to eq(subject.target.version)
it "must return the #target's #software_version" do
expect(subject.software_version).to eq(subject.target.software_version)
end
end

context "when no target has been set" do
subject { test_class.new }

it "must return nil" do
expect(subject.version).to be(nil)
expect(subject.software_version).to be(nil)
end
end
end
Expand Down
28 changes: 24 additions & 4 deletions spec/target_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
expect(subject.software).to be(nil)
end

it "must default #version to nil" do
expect(subject.version).to be(nil)
it "must default #software_version to nil" do
expect(subject.software_version).to be(nil)
end

context "when given the arch: keyword argument" do
Expand Down Expand Up @@ -67,13 +67,23 @@
end
end

context "when given the software_version: keyword argument" do
let(:software_version) { '1.2.3' }

subject { described_class.new(software_version: software_version) }

it "must set #software_version" do
expect(subject.software_version).to be(software_version)
end
end

context "when given the version: keyword argument" do
let(:version) { '1.2.3' }

subject { described_class.new(version: version) }

it "must set #version" do
expect(subject.version).to be(version)
it "must set #software_version" do
expect(subject.software_version).to be(version)
end
end

Expand All @@ -89,4 +99,14 @@
end
end
end

describe "#version" do
let(:software_version) { '1.2.3' }

subject { described_class.new(software_version: software_version) }

it "must be an alias for #software_version" do
expect(subject.version).to eq(subject.software_version)
end
end
end

0 comments on commit 72213ec

Please sign in to comment.