Skip to content

Normalizing Data

Joe edited this page Jul 4, 2020 · 22 revisions

There are two options when normalizing data, you can use the leakdb-curator program or the normalizer.py Python script. Both normalizers are responsible for parsing various formats and outputs a standard format (newline delimited JSON) that is used by the rest of the LeakDB tool set. Generally when normalizing datasets the performance limiting factor will be your disk speed, so there shouldn't be a significant performance difference between using the Python and Go versions. The normalized format is newline-delimited JSON or JSONL, or more simply one JSON object per-line:

{"email": "johndoe@example.com", "domain": "example.com", "password": "monkey123", "user": "johndoe"}
{"email": "janedoe@example.com", "domain": "example.com", "password": "hunter2", "user": "janedoe"}

The domain and user are generally based on splitting the email, but a parser could specify alternate values if present in a leak.

LeakDB Curator Example

There is a normalizer built into the leakdb-curator program:

./leakdb-curator normalize --format colon-newline --recursive --target ./antipublic/ --output ./antipublic.json

However, if you need to quickly add new formats it may be easier to extend the Python implementation.

Python Script Example

Here is a basic example of recursively parsing a directory ./antipublic full of colon/newline delimited files and outputting the results (of all files) into ./antipublic.json:

./normalizer.py --format colon-newline --recursive --target ./antipublic/ --output ./antipublic.json

You can also use --skip-prefix and --skip-suffix to skip certain files (e.g., .zip) in the target directory.

Once you've normalized all of your data, you'll want to decide on a serverless or server deployment, see their respective pages for details.

Adding Parsers (Python)

Additional or custom format parsers can quickly be added to the normalizer script. To start, create a new subclass of the Parser class. The sub-classes should have name (string), a pattern (regex), and implement a parse_line() function, which is called once per line in an input file if the line matches the pattern regex.

For example, say we want to parse the following format:

johndoe@gmail.com:hunter2
janedoe@gmail.com:monkey3

As we can see the format is the email address and password separated with a colon : and delimited with a newline \n:

Colon/Newline Parser

class ColonNewlineParser(Parser):
    ''' Parses colon/newline delimited text files '''

    name = 'colon-newline'
    pattern = re.compile(Parser.email_regex+r':'+Parser.password_regex)

    def parse_line(self, line):
        ''' Parse a colon newline delimited line '''
        if not self.pattern.match(line):
            return None
        parts = line.split(':')
        email = parts[0]
        password = ''.join(parts[1:])
        if '@' in email:
            user, domain = email.split('@')
            return email, user, domain, password
  • name - The name of the parser
  • pattern - Should be a regex matched against each line of a file, if the line does not match the pattern you should return None. The parent class Parser implements a few common regexes that can be reused e.g., Parser.email_regex.
  • parse_line - This function is called for each line in a file. It should parse the line and return email, user, domain and password, or None if the line is invalid. If the file is not newline delimited you can also overload the parse_file function (which calls parse_line).

Lastly add your new class to the PARSERS dictionary with the name as the key and the class itself as the value:

PARSERS = {
    ColonNewlineParser.name: ColonNewlineParser,
    SemicolonNewlineParser.name: SemicolonNewlineParser,
    WhitespaceParser.name: WhitespaceParser,
}

Your parser should now show up as a valid --format, which you can see using --help:

$ ./normalizer.py --help
usage: normalizer.py [-h] --target [TARGETS [TARGETS ...]] [--output OUTPUT]
                     --format {colon-newline,semicolon-newline,whitespace}
                     [--append] [--recursive] [--skip-suffix SKIP_SUFFIX]
                     [--skip-prefix SKIP_PREFIX]

Normalizes leaked database files for use in BigQuery

optional arguments:
  -h, --help            show this help message and exit
  --target [TARGETS [TARGETS ...]], -t [TARGETS [TARGETS ...]]
                        file or directory with leak file(s)
  --output OUTPUT, -o OUTPUT
                        output file with hooks (default: cwd)
  --format {colon-newline,semicolon-newline,whitespace}, -f {colon-newline,semicolon-newline,whitespace}
                        file format
  --append, -a          append output file (default: false)
  --recursive, -r       recursively scan directories (default: false)
  --skip-suffix SKIP_SUFFIX, -s SKIP_SUFFIX
                        skip files with a given suffix
  --skip-prefix SKIP_PREFIX, -S SKIP_PREFIX
                        skip files with a given prefix

Clone this wiki locally