mirror of
https://github.com/adulau/netbeacon.git
synced 2024-11-23 02:27:08 +00:00
-t and -s added (time and sequence check)
-s option is checking the expected sequence value and store the last seen value in a persistent shelve. The sequence is an incremented unsigned integer value. -t option to check time delta or not
This commit is contained in:
parent
40aeeb4fe3
commit
6544699165
1 changed files with 36 additions and 3 deletions
39
nb_verify.py
39
nb_verify.py
|
@ -2,6 +2,7 @@ import socket
|
|||
import datetime
|
||||
import time
|
||||
import sys
|
||||
from optparse import OptionParser
|
||||
|
||||
try:
|
||||
from hashlib import sha1
|
||||
|
@ -37,6 +38,30 @@ def deltafromnow(epoch=None):
|
|||
now = time.mktime(t.timetuple())
|
||||
return now-epoch
|
||||
|
||||
def validateseq(seq=None, update=True):
|
||||
if seq is None:
|
||||
return False
|
||||
if not 'seq' in s:
|
||||
s['seq'] = seq
|
||||
return True
|
||||
elif seq == (s['seq']+1):
|
||||
s['seq'] = s['seq'] + 1
|
||||
return s['seq']
|
||||
else:
|
||||
return False
|
||||
|
||||
usage = "usage: %prog [options] <netbeacon messages>"
|
||||
parser = OptionParser(usage)
|
||||
#parser.add_option("-i","--id", dest="id", help="id of the netbeacon message processed")
|
||||
parser.add_option("-t","--timedelta",dest="timedelta", action='store_true', help="show timedelta")
|
||||
parser.add_option("-s","--storeseq", dest="storeseq", action='store_true', help="store sequence and validate sequence")
|
||||
|
||||
(options, args) = parser.parse_args()
|
||||
|
||||
if options.storeseq:
|
||||
import shelve
|
||||
s = shelve.open("netbeacon.seq")
|
||||
|
||||
for line in sys.stdin:
|
||||
line = line.rstrip()
|
||||
m = {}
|
||||
|
@ -45,9 +70,17 @@ for line in sys.stdin:
|
|||
message = m['header']+";"+str(m['epoch'])+";"+str(m['sequence'])+";"
|
||||
if m['hmac'] == nbsign(message=message):
|
||||
print "valid signature for "+message
|
||||
timedelta = deltafromnow(epoch=m['epoch'])
|
||||
print "Time delay "+str(timedelta)
|
||||
if options.timedelta:
|
||||
timedelta = deltafromnow(epoch=m['epoch'])
|
||||
print "Time delay "+str(timedelta)
|
||||
if options.storeseq:
|
||||
seq = validateseq(seq=m['sequence'])
|
||||
if seq:
|
||||
print "Sequence ok "+str(seq)
|
||||
else:
|
||||
print "Sequence nok - received ("+str(m['sequence'])+") expected ("+str(s['seq']+1)+")"
|
||||
else:
|
||||
print "(!) invalid signature for "+message
|
||||
|
||||
#signature = line.rsplit(';')[-1:]
|
||||
if options.storeseq:
|
||||
s.close()
|
||||
|
|
Loading…
Reference in a new issue