You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
235 lines
9.1 KiB
235 lines
9.1 KiB
5 years ago
|
"""Sherlock Sites Information Module
|
||
|
|
||
3 years ago
|
This module supports storing information about websites.
|
||
5 years ago
|
This is the raw data that will be used to search for usernames.
|
||
|
"""
|
||
|
import json
|
||
|
import requests
|
||
2 years ago
|
import secrets
|
||
5 years ago
|
|
||
3 years ago
|
class SiteInformation:
|
||
4 years ago
|
def __init__(self, name, url_home, url_username_format, username_claimed,
|
||
2 years ago
|
information, is_nsfw, username_unclaimed=secrets.token_urlsafe(10)):
|
||
5 years ago
|
"""Create Site Information Object.
|
||
|
|
||
3 years ago
|
Contains information about a specific website.
|
||
5 years ago
|
|
||
|
Keyword Arguments:
|
||
|
self -- This object.
|
||
|
name -- String which identifies site.
|
||
|
url_home -- String containing URL for home of site.
|
||
|
url_username_format -- String containing URL for Username format
|
||
|
on site.
|
||
|
NOTE: The string should contain the
|
||
|
token "{}" where the username should
|
||
|
be substituted. For example, a string
|
||
|
of "https://somesite.com/users/{}"
|
||
|
indicates that the individual
|
||
|
usernames would show up under the
|
||
|
"https://somesite.com/users/" area of
|
||
3 years ago
|
the website.
|
||
5 years ago
|
username_claimed -- String containing username which is known
|
||
3 years ago
|
to be claimed on website.
|
||
5 years ago
|
username_unclaimed -- String containing username which is known
|
||
3 years ago
|
to be unclaimed on website.
|
||
5 years ago
|
information -- Dictionary containing all known information
|
||
3 years ago
|
about website.
|
||
5 years ago
|
NOTE: Custom information about how to
|
||
|
actually detect the existence of the
|
||
|
username will be included in this
|
||
|
dictionary. This information will
|
||
|
be needed by the detection method,
|
||
|
but it is only recorded in this
|
||
|
object for future use.
|
||
2 years ago
|
is_nsfw -- Boolean indicating if site is Not Safe For Work.
|
||
5 years ago
|
|
||
|
Return Value:
|
||
|
Nothing.
|
||
|
"""
|
||
|
|
||
3 years ago
|
self.name = name
|
||
|
self.url_home = url_home
|
||
5 years ago
|
self.url_username_format = url_username_format
|
||
5 years ago
|
|
||
3 years ago
|
self.username_claimed = username_claimed
|
||
2 years ago
|
self.username_unclaimed = secrets.token_urlsafe(32)
|
||
3 years ago
|
self.information = information
|
||
2 years ago
|
self.is_nsfw = is_nsfw
|
||
|
|
||
5 years ago
|
return
|
||
|
|
||
|
def __str__(self):
|
||
|
"""Convert Object To String.
|
||
|
|
||
|
Keyword Arguments:
|
||
|
self -- This object.
|
||
|
|
||
|
Return Value:
|
||
|
Nicely formatted string to get information about this object.
|
||
|
"""
|
||
3 years ago
|
|
||
5 years ago
|
return f"{self.name} ({self.url_home})"
|
||
|
|
||
|
|
||
3 years ago
|
class SitesInformation:
|
||
5 years ago
|
def __init__(self, data_file_path=None):
|
||
|
"""Create Sites Information Object.
|
||
|
|
||
3 years ago
|
Contains information about all supported websites.
|
||
5 years ago
|
|
||
|
Keyword Arguments:
|
||
|
self -- This object.
|
||
|
data_file_path -- String which indicates path to data file.
|
||
|
The file name must end in ".json".
|
||
|
|
||
|
There are 3 possible formats:
|
||
|
* Absolute File Format
|
||
|
For example, "c:/stuff/data.json".
|
||
|
* Relative File Format
|
||
|
The current working directory is used
|
||
|
as the context.
|
||
|
For example, "data.json".
|
||
|
* URL Format
|
||
|
For example,
|
||
|
"https://example.com/data.json", or
|
||
|
"http://example.com/data.json".
|
||
|
|
||
|
An exception will be thrown if the path
|
||
|
to the data file is not in the expected
|
||
|
format, or if there was any problem loading
|
||
|
the file.
|
||
|
|
||
|
If this option is not specified, then a
|
||
|
default site list will be used.
|
||
|
|
||
|
Return Value:
|
||
|
Nothing.
|
||
|
"""
|
||
|
|
||
4 years ago
|
if not data_file_path:
|
||
4 years ago
|
# The default data file is the live data.json which is in the GitHub repo. The reason why we are using
|
||
3 years ago
|
# this instead of the local one is so that the user has the most up-to-date data. This prevents
|
||
4 years ago
|
# users from creating issue about false positives which has already been fixed or having outdated data
|
||
|
data_file_path = "https://raw.githubusercontent.com/sherlock-project/sherlock/master/sherlock/resources/data.json"
|
||
|
|
||
|
# Ensure that specified data file has correct extension.
|
||
|
if not data_file_path.lower().endswith(".json"):
|
||
4 years ago
|
raise FileNotFoundError(f"Incorrect JSON file extension for data file '{data_file_path}'.")
|
||
5 years ago
|
|
||
4 years ago
|
# if "http://" == data_file_path[:7].lower() or "https://" == data_file_path[:8].lower():
|
||
3 years ago
|
if data_file_path.lower().startswith("http"):
|
||
4 years ago
|
# Reference is to a URL.
|
||
5 years ago
|
try:
|
||
|
response = requests.get(url=data_file_path)
|
||
|
except Exception as error:
|
||
3 years ago
|
raise FileNotFoundError(
|
||
|
f"Problem while attempting to access data file URL '{data_file_path}': {error}"
|
||
|
)
|
||
|
|
||
|
if response.status_code != 200:
|
||
5 years ago
|
raise FileNotFoundError(f"Bad response while accessing "
|
||
|
f"data file URL '{data_file_path}'."
|
||
3 years ago
|
)
|
||
|
try:
|
||
|
site_data = response.json()
|
||
|
except Exception as error:
|
||
|
raise ValueError(
|
||
|
f"Problem parsing json contents at '{data_file_path}': {error}."
|
||
|
)
|
||
|
|
||
5 years ago
|
else:
|
||
4 years ago
|
# Reference is to a file.
|
||
5 years ago
|
try:
|
||
|
with open(data_file_path, "r", encoding="utf-8") as file:
|
||
|
try:
|
||
|
site_data = json.load(file)
|
||
|
except Exception as error:
|
||
3 years ago
|
raise ValueError(
|
||
|
f"Problem parsing json contents at '{data_file_path}': {error}."
|
||
|
)
|
||
|
|
||
|
except FileNotFoundError:
|
||
5 years ago
|
raise FileNotFoundError(f"Problem while attempting to access "
|
||
|
f"data file '{data_file_path}'."
|
||
3 years ago
|
)
|
||
7 months ago
|
|
||
7 months ago
|
site_data.pop('$schema', None)
|
||
5 years ago
|
|
||
|
self.sites = {}
|
||
|
|
||
3 years ago
|
# Add all site information from the json file to internal site list.
|
||
5 years ago
|
for site_name in site_data:
|
||
|
try:
|
||
5 years ago
|
|
||
5 years ago
|
self.sites[site_name] = \
|
||
|
SiteInformation(site_name,
|
||
|
site_data[site_name]["urlMain"],
|
||
|
site_data[site_name]["url"],
|
||
|
site_data[site_name]["username_claimed"],
|
||
2 years ago
|
site_data[site_name],
|
||
|
site_data[site_name].get("isNSFW",False)
|
||
|
|
||
3 years ago
|
)
|
||
5 years ago
|
except KeyError as error:
|
||
3 years ago
|
raise ValueError(
|
||
|
f"Problem parsing json contents at '{data_file_path}': Missing attribute {error}."
|
||
|
)
|
||
7 months ago
|
except TypeError:
|
||
7 months ago
|
print(f"Encountered TypeError parsing json contents for target '{site_name}' at {data_file_path}\nSkipping target.\n")
|
||
5 years ago
|
|
||
|
return
|
||
|
|
||
7 months ago
|
def remove_nsfw_sites(self, do_not_remove: list = []):
|
||
2 years ago
|
"""
|
||
|
Remove NSFW sites from the sites, if isNSFW flag is true for site
|
||
|
|
||
|
Keyword Arguments:
|
||
|
self -- This object.
|
||
|
|
||
|
Return Value:
|
||
|
None
|
||
|
"""
|
||
2 years ago
|
sites = {}
|
||
7 months ago
|
do_not_remove = [site.casefold() for site in do_not_remove]
|
||
2 years ago
|
for site in self.sites:
|
||
7 months ago
|
if self.sites[site].is_nsfw and site.casefold() not in do_not_remove:
|
||
2 years ago
|
continue
|
||
|
sites[site] = self.sites[site]
|
||
|
self.sites = sites
|
||
2 years ago
|
|
||
4 years ago
|
def site_name_list(self):
|
||
5 years ago
|
"""Get Site Name List.
|
||
|
|
||
|
Keyword Arguments:
|
||
|
self -- This object.
|
||
|
|
||
|
Return Value:
|
||
|
List of strings containing names of sites.
|
||
|
"""
|
||
|
|
||
3 years ago
|
return sorted([site.name for site in self], key=str.lower)
|
||
5 years ago
|
|
||
5 years ago
|
def __iter__(self):
|
||
|
"""Iterator For Object.
|
||
|
|
||
|
Keyword Arguments:
|
||
|
self -- This object.
|
||
|
|
||
|
Return Value:
|
||
|
Iterator for sites object.
|
||
|
"""
|
||
|
|
||
5 years ago
|
for site_name in self.sites:
|
||
|
yield self.sites[site_name]
|
||
5 years ago
|
|
||
|
def __len__(self):
|
||
|
"""Length For Object.
|
||
|
|
||
|
Keyword Arguments:
|
||
|
self -- This object.
|
||
|
|
||
|
Return Value:
|
||
|
Length of sites object.
|
||
|
"""
|
||
|
return len(self.sites)
|