private static NodePairings GetPairings(List <LabeledVertex> sourceGraphLabeled, List <LabeledVertex> imageGraphLabeled)
        {
            NodePairings nodePairings = new NodePairings(sourceGraph: sourceGraphLabeled, imageGraph: imageGraphLabeled);

            Random rnd = new Random();
            Dictionary <Code[], IEnumerable <LabeledVertex> > sourceGraphGrouped;
            Dictionary <Code[], IEnumerable <LabeledVertex> > imageGraphGrouped;

            GetGrouped(sourceGraphLabeled: sourceGraphLabeled, imageGraphLabeled: imageGraphLabeled, sourceGraphGrouped: out sourceGraphGrouped, imageGraphGrouped: out imageGraphGrouped);

            //Parallel.ForEach(sourceGraphGrouped.Keys, (codeGroup) =>
            foreach (var codeGroup in sourceGraphGrouped.Keys)
            {
                //not gonna lock for now
                foreach (var sourceGraphVertex in sourceGraphGrouped[codeGroup])
                {
                    VertexMatch winningPair;
                    if (!imageGraphGrouped.ContainsKey(codeGroup))
                    {
                        winningPair = null;
                    }
                    //Parallel.ForEach(imageGraphGrouped[codeGroup].OrderBy(x => rnd.Next()).ToList(), (imageGraphCandidate) =>

                    else
                    {
                        var vertexPossiblePairings = new ConcurrentBag <VertexMatch>();
                        foreach (var imageGraphCandidate in imageGraphGrouped[codeGroup].OrderBy(x => rnd.Next()).ToList())
                        {
                            vertexPossiblePairings.Add(new VertexMatch()
                            {
                                SourceGraphVertex = sourceGraphVertex, ImageGraphVertex = imageGraphCandidate, Score = VertexScorer.GetScore(sourceGraphVertex, imageGraphCandidate, nodePairings)
                            });
                        }
                        winningPair = vertexPossiblePairings.Where(x => x.Score > 0).OrderByDescending(x => x.Score).ThenBy(x => rnd.Next()).FirstOrDefault();
                    }
                    //});
                    if (winningPair != null)
                    {
                        lock (nodePairings.Pairings)
                        {
                            winningPair.NormalizedScore = winningPair.Score / VertexScorer.GetSelfScore(winningPair.SourceGraphVertex);
                            nodePairings.Pairings[winningPair.ImageGraphVertex].Add(winningPair);
                            nodePairings.TotalScore += winningPair.Score;
                        }
                    }
                }
            }
            // });

            return(nodePairings);
        }
        public static NodePairings GetGraphSelfScore(List <LabeledVertex> labeledGraph)
        {
            NodePairings nodePairings = new NodePairings(imageGraph: labeledGraph, sourceGraph: labeledGraph);

            foreach (var node in labeledGraph)
            {
                double score = VertexScorer.GetSelfScore(node);
                nodePairings.Pairings[node].Add(new VertexMatch()
                {
                    SourceGraphVertex = node, ImageGraphVertex = node, Score = score, NormalizedScore = 1
                });
                nodePairings.TotalScore += score;
            }
            return(nodePairings);
        }
        public static NodePairings GetDistance(List <InstructionNode> sourceGraph, List <InstructionNode> imageGraph)
        {
            List <LabeledVertex> sourceGraphLabeled = GetLabeled(sourceGraph);
            List <LabeledVertex> imageGraphLabeled  = GetLabeled(imageGraph);
            var retNodes    = sourceGraph.Where(x => VertexScorer.OutDataCodes.Contains(x.Instruction.OpCode.Code));
            var backRetTree = retNodes.SelectMany(x => BackSearcher.GetBackDataTree(x)).Distinct().Concat(retNodes).ToList();

            backRetTree.AddRange(backRetTree.ToList().SelectMany(x => x.BranchProperties.Branches.Select(y => y.OriginatingNode)));
            var backRetTreeIndexes = backRetTree.Select(x => x.InstructionIndex).ToList();

            foreach (var backRetNode in sourceGraphLabeled.Where(x => backRetTreeIndexes.Contains(x.Index)))
            {
                backRetNode.IsInReturnBackTree = true;
            }
            NodePairings bestMatch  = GetPairings(sourceGraphLabeled, imageGraphLabeled);
            object       lockObject = new object();

            //TODO change back to 10
            Parallel.For(1, 1, (i) =>
            {
                NodePairings pairing = GetPairings(sourceGraphLabeled, imageGraphLabeled);
                if (pairing.TotalScore > 1)
                {
                    throw new Exception("");
                }
                lock (lockObject)
                {
                    if (pairing.TotalScore > bestMatch.TotalScore)
                    {
                        bestMatch = pairing;
                    }
                }
            });
            bestMatch.SourceSelfPairings = GetGraphSelfScore(sourceGraphLabeled);
            bestMatch.ImageSelfPairings  = GetGraphSelfScore(imageGraphLabeled);
            return(bestMatch);
        }
