示例#1
0
        /// <summary>
        /// Dumps the contents of every input assertion list that it can
        /// </summary>
        /// <returns>Status code. 0 => success, non-zero => failure</returns>
        private int DumpInputAssertionLists()
        {
            Console.Error.WriteLine("\nStarting dump of input assertion lists");

            // Start timing
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            InputAssertionListChecker inputAssertionListChecker = new InputAssertionListChecker(m_cache);
            ConcurrentDictionary <CacheError, int> cacheErrors  = new ConcurrentDictionary <CacheError, int>();

            IEnumerable <InputAssertionList> inputAssertionLists;

            Func <string, bool> inputListCheck;

            if (m_inputAssertionListDumpMustIncludeRegex == null)
            {
                if (m_inputAssertionListDumpMustNotIncludeRegex == null)
                {
                    inputListCheck = (inputList) => true;
                }
                else
                {
                    inputListCheck = (inputList) => !m_inputAssertionListDumpMustNotIncludeRegex.IsMatch(inputList);
                }
            }
            else
            {
                if (m_inputAssertionListDumpMustNotIncludeRegex == null)
                {
                    inputListCheck = (inputList) => m_inputAssertionListDumpMustIncludeRegex.IsMatch(inputList);
                }
                else
                {
                    inputListCheck = (inputList) => m_inputAssertionListDumpMustIncludeRegex.IsMatch(inputList) && !m_inputAssertionListDumpMustNotIncludeRegex.IsMatch(inputList);
                }
            }

            if (m_inputWeakFingerprints.Count > 0)
            {
                // Get lists with user provided weak fingerprints
                Console.Error.WriteLine("\nChecking through the " + m_inputWeakFingerprints.Count + " provided weak fingerprints");
                inputAssertionLists = inputAssertionListChecker.GetSuspectInputAssertionLists(m_inputWeakFingerprints, inputListCheck, cacheErrors, m_weakFingerprintsFound);
            }
            else
            {
                // Get lists through sessions
                try
                {
                    Console.Error.WriteLine("\nChecking through the sessions using the following regex: " + m_sessionRegex.ToString());
                    inputAssertionLists = inputAssertionListChecker.GetSuspectInputAssertionLists(m_sessionRegex, inputListCheck, cacheErrors, m_weakFingerprintsFound);
                }
                catch (NotImplementedException e)
                {
                    // Not all cache implementations implement all of the interface methods
                    WriteError("Exception caught: " + e.Message);
                    WriteError("The implementation of the specified cache does not implement all required methods for this tool to be able to perform a check.");
                    return(1);
                }
            }

            // Output input assertion lists
            List <StrongFingerprint> fingerprints = new List <StrongFingerprint>();

            foreach (var inputAssertion in inputAssertionLists)
            {
                fingerprints.Add(inputAssertion.StrongFingerprintValue);
                m_outputDestination.WriteLine(string.Format(CultureInfo.InvariantCulture, "StrongFingerprint: {0}\n{1}\n",
                                                            inputAssertion.StrongFingerprintValue,
                                                            inputAssertion.InputAssertionListContents));
            }

            // It is not until here where the summary information is complete.
            string summary;

            if (inputAssertionListChecker.NumSessionsChecked > 0)
            {
                summary = string.Format(CultureInfo.InvariantCulture, "\nFound {0} matching fingerprints out of {1} unique input lists from {2} sessions\n",
                                        fingerprints.Count,
                                        inputAssertionListChecker.NumInputListsChecked,
                                        inputAssertionListChecker.NumSessionsChecked);
            }
            else
            {
                summary = string.Format(CultureInfo.InvariantCulture, "\nFound {0} matching fingerprints out of {1} unique input lists\n",
                                        fingerprints.Count,
                                        inputAssertionListChecker.NumInputListsChecked);
            }

            m_outputDestination.WriteLine(summary);

            if (inputAssertionListChecker.NumSessionsChecked > 0)
            {
                foreach (var fingerprint in fingerprints)
                {
                    var sessionIds = inputAssertionListChecker.GetSessionsWithFingerprint(fingerprint).ToList();

                    if (sessionIds.Count > 0)
                    {
                        m_outputDestination.WriteLine("{0} sessions contain the strong fingerprint {1}:", sessionIds.Count, fingerprint);
                        foreach (var sessionId in sessionIds)
                        {
                            m_outputDestination.WriteLine("  Session: {0}", sessionId);
                        }

                        m_outputDestination.WriteLine();
                    }
                }
            }

            // Output any cache errors found during input list dump
            OutputCacheErrors(cacheErrors.Keys);

            // Stop timing
            stopwatch.Stop();

            Console.Error.WriteLine(summary);
            Console.Error.WriteLine("Total time to dump input assertion lists: " + stopwatch.Elapsed.TotalSeconds.ToString("F", CultureInfo.InvariantCulture) + " seconds");

            return(0);
        }
