首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >提高Ruby Resolv速度

提高Ruby Resolv速度
EN

Stack Overflow用户
提问于 2016-04-20 20:27:33
回答 2查看 643关注 0票数 1

我正在尝试建立一个子域蛮力与我的客户一起使用-我的工作是安全/笔测试。目前,我能够让Resolv在10秒内查找大约70台主机,或多或少,并想知道是否有方法让它做更多。我已经看到了其他的脚本,主要是基于Python的,可以实现比这快得多的速度。我不知道如何增加Resolv并行请求的数量,或者我是否应该拆分列表。请注意,我已经将谷歌的DNS服务器放在示例代码中,但将使用内部服务器进行实时使用。

我用来调试这个问题的粗略代码是:

代码语言:javascript
复制
require 'resolv'

def subdomains
  puts "Subdomain enumeration beginning at #{Time.now.strftime("%H:%M:%S")}"
  subs = []
  domains = File.open("domains.txt", "r") #list of domain names line by line.
  Resolv.new(:nameserver => ['8.8.8.8', '8.8.4.4'])
    File.open("tiny.txt", "r").each_line do |subdomain|
      subdomain.chomp!
    domains.each do |d|
      puts "Checking #{subdomain}.#{d}"
      ip = Resolv.new.getaddress "#{subdomain}.#{d}" rescue ""
        if ip != nil
          subs << subdomain+"."+d << ip
      end
    end
  end
  test = subs.each_slice(4).to_a
    test.each do |z|
      if !z[1].nil? and !z[3].nil?
    puts z[0] + "\t" + z[1] + "\t\t" + z[2] + "\t" + z[3]
  end
end
  puts "Finished at #{Time.now.strftime("%H:%M:%S")}"
end

subdomains

domains.txt是我的客户端域名列表,例如google.com、bbc.co.uk、apple.com,而'tiny.txt‘是潜在的子域名列表,例如ftp、www、dev、文件、上传。然后,Resolv将查找files.bbc.co.uk,例如,并让我知道它是否存在。

EN

回答 2

Stack Overflow用户

发布于 2016-04-20 22:52:58

一件事是,您正在使用Google名称服务器创建一个新的Resolv实例,但从未使用过它;您创建了一个全新的Resolv实例来执行getaddress调用,因此该实例可能使用了一些默认的名称服务器,而不是Google的。您可以将代码更改为如下所示:

代码语言:javascript
复制
resolv = Resolv.new(:nameserver => ['8.8.8.8', '8.8.4.4'])
# ...
ip = resolv.getaddress "#{subdomain}.#{d}" rescue ""

此外,我建议使用File.readlines方法来简化代码:

代码语言:javascript
复制
domains = File.readlines("domains.txt").map(&:chomp)
subdomains = File.readlines("tiny.txt").map(&:chomp)

此外,您正在拯救坏的ip并将其设置为空字符串,但是在下一行中,您将测试非nil,因此所有结果都应该通过,我认为这不是您想要的结果。

我重构了你的代码,但没有测试它。这是我想出来的,可能会更清楚:

代码语言:javascript
复制
def subdomains
  puts "Subdomain enumeration beginning at #{Time.now.strftime("%H:%M:%S")}"
  domains = File.readlines("domains.txt").map(&:chomp)
  subdomains = File.readlines("tiny.txt").map(&:chomp)

  resolv = Resolv.new(:nameserver => ['8.8.8.8', '8.8.4.4'])

  valid_subdomains = subdomains.each_with_object([]) do |subdomain, valid_subdomains|
    domains.each do |domain|
      combined_name = "#{subdomain}.#{domain}"
      puts "Checking #{combined_name}"
      ip = resolv.getaddress(combined_name) rescue nil
      valid_subdomains << "#{combined_name}#{ip}" if ip
    end
  end

  valid_subdomains.each_slice(4).each do |z|
    if z[1] && z[3]
      puts "#{z[0]}\t#{z[1]}\t\t#{z[2]}\t#{z[3]}"
    end
  end

  puts "Finished at #{Time.now.strftime("%H:%M:%S")}"
end

