Exemplo n.º 1
0
def create_attack(attackedSensor, goal):
	usedSensors = (attackedSensor,)

	dbOp.connectToDatabase("data/db") ##
	
	# readings & info & also segments

	noOfDimensions = dbOp.getNoOfDimensions(rootNodeID = attackedSensor)
	nodesSegmentsDic = {}
	
	for nodeID in usedSensors:
	
		readingsInfo = dbOp.selectReadingsFromNode(nodeID)
		(dTraining, dTesting, rTr, rTe) = data.getTrainingTesting(readingsInfo)
		readings = [Reading(r[0],r[1],r[2],r[3]) for r in rTr]
		
		# get segments
		segsInfo = dbOp.selectSegmentsFromNode(nodeID)
		segments = [Segment(segInfo[0],segInfo[6],segInfo[1],segInfo[2],segInfo[3],segInfo[4]) for segInfo in segsInfo]
		
		for (i, segment) in enumerate(segments):
			segReadings = readings[i*noOfDimensions:(i+1)*noOfDimensions]
			segment.set_readings(segReadings)
			
		# set segments
		nodesSegmentsDic[nodeID] = segments
		
	# conditional probabilities table
	K = dbOp.selectClusterGroup(root_node_id=attackedSensor)[0][1]
	condProbsTable = [[0]*K for i in range(K)]
	condProbs = dbOp.selectCondProbs(root_node_id=attackedSensor)
	for probArray in condProbs:
		bCluster = probArray[1]
		aCluster = probArray[2]
		prob = probArray[3]
		condProbsTable[bCluster][aCluster] = prob

	dbOp.closeConnectionToDatabase() ##
	
	mcMimicry = MCMimicry()
	return (mcMimicry, nodesSegmentsDic, condProbsTable, dTraining, dTesting)
Exemplo n.º 2
0
def prep():
    #dummy = raw_input('This is gonna take forever (press any key to continue)')
    dbOp.connectToDatabase("data/db")
    nodes = map(lambda x: x[0], dbOp.selectAllNodes())
    for node in nodes:
        try:
            print ">>", node
            readings = dbOp.selectReadingsFromNode(node)
            (dTr, dTe, rTr, rTe) = data.getTrainingTesting(readings)
            if len(dTr) == 0: raise Exception('No training data')
            mcMimicry = MCMimicry(dTr)
            (w, segments, centroids, labels, condProbTable, K,
             score) = mcMimicry.prepare()
            if w is None: continue
            # insert cluster group
            dbOp.insertClusterGroup(node, K, w)
            # insert clusters
            for (i, centroid) in enumerate(centroids):
                dbOp.insertCluster(node, i, str(centroid))
                print ">>>cluster", i
            # insert conditional probabilities
            for (i, bClusterList) in enumerate(condProbTable):
                for (j, aClusterProb) in enumerate(bClusterList):
                    dbOp.insertConditionalProbability(node, i, j, aClusterProb)
                    print ">>>", i, j
            # insert reading segments
            uouo = len(segments)
            for (i, segment) in enumerate(segments):
                print ">>>segment", i + 1, "/", uouo
                start_date = rTr[i * w][1]
                start_time = rTr[i * w][2]
                end_date = rTr[i * w + w - 1][1]
                end_time = rTr[i * w + w - 1][2]
                print start_time, '-', end_time
                cluster_id = int(labels[i])
                dbOp.insertReadingSegment(node, start_date, start_time,
                                          end_date, end_time, node, cluster_id)
            dbOp.setNodeAvail(node)
        except:
            print "Node", node, "failed to initialize"
    dbOp.closeConnectionToDatabase()
