Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite #29

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# Created by https://www.gitignore.io/api/code,python,windows
# Edit at https://www.gitignore.io/?templates=code,python,windows

tox.ini
.gitignore

### Code ###
.vscode/*

Expand Down Expand Up @@ -142,4 +145,4 @@ TransportSecurity
wordlist.txt
.venv

flake8.xml
flake8.xml
194 changes: 0 additions & 194 deletions hstsparser.py

This file was deleted.

42 changes: 42 additions & 0 deletions hstsparser/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
HSTSParser: Parse browser HSTS databases into forensic artifacts.

Usage:
hstsparser table [--firefox|--chrome] [--sort=<column>] [<path>]
hstsparser -h | --help
hstsparser --version

Options:
-h --help Show this screen.
--version Show version.
path Folder to search, or path to HSTS file.
--sort=<column> Sort a specified column [default: Last Accessed].
"""

__version__ = '1.2.0'

import importlib
import platform

from docopt import docopt

from hstsparser.utils import HSTSReader


system = platform.system()

if system == 'Windows':
win32api = importlib.import_module('win32api')
*drives, _ = win32api.GetLogicalDriveStrings().split('\000')
print(drives)


def main() -> None:
"""Entry point for the command-line alias."""
arguments = docopt(__doc__, version=f'hstsparser {__version__}')
browser = 'firefox'
if browser == 'chrome':
pass
else:
pass
print(arguments.get('--sort'))
5 changes: 5 additions & 0 deletions hstsparser/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Executed on 'python -m hstsparser'."""

from hstsparser import main

main()
17 changes: 17 additions & 0 deletions hstsparser/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Constants and Enums for use in HSTSReaders."""


class Paths:
"""Location of HSTS databases."""

class Windows:
"""Location of HSTS Files on Windows."""

CHROME = r'Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*\SiteSecurityServiceState.txt'
FIREFOX = r'Users\*\AppData\Local\Google\Chrome\User Data\TransportSecurity'

class Linux:
"""Location of HSTS Files on Linux."""

CHROME = 'home/*/.config/google-chrome*/Default/TransportSecurity'
FIREFOX = 'home/*/.mozilla/firefox/*/SiteSecurityServiceState.txt'
49 changes: 49 additions & 0 deletions hstsparser/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Utilities for reading and parsing HSTS databases."""

import re
from datetime import datetime, timedelta
from pathlib import Path
from typing import Iterator, TextIO, Union


class HSTSReader:
"""Implements a HSTS database reader."""

def __init__(self, file: Union[str, Path], browser: str) -> None:
"""Initialise attributes."""
if type(file) is str:
file = Path(file).expanduser()
self.file = file
self.browser = browser

@staticmethod
def chrome(file: TextIO) -> Iterator:
"""Read a chrome database."""
...

@staticmethod
def firefox(file: TextIO) -> Iterator:
"""Read a firefox database."""
pattern = re.compile(r'\s+|:|,')
for line in file:
record = pattern.split(line)
domain, htype, visits, access, expires, _, subdomains, *_ = record
expiry = int(expires) / 1000 if expires.isnumeric() else 32503680000
yield [
domain.split('^')[0],
int(visits),
datetime.utcfromtimestamp(0) + timedelta(int(access)),
datetime.fromtimestamp(expiry),
htype,
'No' if subdomains == '0' else 'Yes'
]

def __iter__(self) -> Iterator:
"""Return the relevant iterator."""
with self.file.open() as file:
if self.browser == 'firefox':
yield from self.firefox(file)
elif self.browser == 'chrome':
yield from self.chrome(file)
else:
raise Exception
Loading