From 4ee1c2b9846207e4a9e333215f894d2403547a7b Mon Sep 17 00:00:00 2001 From: joon Date: Sun, 24 Jul 2022 11:46:31 -0700 Subject: [PATCH] Work in progress --- .gitignore | 163 ++++++++++++++++++++++++++++++++++ README.md | 3 + pyproject.toml | 3 + setup.cfg | 17 ++++ wtfis/__init__.py | 0 wtfis/clients/__init__.py | 0 wtfis/clients/passivetotal.py | 46 ++++++++++ wtfis/clients/virustotal.py | 34 +++++++ wtfis/main.py | 32 +++++++ wtfis/models/__init__.py | 0 wtfis/models/passivetotal.py | 21 +++++ wtfis/models/virustotal.py | 50 +++++++++++ 12 files changed, 369 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 pyproject.toml create mode 100644 setup.cfg create mode 100644 wtfis/__init__.py create mode 100644 wtfis/clients/__init__.py create mode 100644 wtfis/clients/passivetotal.py create mode 100644 wtfis/clients/virustotal.py create mode 100644 wtfis/main.py create mode 100644 wtfis/models/__init__.py create mode 100644 wtfis/models/passivetotal.py create mode 100644 wtfis/models/virustotal.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..09e0029 --- /dev/null +++ b/.gitignore @@ -0,0 +1,163 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ + +# VSCode +.vscode/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..a4ddce5 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# WTFis + +Passive domain lookup tool \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..fed528d --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..f962f22 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,17 @@ +[metadata] +name = wtfis +version = 0.0.1 + +[options] +packages = find: +install_requires = + pydantic + requests + tldextract + +[options.entry_points] +console_scripts = + wtfis = wtfis.main:main + +[flake8] +max-line-length = 120 diff --git a/wtfis/__init__.py b/wtfis/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/wtfis/clients/__init__.py b/wtfis/clients/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/wtfis/clients/passivetotal.py b/wtfis/clients/passivetotal.py new file mode 100644 index 0000000..a8fbe08 --- /dev/null +++ b/wtfis/clients/passivetotal.py @@ -0,0 +1,46 @@ +import json +import requests + +from requests.exceptions import HTTPError, JSONDecodeError +# from pydantic import ValidationError +from typing import Optional + +from wtfis.models.passivetotal import Whois + + +class PTClient: + """ + Passivetotal client + """ + baseurl = "https://api.riskiq.net/pt/v2" + + def __init__(self, api_user: str, api_key: str) -> None: + self.s = requests.Session() + self.s.auth = (api_user, api_key) + + def _get(self, request: str, params: Optional[dict] = None) -> Optional[dict]: + try: + resp = self.s.get(self.baseurl + request, params=params) + resp.raise_for_status() + + return json.loads(json.dumps((resp.json()))) + except (HTTPError, JSONDecodeError): + raise + + def passive(self, domain: str) -> dict: + return self._get( + "/dns/passive", + params={ + "query": domain, + }, + ) + + def get_whois(self, domain: str) -> Optional[Whois]: + return Whois.parse_obj( + self._get( + "/whois", + params={ + "query": domain + } + ) + ) diff --git a/wtfis/clients/virustotal.py b/wtfis/clients/virustotal.py new file mode 100644 index 0000000..08dc74f --- /dev/null +++ b/wtfis/clients/virustotal.py @@ -0,0 +1,34 @@ +import json +import requests + +from requests.exceptions import HTTPError, JSONDecodeError +# from pydantic import ValidationError +from typing import Optional + +from wtfis.models.virustotal import Domain + + +class VTClient: + """ + Virustotal client + """ + baseurl = "https://www.virustotal.com/api/v3" + + def __init__(self, api_key: str) -> None: + self.s = requests.Session() + self.s.headers = { + "x-apikey": api_key, + "Accept": "application/json", + } + + def _get(self, request: str) -> Optional[dict]: + try: + resp = self.s.get(self.baseurl + request) + resp.raise_for_status() + + return json.loads(json.dumps((resp.json())))["data"]["attributes"] + except (HTTPError, JSONDecodeError): + raise + + def get_domain(self, domain: str) -> Domain: + return Domain.parse_obj(self._get(f"/domains/{domain}")) diff --git a/wtfis/main.py b/wtfis/main.py new file mode 100644 index 0000000..fc320bd --- /dev/null +++ b/wtfis/main.py @@ -0,0 +1,32 @@ +import datetime +import os +import sys + +from dotenv import load_dotenv +from prompt_toolkit import HTML, print_formatted_text as print + +from wtfis.clients.passivetotal import PTClient +from wtfis.clients.virustotal import VTClient +from wtfis.models.virustotal import Domain + + +def iso_date(unix_time: int) -> str: + return datetime.datetime.utcfromtimestamp(unix_time).isoformat() + + +def main(): + # Load environment variables + load_dotenv() + + # Run + vt = VTClient(os.environ.get("VT_API_KEY")) + domain = Domain.parse_obj(vt.get_domain(sys.argv[1])) + + print(HTML(f"Reputation: {domain.reputation}")) + print(HTML(f"Registrar: {domain.registrar}")) + print(HTML(f"Last DNS Records Date: {iso_date(domain.last_dns_records_date)}")) + + pt = PTClient(os.environ.get("PT_API_USER"), os.environ.get("PT_API_KEY")) + passive = pt.get_whois(sys.argv[1]) + + print(passive) diff --git a/wtfis/models/__init__.py b/wtfis/models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/wtfis/models/passivetotal.py b/wtfis/models/passivetotal.py new file mode 100644 index 0000000..85c4505 --- /dev/null +++ b/wtfis/models/passivetotal.py @@ -0,0 +1,21 @@ +from pydantic import BaseModel +from typing import List + + +class Registrant(BaseModel): + organization: str + email: str + name: str + telephone: str + + +class Whois(BaseModel): + contactEmail: str + expiresAt: str + name: str + nameServers: List[str] + organization: str + registered: str + registrant: Registrant + registrar: str + registryUpdatedAt: str diff --git a/wtfis/models/virustotal.py b/wtfis/models/virustotal.py new file mode 100644 index 0000000..3f4fe7a --- /dev/null +++ b/wtfis/models/virustotal.py @@ -0,0 +1,50 @@ +from pydantic import BaseModel +from typing import Dict, List, Optional + + +class AnalysisResult(BaseModel): + category: str + engine_name: str + method: str + result: str + + +class LastAnalysisResults(BaseModel): + __root__: Dict[str, AnalysisResult] + + +class LastAnalysisStats(BaseModel): + harmless: int + malicious: int + suspicious: int + timeout: int + undetected: int + + +class Popularity(BaseModel): + rank: int + timestamp: int + + +class PopularityRanks(BaseModel): + __root__: Dict[str, Popularity] + + +class Domain(BaseModel): + """ + Essential VT domain fields + """ + creation_date: int + jarm: str + last_analysis_results: LastAnalysisResults + last_analysis_stats: LastAnalysisStats + last_dns_records_date: int + last_https_certificate_date: int + last_modification_date: int + last_update_date: int + popularity_ranks: PopularityRanks + registrar: str + reputation: int + tags: List[str] + whois: str + whois_date: Optional[int]