|
|
|
@ -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
|
|
|
|
|