Esempio n. 1
0
 private void WriteDependencies(AppProfile app)
 {
     foreach (string s in app.MetaData.UniqueDependencies)
     {
         TextWriter.WriteLine(s);
     }
 }
Esempio n. 2
0
        public override void WriteApp(AppProfile app)
        {
            // Store the results here temporarily building up a list of items in the desired order etc.
            Dictionary <string, object> itemList = new Dictionary <string, object>();;

            if (app.SimpleTagsOnly)
            {
                List <string> keys = new List <string>(app.MetaData.UniqueTags);
                itemList.Add("tags", keys);
                keys.Sort();
            }
            else
            {
                if (!app.ExcludeRollup)
                {
                    itemList.Add("AppProfile", app);
                }

                //matches are added this way to avoid output of entire set of MatchItem properties which include full rule/patterns/cond.
                List <MatchItems> matches = new List <MatchItems>();

                foreach (MatchRecord match in app.MatchList)
                {
                    MatchItems matchItem = new MatchItems(match);
                    matches.Add(matchItem);
                }

                itemList.Add("matchDetails", matches);
            }

            jsonResult.Add(itemList);
        }
Esempio n. 3
0
        public void WriteAppMeta(AppProfile app)
        {
            //write predefined characteristics
            TextWriter.WriteLine(MakeHeading("Project Info"));
            TextWriter.WriteLine("Name: {0}", app.MetaData.ApplicationName + " " + app.MetaData.SourceVersion);
            TextWriter.WriteLine("Description: {0}", app.MetaData.Description);
            TextWriter.WriteLine("Source path: {0}", app.SourcePath);
            TextWriter.WriteLine("Authors: {0}", app.MetaData.Authors);
            TextWriter.WriteLine("Last Updated: {0}", app.MetaData.LastUpdated);
            TextWriter.WriteLine("Languages: {0}", StringList(app.MetaData.Languages));
            TextWriter.WriteLine(MakeHeading("Scan Settings"));
            TextWriter.WriteLine("AppInspector version: {0}", app.Version);
            TextWriter.WriteLine("Rules path: {0}", StringList(app.MetaData.RulePaths));
            TextWriter.WriteLine("Run Arguments: {0}", app.Args);
            TextWriter.WriteLine("Date scanned: {0}", app.DateScanned);
            TextWriter.WriteLine(MakeHeading("Source Info"));
            TextWriter.WriteLine("Application type: {0}", StringList(app.MetaData.AppTypes));
            TextWriter.WriteLine("Package types: {0}", StringList(app.MetaData.PackageTypes));
            TextWriter.WriteLine("File extensions: {0}", StringList(app.MetaData.FileExtensions));
            TextWriter.WriteLine(MakeHeading("Detetected Targets"));
            TextWriter.WriteLine("Output types: {0}", StringList(app.MetaData.Outputs));
            TextWriter.WriteLine("OS Targets: {0}", StringList(app.MetaData.OSTargets));
            TextWriter.WriteLine("CPU Targets: {0}", StringList(app.MetaData.CPUTargets));
            TextWriter.WriteLine("Cloud targets: {0}", StringList(app.MetaData.CloudTargets));
            TextWriter.WriteLine(MakeHeading("Stats"));
            TextWriter.WriteLine("Files analyzed: {0}", app.MetaData.FilesAnalyzed);
            TextWriter.WriteLine("Files skipped: {0}", app.MetaData.FilesSkipped);
            TextWriter.WriteLine("Total files: {0}", app.MetaData.TotalFiles);
            TextWriter.WriteLine("Detections: {0} in {1} file(s)", app.MetaData.TotalMatchesCount, app.MetaData.FilesAffected);
            TextWriter.WriteLine("Unique detections: {0}", app.MetaData.UniqueMatchesCount);
            TextWriter.WriteLine(MakeHeading("UniqueTags"));
            foreach (string tag in app.MetaData.UniqueTags)
            {
                TextWriter.WriteLine(tag);
            }

            TextWriter.WriteLine(MakeHeading("Select Counters"));
            foreach (TagCounter counter in app.MetaData.TagCounters)
            {
                TextWriter.WriteLine("{0} count: {1}", counter.ShortTag, counter.Count);
            }

            TextWriter.WriteLine(MakeHeading("Customizable tag groups"));
            //iterate through customizable tag group lists
            foreach (string tagGroupCategory in app.KeyedTagInfoLists.Keys)
            {
                List <TagInfo> list = app.KeyedTagInfoLists[tagGroupCategory];
                TextWriter.WriteLine(string.Format("[{0}]", tagGroupCategory));
                foreach (TagInfo tagInfo in list)
                {
                    TextWriter.WriteLine("Tag:{0},Detected:{1},Confidence:{2}", tagInfo.Tag, tagInfo.Detected, tagInfo.Confidence);
                }
            }
        }
Esempio n. 4
0
        public override void WriteApp(AppProfile app)
        {
            //option for tags only vs full match record structure; used tagdiff and tagtest commands
            if (app.SimpleTagsOnly)
            {
                List <string> tags = new List <string>();
                foreach (MatchRecord match in app.MatchList)
                {
                    foreach (string tag in match.Issue.Rule.Tags)
                    {
                        tags.Add(tag);
                    }
                }

                tags.Sort();
                foreach (string tag in tags)
                {
                    TextWriter.WriteLine(tag);
                }
            }
            else
            {
                if (!app.ExcludeRollup)
                {
                    WriteAppMeta(app);
                }

                TextWriter.WriteLine(MakeHeading("Dependencies"));
                WriteDependencies(app);

                TextWriter.WriteLine(MakeHeading("Match Details"));
                foreach (MatchRecord match in app.MatchList)
                {
                    WriteMatch(match);
                }
            }

            TextWriter.WriteLine("\nEnd of report");
        }