Exemplo n.º 3
0
def prep():
	#dummy = raw_input('This is gonna take forever (press any key to continue)')
	dbOp.connectToDatabase("data/db")
	nodes = map(lambda x: x[0], dbOp.selectAllNodes())
	for node in nodes:
		try:
			print ">>", node
			readings = dbOp.selectReadingsFromNode(node)
			(dTr, dTe, rTr, rTe) = data.getTrainingTesting(readings)
			if len(dTr) == 0: raise Exception('No training data')
			mcMimicry = MCMimicry(dTr)
			(w, segments, centroids, labels, condProbTable, K, score) = mcMimicry.prepare()
			if w is None: continue
			# insert cluster group
			dbOp.insertClusterGroup(node, K, w)
			# insert clusters
			for (i, centroid) in enumerate(centroids):
				dbOp.insertCluster(node, i, str(centroid))
				print ">>>cluster", i
			# insert conditional probabilities
			for (i, bClusterList) in enumerate(condProbTable):
				for (j, aClusterProb) in enumerate(bClusterList):
					dbOp.insertConditionalProbability(node, i, j, aClusterProb)
					print ">>>", i,j
			# insert reading segments
			uouo=len(segments)
			for (i, segment) in enumerate(segments):
				print ">>>segment", i+1,"/",uouo
				start_date = rTr[i*w][1]
				start_time = rTr[i*w][2]
				end_date = rTr[i*w + w -1][1]
				end_time = rTr[i*w + w -1][2]
				print start_time, '-', end_time
				cluster_id = int(labels[i])
				dbOp.insertReadingSegment(node, start_date, start_time, end_date, end_time, node, cluster_id)
			dbOp.setNodeAvail(node)
		except:
			print "Node", node, "failed to initialize"
	dbOp.closeConnectionToDatabase()
Exemplo n.º 4
0
def launch(atck, attackedSensor, usedSensors, goal, tdelay):
	
	dbOp.connectToDatabase("data/db") ##
	
	# readings & info & also segments

	noOfDimensions = dbOp.getNoOfDimensions(rootNodeID = attackedSensor)
	nodesSegmentsDic = {}
	
	for nodeID in usedSensors:
	
		readingsInfo = dbOp.selectReadingsFromNode(nodeID)
		(dTraining, dTesting, rTr, rTe) = data.getTrainingTesting(readingsInfo)
		readings = [Reading(r[0],r[1],r[2],r[3]) for r in rTr]
		
		# get segments
		segsInfo = dbOp.selectSegmentsFromNode(nodeID)
		segments = [Segment(segInfo[0],segInfo[6],segInfo[1],segInfo[2],segInfo[3],segInfo[4]) for segInfo in segsInfo]
		
		for (i, segment) in enumerate(segments):
			segReadings = readings[i*noOfDimensions:(i+1)*noOfDimensions]
			segment.set_readings(segReadings)
			
		# set segments
		nodesSegmentsDic[nodeID] = segments
		
	# conditional probabilities table
	K = dbOp.selectClusterGroup(root_node_id=attackedSensor)[0][1]
	condProbsTable = [[0]*K for i in range(K)]
	condProbs = dbOp.selectCondProbs(root_node_id=attackedSensor)
	for probArray in condProbs:
		bCluster = probArray[1]
		aCluster = probArray[2]
		prob = probArray[3]
		condProbsTable[bCluster][aCluster] = prob

	dbOp.closeConnectionToDatabase() ##
	
	mcMimicry = MCMimicry()
	if atck == 0:
		(startSignal, iSignal) = mcMimicry.tree_attack(attackedSensor, goal, tdelay, sensorsSegmentsReadingsDic, cond_probs_table)
	elif atck == 1:
		(startSignal, iSignal) = mcMimicry.greedy_attack(attackedSensor, goal, tdelay, nodesSegmentsDic, condProbsTable)
	elif atck == 2:
		(startSignal, iSignal) = mcMimicry.random_cluster_attack(attackedSensor, goal, tdelay, nodesSegmentsDic, condProbsTable)
	elif atck == 3:
		(startSignal, iSignal) = mcMimicry.greedy_smooth_cluster_attack(attackedSensor, goal, tdelay, nodesSegmentsDic, condProbsTable)
	elif atck == 4:
		(startSignal, iSignal) = mcMimicry.first_cluster_attack(attackedSensor, goal, tdelay, nodesSegmentsDic, condProbsTable)
	elif atck == 5:
		(startSignal, iSignal) = mcMimicry.super_cluster_attack(attackedSensor, goal, tdelay, nodesSegmentsDic, condProbsTable)
	elif atck == 6:
		(startSignal, iSignal) = mcMimicry.rgv_cluster_attack(attackedSensor, goal, tdelay, nodesSegmentsDic, condProbsTable, dTesting, propvarderiv=0.65, propdifftemp=0.35, comeBack=1)
	elif atck == 7:
		(startSignal, iSignal) = mcMimicry.softmax_cluster_attack(attackedSensor, goal, tdelay, nodesSegmentsDic, condProbsTable, dTesting, propvarderiv=0.75, temp=0.03, comeBack=1)
	elif atck == 8:
		(startSignal, iSignal) = mcMimicry.not_working_attack(attackedSensor, goal, tdelay, nodesSegmentsDic, condProbsTable, dTesting, comeBack=True)
	elif atck == 9:
		(startSignal, iSignal) = mcMimicry.cluster_attack(attackedSensor, goal, tdelay, nodesSegmentsDic, condProbsTable)
	elif atck == 10:
		(startSignal, iSignal) = mcMimicry.ditto_attack(attackedSensor, goal, tdelay, nodesSegmentsDic, condProbsTable, dTesting, comeBack=True)
	elif atck == 11:
		(startSignal, iSignal) = mcMimicry.rwgm_attack(attackedSensor, goal, tdelay, nodesSegmentsDic, condProbsTable, dTesting, propMean=0.45, propWDer1=0.2, propDTemp=0.65, comeBack=True)
	else:
		return None
	return (startSignal, iSignal, dTraining, dTesting)