Beispiel #4
0
        public static int GetEdgeMatchScore(LabeledEdge sourceEdge, LabeledEdge imageEdge, SharedSourceOrDest sharingSourceOrDest, NodePairings pairings, IndexImportance indexImportance, bool usePastPairings = true)
        {
            int           edgeMatchScore = 0;
            LabeledVertex sourceEdgeVertex;
            LabeledVertex imageEdgeVertex;

            if (sharingSourceOrDest == SharedSourceOrDest.Source)
            {
                sourceEdgeVertex = sourceEdge.DestinationVertex;
                imageEdgeVertex  = imageEdge.DestinationVertex;
            }
            else
            {
                sourceEdgeVertex = sourceEdge.SourceVertex;
                imageEdgeVertex  = imageEdge.SourceVertex;
            }

            if (imageEdge.Index == sourceEdge.Index || indexImportance == IndexImportance.NotImportant)
            {
                edgeMatchScore += EdgeScorePoints.IndexMatch;
            }
            if (usePastPairings)
            {
                lock (pairings)
                {
                    if (pairings.Pairings[imageEdgeVertex].Any(x => x.ImageGraphVertex == sourceEdgeVertex))
                    {
                        edgeMatchScore += EdgeScorePoints.TargetVertexArePaired;
                    }
                }
            }
            if (sourceEdgeVertex.Opcode == imageEdgeVertex.Opcode)
            {
                edgeMatchScore += EdgeScorePoints.TargetVertexCodeExactMatch;
            }
            else if (CodeGroups.AreSameGroup(sourceEdgeVertex.Opcode, imageEdgeVertex.Opcode))
            {
                edgeMatchScore += EdgeScorePoints.TargetVertexCodeFamilyMatch;
            }
            if (sourceEdge.EdgeType != EdgeType.ProgramFlowAffecting)
            {
                edgeMatchScore *= ImportantEdgeTypeMultiplier;
            }
            if (sourceEdge.SourceVertex.IsInReturnBackTree && sourceEdge.DestinationVertex.IsInReturnBackTree)
            {
                //edgeMatchScore *= ImportantEdgeTypeMultiplier;
            }
            return(edgeMatchScore);
        }
