Skip to content

Commit

Permalink
Added Nmap::XML::PortUsed for Nmap::XML::OS#ports_used.
Browse files Browse the repository at this point in the history
  • Loading branch information
postmodern committed Jan 25, 2024
1 parent dbaff0c commit dc85304
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
11 changes: 8 additions & 3 deletions lib/nmap/xml/os.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_relative 'os_class'
require_relative 'os_match'
require_relative 'port_used'

module Nmap
class XML
Expand Down Expand Up @@ -93,12 +94,16 @@ def matches
#
# Parses the ports used for guessing the OS.
#
# @return [Array<Integer>]
# @return [Array<PortUsed>]
# The ports used.
#
def ports_used
@ports_used ||= @node.xpath("portused/@portid").map do |port|
port.inner_text.to_i
@ports_used ||= @node.xpath("portused").map do |portused|
PortUsed.new(
portused['state'].to_sym,
portused['proto'].to_sym,
portused['portid'].to_i
)
end
end

Expand Down
15 changes: 15 additions & 0 deletions lib/nmap/xml/port_used.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Nmap
class XML
#
# Represents a port used to perform OS fingerprinting.
#
# @since 1.1.0
#
class PortUsed < Struct.new(:state,:protocol,:port)

alias to_i port
alias to_int port

end
end
end
5 changes: 4 additions & 1 deletion spec/xml/os_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
subject { super().ports_used }

it { expect(subject).not_to be_empty }
it { expect(subject).to all(be_between(0,65535)) }
it { expect(subject).to all(be_kind_of(Nmap::XML::PortUsed)) }
it { expect(subject.map(&:state)).to all(eq(:open).or(eq(:closed))) }
it { expect(subject.map(&:protocol)).to all(eq(:tcp).or(eq(:udp))) }
it { expect(subject.map(&:port)).to all(be_between(0,65535)) }
end

describe "#fingerprint" do
Expand Down
22 changes: 22 additions & 0 deletions spec/xml/port_used_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'spec_helper'
require 'nmap/xml/port_used'

describe Nmap::XML::PortUsed do
let(:state) { :open }
let(:protocol) { :tcp }
let(:port) { 22 }

subject { described_class.new(state,protocol,port) }

describe "#to_i" do
it "must return the port number" do
expect(subject.to_i).to eq(port)
end
end

describe "#to_int" do
it "must return the port number" do
expect(subject.to_int).to eq(port)
end
end
end

0 comments on commit dc85304

Please sign in to comment.