CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/lib/rex/post/meterpreter/extensions/stdapi/net/resolve.rb
Views: 1904
# -*- coding: binary -*-12require 'rex/post/meterpreter/extensions/stdapi/tlv'34module Rex5module Post6module Meterpreter7module Extensions8module Stdapi9module Net1011###12#13# This class provides DNS resolution from the perspective14# of the remote host.15#16###17class Resolve1819##20#21# Constructor22#23##2425#26# Initializes a Resolve instance that is used to resolve network addresses27# on the remote machine.28#29def initialize(client)30self.client = client31end3233def resolve_host(hostname, family=AF_INET)34request = Packet.create_request(COMMAND_ID_STDAPI_NET_RESOLVE_HOST)35request.add_tlv(TLV_TYPE_HOST_NAME, hostname)36request.add_tlv(TLV_TYPE_ADDR_TYPE, family)3738response = client.send_request(request)3940raw = response.get_tlv_value(TLV_TYPE_IP)4142return raw_to_host_ip_pair(hostname, raw)43end4445def resolve_hosts(hostnames, family=AF_INET)46request = Packet.create_request(COMMAND_ID_STDAPI_NET_RESOLVE_HOSTS)47request.add_tlv(TLV_TYPE_ADDR_TYPE, family)4849hostnames.each do |hostname|50request.add_tlv(TLV_TYPE_HOST_NAME, hostname)51end5253response = client.send_request(request)5455hosts = []56raws = []5758response.each(TLV_TYPE_IP) do |raw|59raws << raw60end61620.upto(hostnames.length - 1) do |i|63raw = raws[i]64host = hostnames[i]6566hosts << raw_to_host_ip_pair(host, raw&.value)67end6869return hosts70end7172def raw_to_host_ip_pair(host, raw)73if raw.nil? or host.nil?74return nil75end7677ip = nil78if raw.length == 4 || raw.length == 1679ip = Rex::Socket.addr_ntoa(raw)80elsif raw.length != 081wlog("hostname resolution failed, the returned address is corrupt (hostname: #{host}, length: #{raw.length})")82end8384result = { :hostname => host, :ip => ip }8586return result87end8889protected9091attr_accessor :client # :nodoc:9293end9495end; end; end; end; end; end969798