Class: Uirusu::CLI::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/uirusu/cli/application.rb

Instance Method Summary collapse

Constructor Details

#initializeApplication

Creates a new instance of the [Application] class



7
8
9
10
11
12
13
14
# File 'lib/uirusu/cli/application.rb', line 7

def initialize
	@options = {}
	@config = {}
	@hashes = Array.new
	@files_of_hashes = Array.new
	@sites = Array.new
	@uploads = Array.new
end

Instance Method Details

#load_configObject

Loads the .uirusu config file for the api key



123
124
125
126
127
128
129
130
# File 'lib/uirusu/cli/application.rb', line 123

def load_config
	if File.exists?(File.expand_path(CONFIG_FILE))
		@config = YAML.load_file File.expand_path(CONFIG_FILE)
	else
		STDERR.puts "[!] #{CONFIG_FILE} does not exist. Please run #{APP_NAME} --create-config, to create it."
		exit
	end
end

#main(args) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/uirusu/cli/application.rb', line 189

def main(args)
	parse_options(args)
	load_config
	
	if @options['output'] == :stdout
		output_method = :to_stdout
	elsif @options['output'] == :yaml
		output_method = :to_yaml
	elsif @options['output'] == :xml
		output_method = :to_xml
		print "<results>\n"
	end

	if @files_of_hashes != nil
		@files_of_hashes.each do |file|
			f = File.open(file, 'r')

		  f.each do |hash|
		  	hash.chomp!
		    @hashes.push hash
		  end
		end
	end		

	if @hashes != nil
		@hashes.each do |hash|
			results = Uirusu::VTFile.query_report(@config["virustotal"]["api-key"], hash)
			result = Uirusu::VTResult.new(hash, results)
			print result.send output_method if result != nil
		end
	end

	if @sites != nil
		@sites.each do |url|
			results = scan_and_wait(Uirusu::VTUrl, url, 5)
			result = Uirusu::VTResult.new(results[0], results[1])
			print result.send output_method if result != nil
		end
	end

	if @uploads != nil
		@uploads.each do |upload|
			results = scan_and_wait(Uirusu::VTFile, upload, 5)
			result = Uirusu::VTResult.new(results[0], results[1])						
			print result.send output_method if result != nil
		end
	end

	if @options['output'] == :xml
		print "</results>\n"
	end
end

#parse_options(args) ⇒ Hash

Parses the command the line options and returns the parsed options hash

Returns:

  • (Hash)

    of the parsed options



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/uirusu/cli/application.rb', line 19

def parse_options(args)
	begin
		@options['output'] = :stdout
		@options['verbose'] = false
	
		opt = OptionParser.new do |opt|
			opt.banner =	"#{APP_NAME} v#{VERSION}\nJacob Hammack\nhttp://www.hammackj.com\n\n"
			opt.banner << "Usage: #{APP_NAME} <options>"
			opt.separator('')
			opt.separator("File Options")
	
			opt.on('-h HASH', '--search-hash HASH', 'Searches a single hash on virustotal.com') do |hash| 
				@hashes.push(hash)
			end

			opt.on('-f FILE', '--search-hash-file FILE', 'Searches a each hash in a file of hashes on virustotal.com') do |file|
				if File.exists?(file)
					puts "[+] Adding file #{file}" if @options["verbose"]
					@files_of_hashes.push(file)
				else
					puts "[!] #{file} does not exist, please check your input!\n"
				end
			end
		
			opt.on('-u FILE', '--upload-file FILE', 'Uploads a file to virustotal.com for analysis') do |file|
				if File.exists?(file)
					puts "[+] Adding file #{file}" if @options["verbose"]
					@uploads.push(file)
				else
					puts "[!] #{file} does not exist, please check your input!\n"
				end
			end

			opt.separator('')
			opt.separator("Url Options")
			
			opt.on('-s SITE', '--search-site SITE', 'Searches for a single url on virustotal.com') { |site| 
				@sites.push(site)
			}
									
			opt.separator('')
			opt.separator('Output Options')

			opt.on('-x', '--xml-output', 'Print results as xml to stdout') do
				@options["output"] = :xml
			end

			opt.on('-y', '--yaml-output', 'Print results as yaml to stdout') do
				@options['output'] = :yaml
			end
		
			opt.on('--stdout-output', 'Print results as normal text line to stdout, this is default') do
				@options['output'] = :stdout
			end

			opt.separator ''
			opt.separator 'Advanced Options'

			opt.on('-c', '--create-config', 'Creates a skeleton config file to use') do					
				if File.exists?(File.expand_path(CONFIG_FILE)) == false
					File.open(File.expand_path(CONFIG_FILE), 'w+') do |f| 
						f.write("virustotal: \n  api-key: \n  timeout: 25\n\n") 
					end

					puts "[*] An empty #{File.expand_path(CONFIG_FILE)} has been created. Please edit and fill in the correct values."
					exit
				else
					puts "[!]  #{File.expand_path(CONFIG_FILE)} already exists. Please delete it if you wish to re-create it."
					exit
				end
			end

			opt.on('--[no-]verbose', 'Print verbose information') do |v|
				@options["verbose"] = v
			end
	
			opt.separator ''
			opt.separator 'Other Options'
	
			opt.on('-v', '--version', "Shows application version information") do
				puts "#{APP_NAME} - #{VERSION}"
				exit
			end

			opt.on_tail("-?", "--help", "Show this message") { |help|
				puts opt.to_s + "\n"
				exit
			} 
		end
				
	  if ARGV.length != 0 
	    opt.parse!
	  else
	    puts opt.to_s + "\n"
		  exit
		end		
	rescue OptionParser::MissingArgument => m
		puts opt.to_s + "\n"
		exit
	end
end

#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


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/uirusu/cli/application.rb', line 138

def scan_and_wait(mod, resource, attempts)
	method = nil
	retries = attempts
	
	if mod.name == "Uirusu::VTFile"
		method = mod.method :scan_file
	else
		method = mod.method :scan_url
	end

	begin
		STDERR.puts "[*] Attempting to upload file #{resource}" if  @options["verbose"]
		result = method.call(@config["virustotal"]["api-key"], resource)					
	rescue => e
		STDERR.puts "[!] An error has occured uploading the file. Retrying 60 seconds up #{retries} retries.\n" if  @options["verbose"]
		if retries >= 0
			sleep 60
			retry
			retries = retries - 1
		end
	end
	
	begin
		if result['response_code']	== 1
			results = mod.query_report(@config["virustotal"]["api-key"], result['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"], result['resource'])
			end
		
			return [result['resource'], results]
		elsif result['response_code']	== -2
			STDERR.puts "[!] Virustotal limits exceeded, ***do not edit the timeout values.***" 
			exit(1)
		else
			nil
		end	
	rescue => e					
		STDERR.puts "[!] An error has occured retrieving the report. Retrying 60 seconds up #{retries} retries.\n" if  @options["verbose"]
		if retries >= 0
			sleep 60
			retry
			retries = retries - 1
		end				
	end
end