-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresults.py
executable file
·72 lines (63 loc) · 2.22 KB
/
results.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Methods to work with certspotter's results
"""
import argparse
import io
import json
from collections import OrderedDict
from typing import Generator, Union
__all__ = ['read_data']
def read_data(input_data: Union[str, bytes, io.IOBase]) -> Generator[int, None, None]:
"""
Reads and interprets certspotter results from it's stdout output
This is an iterator, yielding a dictionary for each certificate
Parameters:
input_data: string of results or file-like object (bytes or string)
Returns:
result: dictionary with the lines as key-value pairs
ToDo: Keep "raw" data
"""
if isinstance(input_data, str):
iterator = input_data.splitlines()
elif isinstance(input_data, (io.IOBase, argparse.FileType)):
iterator = input_data
else:
raise TypeError('Could not detect how to handle input of type '
'%r.' % type(input_data))
result = OrderedDict()
for line in iterator:
if isinstance(line, bytes):
line = line.decode()
line = line.rstrip()
if not line:
continue
if line.startswith('\t'):
key, value = line.split('=', maxsplit=1)
key = key.strip()
value = value.strip()
if key == 'DNS Name':
if key in result:
result[key].append(value)
else:
result[key] = [value]
elif key == 'Filename':
pass
else:
result[key] = value
else:
if result:
yield result
result = OrderedDict()
result['id'] = line.strip(' :')
if result:
yield result # final block
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('certspotter_results', type=argparse.FileType('r'),
help="certspotter's results file, - for stdin")
parser.add_argument('parsing_results', type=argparse.FileType('w'),
help="parsed results as JSON, - for stdout")
args = parser.parse_args()
json.dump(list(read_data(args.certspotter_results)), args.parsing_results)