Skip to content

Commit e410683

Browse files
authored
Land #2, use net/http instead of rex
2 parents 8c51222 + 8835dae commit e410683

File tree

2 files changed

+1967
-1931
lines changed
  • lib
    • metasploit/framework/data_service/remote/http
    • msf/ui/console/command_dispatcher

2 files changed

+1967
-1931
lines changed

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

Lines changed: 34 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
require 'metasploit/framework/data_service/remote/http/remote_service_endpoint'
22
require 'metasploit/framework/data_service'
33
require 'metasploit/framework/data_service/remote/http/data_service_auto_loader'
4+
require 'net/http'
5+
require 'net/https'
46

57
#
68
# Parent data service for managing metasploit data in/on a separate process/machine over HTTP(s)
@@ -13,7 +15,7 @@ class RemoteHTTPDataService
1315
include DataServiceAutoLoader
1416

1517
ONLINE_TEST_URL = "/api/1/msf/online"
16-
EXEC_ASYNC = {:exec_async => true}
18+
EXEC_ASYNC = { :exec_async => true }
1719
GET_REQUEST = 'GET'
1820
POST_REQUEST = 'POST'
1921

@@ -30,7 +32,7 @@ def initialize(endpoint)
3032
# POST data and don't wait for the endpoint to process the data before getting a response
3133
#
3234
def post_data_async(path, data_hash)
33-
post_data(path, data_hash.merge(EXEC_ASYNC))
35+
make_request(POST_REQUEST, path, data_hash.merge(EXEC_ASYNC))
3436
end
3537

3638
#
@@ -42,30 +44,7 @@ def post_data_async(path, data_hash)
4244
# @return A wrapped response (ResponseWrapper), see below.
4345
#
4446
def post_data(path, data_hash)
45-
begin
46-
raise 'Data to post to remote service cannot be null or empty' if (data_hash.nil? || data_hash.empty?)
47-
48-
puts "#{Time.now} - Posting #{data_hash} to #{path}"
49-
client = @client_pool.pop()
50-
request_opts = build_request_opts(POST_REQUEST, data_hash, path)
51-
request = client.request_raw(request_opts)
52-
response = client._send_recv(request)
53-
54-
if response.code == 200
55-
#puts "POST request: #{path} with body: #{json_body} sent successfully"
56-
return SuccessResponse.new(response)
57-
else
58-
puts "POST request: #{path} with body: #{json_body} failed with code: #{response.code} message: #{response.body}"
59-
return FailedResponse.new(response)
60-
end
61-
rescue Exception => e
62-
puts "Problem with POST request: #{e.message}"
63-
e.backtrace.each do |line|
64-
puts "#{line}\n"
65-
end
66-
ensure
67-
@client_pool << client
68-
end
47+
make_request(POST_REQUEST, path, data_hash)
6948
end
7049

7150
#
@@ -77,23 +56,34 @@ def post_data(path, data_hash)
7756
# @return A wrapped response (ResponseWrapper), see below.
7857
#
7958
def get_data(path, data_hash = nil)
80-
begin
59+
make_request(GET_REQUEST, path, data_hash)
60+
end
8161

82-
puts "#{Time.now} - Getting #{path} with #{data_hash ? data_hash : "nil"}"
62+
def make_request(request_type, path, data_hash = nil)
63+
begin
64+
puts "#{Time.now} - HTTP #{request_type} request to #{path} with #{data_hash ? data_hash : "nil"}"
8365
client = @client_pool.pop()
84-
request_opts = build_request_opts(GET_REQUEST, data_hash, path)
85-
request = client.request_raw(request_opts)
86-
response = client._send_recv(request)
66+
case request_type
67+
when GET_REQUEST
68+
request = Net::HTTP::Get.new(path)
69+
when POST_REQUEST
70+
request = Net::HTTP::Post.new(path)
71+
else
72+
raise Exception, 'A request_type must be specified'
73+
end
74+
built_request = build_request(request, data_hash)
75+
response = client.request(built_request)
8776

88-
if (response.code == 200)
77+
if response.code == "200"
8978
# puts 'request sent successfully'
9079
return SuccessResponse.new(response)
9180
else
92-
puts "GET request: #{path} failed with code: #{response.code} message: #{response.body}"
81+
puts "HTTP #{request_type} request: #{path} failed with code: #{response.code} message: #{response.body}"
9382
return FailedResponse.new(response)
9483
end
9584
rescue Exception => e
96-
puts "Problem with GET request: #{e.message}"
85+
puts "Problem with HTTP #{request_type} request: #{e.message}"
86+
e.backtrace.each { |line| puts "#{line}\n" }
9787
ensure
9888
@client_pool << client
9989
end
@@ -205,13 +195,8 @@ def append_workspace(data_hash)
205195
data_hash
206196
end
207197

208-
def build_request_opts(request_type, data_hash, path)
209-
request_opts = {
210-
'method' => request_type,
211-
'ctype' => 'application/json',
212-
'uri' => path
213-
}
214-
198+
def build_request(request, data_hash)
199+
request.content_type = 'application/json'
215200
if (!data_hash.nil? && !data_hash.empty?)
216201
data_hash.each do |k,v|
217202
if v.is_a?(Msf::Session)
@@ -223,25 +208,24 @@ def build_request_opts(request_type, data_hash, path)
223208
end
224209
end
225210
json_body = append_workspace(data_hash).to_json
226-
request_opts['data'] = json_body
211+
request.body = json_body
227212
end
228213

229214
if (!@headers.nil? && !@headers.empty?)
230-
request_opts['headers'] = @headers
215+
@headers.each do |key, value|
216+
request[key] = value
217+
end
231218
end
232219

233-
request_opts
220+
request
234221
end
235222

236223
def build_client_pool(size)
237224
@client_pool = Queue.new()
238225
(1..size).each {
239-
@client_pool << Rex::Proto::Http::Client.new(
240-
@endpoint.host,
241-
@endpoint.port,
242-
{},
243-
@endpoint.use_ssl,
244-
@endpoint.ssl_version)
226+
http = Net::HTTP.new(@endpoint.host, @endpoint.port)
227+
http.use_ssl = true if @endpoint.use_ssl
228+
@client_pool << http
245229
}
246230
end
247231

0 commit comments

Comments
 (0)