Land #2085, use the new network_interface gem.

unstable
Tod Beardsley 2013-07-10 13:15:01 -05:00
commit 8ade33552c
5 changed files with 543 additions and 533 deletions

View File

@ -23,6 +23,7 @@ group :db do
end
group :pcap do
gem 'network_interface'
# For sniffer and raw socket modules
gem 'pcaprub'
end

View File

@ -31,6 +31,7 @@ GEM
metaclass (~> 0.0.1)
msgpack (0.5.4)
multi_json (1.0.4)
network_interface (0.0.1)
nokogiri (1.5.9)
packetfu (1.1.8)
pcaprub (0.11.3)
@ -68,6 +69,7 @@ DEPENDENCIES
json
metasploit_data_models (~> 0.16.1)
msgpack
network_interface
nokogiri
packetfu (= 1.1.8)
pcaprub

View File

@ -53,6 +53,14 @@ module Capture
@pcaprub_error = e
end
begin
require 'network_interface'
@network_interface_loaded = true
rescue ::Exception => e
@network_interface_loaded = false
@network_interface_error = e
end
end
def stats_recv(pcap=self.capture)
@ -376,6 +384,9 @@ module Capture
if not @pcaprub_loaded
print_status("The Pcaprub module is not available: #{@pcaprub_error}")
raise RuntimeError, "Pcaprub not available"
elsif not @network_interface_loaded
print_status("The NetworkInterface module is not available: #{@network_interface_error}")
raise RuntimeError, "NetworkInterface not available"
else
true
end
@ -406,29 +417,24 @@ module Capture
#Netifaces code
# netifaces code is not available in pcaprub 0.9.2 and prior,
# which is going to be installed in a lot of places. Modules
# which want it should check explicitly for it. TODO: Bug upstream
# to release it for real in 0.9.3
def netifaces_implemented?
@pcaprub_loaded and
Pcap.respond_to?(:lookupaddrs) and
Pcap.respond_to?(:interfaces) and
Pcap.respond_to?(:addresses)
@network_interface_loaded and
NetworkInterface.respond_to?(:interfaces) and
NetworkInterface.respond_to?(:addresses)
end
def list_interfaces
check_pcaprub_loaded
Pcap.interfaces
NetworkInterface.interfaces
end
def is_interface?(dev)
check_pcaprub_loaded
if RUBY_PLATFORM == "i386-mingw32"
if dev =~ /\\Device\\NPF_\{[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}\}/
return Pcap.interfaces.include?(dev)
return NetworkInterface.interfaces.include?(dev)
elsif dev.to_s =~ /^[0-9]{1,2}$/
if (dev.to_i <= Pcap.interfaces.length) and (dev.to_i >= 0)
if (dev.to_i <= NetworkInterface.interfaces.length) and (dev.to_i >= 0)
return true
else
return false
@ -437,7 +443,7 @@ module Capture
return false
end
else
return Pcap.interfaces.include?(dev)
return NetworkInterface.interfaces.include?(dev)
end
end
@ -447,7 +453,7 @@ module Capture
if RUBY_PLATFORM == "i386-mingw32"
if dev.to_s =~ /^[0-9]{1,2}$/
if is_interface?(dev)
Pcap.interfaces[(dev.to_i) - 1]
NetworkInterface.interfaces[(dev.to_i) - 1]
else
return dev
end
@ -462,80 +468,80 @@ module Capture
def get_mac(dev)
check_pcaprub_loaded
dev = get_interface_guid(dev)
addrs = Pcap.addresses(dev)
addrs = NetworkInterface.addresses(dev)
raise RuntimeError, "Interface #{dev} does not exist" if !addrs
raise RuntimeError, "Can not get mac address for interface #{dev}" if !addrs[Pcap::AF_LINK][0]['addr']
addrs[Pcap::AF_LINK][0]['addr']
raise RuntimeError, "Can not get mac address for interface #{dev}" if !addrs[NetworkInterface::AF_LINK][0]['addr']
addrs[NetworkInterface::AF_LINK][0]['addr']
end
def get_ipv4_addr_count(dev)
check_pcaprub_loaded
dev = get_interface_guid(dev)
addrs = Pcap.addresses(dev)
addrs = NetworkInterface.addresses(dev)
raise RuntimeError, "Interface #{dev} does not exist" if !addrs
addrs[Pcap::AF_INET].length
addrs[NetworkInterface::AF_INET].length
end
def get_ipv4_addr(dev, num=0)
check_pcaprub_loaded
dev = get_interface_guid(dev)
addrs = Pcap.addresses(dev)
addrs = NetworkInterface.addresses(dev)
raise RuntimeError, "Interface #{dev} do not exists" if !addrs
raise RuntimeError, "Interface #{dev} do not have an ipv4 address at position #{num}" if addrs[Pcap::AF_INET].length < num + 1
raise RuntimeError, "Can not get the IPv4 address for interface #{dev}" if !addrs[Pcap::AF_INET][num]['addr']
addrs[Pcap::AF_INET][num]['addr']
raise RuntimeError, "Interface #{dev} do not have an ipv4 address at position #{num}" if addrs[NetworkInterface::AF_INET].length < num + 1
raise RuntimeError, "Can not get the IPv4 address for interface #{dev}" if !addrs[NetworkInterface::AF_INET][num]['addr']
addrs[NetworkInterface::AF_INET][num]['addr']
end
def get_ipv4_netmask(dev, num=0)
check_pcaprub_loaded
dev = get_interface_guid(dev)
addrs = Pcap.addresses(dev)
addrs = NetworkInterface.addresses(dev)
raise RuntimeError, "Interface #{dev} do not exists" if !addrs
raise RuntimeError, "Interface #{dev} do not have an ipv4 address at position #{num}" if addrs[Pcap::AF_INET].length < num + 1
raise RuntimeError, "Can not get IPv4 netmask for interface #{dev}" if !addrs[Pcap::AF_INET][num]['netmask']
addrs[Pcap::AF_INET][num]['netmask']
raise RuntimeError, "Interface #{dev} do not have an ipv4 address at position #{num}" if addrs[NetworkInterface::AF_INET].length < num + 1
raise RuntimeError, "Can not get IPv4 netmask for interface #{dev}" if !addrs[NetworkInterface::AF_INET][num]['netmask']
addrs[NetworkInterface::AF_INET][num]['netmask']
end
def get_ipv4_broadcast(dev, num=0)
check_pcaprub_loaded
dev = get_interface_guid(dev)
addrs = Pcap.addresses(dev)
addrs = NetworkInterface.addresses(dev)
raise RuntimeError, "Interface #{dev} do not exists" if !addrs
raise RuntimeError, "Interface #{dev} do not have an ipv4 address at position #{num}" if addrs[Pcap::AF_INET].length < num + 1
raise RuntimeError, "Can not get IPv4 broadcast address for interface #{dev}" if !addrs[Pcap::AF_INET][num]['broadcast']
addrs[Pcap::AF_INET][num]['broadcast']
raise RuntimeError, "Interface #{dev} do not have an ipv4 address at position #{num}" if addrs[NetworkInterface::AF_INET].length < num + 1
raise RuntimeError, "Can not get IPv4 broadcast address for interface #{dev}" if !addrs[NetworkInterface::AF_INET][num]['broadcast']
addrs[NetworkInterface::AF_INET][num]['broadcast']
end
def get_ipv6_addr_count(dev)
check_pcaprub_loaded
dev = get_interface_guid(dev)
raise RuntimeError, "IPv6 information is not available on this platform" if not ::Pcap.const_defined?(:AF_INET6)
addrs = Pcap.addresses(dev)
raise RuntimeError, "IPv6 information is not available on this platform" if not ::NetworkInterface.const_defined?(:AF_INET6)
addrs = NetworkInterface.addresses(dev)
raise RuntimeError, "Interface #{dev} do not exists" if !addrs
addrs[Pcap::AF_INET6].length
addrs[NetworkInterface::AF_INET6].length
end
# NOTE: IPv6 is not implemented on Windows
def get_ipv6_addr(dev, num=0)
check_pcaprub_loaded
dev = get_interface_guid(dev)
raise RuntimeError, "IPv6 information is not available on this platform" if not ::Pcap.const_defined?(:AF_INET6)
addrs = Pcap.addresses(dev)
raise RuntimeError, "IPv6 information is not available on this platform" if not ::NetworkInterface.const_defined?(:AF_INET6)
addrs = NetworkInterface.addresses(dev)
raise RuntimeError, "Interface #{dev} do not exists" if !addrs
raise RuntimeError, "Interface #{dev} do not have an ipv6 address at position #{num}" if addrs[Pcap::AF_INET6].length < num + 1
raise RuntimeError, "Can not get ipv6 address for interface #{dev}" if !addrs[Pcap::AF_INET6][num]['addr']
addrs[Pcap::AF_INET6][num]['addr'].gsub(/%(.)*$/,'')
raise RuntimeError, "Interface #{dev} do not have an ipv6 address at position #{num}" if addrs[NetworkInterface::AF_INET6].length < num + 1
raise RuntimeError, "Can not get ipv6 address for interface #{dev}" if !addrs[NetworkInterface::AF_INET6][num]['addr']
addrs[NetworkInterface::AF_INET6][num]['addr'].gsub(/%(.)*$/, '')
end
def get_ipv6_netmask(dev, num=0)
check_pcaprub_loaded
dev = get_interface_guid(dev)
raise RuntimeError, "IPv6 information is not available on this platform" if not ::Pcap.const_defined?(:AF_INET6)
addrs = Pcap.addresses(dev)
raise RuntimeError, "IPv6 information is not available on this platform" if not ::NetworkInterface.const_defined?(:AF_INET6)
addrs = NetworkInterface.addresses(dev)
raise RuntimeError, "Interface #{dev} do not exists" if !addrs
raise RuntimeError, "Interface #{dev} do not have an ipv6 address at position #{num}" if addrs[Pcap::AF_INET6].length < num + 1
raise RuntimeError, "Can not get ipv6 netmask address for interface #{dev}" if !addrs[Pcap::AF_INET6][num]['netmask']
addrs[Pcap::AF_INET6][num]['netmask']
raise RuntimeError, "Interface #{dev} do not have an ipv6 address at position #{num}" if addrs[NetworkInterface::AF_INET6].length < num + 1
raise RuntimeError, "Can not get ipv6 netmask address for interface #{dev}" if !addrs[NetworkInterface::AF_INET6][num]['netmask']
addrs[NetworkInterface::AF_INET6][num]['netmask']
end
# Protocol-specific encoding/decoding methods until more

