Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/post/meterpreter/extensions/stdapi/net/config.rb
Views: 11794
# -*- coding: binary -*-12require 'rex/post/meterpreter/extensions/stdapi/tlv'3require 'rex/post/meterpreter/extensions/stdapi/net/arp'4require 'rex/post/meterpreter/extensions/stdapi/net/route'5require 'rex/post/meterpreter/extensions/stdapi/net/netstat'6require 'rex/post/meterpreter/extensions/stdapi/net/interface'78module Rex9module Post10module Meterpreter11module Extensions12module Stdapi13module Net1415###16#17# This class provides an interface to the network configuration18# that exists on the remote machine, such as interfaces, and19# routes.20#21###22class Config2324##25#26# Constructor27#28##2930#31# Initializes a Config instance that is used to get information about the32# network configuration of the remote machine.33#34def initialize(client)35self.client = client36end3738##39#40# Interfaces41#42##4344#45# Enumerates each interface.46#47def each_interface(&block)48get_interfaces().each(&block)49end5051# Returns an array of network interfaces with each element.52#53# @return [Array<Interface>]54def get_interfaces55request = Packet.create_request(COMMAND_ID_STDAPI_NET_CONFIG_GET_INTERFACES)56ifaces = []5758response = client.send_request(request)5960response.each(TLV_TYPE_NETWORK_INTERFACE) { |iface|61addrs = []62netmasks = []63scopes = []64while (a = iface.get_tlv_value(TLV_TYPE_IP, addrs.length))65# Netmasks aren't tightly associated with addresses, they're66# just thrown all together in the interface TLV ordered to67# match up. This could be done better by creating another68# GroupTlv type for addresses containing an address, a netmask,69# and possibly a scope.70n = iface.get_tlv_value(TLV_TYPE_NETMASK, addrs.length)71if (n.nil?)72# Some systems can't report a netmask, only a network73# prefix, so figure out the netmask from that.74n = iface.get_tlv_value(TLV_TYPE_IP_PREFIX, addrs.length)75if n76n = Rex::Socket.bit2netmask(n, !!(a.length == 16))77end78else79n = Rex::Socket.addr_ntoa(n)80end81s = iface.get_tlv_value(TLV_TYPE_IP6_SCOPE, addrs.length)82scopes[addrs.length] = s if s83netmasks[addrs.length] = n if n84addrs << Rex::Socket.addr_ntoa(a)85end86ifaces << Interface.new(87:index => iface.get_tlv_value(TLV_TYPE_INTERFACE_INDEX),88:mac_addr => iface.get_tlv_value(TLV_TYPE_MAC_ADDRESS),89:mac_name => iface.get_tlv_value(TLV_TYPE_MAC_NAME),90:mtu => iface.get_tlv_value(TLV_TYPE_INTERFACE_MTU),91:flags => iface.get_tlv_value(TLV_TYPE_INTERFACE_FLAGS),92:addrs => addrs,93:netmasks => netmasks,94:scopes => scopes95)96}9798return ifaces99end100101alias interfaces get_interfaces102103##104#105# Network connections106#107##108109#110# Returns an array of network connection entries with each element being a Netstat.111#112113def get_netstat114request = Packet.create_request(COMMAND_ID_STDAPI_NET_CONFIG_GET_NETSTAT)115netstat = []116117response = client.send_request(request)118119# Build out the array of netstat120response.each(TLV_TYPE_NETSTAT_ENTRY) { |connection|121netstat << Netstat.new(122:local_addr => connection.get_tlv_value(TLV_TYPE_LOCAL_HOST_RAW),123:remote_addr => connection.get_tlv_value(TLV_TYPE_PEER_HOST_RAW),124:local_port => connection.get_tlv_value(TLV_TYPE_LOCAL_PORT),125:remote_port => connection.get_tlv_value(TLV_TYPE_PEER_PORT),126:protocol => connection.get_tlv_value(TLV_TYPE_MAC_NAME), # tcp/tcp6/udp/udp6127:state => connection.get_tlv_value(TLV_TYPE_SUBNET_STRING),128:uid => connection.get_tlv_value(TLV_TYPE_PID),129:inode => connection.get_tlv_value(TLV_TYPE_ROUTE_METRIC),130:pid_name => connection.get_tlv_value(TLV_TYPE_PROCESS_NAME)131)132}133134return netstat135end136137alias netstat get_netstat138139##140#141# Routing142#143##144145#146# Returns an array of arp entries with each element being an Arp.147#148149def get_arp_table150request = Packet.create_request(COMMAND_ID_STDAPI_NET_CONFIG_GET_ARP_TABLE)151arps = []152153response = client.send_request(request)154155# Build out the array of arp156response.each(TLV_TYPE_ARP_ENTRY) { |arp|157arps << Arp.new(158:ip_addr => arp.get_tlv_value(TLV_TYPE_IP),159:mac_addr => arp.get_tlv_value(TLV_TYPE_MAC_ADDRESS),160:interface => arp.get_tlv_value(TLV_TYPE_MAC_NAME)161)162}163164return arps165end166167alias arp_table get_arp_table168169#170# Enumerates each route.171#172def each_route(&block)173get_routes().each(&block)174end175176#177# Returns an array of routes with each element being a Route.178#179def get_routes180request = Packet.create_request(COMMAND_ID_STDAPI_NET_CONFIG_GET_ROUTES)181routes = []182183response = client.send_request(request)184185# Build out the array of routes186# Note: This will include both IPv4 and IPv6 routes187response.each(TLV_TYPE_NETWORK_ROUTE) { |route|188routes << Route.new(189route.get_tlv_value(TLV_TYPE_SUBNET),190route.get_tlv_value(TLV_TYPE_NETMASK),191route.get_tlv_value(TLV_TYPE_GATEWAY),192route.get_tlv_value(TLV_TYPE_STRING),193route.get_tlv_value(TLV_TYPE_ROUTE_METRIC))194}195196return routes197end198199alias routes get_routes200201#202# Adds a route to the target machine.203#204def add_route(subnet, netmask, gateway)205request = Packet.create_request(COMMAND_ID_STDAPI_NET_CONFIG_ADD_ROUTE)206207request.add_tlv(TLV_TYPE_SUBNET_STRING, subnet)208request.add_tlv(TLV_TYPE_NETMASK_STRING, netmask)209request.add_tlv(TLV_TYPE_GATEWAY_STRING, gateway)210211client.send_request(request)212213return true214end215216#217# Removes a route from the target machine.218#219def remove_route(subnet, netmask, gateway)220request = Packet.create_request(COMMAND_ID_STDAPI_NET_CONFIG_REMOVE_ROUTE)221222request.add_tlv(TLV_TYPE_SUBNET_STRING, subnet)223request.add_tlv(TLV_TYPE_NETMASK_STRING, netmask)224request.add_tlv(TLV_TYPE_GATEWAY_STRING, gateway)225226client.send_request(request)227228return true229end230231#232# Gets the current proxy configuration233#234def get_proxy_config()235request = Packet.create_request(COMMAND_ID_STDAPI_NET_CONFIG_GET_PROXY)236237response = client.send_request(request)238239proxy_config = {240:autodetect => response.get_tlv_value(TLV_TYPE_PROXY_CFG_AUTODETECT),241:autoconfigurl => response.get_tlv_value(TLV_TYPE_PROXY_CFG_AUTOCONFIGURL),242:proxy => response.get_tlv_value(TLV_TYPE_PROXY_CFG_PROXY),243:proxybypass => response.get_tlv_value(TLV_TYPE_PROXY_CFG_PROXYBYPASS)244}245246return proxy_config247end248249protected250251attr_accessor :client # :nodoc:252253end254255end; end; end; end; end; end256257258