Skip to content

Commit fc66683

Browse files
committed
fixes rapid7#8928
1 parent b9bed5a commit fc66683

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

lib/rex/google/geolocation.rb

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ module Rex
77
module Google
88
# @example
99
# g = Rex::Google::Geolocation.new
10+
# g.set_api_key('example')
1011
# g.add_wlan("00:11:22:33:44:55", "example", -80)
1112
# g.fetch!
1213
# puts g, g.google_maps_url
1314
class Geolocation
14-
GOOGLE_API_URI = "https://maps.googleapis.com/maps/api/browserlocation/json?browser=firefox&sensor=true&"
15+
GOOGLE_API_URI = "https://www.googleapis.com/geolocation/v1/geolocate?key="
1516

1617
attr_accessor :accuracy
1718
attr_accessor :latitude
@@ -24,36 +25,39 @@ def initialize
2425

2526
# Ask Google's Maps API for the location of a given set of BSSIDs (MAC
2627
# addresses of access points), ESSIDs (AP names), and signal strengths.
27-
def fetch!
28-
@uri.query << @wlan_list.take(10).join("&wifi=")
29-
request = Net::HTTP::Get.new(@uri.request_uri)
28+
def fetch!
29+
request = Net::HTTP::Post.new(@uri.request_uri)
30+
request.body = {'wifiAccessPoints' => @wlan_list}.to_json
31+
request['Content-Type'] = 'application/json'
3032
http = Net::HTTP.new(@uri.host, @uri.port)
3133
http.use_ssl = true
3234
response = http.request(request)
3335

36+
msg = "Failure connecting to Google for location lookup."
3437
if response && response.code == '200'
3538
results = JSON.parse(response.body)
3639
self.latitude = results["location"]["lat"]
3740
self.longitude = results["location"]["lng"]
3841
self.accuracy = results["accuracy"]
42+
elsif response && response.body && response.code != '404' # we can json load and get a good error message
43+
msg += " Code #{results['error']['code']} for query #{@uri} with error #{results['error']['message']}"
44+
fail msg
3945
else
40-
msg = "Failure connecting to Google for location lookup."
4146
msg += " Code #{response.code} for query #{@uri}" if response
4247
fail msg
4348
end
4449
end
4550

4651
# Add an AP to the list to send to Google when {#fetch!} is called.
4752
#
48-
# Turns out Google's API doesn't really care about ESSID or signal strength
49-
# as long as you have BSSIDs. Presumably adding them will make it more
50-
# accurate? Who knows.
51-
#
5253
# @param mac [String] in the form "00:11:22:33:44:55"
53-
# @param ssid [String] ESSID associated with the mac
5454
# @param signal_strength [String] a thing like
55-
def add_wlan(mac, ssid = nil, signal_strength = nil)
56-
@wlan_list.push(URI.encode("mac:#{mac.upcase}|ssid:#{ssid}|ss=#{signal_strength.to_i}"))
55+
def add_wlan(mac, signal_strength)
56+
@wlan_list.push({ :macAddress => mac.upcase.to_s, :signalStrength => signal_strength.to_s })
57+
end
58+
59+
def set_api_key(key)
60+
@uri = URI.parse(URI.encode(GOOGLE_API_URI + key))
5761
end
5862

5963
def google_maps_url

modules/post/multi/gather/wlan_geolocate.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ def initialize(info={})
2121

2222
register_options(
2323
[
24-
OptBool.new('GEOLOCATE', [ false, 'Use Google APIs to geolocate Linux, Windows, and OS X targets.', false])
24+
OptBool.new('GEOLOCATE', [ false, 'Use Google APIs to geolocate Linux, Windows, and OS X targets.', false]),
25+
OptString.new('APIKEY', [ false, 'Key for Google APIs if error is received without one.', '']),
2526
])
2627

2728
end
@@ -83,13 +84,16 @@ def parse_wireless_osx(listing)
8384

8485
def perform_geolocation(wlan_list)
8586
if wlan_list.blank?
86-
print_error("Unable to enumerate wireless networks from the target. Wireless may not be present or enabled.")
87+
print_error('Unable to enumerate wireless networks from the target. Wireless may not be present or enabled.')
88+
return
89+
elsif datastore['APIKEY'].empty?
90+
print_error("Google API key is required.")
8791
return
8892
end
8993
g = Rex::Google::Geolocation.new
90-
94+
g.set_api_key(datastore['APIKEY'])
9195
wlan_list.each do |wlan|
92-
g.add_wlan(*wlan)
96+
g.add_wlan(wlan[0], wlan[2]) # bssid, signalstrength
9397
end
9498

9599
begin

0 commit comments

Comments
 (0)