Exemplo n.º 5
0
parser.add_argument("--d", nargs='?', default="1", help="Delay in seconds")
args = parser.parse_args()

##########################
# s
##########################
attackedSensor = int(args.s)
usedSensors = (attackedSensor, )
dbOp.connectToDatabase("data/db")  ##

# readings & info & segments
noOfDimensions = dbOp.getNoOfDimensions(rootNodeID=attackedSensor)
nodesSegmentsDic = {}

for nodeID in usedSensors:
    readingsInfo = dbOp.selectReadingsFromNode(nodeID)
    (dTraining, dTesting, rTr, rTe) = data.getTrainingTesting(readingsInfo)
    readings = [Reading(r[0], r[1], r[2], r[3]) for r in rTr]

    # get segments
    segsInfo = dbOp.selectSegmentsFromNode(nodeID)
    segments = [
        Segment(segInfo[0], segInfo[6], segInfo[1], segInfo[2], segInfo[3],
                segInfo[4]) for segInfo in segsInfo
    ]

    for (i, segment) in enumerate(segments):
        segReadings = readings[i * noOfDimensions:(i + 1) * noOfDimensions]
        segment.set_readings(segReadings)

    # set segments
Exemplo n.º 6
0
def launch(atck, attackedSensor, usedSensors, goal, tdelay):

    dbOp.connectToDatabase("data/db")  ##

    # readings & info & also segments

    noOfDimensions = dbOp.getNoOfDimensions(rootNodeID=attackedSensor)
    nodesSegmentsDic = {}

    for nodeID in usedSensors:

        readingsInfo = dbOp.selectReadingsFromNode(nodeID)
        (dTraining, dTesting, rTr, rTe) = data.getTrainingTesting(readingsInfo)
        readings = [Reading(r[0], r[1], r[2], r[3]) for r in rTr]

        # get segments
        segsInfo = dbOp.selectSegmentsFromNode(nodeID)
        segments = [
            Segment(segInfo[0], segInfo[6], segInfo[1], segInfo[2], segInfo[3],
                    segInfo[4]) for segInfo in segsInfo
        ]

        for (i, segment) in enumerate(segments):
            segReadings = readings[i * noOfDimensions:(i + 1) * noOfDimensions]
            segment.set_readings(segReadings)

        # set segments
        nodesSegmentsDic[nodeID] = segments

    # conditional probabilities table
    K = dbOp.selectClusterGroup(root_node_id=attackedSensor)[0][1]
    condProbsTable = [[0] * K for i in range(K)]
    condProbs = dbOp.selectCondProbs(root_node_id=attackedSensor)
    for probArray in condProbs:
        bCluster = probArray[1]
        aCluster = probArray[2]
        prob = probArray[3]
        condProbsTable[bCluster][aCluster] = prob

    dbOp.closeConnectionToDatabase()  ##

    mcMimicry = MCMimicry()
    if atck == 0:
        (startSignal,
         iSignal) = mcMimicry.tree_attack(attackedSensor, goal, tdelay,
                                          sensorsSegmentsReadingsDic,
                                          cond_probs_table)
    elif atck == 1:
        (startSignal,
         iSignal) = mcMimicry.greedy_attack(attackedSensor, goal, tdelay,
                                            nodesSegmentsDic, condProbsTable)
    elif atck == 2:
        (startSignal,
         iSignal) = mcMimicry.random_cluster_attack(attackedSensor, goal,
                                                    tdelay, nodesSegmentsDic,
                                                    condProbsTable)
    elif atck == 3:
        (startSignal, iSignal) = mcMimicry.greedy_smooth_cluster_attack(
            attackedSensor, goal, tdelay, nodesSegmentsDic, condProbsTable)
    elif atck == 4:
        (startSignal,
         iSignal) = mcMimicry.first_cluster_attack(attackedSensor, goal,
                                                   tdelay, nodesSegmentsDic,
                                                   condProbsTable)
    elif atck == 5:
        (startSignal,
         iSignal) = mcMimicry.super_cluster_attack(attackedSensor, goal,
                                                   tdelay, nodesSegmentsDic,
                                                   condProbsTable)
    elif atck == 6:
        (startSignal,
         iSignal) = mcMimicry.rgv_cluster_attack(attackedSensor,
                                                 goal,
                                                 tdelay,
                                                 nodesSegmentsDic,
                                                 condProbsTable,
                                                 dTesting,
                                                 propvarderiv=0.65,
                                                 propdifftemp=0.35,
                                                 comeBack=1)
    elif atck == 7:
        (startSignal,
         iSignal) = mcMimicry.softmax_cluster_attack(attackedSensor,
                                                     goal,
                                                     tdelay,
                                                     nodesSegmentsDic,
                                                     condProbsTable,
                                                     dTesting,
                                                     propvarderiv=0.75,
                                                     temp=0.03,
                                                     comeBack=1)
    elif atck == 8:
        (startSignal, iSignal) = mcMimicry.not_working_attack(attackedSensor,
                                                              goal,
                                                              tdelay,
                                                              nodesSegmentsDic,
                                                              condProbsTable,
                                                              dTesting,
                                                              comeBack=True)
    elif atck == 9:
        (startSignal,
         iSignal) = mcMimicry.cluster_attack(attackedSensor, goal, tdelay,
                                             nodesSegmentsDic, condProbsTable)
    elif atck == 10:
        (startSignal, iSignal) = mcMimicry.ditto_attack(attackedSensor,
                                                        goal,
                                                        tdelay,
                                                        nodesSegmentsDic,
                                                        condProbsTable,
                                                        dTesting,
                                                        comeBack=True)
    elif atck == 11:
        (startSignal, iSignal) = mcMimicry.rwgm_attack(attackedSensor,
                                                       goal,
                                                       tdelay,
                                                       nodesSegmentsDic,
                                                       condProbsTable,
                                                       dTesting,
                                                       propMean=0.45,
                                                       propWDer1=0.2,
                                                       propDTemp=0.65,
                                                       comeBack=True)
    else:
        return None
    return (startSignal, iSignal, dTraining, dTesting)
