adding in a new option type

this will grab the first ipv4 address on a given iface
bug/bundler_fix
darkbushido 2017-05-02 09:31:16 -05:00
parent 97095ab311
commit a6afd0b9bf
2 changed files with 93 additions and 0 deletions

View File

@ -0,0 +1,59 @@
# -*- coding: binary -*-
module Msf
###
#
# Network address option.
#
###
class OptAddressLocal < OptBase
def type
return 'address'
end
def normalize(value)
return nil unless value.kind_of?(String)
if (value =~ /^iface:(.*)/)
iface = $1
return false if not NetworkInterface.interfaces.include?(iface)
ip_address = NetworkInterface.addresses(iface).values.flatten.collect{|x| x['addr']}.select do |addr|
begin
IPAddr.new(addr).ipv4?
rescue IPAddr::InvalidAddressError => e
false
end
end
return false if ip_address.blank?
return ip_address
end
return value
end
def valid?(value, check_empty: true)
return false if check_empty && empty_required_value?(value)
return false unless value.kind_of?(String) or value.kind_of?(NilClass)
if (value != nil and not value.empty?)
begin
getaddr_result = ::Rex::Socket.getaddress(value, true)
# Covers a wierdcase where an incomplete ipv4 address will have it's
# missing octets filled in with 0's. (e.g 192.168 become 192.0.0.168)
# which does not feel like a legit behaviour
if value =~ /^\d{1,3}(\.\d{1,3}){1,3}$/
return false unless value =~ Rex::Socket::MATCH_IPV4
end
rescue
return false
end
end
return super
end
end
end

View File

@ -0,0 +1,34 @@
# -*- coding:binary -*-
require 'spec_helper'
require 'msf/core/option_container'
RSpec.describe Msf::OptAddressLocal do
valid_values = [
{ :value => "192.0.2.0/24", :normalized => "192.0.2.0/24" },
{ :value => "192.0.2.0", :normalized => "192.0.2.0" },
{ :value => "127.0.0.1", :normalized => "127.0.0.1" },
{ :value => "2001:db8::", :normalized => "2001:db8::" },
{ :value => "::1", :normalized => "::1" }
]
invalid_values = [
# Too many dots
{ :value => "192.0.2.0.0" },
# Not enough
{ :value => "192.0.2" },
# Non-string values
{ :value => true},
{ :value => 5 },
{ :value => []},
{ :value => [1,2]},
{ :value => {}},
]
it_behaves_like "an option", valid_values, invalid_values, 'address'
end