· 5 years ago · Jun 24, 2020, 05:26 PM
1## use libraries
2import json
3import pymongo
4import sys
5from just_config.configuration import Configuration
6import requests
7import os
8import sys
9import shutil
10from datetime import datetime
11import difflib
12import logzero
13import logging
14from logzero import logger
15from colorama import init
16from termcolor import colored
17import time
18
19## read config file
20currentPot = "current.potfile"
21oldPot = "old.potfile"
22newSites = "newsites.txt"
23MAClist = []
24wifiList = []
25
26init()
27
28now = datetime.now()
29
30logzero.logfile("logs/" + str(now.strftime("%d-%m-%Y--%H-%M")) + ".log")
31formatter = logging.Formatter('%(asctime)-15s - %(levelname)s: %(message)s')
32logzero.formatter(formatter)
33logger.info("Script started")
34
35cfg = Configuration()
36
37logger.info("Initialization done")
38
39## check if everything is set up correctly
40if(cfg["wpasecKey"] == None):
41 logger.error("wpa-sec API key not set. Please set your key in 'default.ini' file and try again")
42 sys.exit()
43elif(cfg["wigleKey"] == None):
44 logger.error("WiGLE API key not set. Please set your key in 'default.ini' file and try again")
45 sys.exit()
46elif(cfg["mongoDB_URI"] == None):
47 logger.error("MongoDB URI not set. Please set your URI in 'default.ini' file and try again")
48 sys.exit()
49
50## Downlaod potfile from wpa-sec
51logger.info("Trying to download file...")
52
53cookie = {"key": cfg["wpasecKey"]}
54url = "https://wpa-sec.stanev.org/?api&dl=1"
55header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"}
56
57try:
58 req = requests.get(url, cookies=cookie)
59 logger.debug("Request made with header " + str(header) + " to URL " + str(url))
60 status = req.raise_for_status()
61except requests.exceptions.HTTPError as e:
62 logger.error(e)
63
64with open("temp.potfile", "wb") as f:
65 f.write(req.content)
66
67logger.info("File downloaded succesfully as 'temp.potfile'")
68
69## move content of current.pofile to old.potfile
70logger.info("Moving 'current.potfile' to 'old.potfile' and 'temp.potfile' to 'current.potfile'")
71
72now = datetime.now()
73
74# create archived potfile, rename it and move
75archivedFileName = str(now.strftime("%d-%m-%Y--%H-%M")) + "_archived.potfile"
76os.rename(oldPot, archivedFileName)
77shutil.move(archivedFileName, "archive/" + archivedFileName)
78
79# rename current potfile to old potfile
80os.rename(currentPot, oldPot)
81
82# rename temp potfile (downloaded) to current potfile
83os.rename("temp.potfile", currentPot)
84
85logger.info("Potfiles succesfully swapped")
86
87## Compare to last sessions
88logger.info("Comparing files...")
89
90with open(oldPot, "r") as file1:
91 with open(currentPot, "r") as file2:
92 diff = difflib.unified_diff(file1.readlines(), file2.readlines(), fromfile=oldPot, tofile=currentPot,)
93 with open("newsites.txt", "w") as outF:
94 for item in diff:
95 if(item[0:1] == "+"):
96 logger.debug("New site: " + str(item))
97 outF.write(item[1:])
98
99logger.info("Comparison done, new sites written to 'newsites.txt'")
100
101## Parse list, remove unnecessary shit, add ":" to MAC address
102logger.info("Parsing 'newsites.txt', creating non-complete wifiOBJ...")
103
104f = open(newSites, "r", encoding="utf-8")
105content = f.readlines()[1:]
106
107for item in content:
108 splitContent = item.split(":")
109 mac = splitContent[0]
110 mac = ":".join(mac[i:i+2] for i in range(0, len(mac), 2))
111 mac = mac.upper()
112 ssid = splitContent[2]
113 if(splitContent[3][-1] == "\n"):
114 password = splitContent[3][:-1]
115 else:
116 password = splitContent[3]
117
118 MAClist.append(mac)
119 wifiOBJ = {"SSID": ssid, "password": password, "MAC": mac}
120 wifiList.append(wifiOBJ)
121 logging.debug("Parsed newsites, resulting wifiOBJ: " + str(wifiOBJ))
122
123logger.info("File parsed sucesfully")
124
125## Contact WiGLE API, get all necessary info
126logger.info("Requesting WiGLE API for additional data...")
127
128wigleURL = "https://api.wigle.net/api/v2/network/detail"
129
130iterator = 0
131for site in MAClist:
132 try:
133 res = requests.get(wigleURL, headers={"Authorization": "Basic " + cfg["wigleKey"][1:93]}, params={"netid": site})
134 jsonRes = res.json()
135 status = res.raise_for_status()
136 logger.debug(res.text)
137 lat = jsonRes["results"][0]["trilat"]
138 lon = jsonRes["results"][0]["trilong"]
139 wifiList[iterator].update({"position": [lat, lon]})
140 logger.debug("Got info from WiGLE for " + site)
141 iterator+=1
142 time.sleep(2)
143 except requests.exceptions.HTTPError as e:
144 logger.error(e)
145
146logger.info("WiGLE requests succesfull, data appended to wifiOBJ")
147
148## Construct WiFiOBJ, save to a file
149logger.info("Saving wifiOBJ to a file...")
150
151for item in wifiList:
152 item.update({"WPS": "", "notes": "", "author": cfg["author"]})
153
154# with open("wifiOBJ.json", "wb") as f:
155# for entry in wifiList:
156# json.dump(entry, f)
157
158logger.info("Write succesfull")
159
160## Connect to a mongoDB
161logger.info("Connecting to mongoDB...")
162
163client = pymongo.MongoClient(cfg["mongoDB_URI"])
164db = client[cfg["mongoDB_DB"]]
165collection = db[cfg["mongoDB_collection"]]
166
167logger.info("Connected succesfully")
168
169## Insert all new sites into database
170logger.info("Inserting new WiFis into database...")
171
172for item in wifiList:
173 collection.insert_one(item)
174 logger.debug("MAC " +item["MAC"] + " inserted into dabatase")
175
176logger.info("All networks inserted succesfully")
177
178## Check and delete duplicit sites
179logger.info("Deleting duplicates...")
180
181ids = []
182macs = []
183
184for x in collection.find():
185 macs.append(x["MAC"])
186 ids.append(x["_id"])
187
188seen = {}
189dupes = []
190
191for x in macs:
192 if x not in seen:
193 seen[x] = 1
194 else:
195 if seen[x] == 1:
196 dupes.append(x)
197 seen[x] += 1
198
199for a in range(len(dupes)):
200 dbQ = {"_id": ids[macs.index(dupes[a])]}
201 collection.delete_one(dbQ)
202 logging.debug("Deleted duplicate " + str(ids[macs.index(dupes[a])]) + " and MAC:", macs[a])
203
204logger.info("All dupes deleted")
205logger.info("Script is done. Exiting...")