Method: Uirusu::CLI::Application#scan_and_wait

Defined in:
lib/uirusu/cli/application.rb

#scan_and_wait(mod, resource, attempts) ⇒ Object

Submits a file/url and waits for analysis to be complete and returns the results.

Parameters:

  • mod
  • resource
  • attempts


221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/uirusu/cli/application.rb', line 221

def scan_and_wait(mod, resource, attempts)
  method = nil
  retries = attempts

  if mod.name == "Uirusu::VTFile"
    STDERR.puts "[*] Attempting to rescan #{resource}" if  @options['verbose']
    method = @options['rescan'] ? mod.method(:rescan_file) : mod.method(:scan_file)
  else
    STDERR.puts "[*] Attempting to upload file #{resource}" if  @options['verbose']
    method = mod.method :scan_url
  end

  begin
    result = method.call(@config['virustotal']['api-key'], resource)
  rescue => e
    if @options['rescan']
      STDERR.puts "[!] An error has occurred with the rescan request.  Retrying 60 seconds up #{retries} retries: #{e.message}\n" if  @options['verbose']
    else
      STDERR.puts "[!] An error has occurred uploading the file. Retrying 60 seconds up #{retries} retries.\n" if  @options['verbose']
    end

    if retries >= 0
      sleep 60
      retries = retries - 1
      retry
    end
  end

  begin

    # Convert all single result replies to an array.  This is because
    # rescan_file returns an array of results if more than one hash
    # is requested to be rescanned.
    result_array = result.is_a?(Array) ? result : [ result ]

    result_array.collect do |r|
      if r['response_code'] == 1
        STDERR.puts "[*] Attempting to parse the results for: #{r['resource']}" if @options['verbose']
        results = mod.query_report(@config['virustotal']['api-key'], r['resource'])

        while results['response_code'] != 1
          STDERR.puts "[*] File has not been analyized yet, waiting 60 seconds to try again" if  @options['verbose']
          sleep 60
          results = mod.query_report(@config['virustotal']['api-key'], r['resource'])
        end

        return r['resource'], results

      elsif r['response_code'] == 0 and @options['rescan']
        STDERR.puts "[!] Unknown Virustotal error for rescan of #{r['resource']}." if @options['verbose']
        next

      elsif r['response_code'] == -1 and @options['rescan']
        STDERR.puts "[!] Virustotal does not have a sample of #{r['resource']}." if @options['verbose']
        next

      elsif r['response_code'] == -2
        STDERR.puts "[!] Virustotal limits exceeded, ***do not edit the timeout values.***"
        exit(1)
      else
        nil
      end
    end
  rescue => e
    STDERR.puts "[!] An error has occurred retrieving the report. Retrying 60 seconds up #{retries} retries. #{e.message}\n" if  @options['verbose']
    if retries >= 0
      sleep 60
      retries = retries - 1
      retry
    end
  end
end