View File

@ -44,7 +44,7 @@ class Metasploit3 < Msf::Auxiliary
@netifaces = true
if not netifaces_implemented?
print_error("WARNING : Pcaprub is not uptodate, some functionality will not be available")
print_error("WARNING : NetworkInterface is not up-to-date, some functionality will not be available")
@netifaces = false
end

View File

@ -23,37 +23,40 @@ $:.unshift(ENV['MSF_LOCAL_LIB']) if ENV['MSF_LOCAL_LIB']
if RUBY_PLATFORM == "i386-mingw32"
begin
require 'pcaprub'
require 'network_interface'
rescue ::Exception => e
$stderr.puts "Error: pcaprub is not installed..."
$stderr.puts "Error: NetworkInterface is not installed..."
exit
end
unless (Pcap.respond_to?(:lookupaddrs) and
Pcap.respond_to?(:interfaces) and
Pcap.respond_to?(:addresses))
$stderr.puts "Error: Looks like you are not running the latest version of pcaprub"
unless (
NetworkInterface.respond_to?(:interfaces) and
NetworkInterface.respond_to?(:addresses) and
NetworkInterface.respond_to?(:interface_info)
)
$stderr.puts "Error: Looks like you are not running the latest version of NetworkInterface"
exit
end
found = false
Pcap.interfaces.each_with_index do |iface, i|
NetworkInterface.interfaces.each_with_index do |iface, i|
found = true
detail = Pcap.interface_info(iface)
addr = Pcap.addresses(iface)
detail = NetworkInterface.interface_info(iface)
addr = NetworkInterface.addresses(iface)
puts "#" * 70
puts ""
puts "INDEX : " + (i + 1).to_s
puts "NAME : " + detail["name"]
puts "DESCRIPTION : " + detail["description"]
puts "GUID : " + detail["guid"]
if addr[Pcap::AF_LINK][0]['addr']
puts "MAC ADDRESSE : #{addr[Pcap::AF_LINK][0]['addr']}"
if addr[NetworkInterface::AF_LINK][0]['addr']
puts "MAC ADDRESS : #{addr[NetworkInterface::AF_LINK][0]['addr']}"
else
puts "MAC ADDRESSE : NONE"
puts "MAC ADDRESS : NONE"
end
if addr[Pcap::AF_INET][0]['addr'] and addr[Pcap::AF_INET][0]['netmask']
puts "IP ADDRESSE : #{addr[Pcap::AF_INET][0]['addr']}/#{addr[Pcap::AF_INET][0]['netmask']}"
if addr[NetworkInterface::AF_INET][0]['addr'] and addr[NetworkInterface::AF_INET][0]['netmask']
puts "IP ADDRESS : #{addr[NetworkInterface::AF_INET][0]['addr']}/#{addr[NetworkInterface::AF_INET][0]['netmask']}"
else
puts "IP ADDRESSE : NONE"
puts "IP ADDRESS : NONE"
end
puts ""
end
@ -63,8 +66,6 @@ if RUBY_PLATFORM == "i386-mingw32"
$stderr.puts "Error, no network interfaces have been detected"
end
else
$stderr.puts "Error: This script is usefull only on Windows, under other OS just use the built-in commands (ifconfig, ip link show, ...)"
$stderr.puts "Error: This script is useful only on Windows, under other OS just use the built-in commands (ifconfig, ip link show, ...)"
exit
end