Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mxrch
GitHub Repository: mxrch/GHunt
Path: blob/master/ghunt/modules/geolocate.py
252 views
1
import os
2
3
from ghunt import globals as gb
4
from ghunt.helpers.utils import get_httpx_client
5
from ghunt.apis.geolocation import GeolocationHttp
6
from ghunt.helpers import auth
7
8
import httpx
9
from geopy.geocoders import Nominatim
10
11
from typing import *
12
from pathlib import Path
13
import json
14
15
16
async def main(as_client: httpx.AsyncClient, bssid: str, input_file: Path, json_file: Path=None):
17
# Verifying args
18
body = None
19
if input_file:
20
if not input_file.exists():
21
print(f"[-] The input file \"{input_file}\" doesn't exist.")
22
exit(os.EX_IOERR)
23
with open(input_file, "r", encoding="utf-8") as f:
24
try:
25
body = json.load(f)
26
except json.JSONDecodeError:
27
print(f"[-] The input file \"{input_file}\" is not a valid JSON file.")
28
exit(os.EX_IOERR)
29
30
if not as_client:
31
as_client = get_httpx_client()
32
33
ghunt_creds = await auth.load_and_auth(as_client)
34
35
geo_api = GeolocationHttp(ghunt_creds)
36
found, resp = await geo_api.geolocate(as_client, bssid=bssid, body=body)
37
if not found:
38
print("[-] The location wasn't found.")
39
exit(os.EX_DATAERR)
40
41
geolocator = Nominatim(user_agent="nominatim")
42
location = geolocator.reverse(f"{resp.location.latitude}, {resp.location.longitude}", timeout=10)
43
raw_address = location.raw['address']
44
address = location.address
45
46
gb.rc.print("📍 Location found !\n", style="plum2")
47
gb.rc.print(f"🛣️ [italic]Accuracy : {resp.accuracy} meters[/italic]\n")
48
gb.rc.print(f"Latitude : {resp.location.latitude}", style="bold")
49
gb.rc.print(f"Longitude : {resp.location.longitude}\n", style="bold")
50
gb.rc.print(f"🏠 Estimated address : {address}\n")
51
gb.rc.print(f"🗺️ [italic][link=https://www.google.com/maps/search/?q={resp.location.latitude},{resp.location.longitude}]Open in Google Maps[/link][/italic]\n", style=f"cornflower_blue")
52
53
if json_file:
54
from ghunt.objects.encoders import GHuntEncoder;
55
with open(json_file, "w", encoding="utf-8") as f:
56
f.write(json.dumps({
57
"accuracy": resp.accuracy,
58
"latitude": resp.location.latitude,
59
"longitude": resp.location.longitude,
60
"address": raw_address,
61
"pretty_address": address
62
}, cls=GHuntEncoder, indent=4))
63
gb.rc.print(f"[+] JSON output wrote to {json_file} !", style="italic")
64
65
await as_client.aclose()
66