class Ronin::Support::Network::ASN::List

Represents the entire list of all IPv4 and IPv6 ASNs.

@api public

@since 1.0.0

Constants

FILE_NAME

File name of the combined IPv4 and IPv6 ASN list file.

ONE_DAY

One day in seconds.

PATH

The path to ‘~/.local/share/ronin/ronin-support/ip2asn-combined.tsv.gz` list file.

URL

The ‘iptoasn.com/data/ip2asn-combined.tsv.gz` URL.

Attributes

path[R]

The path to the list file.

@return [String]

Public Class Methods

download(url: URL, path: PATH) click to toggle source

Downloads the list file.

@param [String] url

An optional alternate URL to download the `ip2asn-combined.tsv.gz`
file.

@param [String] path

An optional alternate path to the list file.
# File lib/ronin/support/network/asn/list.rb, line 111
def self.download(url: URL, path: PATH)
  uri = URI(url)

  Net::HTTP.start(uri.host,uri.port, use_ssl: true) do |http|
    request  = Net::HTTP::Get.new(uri.path)

    http.request(request) do |response|
      FileUtils.mkdir_p(File.dirname(path))

      File.open("#{path}.part",'wb') do |file|
        response.read_body do |chunk|
          file.write(chunk)
        end
      end

      FileUtils.mv("#{path}.part",path)
    end
  end
end
downloaded?(path=PATH) click to toggle source

Determines whether the list file has been previously downloaded.

@param [String] path

An optional alternate path to the list file.

@return [Boolean]

# File lib/ronin/support/network/asn/list.rb, line 82
def self.downloaded?(path=PATH)
  File.file?(path)
end
load_file(path=PATH) click to toggle source

Loads the contents of the list file into memory.

@param [String] path

An optional alternate path to the list file.

@return [List]

The loaded list.
# File lib/ronin/support/network/asn/list.rb, line 200
def self.load_file(path=PATH)
  list = new(path)

  parse(path) do |record|
    list << record
  end

  return list
end
new(path) click to toggle source

Initializes the list file.

@param [String] path

The path to the list file.

@api private

Calls superclass method
# File lib/ronin/support/network/asn/list.rb, line 65
def initialize(path)
  super()

  @path = path

  @ipv4_prefixes = {}
  @ipv6_prefixes = {}
end
parse(path=PATH) { |record| ... } click to toggle source

Parses the contents of the list file.

@param [String] path

An optional alternate path to the list file.

@yield [record]

If a block is given, it will be passed each parsed record from the
list file.

@yieldparam [Record] record

A parsed record in the list file.

@return [Enumerator]

If no block is given, an Enumerator object will be returned.
# File lib/ronin/support/network/asn/list.rb, line 169
def self.parse(path=PATH)
  return enum_for(__method__,path) unless block_given?

  io = if File.extname(path) == '.gz' then Zlib::GzipReader.open(path)
       else                                File.new(path)
       end

  io.each_line do |line|
    line.chomp!

    first, last, number, country_code, name = line.split("\t",5)

    range  = IPRange::Range.new(first,last)
    number = number.to_i

    country_code = nil if country_code == 'None'
    name         = nil if name         == 'Not routed'

    yield Record.new(number,range,country_code,name)
  end
end
stale?(path=PATH) click to toggle source

Determines if the downloaded list file is older than one day.

@param [String] path

An optional alternate path to the list file.

@return [Boolean]

# File lib/ronin/support/network/asn/list.rb, line 97
def self.stale?(path=PATH)
  !File.file?(path) || File.stat(path).mtime < (Time.now - ONE_DAY)
end
update(url: URL, path: PATH) click to toggle source

Optionally update the cached list file if it is older than one day.

@param [String] url

An optional alternate URL to download the `ip2asn-combined.tsv.gz`
file.

@param [String] path

An optional alternate path to the list file.
# File lib/ronin/support/network/asn/list.rb, line 141
def self.update(url: URL, path: PATH)
  if !downloaded?(path)
    download(url: url, path: path)
  elsif stale?(path)
    begin
      download(url: url, path: path)
    rescue
      # ignore any network failures
    end
  end
end

Public Instance Methods

<<(record) click to toggle source

Adds a record to the list.

@param [Record] record

@return [self]

@api private

Calls superclass method
# File lib/ronin/support/network/asn/list.rb, line 219
def <<(record)
  super(record)

  prefixes = if record.range.ipv6? then @ipv6_prefixes
             else                       @ipv4_prefixes
             end

  records = (prefixes[record.range.prefix] ||= [])
  records << record
  return self
end
inspect() click to toggle source

Inspects the list.

@return [String]

# File lib/ronin/support/network/asn/list.rb, line 266
def inspect
  "#<#{self.class}: #{@path}>"
end
ip(ip) click to toggle source

Finds the ASN record for the given IP address.

@param [IP, IPaddr, String] ip

The IP address to search for.

@return [Record, nil]

The ASN record for the IP address or `nil` if none could be found.
# File lib/ronin/support/network/asn/list.rb, line 240
def ip(ip)
  address = ip.to_s
  ip      = IPAddr.new(ip) unless ip.kind_of?(IPAddr)

  prefixes = if ip.ipv6? then @ipv6_prefixes
             else             @ipv4_prefixes
             end

  prefixes.each do |prefix,records|
    if address.start_with?(prefix)
      records.each do |record|
        if record.include?(ip)
          return record
        end
      end
    end
  end

  return nil
end