2012-12-05 06:54:56 +00:00
|
|
|
import socket
|
|
|
|
import datetime
|
|
|
|
import time
|
2012-12-05 17:46:48 +00:00
|
|
|
from optparse import OptionParser
|
|
|
|
|
2012-12-05 06:54:56 +00:00
|
|
|
try:
|
|
|
|
from hashlib import sha1
|
|
|
|
except ImportError:
|
|
|
|
from sha import sha as sha1
|
|
|
|
import hmac
|
|
|
|
|
|
|
|
## nb;epochvalue;sq;hmac
|
|
|
|
## hmacfunc("nb;epochvalue;sq;", psk)
|
|
|
|
def nbsign(message=None, psk="netbeacon"):
|
|
|
|
auth = hmac.new(psk, message, sha1)
|
|
|
|
return auth.hexdigest()
|
|
|
|
|
|
|
|
# format: nb;1354687980;1;500f5e18df881bb1dd22ee3c468209669a13e4ef
|
2012-12-08 10:59:04 +00:00
|
|
|
def nbmessage(seq=1, psk="netbeacon"):
|
2012-12-05 06:54:56 +00:00
|
|
|
m = ""
|
|
|
|
m = m + "nb"
|
|
|
|
m = m + ";"
|
|
|
|
t = datetime.datetime.now()
|
|
|
|
now = time.mktime(t.timetuple())
|
|
|
|
m = m + (str(int(now)))
|
|
|
|
m = m + ";"
|
|
|
|
m = m + str(seq)
|
|
|
|
m = m + ";"
|
2012-12-08 10:59:04 +00:00
|
|
|
m = m + nbsign(message=m,psk=psk)
|
2012-12-05 06:54:56 +00:00
|
|
|
return m
|
|
|
|
|
|
|
|
def nbsend(destination=None,payload=None, logging=False):
|
|
|
|
if destination is None:
|
|
|
|
return False
|
|
|
|
if logging:
|
|
|
|
print (payload)
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
|
|
|
sock.sendto(payload, (destination, 12345))
|
|
|
|
return True
|
|
|
|
|
2012-12-08 12:44:05 +00:00
|
|
|
usage = "usage: %prog [options]"
|
2012-12-05 17:46:48 +00:00
|
|
|
parser = OptionParser(usage)
|
2012-12-08 10:59:04 +00:00
|
|
|
parser.add_option("-p","--psk", dest="psk", help="pre-shared key used by the HMAC-SHA1 (default: netbeacon)")
|
2012-12-05 17:46:48 +00:00
|
|
|
parser.add_option("-s","--storeseq", dest="storeseq", action='store_true', help="store sequence and validate sequence")
|
2012-12-05 17:53:50 +00:00
|
|
|
parser.add_option("-i","--iteration", dest="iteration", type=int, help="set the number of interation for sending the netbeacon")
|
2013-06-08 08:00:12 +00:00
|
|
|
parser.add_option("-d","--destination", dest="destinations", action="append", help="set the destination(s) IPv4 address (default: 127.0.0.1)")
|
2012-12-08 10:59:04 +00:00
|
|
|
parser.add_option("-v","--verbose", dest="verbose", action='store_true', help="output netbeacon sent")
|
2012-12-05 17:46:48 +00:00
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
2012-12-08 10:59:04 +00:00
|
|
|
if options.psk:
|
|
|
|
psk = options.psk
|
|
|
|
else:
|
|
|
|
psk = "netbeacon"
|
|
|
|
|
2013-06-08 08:00:12 +00:00
|
|
|
destinations = []
|
|
|
|
|
|
|
|
if not options.destinations:
|
|
|
|
destinations.append("127.0.0.1")
|
|
|
|
else:
|
|
|
|
for v in options.destinations:
|
|
|
|
destinations.append(v)
|
|
|
|
|
2012-12-05 17:46:48 +00:00
|
|
|
if options.storeseq:
|
|
|
|
import shelve
|
|
|
|
s = shelve.open("netbeacon-send.seq")
|
2013-06-08 08:00:12 +00:00
|
|
|
for destination in destinations:
|
|
|
|
k = 'seq:' + str(destination)
|
|
|
|
print (k)
|
|
|
|
if k not in s:
|
|
|
|
s[k] = 1
|
|
|
|
seqstart = s[k]+1
|
2012-12-05 17:46:48 +00:00
|
|
|
else:
|
|
|
|
seqstart = 1
|
|
|
|
|
2012-12-05 17:53:50 +00:00
|
|
|
if not options.iteration:
|
|
|
|
options.iteration=10
|
|
|
|
|
2013-06-08 08:00:12 +00:00
|
|
|
for destination in destinations:
|
|
|
|
for x in range(seqstart,seqstart+options.iteration):
|
|
|
|
nbsend(destination=destination, payload=nbmessage(x, psk=psk), logging=options.verbose)
|
|
|
|
if options.storeseq:
|
|
|
|
s['seq:'+str(destination)] = x
|
2012-12-05 17:53:50 +00:00
|
|
|
|
2012-12-05 17:46:48 +00:00
|
|
|
if options.storeseq:
|
|
|
|
s.close()
|