Beispiel #5
0
        public static int ScoreEdges(List <LabeledEdge> sourceVertexEdges, List <LabeledEdge> imageVertexEdges, NodePairings pairings, SharedSourceOrDest sharedSourceOrDest)
        {
            int    totalScore               = 0;
            var    edgePairings             = new List <EdgeMatch>();
            var    unmachedImageVertexEdges = new List <LabeledEdge>(imageVertexEdges);
            Random rnd = new Random();

            foreach (var sourceVertexEdge in sourceVertexEdges.OrderBy(x => rnd.Next()))
            {
                var           pairingScores = new List <EdgeMatch>();
                LabeledVertex vertexToMatch;
                if (sharedSourceOrDest == SharedSourceOrDest.Source)
                {
                    vertexToMatch = sourceVertexEdge.DestinationVertex;
                }
                else
                {
                    vertexToMatch = sourceVertexEdge.SourceVertex;
                }
                Func <LabeledEdge, bool> baseCondition = x => x.EdgeType == sourceVertexEdge.EdgeType;
                IndexImportance          indexImportance;
                if (sourceVertexEdge.EdgeType == EdgeType.ProgramFlowAffecting)
                {
                    indexImportance = IndexImportance.Important;
                }
                else
                {
                    indexImportance = opCodeInfo.GetIndexImportance(sourceVertexEdge.DestinationVertex.Opcode);
                }
                Func <LabeledEdge, bool> predicate;
                if (indexImportance == IndexImportance.Critical)
                {
                    predicate = x => baseCondition(x) && x.Index == sourceVertexEdge.Index;
                }
                else
                {
                    predicate = x => baseCondition(x);
                }
                var relevantImageVertexEdges = unmachedImageVertexEdges.Where(predicate);
                relevantImageVertexEdges.ForEach(imageVertexEdge => pairingScores.Add(new EdgeMatch()
                {
                    ImageGraphEdge = imageVertexEdge, SourceVertexEdge = sourceVertexEdge, Score = GetEdgeMatchScore(sourceVertexEdge, imageVertexEdge, sharedSourceOrDest, pairings, indexImportance)
                }));
                var winningMatchGroup = pairingScores.Where(x => x.Score > 0).GroupBy(x => x.Score).OrderByDescending(x => x.Key).FirstOrDefault();
                if (winningMatchGroup == null || !winningMatchGroup.Any())
                {
                    edgePairings.Add(new EdgeMatch()
                    {
                        SourceVertexEdge = sourceVertexEdge, ImageGraphEdge = null, Score = 0
                    });
                }
                else
                {
                    var winningMatch = winningMatchGroup.OrderBy(x => rnd.Next()).First();
                    unmachedImageVertexEdges.Remove(winningMatch.ImageGraphEdge);
                    winningMatch.Score = GetEdgeMatchScore(sourceVertexEdge, winningMatch.ImageGraphEdge, sharedSourceOrDest, pairings, indexImportance, false);
                    var scoreRelatedToMax = (double)winningMatch.Score / GetEdgeMatchScore(sourceVertexEdge, sourceVertexEdge, sharedSourceOrDest, pairings, IndexImportance.Important, false);
                    edgePairings.Add(winningMatch);
                    totalScore += winningMatch.Score;
                }
            }
            //totalScore -= unmachedImageVertexEdges.Count * EdgeScorePoints.ExactMatch;
            return(totalScore);
        }
Beispiel #6
0
        public static double GetScore(LabeledVertex sourceGraphVertex, LabeledVertex imageGraphVertex, NodePairings pairings)
        {
            double score = 0;

            if (sourceGraphVertex.Opcode == imageGraphVertex.Opcode)
            {
                score += VertexScorePoints.CodeMatch;
            }
            else
            {
                score += VertexScorePoints.CodeFamilyMatch;
            }
            if (sourceGraphVertex.Operand == imageGraphVertex.Operand)
            {
                score += VertexScorePoints.OperandMatch;
            }
            var backEdgeScore    = EdgeScorer.ScoreEdges(sourceGraphVertex.BackEdges, imageGraphVertex.BackEdges, pairings, SharedSourceOrDest.Dest);
            var forwardEdgeScore = EdgeScorer.ScoreEdges(sourceGraphVertex.ForwardEdges, imageGraphVertex.ForwardEdges, pairings, SharedSourceOrDest.Source);

            score += backEdgeScore + forwardEdgeScore;
            lock (pairings)
            {
                if (pairings.Pairings[imageGraphVertex].Count > 0)
                {
                    //score -= VertexScorePoints.SingleToMultipleVertexMatchPenalty;
                    score *= 0.9;
                }
            }
            if (OutDataCodes.Contains(sourceGraphVertex.Opcode))
            {
                score *= ImportantCodeMultiplier;
            }
            //if (sourceGraphVertex.IsInReturnBackTree)
            //{
            //    score *= ImportantCodeMultiplier;
            //}
            var scoreToDouble = score / GetSelfScore(sourceGraphVertex);

            return(score);
        }