Esempio n. 5
0
        /// <summary>
        /// Add default and/or custom rules paths
        /// Iterate paths and add to ruleset
        /// </summary>
        void ConfigRules()
        {
            WriteOnce.SafeLog("AnalyzeCommand::ConfigRules", LogLevel.Trace);

            RuleSet       rulesSet  = new RuleSet(Program.Logger);
            List <string> rulePaths = new List <string>();

            if (!_arg_ignoreDefaultRules)
            {
                rulePaths.Add(Utils.GetPath(Utils.AppPath.defaultRules));
            }

            if (!string.IsNullOrEmpty(_arg_customRulesPath))
            {
                rulePaths.Add(_arg_customRulesPath);
            }

            foreach (string rulePath in rulePaths)
            {
                if (Directory.Exists(rulePath))
                {
                    rulesSet.AddDirectory(rulePath);
                }
                else if (File.Exists(rulePath))
                {
                    rulesSet.AddFile(rulePath);
                }
                else
                {
                    throw new OpException(ErrMsg.FormatString(ErrMsg.ID.CMD_INVALID_RULE_PATH, rulePath));
                }
            }

            //error check based on ruleset not path enumeration
            if (rulesSet.Count() == 0)
            {
                throw new OpException(ErrMsg.GetString(ErrMsg.ID.CMD_NORULES_SPECIFIED));
            }

            //instantiate a RuleProcessor with the added rules and exception for dependency
            _rulesProcessor = new RuleProcessor(rulesSet, _arg_confidence, _arg_outputUniqueTagsOnly, _arg_simpleTagsOnly, Program.Logger);

            if (_arg_outputUniqueTagsOnly)
            {
                List <TagException> tagExceptions;
                if (File.Exists(Utils.GetPath(Utils.AppPath.tagCounterPref)))
                {
                    tagExceptions = JsonConvert.DeserializeObject <List <TagException> >(File.ReadAllText(Utils.GetPath(Utils.AppPath.tagCounterPref)));
                    string[] exceptions = new string[tagExceptions.Count];
                    for (int i = 0; i < tagExceptions.Count; i++)
                    {
                        exceptions[i] = tagExceptions[i].Tag;
                    }
                    _rulesProcessor.UniqueTagExceptions = exceptions;
                }
            }

            _appProfile      = new AppProfile(_arg_sourcePath, rulePaths, false, _arg_simpleTagsOnly, _arg_outputUniqueTagsOnly);
            _appProfile.Args = "analyze -f " + _arg_fileFormat + " -u " + _arg_outputUniqueTagsOnly.ToString().ToLower() + " -v " +
                               WriteOnce.Verbosity.ToString() + " -x " + _arg_confidence + " -i " + _arg_ignoreDefaultRules.ToString().ToLower();
        }
        /// <summary>
        /// Registers datatypes with html framework liquid and sets up data for use within it and used
        /// with html partial.liquid files that are embedded as resources
        /// </summary>
        /// <param name="app"></param>
        public override void WriteApp(AppProfile app)
        {
            var      htmlTemplateText = File.ReadAllText("html/index.html");
            Assembly test             = Assembly.GetEntryAssembly();

            Template.FileSystem = new EmbeddedFileSystem(Assembly.GetEntryAssembly(), "ApplicationInspector.html.partials");

            RegisterSafeType(typeof(AppProfile));
            RegisterSafeType(typeof(AppMetaData));

            var htmlTemplate = Template.Parse(htmlTemplateText);
            var data         = new Dictionary <string, object>();

            data["AppProfile"] = app;

            //matchitems rather than records created to exclude full rule/patterns/cond.
            List <MatchItems> matches = new List <MatchItems>();

            foreach (MatchRecord match in app.MatchList)
            {
                MatchItems matchItem = new MatchItems(match);
                matches.Add(matchItem);
            }

            data["matchDetails"] = matches;

            var hashData = new Hash();

            hashData["json"] = JsonConvert.SerializeObject(data, Formatting.Indented);
            hashData["application_version"] = Program.GetVersionString();

            //add dynamic sets of groups of taginfo read from preferences for Profile page
            List <TagGroup> tagGroupList = app.GetCategoryTagGroups("profile");

            hashData["groups"] = tagGroupList;

            //add summary values for sorted tags lists of taginfo
            foreach (string outerKey in app.KeyedSortedTagInfoLists.Keys)
            {
                hashData.Add(outerKey, app.KeyedSortedTagInfoLists[outerKey]);
            }

            //add summary metadata lists
            hashData["cputargets"]   = app.MetaData.CPUTargets;
            hashData["apptypes"]     = app.MetaData.AppTypes;
            hashData["packagetypes"] = app.MetaData.PackageTypes;
            hashData["ostargets"]    = app.MetaData.OSTargets;
            hashData["outputs"]      = app.MetaData.Outputs;
            hashData["filetypes"]    = app.MetaData.FileExtensions;
            hashData["tagcounters"]  = app.MetaData.TagCountersUI;

            var htmlResult = htmlTemplate.Render(hashData);

            File.WriteAllText("output.html", htmlResult);

            //writes out json report for convenience and linking to from report page(s)
            String jsonReportPath = Path.Combine("output.json");
            Writer jsonWriter     = WriterFactory.GetWriter("json", jsonReportPath);

            jsonWriter.TextWriter = File.CreateText(jsonReportPath);
            jsonWriter.WriteApp(app);
            jsonWriter.FlushAndClose();

            Utils.OpenBrowser("output.html");
        }
Esempio n. 7
0
 public abstract void WriteApp(AppProfile appCharacteriation);