Example #1
0
        private void ParseAndAnalyze(string php, IVulnerabilityStorage storage)
        {
            FunctionsHandler fh = new FunctionsHandler(Config.FuncSpecSettings);

            fh.LoadJsonSpecifications();

            var extractedFuncs = PHPParseUtils.ParseAndIterate <ClassAndFunctionExtractor>(php, Config.PHPSettings.PHPParserPath).Functions;

            fh.CustomFunctions.AddRange(extractedFuncs);

            var cfg = PHPParseUtils.ParseAndIterate <CFGCreator>(php, Config.PHPSettings.PHPParserPath).Graph;

            var incResolver = new IncludeResolver(new List <File>());
            var fileStack   = new Stack <File>();

            fileStack.Push(new File()
            {
                FullPath = @"C:\TestFile.txt"
            });
            var condAnalyser = new ConditionTaintAnalyser(AnalysisScope.File, incResolver, fileStack, fh);

            var funcMock      = new Mock <Func <ImmutableVariableStorage, IIncludeResolver, AnalysisScope, AnalysisStacks, ImmutableVariableStorage> >();
            var blockAnalyzer = new TaintBlockAnalyzer(storage, incResolver,
                                                       AnalysisScope.File, funcMock.Object, new AnalysisStacks(fileStack), new FunctionAndMethodAnalyzerFactory(), fh);
            var immutableInitialTaint = new DefaultTaintProvider().GetTaint();
            var cfgTaintAnalysis      = new TaintAnalysis(blockAnalyzer, condAnalyser, immutableInitialTaint);
            var taintAnalysis         = new CFGTraverser(new ForwardTraversal(), cfgTaintAnalysis, new QueueWorklist());

            taintAnalysis.Analyze(cfg);
        }
Example #2
0
        private void AssertNoOfVulnsInMultipleCodeFiles(Tuple <string, string>[] codeFiles, int numberOfVulns)
        {
            FunctionsHandler fh = new FunctionsHandler(Config.FuncSpecSettings);

            fh.LoadJsonSpecifications();

            var vulnStorage = new Mock <IVulnerabilityStorage>();

            var parsedFiles = codeFiles.Select(code => new File(PHPParseUtils.ParsePHPCode(code.Item2, Config.PHPSettings.PHPParserPath))
            {
                FullPath = code.Item1,
                CFG      = PHPParseUtils.ParseAndIterate <CFGCreator>(code.Item2, Config.PHPSettings.PHPParserPath).Graph
            }).ToArray();

            Func <ImmutableVariableStorage, IIncludeResolver, AnalysisScope, AnalysisStacks, ImmutableVariableStorage> fileTaintAnalyzer = null;

            fileTaintAnalyzer = (varStorage, inclResolver, scope, stacks) =>
            {
                Preconditions.NotNull(varStorage, "varStorage");
                Preconditions.NotNull(inclResolver, "inclResolver");
                var fileToAnalyze = stacks.IncludeStack.Peek();
                var blockAnalyzer = new TaintBlockAnalyzer(vulnStorage.Object, inclResolver,
                                                           scope, fileTaintAnalyzer, stacks, new FunctionAndMethodAnalyzerFactory(), fh);
                var condAnalyser     = new ConditionTaintAnalyser(scope, inclResolver, stacks.IncludeStack, fh);
                var cfgTaintAnalysis = new PHPAnalysis.Analysis.CFG.TaintAnalysis(blockAnalyzer, condAnalyser, varStorage);
                var analyzer         = new CFGTraverser(new ForwardTraversal(), cfgTaintAnalysis, new ReversePostOrderWorkList(fileToAnalyze.CFG));

                analyzer.Analyze(fileToAnalyze.CFG);
                return(cfgTaintAnalysis.Taints[fileToAnalyze.CFG.Vertices.Single(block => block.IsLeaf)].Out[EdgeType.Normal]);
            };

            foreach (var file in parsedFiles)
            {
                var inclusionResolver = new IncludeResolver(parsedFiles);
                var fileStack         = new Stack <File>();
                fileStack.Push(file);
                var immutableInitialTaint = new DefaultTaintProvider().GetTaint();

                var stacks = new AnalysisStacks(fileStack);
                fileTaintAnalyzer(immutableInitialTaint, inclusionResolver, AnalysisScope.File, stacks);
            }

            vulnStorage.Verify(x => x.AddVulnerability(It.IsAny <IVulnerabilityInfo>()), Times.Exactly(numberOfVulns));
        }
Example #3
0
        public void ReversePostOrderAlgorithm1()
        {
            string phpCode = @"<?php 
$i = 0; 

if (true) {
$x;
} else if (true) {
$y;
} else {
$sdf;
}
echo $x;";
            var    cfg     = PHPParseUtils.ParseAndIterate <CFGCreator>(phpCode, Config.PHPSettings.PHPParserPath).Graph;

            new CFGPruner().Prune(cfg);

            //cfg.VisualizeGraph("cfg", Config.GraphSettings);

            var postOrder = new ReversePostOrderAlgorithm(cfg).CalculateReversePostOrder();
        }
 private CFGCreator ParseAndBuildCFG(string php)
 {
     return(PHPParseUtils.ParseAndIterate <CFGCreator>(php, Config.PHPSettings.PHPParserPath));
 }
Example #5
0
 private ClassAndFunctionExtractor ParseAndExtract(string php)
 {
     return(PHPParseUtils.ParseAndIterate <ClassAndFunctionExtractor>(php, Config.PHPSettings.PHPParserPath));
 }