forked from danwent/Perspectives-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notary_common.py
93 lines (78 loc) · 3.05 KB
/
notary_common.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# This file is part of the Perspectives Notary Server
#
# Copyright (C) 2011 Dan Wendlandt
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import time
import sqlite3
import os
import subprocess
SSL_SCAN="ssl_scan_openssl.py"
SSH_SCAN="ssh_scan_openssh.py"
def start_scan_probe(sid, notary_db):
host_and_port, service_type = sid.split(",")
if service_type == "2":
first_arg = SSL_SCAN
elif service_type == "1":
first_arg = SSH_SCAN
else:
print >> sys.stderr, "ERROR: invalid service_type for '%s'" % sid
return
nul_f = open(os.devnull,'w')
return subprocess.Popen(["python", first_arg, sid, notary_db], stdout=nul_f , stderr=subprocess.STDOUT )
def parse_config(conf_fname):
config = {}
f = open(conf_fname,'r')
for line in f:
try:
key,value = line.strip().split("=")
config[key] = value
except:
pass
return config
def report_observation(notary_db_file, service_id, fp):
conn = sqlite3.connect(notary_db_file)
report_observation_with_conn(conn, service_id, fp)
conn.commit()
conn.close()
def report_observation_with_conn(conn, service_id, fp):
cur_time = int(time.time())
cur = conn.cursor()
cur.execute("select * from observations where service_id = ?", (service_id,))
most_recent_time_by_key = {}
most_recent_key = None
most_recent_time = 0
for row in cur:
k = row[1]
if k not in most_recent_time_by_key or row[3] > most_recent_time_by_key[k]:
most_recent_time_by_key[k] = row[3]
for k in most_recent_time_by_key:
if most_recent_time_by_key[k] > most_recent_time:
most_recent_key = k
most_recent_time = most_recent_time_by_key[k]
if most_recent_key == fp:
# this key was also the most recently seen key before this observation.
# just update the observation row to set the timespan 'end' value to the
# current time.
conn.execute("update observations set end = ? where service_id = ? and key = ? and end = ?",
(cur_time, service_id, fp, most_recent_time))
else:
# key has changed or no observations exist yet for this service_id. Either way
# add a new entry for this key with timespan start and end set to the current time
conn.execute("insert into observations values (?,?,?,?)",
(service_id, fp, cur_time, cur_time))
if fp != most_recent_key:
# if there was a previous key, set its 'end' timespan value to be current
# time minus one seconds
conn.execute("update observations set end = ? where service_id = ? and key = ? and end = ?",
(cur_time - 1, service_id, most_recent_key, most_recent_time))