-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
131 lines (111 loc) · 4.26 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import re
import time
import Gmail
import asyncio
import requests
import platform
import subprocess
from notifypy import Notify
from threading import Thread
from winsdk.windows.devices.geolocation import Geolocator
def mac_address()->list:
response = subprocess.check_output(["arp", "-a"]).decode()
pattern = r"\w{2}-\w{2}-\w{2}-\w{2}-\w{2}-\w{2}"
mac_address = re.findall(pattern, response)
return mac_address
class Device:
def __init__(self) -> None:
self.coordinates = None
self.device_name = None
self.ip_address = None
self.isp = None
self.wifi_name = None
async def getGPSLocation():
locator = Geolocator()
pos = await locator.get_geoposition_async()
return {
"latitude": pos.coordinate.latitude,
"longitude": pos.coordinate.longitude,
}
def getGPSCoordinates(self):
try:
self.coordinates = asyncio.run(Device.getGPSLocation())
except PermissionError:
self.coordinates = {"latitude": None, "longitude": None}
def getIPAddress(self):
url = "https://ip-address-tracker-free.p.rapidapi.com/rapidapi/ip"
headers = {
"content-type": "application/octet-stream",
"X-RapidAPI-Key": "419d81dfdcmsh2f12208b24c1764p1394f9jsnd4b971252d66",
"X-RapidAPI-Host": "ip-address-tracker-free.p.rapidapi.com"
}
response = requests.get(url, headers=headers).json()
self.ip_address = response.get("ip")
self.isp = response.get("isp")
def googleMapLink(self, coordinates: dict) -> str:
url = f"https://www.google.com/maps/search/?api=1&query={coordinates.get('latitude')},{coordinates.get('longitude')}"
return url
def getDeviceName(self):
self.device_name = platform.node()
def getWifiName(self):
response = subprocess.check_output(['netsh', 'wlan', 'show', 'interface']).decode()
pattern = r"SSID\s+:\s[0-9a-zA-Z ]+"
result = re.findall(pattern, response)[0]
wifi_name = result.split(":")
self.wifi_name = wifi_name[1].strip()
def getDeviceData(self) -> dict:
t1 = Thread(target=self.getIPAddress)
t2 = Thread(target=self.getGPSCoordinates)
t3 = Thread(target=self.getDeviceName)
t4 = Thread(target = self.getWifiName)
t1.start(), t2.start(), t3.start(), t4.start()
macaddress = mac_address()[0]
t2.join()
url = self.googleMapLink(self.coordinates)
t1.join(), t3.join(), t4.join()
data = {
"device_name": self.device_name,
"ip_address": self.ip_address,
"mac_address": macaddress,
"map_url": url,
"isp": self.isp,
"wifi_name": self.wifi_name
}
return data
async def anonymous_network(verified_mac_address:tuple):
while True:
mac = mac_address()
if len(mac)!=0:
if mac[0] not in verified_mac_address:
return True
time.sleep(1)
def Notificaton():
notification = Notify()
notification.application_name = "Warning"
notification.icon = "alert.png"
notification.message = "We have detected unusual network activity from your device. To protect your network, please turn off your Wi-Fi. An email has been sent to Admin"
notification.title = ""
notification.send()
if __name__ == "__main__":
connection = False
# tuple of verified mac addresses
verified_mac_addresses = ('d2-20-41-ef-8b-15', "d2-20-41-ef-8b-14")
while True:
if connection is False:
response = asyncio.run(anonymous_network(verified_mac_addresses))
if response is True:
connection = True
credentials = {
"sender" : "", # Sender email address
"receiver" : "", # Receiver email address
"password" : "" # Password
}
thread = Thread(target= Notificaton)
thread.start()
device_info = Device().getDeviceData()
Gmail.sendGmail(device_info, credentials)
thread.join()
else:
mac = mac_address()
if len(mac) == 0:
connection = False