示例#2
0
        /// <summary>
        /// Checks for wide variations in the size of input assertion list file sizes corresponding to the same weak fingerprint
        /// </summary>
        /// <returns>Status code. 0 => success, non-zero => failure</returns>
        private int CheckForInputListAnomalies()
        {
            Console.Error.WriteLine("\nStarting check for input assertion list anomalies");

            // Start timing
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            InputAssertionListChecker inputAssertionListChecker = new InputAssertionListChecker(m_cache, m_inputAssertionListSizeDisparityMinimumFactor);
            ConcurrentDictionary <CacheError, int> cacheErrors  = new ConcurrentDictionary <CacheError, int>();

            IEnumerable <InputAssertionListAnomaly> inputAssertionListAnomalies;

            if (m_inputWeakFingerprints.Count > 0)
            {
                // Check with user provided weak fingerprints
                Console.Error.WriteLine("\nChecking through the " + m_inputWeakFingerprints.Count + " provided weak fingerprints");
                inputAssertionListAnomalies = inputAssertionListChecker.PerformAnomalyCheck(m_inputWeakFingerprints, cacheErrors, m_weakFingerprintsFound);
            }
            else
            {
                // Check through sessions
                try
                {
                    Console.Error.WriteLine("\nChecking through the sessions using the following regex: " + m_sessionRegex.ToString());
                    inputAssertionListAnomalies = inputAssertionListChecker.PerformAnomalyCheck(m_sessionRegex, cacheErrors, m_weakFingerprintsFound);
                }
                catch (NotImplementedException e)
                {
                    // Not all cache implementations implement all of the interface methods
                    WriteError("Exception caught: " + e.Message);
                    WriteError("The implementation of the specified cache does not implement all required methods for this tool to be able to perform a check.");
                    return(1);
                }
            }

            // Output input assertion list anomalies
            int numberOfInputAssertionListAnomalies = 0;

            foreach (InputAssertionListAnomaly inputAssertionListAnomaly in inputAssertionListAnomalies)
            {
                m_outputDestination.WriteLine(inputAssertionListAnomaly.ToString());
                numberOfInputAssertionListAnomalies++;
            }

            if (m_inputWeakFingerprints.Count == 0)
            {
                Console.Error.WriteLine(string.Format(CultureInfo.InvariantCulture, "\nSessions checked/Total sessions: {0}/{1}",
                                                      inputAssertionListChecker.NumSessionsChecked, inputAssertionListChecker.NumSessions));
            }

            Console.Error.WriteLine(string.Format(CultureInfo.InvariantCulture, "\nWeak fingerprints with 2+ strong fingerprints/Total weak fingerprints: {0}/{1}",
                                                  inputAssertionListChecker.NumWeakFingerprintsWithTwoOrMoreSFPs, inputAssertionListChecker.NumWeakFingerprintsChecked));
            Console.Error.WriteLine("\nInput Assertion List Anomalies found: " + numberOfInputAssertionListAnomalies);

            // Output any cache errors found during check
            OutputCacheErrors(cacheErrors.Keys);

            // Stop timing
            stopwatch.Stop();
            Console.Error.WriteLine("\nTotal time to check for input assertion list anomalies: " + stopwatch.Elapsed.TotalSeconds.ToString("F", CultureInfo.CurrentCulture) + " seconds");

            return(0);
        }