summaryrefslogtreecommitdiff
path: root/tools/josef_nagios_auditor.py
blob: 05390f82b82a3a0717b25e587d5c2691b29ce657 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/python
# -*- coding: utf-8 -*-     

import time
import datetime
import base64
import argparse
import errno
from certtools import *

NAGIOS_OK = 0
NAGIOS_WARN = 1
NAGIOS_CRIT = 2
NAGIOS_UNKNOWN = 3

parser = argparse.ArgumentParser(description="")
parser.add_argument('--audit', action='store_true', help="run lightweight auditor verifying consistency in STH")
parser.add_argument('--build-sth', action='store_true', help="get all entries and construct STH")
parser.add_argument('--baseurl', required=True, help="Base URL for CT log")
parser.add_argument('--sthfile',
                    required=True,
                    metavar='file',
                    help="File containing current STH")
parser.add_argument('--keyfile',
                    metavar='file',
                    required=True,
                    help="File containing current STH")

class UTC(datetime.tzinfo):
    def utcoffset(self, dt):
      return datetime.timedelta(hours=0)
    def dst(self, dt):
        return datetime.timedelta(0)

def reduce_layer(layer):
    new_layer = []
    while len(layer) > 1:
        e1 = layer.pop(0)
        e2 = layer.pop(0)
        new_layer.append(internal_hash((e1,e2)))
    return new_layer

def reduce_tree(entries, layers):
    if len(entries) == 0 and layers is []:
        return [[hashlib.sha256().digest()]]
  
    layer_idx = 0
    layers[layer_idx] += entries

    while len(layers[layer_idx]) > 1:
        if len(layers) == layer_idx + 1:
            layers.append([])

        layers[layer_idx + 1] += reduce_layer(layers[layer_idx]) 
        layer_idx += 1
    return layers

def reduce_subtree_to_root(layers):
    while len(layers) > 1:
        if len(layers[1]) == 0:
            layers[1] = layers[0]
        else:
            layers[1] += next_merkle_layer(layers[0])
        del layers[0]

    if len(layers[0]) > 1:
        return next_merkle_layer(layers[0])
    return layers[0]

def get_and_verify_sth(url, key):
    try:
        sth =  get_sth(url)
    except:
        print time.strftime('%H:%M:%S') + " ERROR: Failed to retrieve STH from " + url
        sys.exit(NAGIOS_CRIT)

    # Check signature on the STH
    try:
        check_sth_signature(url, sth, key)
        # write_file("plausible-sth.json", tmp_sth)
    except:
        error_str = time.strftime('%H:%M:%S') + " ERROR: Could not verify signature from " + url
        print error_str
        sys.exit(NAGIOS_CRIT)
    return sth

def fetch_all_sth():
    sths = {}
    for base_url in base_urls:
        # Fetch STH
        try:
            sths[base_url] =  get_sth(base_url)
        except:
            sths[base_url] = None
            error_str = time.strftime('%H:%M:%S') + " ERROR: Failed to retrieve STH from " + base_url
            print error_str
            errors.append(error_str)
            continue

        # Check signature on the STH
        try:
            check_sth_signature(base_url, sths[base_url], logkeys[base_url])
        except:
            error_str = time.strftime('%H:%M:%S') + " ERROR: Could not verify signature from " + base_url
            print error_str
            errors.append(error_str)
            continue

    return sths

def verify_progress(url, old, new):
    if old and new:
        if new["tree_size"] == old["tree_size"]:
            if old["sha256_root_hash"] != new["sha256_root_hash"]:
                print time.strftime('%H:%M:%S') + " CRITICAL: root hash is different for same tree size in " + url
                sys.exit(NAGIOS_CRIT)

        elif new["tree_size"] < old["tree_size"]:
            print time.strftime('%H:%M:%S') + " CRITICAL: new tree smaller than previous tree (%d < %d)" % \
              (new["tree_size"], old["tree_size"])
            sys.exit(NAGIOS_WARN)
    if new:
        age = time.time() - new["timestamp"]/1000
        sth_time = datetime.datetime.fromtimestamp(new['timestamp'] / 1000, UTC()).strftime("%Y-%m-%d %H:%M:%S")
        roothash = new['sha256_root_hash']
        if age > 24 * 3600:
            print time.strftime('%H:%M:%S') + " CRITICAL: %s is older than 24h: %s UTC" % (url, sth_time)
            sys.exit(NAGIOS_CRIT)
        elif age > 12 * 3600:
            print time.strftime('%H:%M:%S') + " WARNING: %s is older than 12h: %s UTC" % (url, sth_time)
            sys.exit(NAGIOS_WARN)
        elif age > 6 * 3600:
            print time.strftime('%H:%M:%S') + " WARNING: %s is older than 6h: %s UTC" % (url, sth_time)
            sys.exit(NAGIOS_WARN)

