Class: Ronin::Support::Network::ASN::List

Inherits:
RecordSet
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ronin/support/network/asn/list.rb

Overview

Represents the entire list of all IPv4 and IPv6 ASNs.

Since:

  • 1.0.0

Constant Summary collapse

FILE_NAME =

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

Since:

  • 1.0.0

'ip2asn-combined.tsv.gz'
URL =

The https://iptoasn.com/data/ip2asn-combined.tsv.gz URL.

Since:

  • 1.0.0

"https://iptoasn.com/data/#{FILE_NAME}"
PATH =

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

Since:

  • 1.0.0

File.join(Home::CACHE_DIR,'ronin','ronin-support',FILE_NAME)
ONE_DAY =

One day in seconds.

Since:

  • 1.0.0

60 * 60 * 24

Instance Attribute Summary collapse

Attributes inherited from RecordSet

#records

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#map_hash

Methods inherited from RecordSet

#countries, #country, #each, #include?, #ipv4, #ipv6, #length, #name, #names, #number, #numbers, #ranges, #to_a

Constructor Details

#initialize(path) ⇒ List

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes the list file.

Parameters:

  • path (String)

    The path to the list file.

Since:

  • 1.0.0



65
66
67
68
69
70
71
72
# File 'lib/ronin/support/network/asn/list.rb', line 65

def initialize(path)
  super()

  @path = path

  @ipv4_prefixes = {}
  @ipv6_prefixes = {}
end

Instance Attribute Details

#pathString (readonly)

The path to the list file.

Returns:

Since:

  • 1.0.0



55
56
57
# File 'lib/ronin/support/network/asn/list.rb', line 55

def path
  @path
end

Class Method Details

.download(url: URL, path: PATH) ⇒ Object

Downloads the list file.

Parameters:

  • url (String) (defaults to: URL)

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

  • path (String) (defaults to: PATH)

    An optional alternate path to the list file.

Since:

  • 1.0.0



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# 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) ⇒ Boolean

Determines whether the list file has been previously downloaded.

Parameters:

  • path (String) (defaults to: PATH)

    An optional alternate path to the list file.

Returns:

  • (Boolean)

Since:

  • 1.0.0



82
83
84
# File 'lib/ronin/support/network/asn/list.rb', line 82

def self.downloaded?(path=PATH)
  File.file?(path)
end

.load_file(path = PATH) ⇒ List

Loads the contents of the list file into memory.

Parameters:

  • path (String) (defaults to: PATH)

    An optional alternate path to the list file.

Returns:

  • (List)

    The loaded list.

Since:

  • 1.0.0



200
201
202
203
204
205
206
207
208
# 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

.parse(path = PATH) {|record| ... } ⇒ Enumerator

Parses the contents of the list file.

Parameters:

  • path (String) (defaults to: PATH)

    An optional alternate path to the list file.

Yields:

  • (record)

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

Yield Parameters:

  • record (Record)

    A parsed record in the list file.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator object will be returned.

Since:

  • 1.0.0



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# 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) ⇒ Boolean

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

Parameters:

  • path (String) (defaults to: PATH)

    An optional alternate path to the list file.

Returns:

  • (Boolean)

Since:

  • 1.0.0



97
98
99
# 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) ⇒ Object

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

Parameters:

  • url (String) (defaults to: URL)

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

  • path (String) (defaults to: PATH)

    An optional alternate path to the list file.

Since:

  • 1.0.0



141
142
143
144
145
146
147
148
149
150
151
# 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

Instance Method Details

#<<(record) ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds a record to the list.

Parameters:

Returns:

  • (self)

Since:

  • 1.0.0



219
220
221
222
223
224
225
226
227
228
229
# 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

#inspectString

Inspects the list.

Returns:

Since:

  • 1.0.0



266
267
268
# File 'lib/ronin/support/network/asn/list.rb', line 266

def inspect
  "#<#{self.class}: #{@path}>"
end

#ip(ip) ⇒ Record?

Finds the ASN record for the given IP address.

Parameters:

  • ip (IP, IPaddr, String)

    The IP address to search for.

Returns:

  • (Record, nil)

    The ASN record for the IP address or nil if none could be found.

Since:

  • 1.0.0



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# 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