此外,您可能希望查看dnsruby gem (https://github.com/alexdalitz/dnsruby)。它可能会做你想做的比Resolv更好的事情。

票数 0
EN

Stack Overflow用户

发布于 2016-04-21 12:26:09

[注意:我已经重写了代码,以便它以块的形式获取IP地址。请参阅https://gist.github.com/keithrbennett/3cf0be2a1100a46314f662aea9b368ed。您可以修改RESOLVE_CHUNK_SIZE常量以平衡性能和资源负载。]

我已经使用dnsruby gem重写了这段代码(主要由英国的Alex Dalitz编写,并由我和其他人贡献)。此版本使用异步消息处理,以便几乎同时处理所有请求。我已经在https://gist.github.com/keithrbennett/3cf0be2a1100a46314f662aea9b368ed上发布了一个要点,但也将在这里发布代码。

请注意,由于您是Ruby的新手,代码中的许多内容可能对您有指导意义,例如方法组织、可枚举方法的使用(例如令人惊叹的“分区”方法)、Struct类、拯救特定的异常类、%w和基准测试。

注意:看起来堆栈溢出强制了最大消息大小,所以这段代码被截断了。请转到上面链接中的GIST获取完整的代码。

代码语言:javascript
复制
#!/usr/bin/env ruby

# Takes a list of subdomain prefixes (e.g.  %w(ftp  xyz)) and a list of domains (e.g. %w(nytimes.com  afp.com)),
# creates the subdomains combining them, fetches their IP addresses (or nil if not found).

require 'dnsruby'
require 'awesome_print'

RESOLVER = Dnsruby::Resolver.new(:nameserver => %w(8.8.8.8  8.8.4.4))

# Experiment with this to get fast throughput but not overload the dnsruby async mechanism:
RESOLVE_CHUNK_SIZE = 50


IpEntry = Struct.new(:name, :ip) do
  def to_s
    "#{name}: #{ip ? ip : '(nil)'}"
  end
end


def assemble_subdomains(subdomain_prefixes, domains)
  domains.each_with_object([]) do |domain, subdomains|
    subdomain_prefixes.each do |prefix|
      subdomains << "#{prefix}.#{domain}"
    end
  end
end


def create_query_message(name)
  Dnsruby::Message.new(name, 'A')
end


def parse_response_for_address(response)
  begin
    a_answer = response.answer.detect { |a| a.type == 'A' }
    a_answer ? a_answer.rdata.to_s : nil
  rescue Dnsruby::NXDomain
    return nil
  end
end


def get_ip_entries(names)

  queue = Queue.new

  names.each do |name|
    query_message = create_query_message(name)
    RESOLVER.send_async(query_message, queue, name)
  end


  # Note: although map is used here, the record in the output array will not necessarily correspond
  # to the record in the input array, since the order of the messages returned is not guaranteed.
  # This is indicated by the lack of block variable specified (normally w/map you would use the element).
  # That should not matter to us though.
  names.map do
    _id, result, error = queue.pop
    name = _id
    case error
      when Dnsruby::NXDomain
        IpEntry.new(name, nil)
      when NilClass
       ip = parse_response_for_address(result)
       IpEntry.new(name, ip)
      else
       raise error
      end
  end
end


def main
  # domains = File.readlines("domains.txt").map(&:chomp)
  domains = %w(nytimes.com  afp.com  cnn.com  bbc.com)

  # subdomain_prefixes = File.readlines("subdomain_prefixes.txt").map(&:chomp)
  subdomain_prefixes = %w(www  xyz)

  subdomains = assemble_subdomains(subdomain_prefixes, domains)

  start_time = Time.now
  ip_entries = subdomains.each_slice(RESOLVE_CHUNK_SIZE).each_with_object([]) do |ip_entries_chunk, results|
    results.concat get_ip_entries(ip_entries_chunk)
  end
  duration = Time.now - start_time

  found, not_found = ip_entries.partition { |entry| entry.ip }

  puts "\nFound:\n\n";  puts found.map(&:to_s);  puts "\n\n"
  puts "Not Found:\n\n"; puts not_found.map(&:to_s); puts "\n\n"

  stats = {
      duration:        duration,
      domain_count:    ip_entries.size,
      found_count:     found.size,
      not_found_count: not_found.size,
  }

  ap stats
end


main
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36743801

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档