def verify_consistency(url, old, new):
    if old and new:
        try:
            if old["tree_size"]!= new["tree_size"]:
                consistency_proof = get_consistency_proof(url, old["tree_size"], new["tree_size"])
                decoded_consistency_proof = []
                for item in consistency_proof:
                    decoded_consistency_proof.append(base64.b64decode(item))
                res = verify_consistency_proof(decoded_consistency_proof, old["tree_size"], new["tree_size"], old["sha256_root_hash"])
                
                if old["sha256_root_hash"] != str(base64.b64encode(res[0])):
                    print time.strftime('%H:%M:%S') + " Verification of old hash failed! " + old["sha256_root_hash"] + str(base64.b64encode(res[0]))
                    sys.exit(NAGIOS_CRIT)
                    # errors.append(time.strftime('%H:%M:%S') + " ERROR: Failed to verify consistency for " + url + ", tree size " + old[url]["tree_size"])
                elif new[url]["sha256_root_hash"] != str(base64.b64encode(res[1])):
                    print time.strftime('%H:%M:%S') + " Verification of new hash failed! " + new["sha256_root_hash"] + str(base64.b64encode(res[1]))
                    sys.exit(NAGIOS_CRIT)
                    # errors.append(time.strftime('%H:%M:%S') + " ERROR: Failed to verify consistency for " + url + ", tree size " + new[url]["tree_size"])
                else:
                    print time.strftime("%H:%M:%S") + " New STH from " + url + ", timestamp: " + \
                        str(new["timestamp"]) + ", size: " + str(new["tree_size"]) + "...OK."

        except:
          print time.strftime('%H:%M:%S') + " ERROR: Could not verify consistency for " + url
          sys.exit(NAGIOS_CRIT)

def verify_inclusion_all(url, old, new):
    if old and new:
        try:
            if old["tree_size"]!= new["tree_size"]:
                entries = []
                while len(entries) + old[url]["tree_size"]!= new[url]["tree_size"]:
                    entries += get_entries(url, str(int(old[url]["tree_size"]) + len(entries)), new[url]["tree_size"] -1)["entries"]

                success = True
                for i in entries:
                    h = get_leaf_hash(base64.b64decode(i["leaf_input"]))
                    if not verify_inclusion_by_hash(url, h):
                        success = False

                if not success:
                    # print time.strftime("%H:%M:%S") + " Verifying inclusion for " + str(len(entries)) + " new entries in " + url + " ...OK"
                # else:
                    print time.strftime('%H:%M:%S') + " ERROR: Failed to prove inclusion of all new entries in " + url
                    sys.exit(NAGIOS_CRIT)
                    # errors.append(time.strftime('%H:%M:%S') + " ERROR: Failed to prove inclusion of all new entries in " + url)
        except:
            print time.strftime('%H:%M:%S') + " ERROR: Failed to prove inclusion of all new entries in " + url
            # errors.append(time.strftime('%H:%M:%S') + " ERROR: Failed to prove inclusion of all new entries in " + url)
            sys.exit(NAGIOS_CRIT)

def fetch_and_build_tree(old_sth, base_url):
    sth = old_sth[base_url]
    subtree = [[]]
    idx = 0

    res_strings = [""]

    print time.strftime('%H:%M:%S') + " Getting all entries from " + base_url
    while idx < sth["tree_size"]:
        pre_size = idx
        entries = get_entries(base_url, idx, sth["tree_size"]-1)["entries"]
        
        new_leafs = []
        for item in entries:
            new_leafs.append(get_leaf_hash(base64.b64decode(item["leaf_input"])))
        idx += len(new_leafs)
        print time.strftime('%H:%M:%S') + " Got entries " + str(pre_size) + " to " + str(idx) + " from " + base_url
        subtree = reduce_tree(new_leafs, subtree)

    root = base64.b64encode(reduce_subtree_to_root(subtree)[0])

    if root == sth["sha256_root_hash"]:
        print time.strftime('%H:%M:%S') + " Verifying root hashes for " + base_url + "...OK."
        res_strings.append("STH for " + base_url + " built successfully.")
    else:
        print time.strftime('%H:%M:%S') + " ERROR: Failed to verify root hashes! STH root: " + sth["sha256_root_hash"] + ", Tree root: " + root
        res_strings.append(time.strftime('%H:%M:%S') + " " + base_url + " Failed! STH root: " + sth["sha256_root_hash"] + " Calculated root: " + root)
        errors.append(time.strftime('%H:%M:%S') + " ERROR: Failed to verify root hash for " + base_url + ", tre size " + sth["tree_size"])

    for item in res_strings:
        print item + "\n"

