Files
metasploit-gs/lib/rex/post/meterpreter/extensions/stdapi/net/interface.rb
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

130 lines
2.7 KiB
Ruby
Raw Normal View History

# -*- coding: binary -*-
2005-04-17 08:02:01 +00:00
2005-04-17 09:04:22 +00:00
require 'ipaddr'
2005-04-17 08:02:01 +00:00
module Rex
module Post
module Meterpreter
module Extensions
module Stdapi
module Net
###
#
# This class represents a logical physical interface
# on the remote machine.
#
###
class Interface
##
#
# Constructor
#
##
2013-08-30 16:28:33 -05:00
2005-11-15 05:22:13 +00:00
#
# Returns a logical interface and initializes it to the supplied
# parameters.
#
def initialize(opts={})
self.index = opts[:index] || -1
self.mac_addr = opts[:mac_addr]
self.mac_name = opts[:mac_name]
self.mtu = opts[:mtu]
self.flags = opts[:flags]
self.addrs = opts[:addrs]
2012-03-13 02:08:34 -06:00
self.netmasks = opts[:netmasks]
2012-03-16 01:46:41 -06:00
self.scopes = opts[:scopes]
2005-04-17 08:02:01 +00:00
end
2013-08-30 16:28:33 -05:00
2005-11-15 05:22:13 +00:00
#
# Returns a pretty string representation of the interface's properties.
#
2005-04-17 08:02:01 +00:00
def pretty
macocts = []
mac_addr.each_byte { |o| macocts << o }
macocts += [0] * (6 - macocts.size) if macocts.size < 6
2013-08-30 16:28:33 -05:00
info = [
["Name" , mac_name ],
["Hardware MAC" , sprintf("%02x:%02x:%02x:%02x:%02x:%02x",
2012-02-24 16:31:01 -07:00
macocts[0], macocts[1], macocts[2],
macocts[3], macocts[4], macocts[5])],
["MTU" , mtu ],
["Flags" , flags ],
]
2013-08-30 16:28:33 -05:00
2012-03-13 02:08:34 -06:00
# If all went as planned, addrs and netmasks will have the same number
# of elements and be properly ordered such that they match up
# correctly.
addr_masks = addrs.zip(netmasks)
2013-08-30 16:28:33 -05:00
2012-03-13 02:08:34 -06:00
addr_masks.select { |a| Rex::Socket.is_ipv4?(a[0]) }.each { |a|
info << [ "IPv4 Address", a[0] ]
info << [ "IPv4 Netmask", a[1] ]
}
2012-03-13 02:08:34 -06:00
addr_masks.select { |a| Rex::Socket.is_ipv6?(a[0]) }.each { |a|
info << [ "IPv6 Address", a[0] ]
info << [ "IPv6 Netmask", a[1] ]
}
2013-08-30 16:28:33 -05:00
pad = info.map{|i| i[0] }.max_by{|k|k.length}.length
2013-08-30 16:28:33 -05:00
2012-02-24 16:31:01 -07:00
ret = sprintf(
2012-02-24 15:27:05 -07:00
"Interface %2d\n" +
2012-02-24 16:31:01 -07:00
"============\n",
index
)
2013-08-30 16:28:33 -05:00
2012-02-24 16:31:01 -07:00
info.map {|k,v|
next if v.nil?
ret << k.ljust(pad) + " : #{v}\n"
}
2013-08-30 16:28:33 -05:00
2012-02-24 16:31:01 -07:00
ret
2005-04-17 08:02:01 +00:00
end
2013-08-30 16:28:33 -05:00
2012-02-24 15:27:05 -07:00
#
# The first address associated with this Interface
2012-02-24 15:27:05 -07:00
#
def ip
addrs.first
end
2013-08-30 16:28:33 -05:00
2005-11-15 05:22:13 +00:00
#
# The index of the interface.
2005-11-15 05:22:13 +00:00
#
attr_accessor :index
2005-11-15 05:22:13 +00:00
#
# An Array of IP addresses bound to the Interface.
2005-11-15 05:22:13 +00:00
#
attr_accessor :addrs
2005-11-15 05:22:13 +00:00
#
# The physical (MAC) address of the NIC.
#
attr_accessor :mac_addr
#
# The name of the interface.
#
attr_accessor :mac_name
2012-02-24 15:27:05 -07:00
#
# The MTU associated with the interface.
#
attr_accessor :mtu
#
# The flags associated with the interface.
#
attr_accessor :flags
2012-03-16 01:46:41 -06:00
#
# An Array of netmasks. This will have the same number of elements as #addrs
#
2012-03-13 02:08:34 -06:00
attr_accessor :netmasks
2012-03-16 01:46:41 -06:00
#
# An Array of IPv6 address scopes. This will have the same number of elements as #addrs
#
attr_accessor :scopes
2005-04-17 08:02:01 +00:00
end
end; end; end; end; end; end