Skip to content

Commit e0837fb

Browse files
committed
Create local copies of loot
1 parent 60e78f0 commit e0837fb

File tree

5 files changed

+33
-25
lines changed

5 files changed

+33
-25
lines changed

lib/metasploit/framework/data_service/remote/http/remote_loot_data_service.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,16 @@ module RemoteLootDataService
66
LOOT_PATH = '/api/1/msf/loot'
77

88
def loot(opts = {})
9-
json_to_open_struct_object(self.get_data(LOOT_PATH, opts), [])
9+
# TODO: Add an option to toggle whether the file data is returned or not
10+
loots = json_to_open_struct_object(self.get_data(LOOT_PATH, opts), [])
11+
# Save a local copy of the file
12+
loots.each do |loot|
13+
if loot.data
14+
local_path = File.join(Msf::Config.loot_directory, File.basename(loot.path))
15+
loot.path = process_file(loot.data, local_path)
16+
end
17+
end
18+
loots
1019
end
1120

1221
def report_loot(opts)

lib/metasploit/framework/data_service/remote/http/response_data_helper.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,27 @@ def json_to_open_struct_object(response_wrapper, returns_on_error = nil)
2323
return returns_on_error
2424
end
2525

26+
# Processes a Base64 encoded file included in a JSON request.
27+
# Saves the file in the location specified in the parameter.
28+
#
29+
# @param base64_file [String] The Base64 encoded file.
30+
# @param save_dir [String] The location to store the file. This should include the file's name.
31+
# @return [String] The location where the file was successfully stored.
32+
def process_file(base64_file, save_dir)
33+
decoded_file = Base64.urlsafe_decode64(base64_file)
34+
begin
35+
# If we are running the data service on the same box this will ensure we only write
36+
# the file if it is somehow not there already.
37+
unless File.exists?(save_dir) && File.read(save_dir) == decoded_file
38+
File.open(save_dir, 'w+') { |file| file.write(decoded_file) }
39+
end
40+
rescue Exception => e
41+
puts "There was an error writing the file: #{e}"
42+
e.backtrace.each { |line| puts "#{line}\n"}
43+
end
44+
save_dir
45+
end
46+
2647
#
2748
# Converts a hash to an open struct
2849
#

lib/msf/core/db_manager/http/servlet/loot_servlet.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,11 @@ def self.get_loot
2727

2828
def self.report_loot
2929
lambda {
30-
3130
job = lambda { |opts|
3231
if opts[:data]
3332
filename = File.basename(opts[:path])
3433
local_path = File.join(Msf::Config.loot_directory, filename)
35-
process_file(opts[:data], local_path)
34+
opts[:path] = process_file(opts[:data], local_path)
3635
end
3736

3837
get_db().report_loot(opts)

lib/msf/core/db_manager/http/servlet/nmap_servlet.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def self.import_nmap_xml_file
1616
lambda {
1717

1818
job = lambda { |opts|
19-
nmap_file = opts[:filename].split('/').last
19+
nmap_file = File.basename(opts[:filename])
2020
nmap_file_path = File.join(Msf::Config.local_directory, nmap_file)
2121
opts[:filename] = process_file(opts[:data], nmap_file_path)
2222
get_db().import_nmap_xml_file(opts)

lib/msf/core/db_manager/http/servlet_helper.rb

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,6 @@ def get_db()
5757
DBManagerProxy.instance.db
5858
end
5959

60-
# Processes a Base64 encoded file included in a JSON request.
61-
# Saves the file in the location specified in the parameter.
62-
#
63-
# @param base64_file [String] The Base64 encoded file.
64-
# @param save_dir [String] The location to store the file. This should include the file's name.
65-
# @return [String] The location where the file was successfully stored.
66-
def process_file(base64_file, save_dir)
67-
decoded_file = Base64.urlsafe_decode64(base64_file)
68-
begin
69-
# If we are running the data service on the same box this will ensure we only write
70-
# the file if it is somehow not there already.
71-
unless File.exists?(save_dir) && File.read(save_dir) == decoded_file
72-
File.open(save_dir, 'r+') { |file| file.write(decoded_file) }
73-
end
74-
rescue Exception => e
75-
puts "There was an error writing the file: #{e}"
76-
e.backtrace.each { |line| puts "#{line}\n"}
77-
end
78-
save_dir
79-
end
80-
8160
#######
8261
private
8362
#######

0 commit comments

Comments
 (0)