def verify_inclusion_by_hash(base_url, leaf_hash):
    try: 
        tmp_sth = get_sth(base_url)
        proof = get_proof_by_hash(base_url, leaf_hash, tmp_sth["tree_size"])

        decoded_inclusion_proof = []
        for item in proof["audit_path"]:
            decoded_inclusion_proof.append(base64.b64decode(item))
        
        root = base64.b64encode(verify_inclusion_proof(decoded_inclusion_proof, proof["leaf_index"], tmp_sth["tree_size"], leaf_hash))

        if tmp_sth["sha256_root_hash"] == root:
            # print "Verifying inclusion for entry " + str(proof["leaf_index"]) + " in " + base_url + "...OK."
            return True
        else:
            print time.strftime('%H:%M:%S') + " ERROR: Could not prove inclusion for entry " + str(proof["leaf_index"]) + " in " + base_url
            errors.append(time.strftime('%H:%M:%S') + " ERROR: Could not prove inclusion for entry " + str(proof["leaf_index"]) + " in " + base_url)
            return False
    except:
        print time.strftime('%H:%M:%S') + " ERROR: Could not prove inclusion for hashed entry in " + base_url
        errors.append(time.strftime('%H:%M:%S') + " ERROR: Could not prove inclusion for hashed entry in " + base_url)
        return False

def verify_inclusion_by_index(base_url, index):
    try: 
        tmp_sth = get_sth(base_url)
        proof = get_proof_by_index(base_url, index, tmp_sth["tree_size"])

        decoded_inclusion_proof = []
        for item in proof["audit_path"]:
            decoded_inclusion_proof.append(base64.b64decode(item))

        root = base64.b64encode(verify_inclusion_proof(decoded_inclusion_proof, index, tmp_sth["tree_size"], get_leaf_hash(base64.b64decode(proof["leaf_input"]))))

        if tmp_sth["sha256_root_hash"] == root:
            print time.strftime('%H:%M:%S') + " Verifying inclusion for entry " + str(index) + " in " + base_url + "...OK."
        else:
            print time.strftime('%H:%M:%S') + " ERROR: Could not prove inclusion for entry " + str(index) + " in " + base_url
            errors.append(time.strftime('%H:%M:%S') + " ERROR: Could not prove inclusion for entry " + str(index) + " in " + base_url)
    except:
        print time.strftime('%H:%M:%S') + " ERROR: Could not prove inclusion for entry " + str(index) + " in " + base_url
        errors.append(time.strftime('%H:%M:%S') + " ERROR: Could not prove inclusion for entry " + str(index) + " in " + base_url)

def get_proof_by_index(baseurl, index, tree_size):
    try:
        params = urllib.urlencode({"leaf_index":index,
                                   "tree_size":tree_size})
        result = \
          urlopen(baseurl + "ct/v1/get-entry-and-proof?" + params).read()
        return json.loads(result)
    except urllib2.HTTPError, e:
        print "ERROR:", e.read()
        sys.exit(0)

def get_all_roots(base_url):
    # print "Fetching roots from " + base_url
    result = urlopen(base_url + "ct/v1/get-roots").read()
    certs = json.loads(result)["certificates"]
    print time.strftime('%H:%M:%S') + " Received " + str(len(certs)) + " certs from " + base_url

    for accepted_cert in certs:
        subject = get_cert_info(base64.decodestring(accepted_cert))["subject"]
        issuer = get_cert_info(base64.decodestring(accepted_cert))["issuer"]
        if subject == issuer:
            root_cert = base64.decodestring(accepted_cert)
            print get_cert_info(root_cert)["subject"]

def read_sth(fn):
    try:
        f = open(fn)
    except IOError, e:
        if e.errno == errno.ENOENT:
            return None
        raise e
    return json.loads(f.read())

def write_file(fn, sth):
    tempname = fn + ".new"
    open(tempname, 'w').write(json.dumps(sth))
    mv_file(tempname, fn)


def main(args):
    try:
        log_key = get_public_key_from_file(args.keyfile)
    except:
        print time.strftime('%H:%M:%S') + " ERROR: Failed to load keyfile " + args.keyfile
        sys.exit(NAGIOS_WARN)

    old_sth = read_sth(args.sthfile)
    new_sth =  get_and_verify_sth(args.baseurl, log_key)
    write_file(args.sthfile, new_sth)

    verify_progress(args.baseurl, old_sth, new_sth)

    verify_consistency(args.baseurl, old_sth, new_sth)

    verify_inclusion_all(args.baseurl, old_sth, new_sth)

    print "Everything OK from " + args.baseurl 
    sys.exit(NAGIOS_OK)


if __name__ == '__main__':
    main(parser.parse_args())