Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/google/geolocation.rb
Views: 11779
#!/usr/bin/env ruby12require 'net/http'3require 'json'45module Rex6module Google7# @example8# g = Rex::Google::Geolocation.new9# g.set_api_key('example')10# g.add_wlan("00:11:22:33:44:55", "example", -80)11# g.fetch!12# puts g, g.google_maps_url13class Geolocation14GOOGLE_API_URI = "https://www.googleapis.com/geolocation/v1/geolocate?key="1516attr_accessor :accuracy17attr_accessor :latitude18attr_accessor :longitude1920def initialize21@uri = URI.parse(URI::DEFAULT_PARSER.escape(GOOGLE_API_URI))22@wlan_list = []23end2425# Ask Google's Maps API for the location of a given set of BSSIDs (MAC26# addresses of access points), ESSIDs (AP names), and signal strengths.27def fetch!28request = Net::HTTP::Post.new(@uri.request_uri)29request.body = {'wifiAccessPoints' => @wlan_list}.to_json30request['Content-Type'] = 'application/json'31http = Net::HTTP.new(@uri.host, @uri.port)32http.use_ssl = true33response = http.request(request)3435msg = "Failure connecting to Google for location lookup."36if response && response.code == '200'37results = JSON.parse(response.body)38self.latitude = results["location"]["lat"]39self.longitude = results["location"]["lng"]40self.accuracy = results["accuracy"]41elsif response && response.body && response.code != '404' # we can json load and get a good error message42results = JSON.parse(response.body)43msg += " Code #{results['error']['code']} for query #{@uri} with error #{results['error']['message']}"44fail msg45else46msg += " Code #{response.code} for query #{@uri}" if response47fail msg48end49end5051# Add an AP to the list to send to Google when {#fetch!} is called.52#53# @param mac [String] in the form "00:11:22:33:44:55"54# @param signal_strength [String] a thing like55def add_wlan(mac, signal_strength)56@wlan_list.push({ :macAddress => mac.upcase.to_s, :signalStrength => signal_strength.to_s })57end5859def set_api_key(key)60@uri = URI.parse(URI::DEFAULT_PARSER.escape(GOOGLE_API_URI + key))61end6263def google_maps_url64"https://maps.google.com/?q=#{latitude},#{longitude}"65end6667def to_s68"Google indicates the device is within #{accuracy} meters of #{latitude},#{longitude}."69end70end71end72end737475