Skip to content

Commit b42b5ec

Browse files
committed
Added Exploit.vulnerable_version? and #vulnerable_version? (closes #151).
1 parent 6dba2a0 commit b42b5ec

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

lib/ronin/exploits/exploit.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,27 @@ def self.software_versions(new_software_versions=nil)
437437
end
438438
end
439439

440+
#
441+
# Determines if the given software version is vulnerable by comparing it
442+
# against {software_versions}.
443+
#
444+
# @param [String] version
445+
# The software version number to compare.
446+
#
447+
# @return [Boolean]
448+
#
449+
# @api semipublic
450+
#
451+
# @since 1.2.0
452+
#
453+
def self.vulnerable_version?(version)
454+
software_version = Support::Software::Version.parse(version)
455+
456+
software_versions.any? do |version_range|
457+
version_range.include?(software_version)
458+
end
459+
end
460+
440461
#
441462
# Returns the type or kind of exploit.
442463
#
@@ -600,6 +621,22 @@ def exploit(dry_run: false)
600621
def validate
601622
end
602623

624+
#
625+
# Determines if the given software version is vulnerable.
626+
#
627+
# @param [String] version
628+
# The software version number to compare.
629+
#
630+
# @return [Boolean]
631+
#
632+
# @api public
633+
#
634+
# @since 1.2.0
635+
#
636+
def vulnerable_version?(version)
637+
self.class.vulnerable_version?(version)
638+
end
639+
603640
#
604641
# Returns a vulnerable test result for the {#test} method.
605642
#

spec/exploit_spec.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,12 +440,66 @@ class OverridesItsInheritedSoftwareVersions < WithSoftwareVersionsSet
440440
end
441441
end
442442

443+
describe ".vulnerable_version?" do
444+
module TestVulnerableVersion
445+
class TestExploit < Ronin::Exploits::Exploit
446+
software_versions [
447+
'>= 1.2.3, < 2.0.0',
448+
'>= 2.3.4, < 2.5.1'
449+
]
450+
end
451+
end
452+
453+
let(:test_class) { TestVulnerableVersion::TestExploit }
454+
455+
subject { test_class }
456+
457+
context "when the given version is within one of the .software_versions ranges" do
458+
it "must return true" do
459+
expect(subject.vulnerable_version?('1.4.2')).to be(true)
460+
end
461+
end
462+
463+
context "when the given version is not within any of the .software_versions ranges" do
464+
it "must return false" do
465+
expect(subject.vulnerable_version?('3.0.0')).to be(false)
466+
end
467+
end
468+
end
469+
443470
describe ".exploit_type" do
444471
subject { described_class }
445472

446473
it { expect(subject.exploit_type).to eq(:exploit) }
447474
end
448475

476+
describe "#vulnerable_version?" do
477+
module TestVulnerableVersion
478+
class TestExploit < Ronin::Exploits::Exploit
479+
software_versions [
480+
'>= 1.2.3, < 2.0.0',
481+
'>= 2.3.4, < 2.5.1'
482+
]
483+
end
484+
end
485+
486+
let(:test_class) { TestVulnerableVersion::TestExploit }
487+
488+
subject { test_class.new }
489+
490+
context "when the given version is within one of the .software_versions ranges" do
491+
it "must return true" do
492+
expect(subject.vulnerable_version?('1.4.2')).to be(true)
493+
end
494+
end
495+
496+
context "when the given version is not within any of the .software_versions ranges" do
497+
it "must return false" do
498+
expect(subject.vulnerable_version?('3.0.0')).to be(false)
499+
end
500+
end
501+
end
502+
449503
describe "#perform_validate" do
450504
it "must call #validate_params" do
451505
expect(subject).to receive(:validate_params)

0 commit comments

Comments
 (0)