public static DSClass Combine(DSClass a, DSClass b) { if(a.length != b.length) { Console.WriteLine("DSClass a and b not equal length."); Console.ReadLine(); return null; } int n = a.length; DSClass ds = new DSClass(n); ds.m[0] = 0; for (int i = 1; i < n; i++) { // for ds.m[i] double x = 0; double y = 0; for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { if ((j & k) == i) x += a.m[j] * b.m[k]; if ((j & k) == 0) y += a.m[j] * b.m[k]; } } ds.m[i] = x / (1 - y); } return ds; }
public static DSClass CombineWithWeight(DSClass a, DSClass b, double weighta, double weightb) { if (a.length != b.length) { Console.WriteLine("DSClass a and b not equal length."); Console.ReadLine(); return null; } int n = a.length; DSClass ds = new DSClass(n); for (int i = 0; i < n; i++) { ds.m[pow(i)] = a.m[pow(i)] * weighta + b.m[pow(i)] * weightb; } return ds; }
static List<IOTEventTrustResult> DeduceModifyCommandMaliciously(int selfId, HashSet<IOTPhenomemon> observedPhenomemons, double currentTime) { IOTGlobal global = (IOTGlobal)Global.getInstance(); //每个节点处理该类事件的次数 int[] eventCount = new int[global.readerNum]; List<IOTEventTrustResult> list = new List<IOTEventTrustResult>(); foreach (IOTPhenomemon p in observedPhenomemons) { double a15_16, a17_20, a21, a6, a7, a17; if (p.likehood <= global.SmallValue) continue; if (p.type != IOTPhenomemonType.RECV_PACKET) continue; else if (p.pkg.Dst != p.nodeId || p.pkg.Type != PacketType.COMMAND) continue; else if (selfId == p.nodeId)//自己不检查自己的可信度 continue; else if (currentTime - p.start < global.checkPhenomemonTimeout) continue; int node = p.nodeId; eventCount[node]++; a15_16 = p.likehood; //likehood of receiving a packet at time p.start a6 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.MOVE_FAST, node, p.start - global.checkPhenomemonTimeout, Scheduler.getInstance().currentTime); a7 = 0.9 - a6; a17_20 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.SEND_COMMAND, node, p.start, p.start + global.sendPacketTimeout, p, ComparePhenomemonByExactTag); List<IOTPhenomemon> similiarList = new List<IOTPhenomemon>(); a17 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.SEND_COMMAND, node, p.start, p.start + global.sendPacketTimeout, p, similiarList, ComparePhenomemonBySimiliarTag); a21 = SimiliarCommand(p, similiarList); double b17 = DSClass.AND(a15_16, a17_20) * CF[(int)IOTEventType.NotModifyCommand]; double b18 = DSClass.AND(DSClass.AND(a15_16, a17), a21) * CF[(int)IOTEventType.ModifyCommandDueToNodeFaulty]; double b19 = DSClass.AND(DSClass.AND(a15_16, a17), a21) * CF[(int)IOTEventType.ModifyCommandMaliciously]; DSClass ds = new DSClass(pow(IOTModifyCommandEventType.END)); ds.SetM(pow(IOTModifyCommandEventType.NotModifyCommand), b17); ds.SetM(pow(IOTModifyCommandEventType.ModifyCommandDueToNodeFaulty), b18); ds.SetM(pow(IOTModifyCommandEventType.ModifyCommandDueToNodeFaulty), b19); ds.SetM(pow(IOTModifyCommandEventType.END) - 1, 1 - b17 - b18 - b19); ds.Cal(); //ds.Output(); //此处,我们先过滤一些正常事件,否则事件太多了 if (ds.b[pow(IOTModifyCommandEventType.NotModifyCommand)] < global.NormalBelief && ds.p[pow(IOTModifyCommandEventType.NotModifyCommand)] < global.NormalPlausibility)//确实是攻击,恶意事件的信念大于正常事件,或恶意事件的信念大于某一个阈值 { string pkgIdent = p.pkg.Prev + "->" + p.pkg.Next + "[" + p.pkg.PrevSenderSeq + "]"; IOTEventTrustResult r = new IOTEventTrustResult(node, selfId, pkgIdent, IOTEventCategoryType.ModifyCommand, ds); r.totalEventCount = eventCount[node]; r.app = p.pkg.AppId; list.Add(r); } } return list; }
static List<IOTEventTrustResult> DeduceMakePacketMaliciously(int selfId, HashSet<IOTPhenomemon> observedPhenomemons, double currentTime) { IOTGlobal global = (IOTGlobal)Global.getInstance(); //每个节点处理该类事件的次数 int[] eventCount = new int[global.readerNum]; List<IOTEventTrustResult> list = new List<IOTEventTrustResult>(); foreach(IOTPhenomemon p in observedPhenomemons) { double a2, a24, a23, a22_10; if (p.likehood <= global.SmallValue) continue; if (p.type != IOTPhenomemonType.RECV_PACKET) continue; else if (p.pkg.Dst == p.nodeId) continue; else if (selfId == p.nodeId)//自己不检查自己的可信度 continue; else if (global.readers[p.nodeId].IsGateway) continue; else if (currentTime - p.start < global.checkPhenomemonTimeout) continue; int node = p.nodeId; eventCount[node]++; a2 = p.likehood; //likehood of receiving a packet at time p.start a24 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.RECV_PACKET, selfId, p.start - global.checkPhenomemonTimeout, p.start); a23 = Inverse(a24); a22_10 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.SAME_PACKET_DATA, selfId, p.start, p.start + global.sendPacketTimeout, p.pkg); double b8 = DSClass.AND(a2, a24) * CF[(int)IOTEventType.NotMakePacket]; double b9 =DSClass.AND(DSClass.OR(a23, a22_10), a2) * CF[(int)IOTEventType.MakePacketMaliciously]; DSClass ds = new DSClass(pow(IOTMakePacketEventType.END)); ds.SetM(pow(IOTMakePacketEventType.NotMakePacket), b8); ds.SetM(pow(IOTMakePacketEventType.MakePacketMaliciously), b9); ds.SetM(pow(IOTMakePacketEventType.END) - 1, 1 - b8 - b9); ds.Cal(); //ds.Output(); //此处,我们先过滤一些正常事件,否则事件太多了 if (ds.b[pow(IOTMakePacketEventType.NotMakePacket)] < global.NormalBelief && ds.p[pow(IOTMakePacketEventType.NotMakePacket)] < global.NormalPlausibility)//确实是攻击,恶意事件的信念大于正常事件,或恶意事件的信念大于某一个阈值 { string pkgIdent = p.pkg.Prev + "->" + p.pkg.Next + "[" + p.pkg.PrevSenderSeq + "]"; IOTEventTrustResult r = new IOTEventTrustResult(node, selfId, pkgIdent, IOTEventCategoryType.MakePacket, ds); r.totalEventCount = eventCount[node]; r.app = p.pkg.AppId; list.Add(r); } } return list; }
static List<IOTEventTrustResult> DeduceMakeCommandMaliciously(int selfId, HashSet<IOTPhenomemon> observedPhenomemons, double currentTime) { IOTGlobal global = (IOTGlobal)Global.getInstance(); //每个节点处理该类事件的次数 int[] eventCount = new int[global.readerNum]; List<IOTEventTrustResult> list = new List<IOTEventTrustResult>(); foreach (IOTPhenomemon p in observedPhenomemons) { double a20, a24, a5, a6, a7, a17, a23, a4; if (p.likehood <= global.SmallValue) continue; if (p.type != IOTPhenomemonType.SEND_COMMAND) continue; else if (p.pkg.Dst != p.nodeId || p.pkg.Type != PacketType.COMMAND) continue; else if (selfId == p.nodeId)//自己不检查自己的可信度 continue; else if (currentTime - p.start < global.checkPhenomemonTimeout) continue; int node = p.nodeId; eventCount[node]++; a17 = p.likehood; //likehood of receiving a packet at time p.start a6 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.MOVE_FAST, node, p.start - global.checkPhenomemonTimeout, Scheduler.getInstance().currentTime); //如果对带宽占用没有知识,则正反都设置为未知。 a4 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.BANDWIDTH_BUSY, selfId, p.start - global.checkPhenomemonTimeout, Scheduler.getInstance().currentTime); a5 = 0.9 - a4; a7 = 0.9 - a6; List<IOTPhenomemon> similiarList = new List<IOTPhenomemon>(); a24 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.SEND_COMMAND, node, p.start - global.sendPacketTimeout, p.start, p, similiarList, ComparePhenomemonBySimiliarTag); a20 = SimiliarCommand(p, similiarList); if (Utility.DoubleEqual(a20, global.SmallValue) || Utility.DoubleEqual(a24, global.SmallValue)) a23 = 0.9 - global.SmallValue; else a23 = Math.Max(a20, a24); double b20 = DSClass.AND(DSClass.AND(a17, a20), a24) * CF[(int)IOTEventType.NotMakeCommand]; double b21 = DSClass.AND(DSClass.AND(DSClass.AND(17, a20), a23), a6) * CF[(int)IOTEventType.NotMakeCommandButMove]; double b22 = DSClass.AND(DSClass.AND(DSClass.AND(17, a20), a23), a4) * CF[(int)IOTEventType.NotMakeCommandButNetworkDelay]; double b23 = DSClass.AND(DSClass.AND(DSClass.AND(DSClass.AND(17, a20), a23), a5), a7) * CF[(int)IOTEventType.MakeCommandMaliciously]; DSClass ds = new DSClass(pow(IOTMakeCommandEventType.END)); ds.SetM(pow(IOTMakeCommandEventType.NotMakeCommand), b20); ds.SetM(pow(IOTMakeCommandEventType.NotMakeCommandButNetworkDelay), b21); ds.SetM(pow(IOTMakeCommandEventType.NotMakeCommandButNetworkDelay), b22); ds.SetM(pow(IOTMakeCommandEventType.MakeCommandMaliciously), b23); ds.SetM(pow(IOTMakeCommandEventType.END) - 1, 1 - b21 - b22 - b23); ds.Cal(); //ds.Output(); //此处,我们先过滤一些正常事件,否则事件太多了 if (ds.b[pow(IOTMakeCommandEventType.NotMakeCommand)] < global.NormalBelief && ds.p[pow(IOTMakeCommandEventType.NotMakeCommand)] < global.NormalPlausibility)//确实是攻击,恶意事件的信念大于正常事件,或恶意事件的信念大于某一个阈值 { string pkgIdent = p.pkg.Prev + "->" + p.pkg.Next + "[" + p.pkg.PrevSenderSeq + "]"; IOTEventTrustResult r = new IOTEventTrustResult(node, selfId, pkgIdent, IOTEventCategoryType.MakeCommand, ds); r.totalEventCount = eventCount[node]; r.app = p.pkg.AppId; list.Add(r); } } return list; }
static List<IOTEventTrustResult> DeduceDropPacketMaliciously(int selfId, HashSet<IOTPhenomemon> observedPhenomemons, double currentTime) { if(Global.getInstance().debug) Console.WriteLine("READER{0} Check DeduceDropPacketMaliciously", selfId); IOTGlobal global = (IOTGlobal)Global.getInstance(); IOTReader selfNode = (IOTReader)global.readers[selfId]; //每个节点处理该类事件的次数 int[] eventCount = new int[global.readerNum]; List<IOTEventTrustResult> list = new List<IOTEventTrustResult>(); foreach (IOTPhenomemon p in observedPhenomemons) { double a1, a2, a3, a4, a5, a6, a7, a19, a27; if (p.likehood <= global.SmallValue) continue; if (p.type != IOTPhenomemonType.RECV_PACKET) continue; else if (p.pkg.Dst == p.nodeId) continue; else if (p.pkg.Type != PacketType.DATA && p.pkg.Type != PacketType.COMMAND) continue; else if (selfId == p.nodeId)//自己不检查自己的可信度 continue; else if (global.readers[p.nodeId].IsGateway) continue; else if (currentTime - p.start < global.checkPhenomemonTimeout) continue; int node = p.nodeId; eventCount[node]++; a1 = p.likehood; //likehood of receiving a packet at time p.start a2 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.SEND_PACKET, node, p.start, p.start + global.sendPacketTimeout, p.pkg); a3 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.NOT_SEND_PACKET, node, p.start, Scheduler.getInstance().currentTime, p.pkg); a4 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.BANDWIDTH_BUSY, selfId, p.start - global.checkPhenomemonTimeout, Scheduler.getInstance().currentTime); //如果对带宽占用没有知识,则正反都设置为未知。 a5 = 0.9 - a4; //这里是否可以用平均值Average? a6 = Utility.Max(new double[]{ ConditionHappened(observedPhenomemons, IOTPhenomemonType.MOVE_FAST, node, p.start - global.checkPhenomemonTimeout, Scheduler.getInstance().currentTime), ConditionHappened(observedPhenomemons, IOTPhenomemonType.MOVE_FAST, p.pkg.Prev, p.start - global.checkPhenomemonTimeout, Scheduler.getInstance().currentTime), ConditionHappened(observedPhenomemons, IOTPhenomemonType.MOVE_FAST, selfId, p.start - global.checkPhenomemonTimeout, Scheduler.getInstance().currentTime) }); a7 = 0.9 - a6; //一个是观测节点和被观测节点的距离,看是否后者发送的消息能否被前者收到 //另一个是,看源节点发送的数据是否能被被观测节点收到 //a19 = Math.Max(FarDistanceLikehood(selfId, node, p.pkg.DstType==NodeType.OBJECT), // FarDistanceLikehood(selfId, p.pkg.Prev, p.pkg.PrevType == NodeType.OBJECT)); //TODO,这里是不是有问题,false和true,在md项目中已修正 if(p.pkg.DstType == NodeType.OBJECT && selfNode.NearbyObjectCache.ContainsKey(p.pkg.Dst)) a19 = FarDistanceLikehood(selfId, node, false); else a19 = FarDistanceLikehood(selfId, node, true); a27 = 0.9 - a19; //A1 AND A2 AND A7 AND A11 -> B1 double b0 = DSClass.AND(a1, a2) * CF[(int)IOTEventType.NotDropPacket]; //A1 AND A2 AND A7 AND A11 -> B1 double b1 = DSClass.AND(DSClass.AND(a1, a3), DSClass.OR(DSClass.OR(a4, a6), a19)) * CF[(int)IOTEventType.NotDropPacketButNotReceivePacket]; //A1 AND A2 AND A7 AND A11 -> B1 double b2 = DSClass.AND(DSClass.AND(a1, a3), a4) * CF[(int)IOTEventType.DropPacketDueToBandwith]; //A1 AND A2 AND A7 AND A11 -> B1 double b3 = DSClass.AND(DSClass.AND(DSClass.AND(DSClass.AND(a1, a3), a5), a7), a27) * CF[(int)IOTEventType.DropPacketMaliciously]; if (global.debug) { Console.WriteLine("{0}->{1}:{2}", selfId, node, p.pkg.SrcSenderSeq); Console.WriteLine("a1:" + a1); Console.WriteLine("a2:" + a2); Console.WriteLine("a3:" + a3); Console.WriteLine("a4:" + a4); Console.WriteLine("a5:" + a5); Console.WriteLine("a6:" + a6); Console.WriteLine("a7:" + a7); Console.WriteLine("a19:" + a19); Console.WriteLine("a27:" + a27); Console.WriteLine("b0:" + b0); Console.WriteLine("b1:" + b1); Console.WriteLine("b2:" + b2); Console.WriteLine("b3:" + b3); } DSClass ds = new DSClass(pow(IOTDropPacketEventType.END)); ds.SetM(pow(IOTDropPacketEventType.NotDropPacket), b0); ds.SetM(pow(IOTDropPacketEventType.NotDropPacketButNotReceivePacket), b1); ds.SetM(pow(IOTDropPacketEventType.DropPacketDueToBandwith), b2); ds.SetM(pow(IOTDropPacketEventType.DropPacketMaliciously), b3); ds.SetM(pow(IOTDropPacketEventType.END) - 1, 1 - b0 - b1 - b2 - b3); ds.Cal(); //ds.Output(); //此处,我们先过滤一些正常事件,否则事件太多了 if (ds.b[pow(IOTEventType.NotDropPacket) + pow(IOTEventType.NotDropPacketButNotReceivePacket)] < global.NormalBelief && ds.p[pow(IOTEventType.NotDropPacket) + pow(IOTEventType.NotDropPacketButNotReceivePacket)] < global.NormalPlausibility)//确实是攻击,恶意事件的信念大于正常事件,或恶意事件的信念大于某一个阈值 { string pkgIdent = p.pkg.Prev + "->" + p.pkg.Next + "[" + p.pkg.PrevSenderSeq + "]"; IOTEventTrustResult r = new IOTEventTrustResult(node, selfId, pkgIdent, IOTEventCategoryType.DropPacket, ds); r.totalEventCount = eventCount[node]; r.app = p.pkg.AppId; list.Add(r); Console.WriteLine("{0:F4} reader{1} think reader{2} is not normal.", currentTime, selfId, p.nodeId); if (global.debug) { r.ds.Output(); } } } return list; }
static List<IOTEventTrustResult> DeduceDropCommandMaliciously(int selfId, HashSet<IOTPhenomemon> observedPhenomemons, double currentTime) { IOTGlobal global = (IOTGlobal)Global.getInstance(); //每个节点处理该类事件的次数 int[] eventCount = new int[global.readerNum]; List<IOTEventTrustResult> list = new List<IOTEventTrustResult>(); foreach (IOTPhenomemon p in observedPhenomemons) { double a15_16, a17_20, a18_20, a19, a6, a7; if (p.likehood <= global.SmallValue) continue; if (p.type != IOTPhenomemonType.RECV_PACKET) continue; else if (p.pkg.Dst != p.nodeId ||p.pkg.Type != PacketType.COMMAND) continue; else if (selfId == p.nodeId)//自己不检查自己的可信度 continue; else if (currentTime - p.start < global.checkPhenomemonTimeout) continue; int node = p.nodeId; eventCount[node]++; a15_16 = p.likehood; //likehood of receiving a packet at time p.start a6 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.MOVE_FAST, node, p.start - global.checkPhenomemonTimeout, Scheduler.getInstance().currentTime); a7 = 0.9 - a6; a17_20 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.SEND_COMMAND, node, p.start, p.start + global.sendPacketTimeout, p, ComparePhenomemonByExactTag); a18_20 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.NOT_SEND_COMMAND, node, p.start, Scheduler.getInstance().currentTime, p.pkg); //第三个参数是“数据包的目的地是否是标签”,由于命令的目的肯定是标签,所以永远为true a19 = FarDistanceLikehood(selfId, node, true); double b14 = DSClass.AND(a15_16, a17_20) * CF[(int)IOTEventType.NotDropCommand]; double b15 = DSClass.AND(DSClass.AND(a15_16, a18_20), DSClass.OR(a6, a19)) * CF[(int)IOTEventType.NotDropCommandButNotDetected]; double b16 = DSClass.AND(DSClass.AND(a15_16, a18_20), a7) * CF[(int)IOTEventType.DropCommandMaliciously]; DSClass ds = new DSClass(pow(IOTDropCommandEventType.END)); ds.SetM(pow(IOTDropCommandEventType.NotDropCommand), b14); ds.SetM(pow(IOTDropCommandEventType.NotDropCommandButNotDetected), b15); ds.SetM(pow(IOTDropCommandEventType.DropCommandMaliciously), b16); ds.SetM(pow(IOTDropCommandEventType.END) - 1, 1 - b14 - b15 - b16); ds.Cal(); //ds.Output(); //此处,我们先过滤一些正常事件,否则事件太多了 if (ds.b[pow(IOTDropCommandEventType.NotDropCommand)] < global.NormalBelief && ds.p[pow(IOTDropCommandEventType.NotDropCommand)] < global.NormalPlausibility)//确实是攻击,恶意事件的信念大于正常事件,或恶意事件的信念大于某一个阈值 { string pkgIdent = p.pkg.Prev + "->" + p.pkg.Next + "[" + p.pkg.PrevSenderSeq + "]"; IOTEventTrustResult r = new IOTEventTrustResult(node, selfId, pkgIdent, IOTEventCategoryType.DropCommand, ds); r.totalEventCount = eventCount[node]; r.app = p.pkg.AppId; list.Add(r); } } return list; }
public IOTEventTrustResult(int node, int reportNode, string pkgIdent, IOTEventCategoryType category, DSClass ds) { this.node = node; this.reportNode = reportNode; this.ds = ds; this.category = category; this.eventIdent = pkgIdent+"-"+category; //TODO app confirmBeliefThrehold = new Dictionary<IOTEventType, double>(); confirmBeliefThrehold[IOTEventType.DropPacketMaliciously] = 0.3; confirmBeliefThrehold[IOTEventType.DropCommandMaliciously] = 0.3; }
static List<IOTEventTrustResult> DeduceDropPacketMaliciously(int selfId, List<IOTPhenomemon> observedPhenomemons, double currentTime) { IOTGlobal global = (IOTGlobal)Global.getInstance(); //每个节点处理该类事件的次数 int[] eventCount = new int[global.readerNum]; List<IOTEventTrustResult> list = new List<IOTEventTrustResult>(); for (int i = 0; i < observedPhenomemons.Count; i++) { double a1, a2, a3, a4, a5, a6, a7; IOTPhenomemon p = observedPhenomemons[i]; if (p.likehood <= global.SmallValue) continue; if (p.type != IOTPhenomemonType.RECV_PACKET) continue; else if (p.pkg.Dst == p.nodeId) continue; int node = p.nodeId; eventCount[node]++; a1 = p.likehood; //likehood of receiving a packet at time p.start a2 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.SEND_PACKET, node, p.start, p.start + global.sendPacketTimeout, p.pkg); a3 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.NOT_SEND_PACKET, node, p.start, p.start + global.sendPacketTimeout, p.pkg); a4 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.BANDWIDTH_BUSY, selfId, p.start - global.checkPhenomemonTimeout, p.start + global.checkPhenomemonTimeout); //如果对带宽占用没有知识,则正反都设置为未知。 if (Utility.DoubleEqual(a4, global.SmallValue)) a5 = global.SmallValue; else a5 = 0.9 - a4; a6 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.MOVE_FAST, node, p.start - global.checkPhenomemonTimeout, p.start + global.checkPhenomemonTimeout); if (Utility.DoubleEqual(a6, global.SmallValue)) a7 = global.SmallValue; else a7 = 0.9 - a6; //A1 AND A2 AND A7 AND A11 -> B1 double b0 = DSClass.AND(a1, a2) * CF[(int)IOTEventType.NotDropPacket]; //A1 AND A2 AND A7 AND A11 -> B1 double b1 = DSClass.AND(DSClass.AND(a1, a3), DSClass.OR(a4, a5)) * CF[(int)IOTEventType.NotDropPacketButNotReceivePacket]; //A1 AND A2 AND A7 AND A11 -> B1 double b2 = DSClass.AND(DSClass.AND(a1, a3), a4) * CF[(int)IOTEventType.DropPacketDueToBandwith]; //A1 AND A2 AND A7 AND A11 -> B1 double b3 = DSClass.AND(DSClass.AND(DSClass.AND(a1, a3), a5), a7) * CF[(int)IOTEventType.DropPacketMaliciously]; DSClass ds = new DSClass(pow(IOTDropPacketEventType.END)); ds.SetM(pow(IOTDropPacketEventType.NotDropPacket), b0); ds.SetM(pow(IOTDropPacketEventType.NotDropPacketButNotReceivePacket), b1); ds.SetM(pow(IOTDropPacketEventType.DropPacketDueToBandwith), b2); ds.SetM(pow(IOTDropPacketEventType.DropPacketMaliciously), b3); ds.SetM(pow(IOTDropPacketEventType.END) - 1, 1 - b0 - b1 - b2 - b3); ds.Cal(); //ds.Output(); //此处,我们先过滤一些正常事件,否则事件太多了 if (ds.b[pow(IOTEventType.NotDropPacket)] < global.NormalBelief || ds.p[pow(IOTEventType.NotDropPacket)] < global.NormalPlausibility)//确实是攻击,恶意事件的信念大于正常事件,或恶意事件的信念大于某一个阈值 { string pkgIdent = p.pkg.Prev + "-" + p.pkg.Next + global.PacketSeq; IOTEventTrustResult r = new IOTEventTrustResult(node, pkgIdent, IOTEventCategoryType.DropPacket, ds); r.totalCount = eventCount[node]; r.app = p.pkg.AppId; list.Add(r); } } return list; }
//根据节点事件的类型来推导节点的可信度 static IOTNodeTrustResult DeduceNodeTrustByCategories(int node, int app, HashDSClass hashedCaterogies, int[] confirmedCount, int[] totalCount) { IOTGlobal global = (IOTGlobal)IOTGlobal.getInstance(); IOTNodeTrustResult nodeTrust = new IOTNodeTrustResult(node, app); double c1 = DSClass.OR(hashedCaterogies.GetM(IOTEventTrust.pow(IOTEventType.NotDropPacket)), hashedCaterogies.GetM(IOTEventTrust.pow(IOTEventType.NotModifyPacket))); //Normal double c2 = DSClass.OR(hashedCaterogies.GetM(IOTEventTrust.pow(IOTEventType.NotDropPacketButNotReceivePacket)), hashedCaterogies.GetM(IOTEventTrust.pow(IOTEventType.ModifyPacketDueToNodeFaulty))); //Node faulty double c3 = DSClass.OR(hashedCaterogies.GetM(IOTEventTrust.pow(IOTEventType.DropPacketDueToBandwith)), hashedCaterogies.GetM(IOTEventTrust.pow(IOTEventType.NotModifyPacketButNetworkFaulty))); //Env faulty double c4 = DSClass.OR(hashedCaterogies.GetM(IOTEventTrust.pow(IOTEventType.DropPacketMaliciously)), hashedCaterogies.GetM(IOTEventTrust.pow(IOTEventType.ModifyPacketMaliciously))); //Maliciously DSClass ds = new DSClass(pow(IOTNodeType.COUNT)); ds.SetM(DSClass.pow((int)IOTNodeType.NORMAL), c1); ds.SetM(DSClass.pow((int)IOTNodeType.NODE_FAULTY), c2); ds.SetM(DSClass.pow((int)IOTNodeType.ENV_FAULTY), c3); ds.SetM(DSClass.pow((int)IOTNodeType.MALICIOUS), c4); ds.Normalize(); ds.Cal(); nodeTrust.ds = ds; for (int i = 0; i < totalCount.Length; i++) { IOTEventCategoryType categoryType = (IOTEventCategoryType)i; int start = IOTEventTrustResult.GetStartTypeByCategory(categoryType); int count = IOTEventTrustResult.GetCountByCategory(categoryType); for (int j = 0; j < count; j++) { int iType = start + j; IOTEventType eventType = (IOTEventType)iType; int confirmed = confirmedCount[iType]; if (!nodeTypeMapping.ContainsKey(eventType)) throw new Exception("No such a event type defined"); if (totalCount[i] == 0) continue; nodeTrust.confirmed[(int)nodeTypeMapping[eventType]] += confirmed; if (ds.b[DSClass.pow(iType)] > global.BeliefThrehold && ds.p[DSClass.pow(iType)] > global.PlausibilityThrehold) nodeTrust.confirmedRate[iType] = (double)confirmed / totalCount[i]; } } return nodeTrust; }
static IOTEventTrustCategoryResult CombineToCategory(int node, string ident, IOTEventCategoryType category, List<IOTEventTrustResult> events) { IOTEventTrustResult e0 = events[0]; int len = e0.ds.m.Length; int app = e0.app; DSClass ds = null; IOTEventTrustCategoryResult r = new IOTEventTrustCategoryResult(node, app, category, null); int categoryCount = IOTEventTrustResult.GetCountByCategory(category); foreach (IOTEventTrustResult e in events) { if (e.app != e0.app || e.node != e0.node || e.category != e0.category) throw new Exception(string.Format( "Error: app and node not match: {0}-{1}->{2}-{3}", e0.app, e0.node, e.app, e.node)); } //先计算归一化因子 int totalweight = 0; double[] weights = new double[events.Count]; foreach (IOTEventTrustResult e in events) { totalweight += e.nodeReportCount; } //调整权重 DSClass[] d = new DSClass[events.Count]; for (int i = 0; i < events.Count; i++) { weights[i] = (double)events[i].nodeReportCount / totalweight; d[i] = events[i].ds; } //进行正交运算 ds = DSClass.CombineWithWeight(d, weights); //计算每种类型在总的事件中确认的比例 ds.Normalize(); ds.Cal(); r.ds = ds; foreach (IOTEventTrustResult e in events) { for (int i = 0; i < categoryCount; i++) { IOTEventType type = (IOTEventType)i; /* if (!IOTEventTrustResult.confirmBeliefThrehold.ContainsKey(type)) continue; double rateThrehold = IOTEventTrustResult.confirmBeliefThrehold[type]; **/ IOTGlobal global = (IOTGlobal)IOTGlobal.getInstance(); double beliefThrehold = global.BeliefThrehold; double plausibilityThrehold = global.PlausibilityThrehold; int offset = IOTEventTrustResult.GetOffsetByCategory(e.category, type); ////计算某类型的事件发生的次数,前提是该事件正交结果可能是发生的 if (e.ds.b[DSClass.pow(offset)] > beliefThrehold && e.ds.p[DSClass.pow(offset)] > plausibilityThrehold //&& ds.b[DSClass.pow(offset)] > beliefThrehold //&& ds.p[DSClass.pow(offset)] > plausibilityThrehold ) r.confirmedEventNums[i]++; } if (e.totalEventCount > r.totalEventCount) r.totalEventCount = e.totalEventCount; } return r; }
public static DSClass CombineWithWeight(DSClass[] d, double[] weights) { int n = d[0].length; DSClass ds = new DSClass(n); for (int i = 0; i < d.Length;i++ ) { DSClass ds1 = d[i]; double w = weights[i]; if (ds1.length != n) throw new Exception("ds length not equal."); for (int j = 0; j < ds1.length; j++) ds.m[j] += ds1.m[j] * w; } return ds; }
static List<IOTEventTrustResult> DeduceDropPacketMaliciously(int selfId, HashSet<IOTPhenomemon> observedPhenomemons, double currentTime) { IOTGlobal global = (IOTGlobal)Global.getInstance(); //每个节点处理该类事件的次数 int[] eventCount = new int[global.readerNum]; List<IOTEventTrustResult> list = new List<IOTEventTrustResult>(); foreach (IOTPhenomemon p in observedPhenomemons) { double a1, a2, a3, a4, a5, a6, a7, a19, a27; if (p.likehood <= global.SmallValue) continue; if (p.type != IOTPhenomemonType.RECV_PACKET) continue; else if (p.pkg.Dst == p.nodeId) continue; else if (selfId == p.nodeId)//自己不检查自己的可信度 continue; else if (global.readers[p.nodeId].IsGateway) continue; else if (currentTime - p.start < global.checkPhenomemonTimeout) continue; int node = p.nodeId; eventCount[node]++; a1 = p.likehood; //likehood of receiving a packet at time p.start a2 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.SEND_PACKET, node, p.start, p.start + global.sendPacketTimeout, p.pkg); a3 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.NOT_SEND_PACKET, node, p.start, Scheduler.getInstance().currentTime, p.pkg); a4 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.BANDWIDTH_BUSY, selfId, p.start - global.checkPhenomemonTimeout, Scheduler.getInstance().currentTime); //如果对带宽占用没有知识,则正反都设置为未知。 a5 = 0.9 - a4; a6 = Utility.Max(new double[]{ ConditionHappened(observedPhenomemons, IOTPhenomemonType.MOVE_FAST, node, p.start - global.checkPhenomemonTimeout, Scheduler.getInstance().currentTime), ConditionHappened(observedPhenomemons, IOTPhenomemonType.MOVE_FAST, p.pkg.Prev, p.start - global.checkPhenomemonTimeout, Scheduler.getInstance().currentTime), ConditionHappened(observedPhenomemons, IOTPhenomemonType.MOVE_FAST, selfId, p.start - global.checkPhenomemonTimeout, Scheduler.getInstance().currentTime) }); a7 = 0.9 - a6; a19 = Math.Max(FarDistanceLikehood(selfId, node, p.pkg.DstType==NodeType.OBJECT), FarDistanceLikehood(selfId, p.pkg.Prev, false)); a27 = 0.9 - a19; //A1 AND A2 AND A7 AND A11 -> B1 double b0 = DSClass.AND(a1, a2) * CF[(int)IOTEventType.NotDropPacket]; //A1 AND A2 AND A7 AND A11 -> B1 double b1 = DSClass.AND(DSClass.AND(a1, a3), DSClass.OR(DSClass.OR(a4, a6), a19)) * CF[(int)IOTEventType.NotDropPacketButNotReceivePacket]; //A1 AND A2 AND A7 AND A11 -> B1 double b2 = DSClass.AND(DSClass.AND(a1, a3), a4) * CF[(int)IOTEventType.DropPacketDueToBandwith]; //A1 AND A2 AND A7 AND A11 -> B1 double b3 = DSClass.AND(DSClass.AND(DSClass.AND(DSClass.AND(a1, a3), a5), a7), a27) * CF[(int)IOTEventType.DropPacketMaliciously]; DSClass ds = new DSClass(pow(IOTDropPacketEventType.END)); ds.SetM(pow(IOTDropPacketEventType.NotDropPacket), b0); ds.SetM(pow(IOTDropPacketEventType.NotDropPacketButNotReceivePacket), b1); ds.SetM(pow(IOTDropPacketEventType.DropPacketDueToBandwith), b2); ds.SetM(pow(IOTDropPacketEventType.DropPacketMaliciously), b3); ds.SetM(pow(IOTDropPacketEventType.END) - 1, 1 - b0 - b1 - b2 - b3); ds.Cal(); //ds.Output(); //此处,我们先过滤一些正常事件,否则事件太多了 if (ds.b[pow(IOTEventType.NotDropPacket) + pow(IOTEventType.NotDropPacketButNotReceivePacket)] < global.NormalBelief && ds.p[pow(IOTEventType.NotDropPacket) + pow(IOTEventType.NotDropPacketButNotReceivePacket)] < global.NormalPlausibility)//确实是攻击,恶意事件的信念大于正常事件,或恶意事件的信念大于某一个阈值 { string pkgIdent = p.pkg.Prev + "->" + p.pkg.Next + "[" + p.pkg.PrevSenderSeq + "]"; IOTEventTrustResult r = new IOTEventTrustResult(node, selfId, pkgIdent, IOTEventCategoryType.DropPacket, ds); r.totalEventCount = eventCount[node]; r.app = p.pkg.AppId; list.Add(r); Console.WriteLine("{0:F4} reader{1} think reader{2} is not normal.", currentTime, selfId, p.nodeId); //r.ds.Output(); } } return list; }
static List<IOTEventTrustResult> DeduceModifyPacketMaliciously(int selfId, HashSet<IOTPhenomemon> observedPhenomemons, double currentTime) { IOTGlobal global = (IOTGlobal)Global.getInstance(); //每个节点处理该类事件的次数 int[] eventCount = new int[global.readerNum]; List<IOTEventTrustResult> list = new List<IOTEventTrustResult>(); foreach (IOTPhenomemon p in observedPhenomemons) { double a1, a2, a3, a4, a6, a8_9; if(p.likehood <= global.SmallValue) continue; if (p.type != IOTPhenomemonType.RECV_PACKET) continue; else if (p.pkg.Dst == p.nodeId) continue; else if (selfId == p.nodeId)//自己不检查自己的可信度 continue; else if (global.readers[p.nodeId].IsGateway) continue; else if (currentTime - p.start < global.checkPhenomemonTimeout) continue; int node = p.nodeId; eventCount[node]++; a1 = p.likehood; //likehood of receiving a packet at time p.start a2 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.SEND_PACKET, node, p.start, p.start + global.sendPacketTimeout, p.pkg); a3 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.NOT_SEND_PACKET, node, p.start, Scheduler.getInstance().currentTime, p.pkg); a4 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.BANDWIDTH_BUSY, selfId, p.start - global.checkPhenomemonTimeout, Scheduler.getInstance().currentTime); a6 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.MOVE_FAST, node, p.start - global.checkPhenomemonTimeout, Scheduler.getInstance().currentTime); a8_9 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.SAME_PACKET_HEADER, selfId, p.start, p.start + global.sendPacketTimeout, p.pkg); double b4 = DSClass.AND(a1, a2) * CF[(int)IOTEventType.NotModifyPacket]; double b5 = DSClass.AND(DSClass.AND(DSClass.AND(a1, a3), a8_9), a4) * CF[(int)IOTEventType.NotModifyPacketButNetworkFaulty]; double b6 = DSClass.AND(DSClass.AND(a1, a3), a8_9) * CF[(int)IOTEventType.ModifyPacketDueToNodeFaulty]; double b7 = DSClass.AND(DSClass.AND(a1, a3), a8_9) * CF[(int)IOTEventType.ModifyPacketMaliciously]; DSClass ds = new DSClass(pow(IOTModifyPacketEventType.END)); ds.SetM(pow(IOTModifyPacketEventType.NotModifyPacket), b4); ds.SetM(pow(IOTModifyPacketEventType.NotModifyPacketButNetworkFaulty), b5); ds.SetM(pow(IOTModifyPacketEventType.ModifyPacketDueToNodeFaulty), b6); ds.SetM(pow(IOTModifyPacketEventType.ModifyPacketMaliciously), b7); ds.SetM(pow(IOTModifyPacketEventType.END) - 1, 1 - b4 - b5 - b6 - b7); ds.Cal(); //ds.Output(); //此处,我们先过滤一些正常事件,否则事件太多了 if (ds.b[pow(IOTModifyPacketEventType.NotModifyPacket)] < global.NormalBelief && ds.p[pow(IOTModifyPacketEventType.NotModifyPacket)] < global.NormalPlausibility)//确实是攻击,恶意事件的信念大于正常事件,或恶意事件的信念大于某一个阈值 { string pkgIdent = p.pkg.Prev + "->" + p.pkg.Next + "[" + p.pkg.PrevSenderSeq + "]"; IOTEventTrustResult r = new IOTEventTrustResult(node, selfId, pkgIdent, IOTEventCategoryType.ModifyPacket, ds); r.totalEventCount = eventCount[node]; r.app = p.pkg.AppId; list.Add(r); } } return list; }
public IOTNodeTrustResult(int node, int org) { this.nodeId = node; this.orgId = org; this.ds = null; this.confirmed = new int[(int)IOTNodeType.COUNT]; this.confirmedRate = new double[(int)IOTEventType.COUNT]; }
public IOTEventTrustCategoryResult(int node, int app, IOTEventCategoryType category, DSClass ds) { this.node = node; this.ds = ds; this.category = category; this.categoryIdent = node + "-" + category; this.app = app; this.confirmedEventNums = new int[(int)IOTEventType.COUNT]; //TODO app }
static List<IOTEventTrustResult> DeduceDropCommandMaliciously(int selfId, List<IOTPhenomemon> observedPhenomemons, double currentTime) { IOTGlobal global = (IOTGlobal)Global.getInstance(); //每个节点处理该类事件的次数 int[] eventCount = new int[global.readerNum]; List<IOTEventTrustResult> list = new List<IOTEventTrustResult>(); for (int i = 0; i < observedPhenomemons.Count; i++) { double a15_16, a17_20, a18_20, a19, a6, a7; IOTPhenomemon p = observedPhenomemons[i]; if (p.likehood <= global.SmallValue) continue; if (p.type != IOTPhenomemonType.RECV_PACKET) continue; else if (p.pkg.Dst != p.nodeId ||p.pkg.Type != PacketType.COMMAND) continue; int node = p.nodeId; eventCount[node]++; a15_16 = p.likehood; //likehood of receiving a packet at time p.start a6 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.MOVE_FAST, node, p.start - global.checkPhenomemonTimeout, p.start + global.checkPhenomemonTimeout); if (Utility.DoubleEqual(a6, global.SmallValue)) a7 = global.SmallValue; else a7 = 0.9 - a6; a17_20 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.SEND_COMMAND, node, p.start, p.start + global.sendPacketTimeout, p, ComparePhenomemonByExactTag); a18_20 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.NOT_SEND_COMMAND, node, p.start, p.start + global.sendPacketTimeout, p.pkg); a19 = ConditionHappened(observedPhenomemons, IOTPhenomemonType.DIST_FAR, node, p.start - global.checkPhenomemonTimeout, p.start + global.checkPhenomemonTimeout); double b14 = DSClass.AND(a15_16, a17_20) * CF[(int)IOTEventType.NotDropCommand]; double b15 = DSClass.AND(DSClass.AND(a15_16, a18_20), DSClass.OR(a6, a19)) * CF[(int)IOTEventType.NotDropCommandButNotDetected]; double b16 = DSClass.AND(DSClass.AND(a15_16, a18_20), a7) * CF[(int)IOTEventType.DropCommandMaliciously]; DSClass ds = new DSClass(pow(IOTDropCommandEventType.END)); ds.SetM(pow(IOTDropCommandEventType.NotDropCommand), b14); ds.SetM(pow(IOTDropCommandEventType.NotDropCommandButNotDetected), b15); ds.SetM(pow(IOTDropCommandEventType.DropCommandMaliciously), b16); ds.SetM(pow(IOTDropCommandEventType.END) - 1, 1 - b14 - b15 - b16); ds.Cal(); //ds.Output(); //此处,我们先过滤一些正常事件,否则事件太多了 if (ds.b[pow(IOTDropCommandEventType.NotDropCommand)] < global.NormalBelief || ds.p[pow(IOTDropCommandEventType.NotDropCommand)] < global.NormalPlausibility)//确实是攻击,恶意事件的信念大于正常事件,或恶意事件的信念大于某一个阈值 { string pkgIdent = p.pkg.Prev + "-" + p.pkg.Next + global.PacketSeq; IOTEventTrustResult r = new IOTEventTrustResult(node, pkgIdent, IOTEventCategoryType.DropCommand, ds); r.totalCount = eventCount[node]; r.app = p.pkg.AppId; list.Add(r); } } return list; }