Exemplo n.º 7
0
import dbOp, data

dbOp.connectToDatabase("data/db")
r7 = dbOp.selectReadingsFromNode(7)
dbOp.closeConnectionToDatabase()
(d3Training, d3Testing, r3Tr, r3Te) = data.getTrainingTesting(r7)

import matplotlib.pyplot as plt
#plt.axis('equal')
plt.plot(d3Training, 'b')
plt.show()
Exemplo n.º 8
0
parser.add_argument("--d", nargs='?', default="1", help="Delay in seconds")
args = parser.parse_args()

##########################
# s
##########################
attackedSensor = int(args.s)
usedSensors = (attackedSensor,)
dbOp.connectToDatabase("data/db") ##
	
# readings & info & segments
noOfDimensions = dbOp.getNoOfDimensions(rootNodeID = attackedSensor)
nodesSegmentsDic = {}
	
for nodeID in usedSensors:
	readingsInfo = dbOp.selectReadingsFromNode(nodeID)
	(dTraining, dTesting, rTr, rTe) = data.getTrainingTesting(readingsInfo)
	readings = [Reading(r[0],r[1],r[2],r[3]) for r in rTr]
		
	# get segments
	segsInfo = dbOp.selectSegmentsFromNode(nodeID)
	segments = [Segment(segInfo[0],segInfo[6],segInfo[1],segInfo[2],segInfo[3],segInfo[4]) for segInfo in segsInfo]
		
	for (i, segment) in enumerate(segments):
		segReadings = readings[i*noOfDimensions:(i+1)*noOfDimensions]
		segment.set_readings(segReadings)
			
	# set segments
	nodesSegmentsDic[nodeID] = segments
		
# conditional probabilities table