예제 #1
0
        /// <summary>
        /// Scan a particular file
        /// </summary>
        /// <param name="strFile">the file we wish to scan</param>
        /// <param name="strAPIs">an array of regular expressions we've loaded</param>
        /// <returns></returns>
        private bool ScanFile(string strFile)
        {
            if (engineLocal.bStopped == true || engineLocal.intFinds > 2000)
            {
                engineLocal.LowerQueueCount();
                return(false);
            }

            try
            {
                byte[]   fileBytes       = File.ReadAllBytes(strFile);
                Encoding encodingForFile = null;
                if (IsTextTester.IsText(out encodingForFile, strFile.ToString(), 100) == false)
                {
                    return(false);
                }


                string[] strLines = File.ReadAllLines(strFile, encodingForFile);
                FileInfo fInfo    = new FileInfo(strFile);

                int intCount = 0;

                foreach (string strLine in strLines)
                {
                    intCount++;

                    if (engineLocal.bStopped == true)
                    {
                        engineLocal.LowerQueueCount();
                        return(false);
                    }

                    try
                    {
                        Match commentregexMatch = null;
                        if (bComments == true)
                        {
                            try
                            {
                                foreach (string strComRegex in strCommentsRegex)
                                {
                                    commentregexMatch = Regex.Match(strLine, strComRegex);
                                    if (commentregexMatch.Success == true)
                                    {
                                        break;
                                    }
                                }
                            }
                            catch (Exception)
                            {
                            }
                        }

                        if (bComments != true || commentregexMatch == null || (commentregexMatch != null && commentregexMatch.Success == false))
                        {
                            if (strAPIs != null) // We're doing a grepify scan
                            {
                                foreach (string strRegex in strAPIs)
                                {
                                    if (engineLocal.bStopped == true)
                                    {
                                        engineLocal.LowerQueueCount();
                                        return(false);
                                    }
                                    else
                                    {
                                        Match regexMatch = null;
                                        if (bCase == true)
                                        {
                                            regexMatch = Regex.Match(strLine, strRegex, RegexOptions.IgnoreCase);
                                        }
                                        else
                                        {
                                            regexMatch = Regex.Match(strLine, strRegex);
                                        }


                                        if (regexMatch != null && regexMatch.Success)
                                        {
                                            if (frmSearch.IsDisposed == false)
                                            {
                                                frmSearch.UpdateList(fInfo.DirectoryName, fInfo.Name, fInfo.Extension, intCount, strLine, strRegex);
                                            }
                                            lock (engineLocal.objCount)
                                            {
                                                engineLocal.intFinds++;
                                                if (engineLocal.intFinds == 2000)
                                                {
                                                    MessageBox.Show("2000 instances found stopping scan", "Stopping scan", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            else if (bRegex == true && strAPIs == null) // Standard term search but with regex
                            {
                                Match regexMatch = null;
                                if (bCase == true)
                                {
                                    regexMatch = Regex.Match(strLine, strTerm, RegexOptions.IgnoreCase);
                                }
                                else
                                {
                                    regexMatch = Regex.Match(strLine, strTerm);
                                }


                                if (regexMatch != null && regexMatch.Success)
                                {
                                    // Update the GUI
                                    frmSearch.UpdateList(fInfo.DirectoryName, fInfo.Name, fInfo.Extension, intCount, strLine);
                                    lock (engineLocal.objCount)
                                    {
                                        engineLocal.intFinds++;
                                        if (engineLocal.intFinds == 2000)
                                        {
                                            MessageBox.Show("2000 instances found stopping scan", "Stopping scan", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                        }
                                    }
                                }
                            }
                            else if (strAPIs == null) // Standard term search without regex
                            {
                                if (strLine.Contains(strTerm) && frmSearch.IsDisposed == false)
                                {
                                    frmSearch.UpdateList(fInfo.DirectoryName, fInfo.Name, fInfo.Extension, intCount, strLine);
                                    lock (engineLocal.objCount)
                                    {
                                        engineLocal.intFinds++;
                                        if (engineLocal.intFinds == 2000)
                                        {
                                            MessageBox.Show("2000 instances found stopping scan", "Stopping scan", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                        }
                                    }
                                }
                                else if (frmSearch.IsDisposed == true)
                                {
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                    }
                }

                fInfo    = null;
                strLines = null;
            }
            catch (Exception)
            {
            }
            finally
            {
                engineLocal.LowerQueueCount();
            }

            return(true);
        }
예제 #2
0
        /// <summary>
        /// Scan a particular file
        /// </summary>
        /// <param name="strFile">the file we wish to scan</param>
        /// <param name="strAPIs">an array of regular expressions we've loaded</param>
        /// <returns></returns>
        private bool ScanFile(string strFile)
        {
            if (engineLocal.bStopped == true || engineLocal.intFinds > Properties.Settings.Default.MaxResults)
            {
                engineLocal.LowerQueueCount();
                return(false);
            }

            try
            {
                byte[]   fileBytes       = File.ReadAllBytes(strFile);
                Encoding encodingForFile = null;
                if (IsTextTester.IsText(out encodingForFile, strFile.ToString(), 100) == false)
                {
                    return(false);
                }


                FileInfo fInfo = new FileInfo(strFile);

                int intCount = 0;

                foreach (string strLine in File.ReadLines(strFile, encodingForFile))
                {
                    intCount++;

                    if (engineLocal.bStopped == true)
                    {
                        engineLocal.LowerQueueCount();
                        return(false);
                    }

                    try
                    {
                        Match commentregexMatch = null;
                        if (bComments == true)
                        {
                            try
                            {
                                foreach (string strComRegex in strCommentsRegex)
                                {
                                    commentregexMatch = Regex.Match(strLine, strComRegex);
                                    if (commentregexMatch.Success == true)
                                    {
                                        break;
                                    }
                                }
                            }
                            catch (Exception)
                            {
                            }
                        }

                        if (bComments != true || commentregexMatch == null || (commentregexMatch != null && commentregexMatch.Success == false))
                        {
                            if (strAPIs != null || lstChecks != null) // We're doing a grepify scan
                            {
                                // V1
                                if (strAPIs != null)
                                {
                                    foreach (string strRegex in strAPIs)
                                    {
                                        if (engineLocal.bStopped == true)
                                        {
                                            engineLocal.LowerQueueCount();
                                            return(false);
                                        }
                                        else
                                        {
                                            Match regexMatch = null;
                                            if (bCase == false)
                                            {
                                                regexMatch = Regex.Match(strLine, strRegex, RegexOptions.IgnoreCase);
                                            }
                                            else
                                            {
                                                regexMatch = Regex.Match(strLine, strRegex);
                                            }


                                            if (regexMatch != null && regexMatch.Success)
                                            {
                                                if (frmSearch.IsDisposed == false)
                                                {
                                                    frmSearch.UpdateList(fInfo.DirectoryName, fInfo.Name, fInfo.Extension, intCount, strLine, strRegex, null);
                                                }
                                                lock (engineLocal.objCount)
                                                {
                                                    engineLocal.intFinds++;
                                                    if (engineLocal.intFinds == Properties.Settings.Default.MaxResults)
                                                    {
                                                        MessageBox.Show("Maximum results found stopping scan", "Stopping scan", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }


                                // V2
                                if (lstChecks != null)
                                {
                                    foreach (Grepifyv2Check gv2Check in lstChecks)
                                    {
                                        string[] strExts = gv2Check.strExts.Split(';');
                                        bool     bFound  = false;

                                        foreach (string strExt in strExts)
                                        {
                                            try
                                            {
                                                if (Path.GetExtension(strFile).EndsWith(strExt.Split('.')[1]) == true)
                                                {
                                                    bFound = true;
                                                }
                                            }
                                            catch (Exception)
                                            {
                                            }
                                        }


                                        if (engineLocal.bStopped == true)
                                        {
                                            engineLocal.LowerQueueCount();
                                            return(false);
                                        }
                                        else if (bFound == true)
                                        {
                                            Match regexMatch = null;
                                            if (bCase == false)
                                            {
                                                regexMatch = Regex.Match(strLine, gv2Check.strRegex, RegexOptions.IgnoreCase);
                                            }
                                            else
                                            {
                                                regexMatch = Regex.Match(strLine, gv2Check.strRegex);
                                            }


                                            if (regexMatch != null && regexMatch.Success)
                                            {
                                                if (frmSearch.IsDisposed == false)
                                                {
                                                    frmSearch.UpdateList(fInfo.DirectoryName, fInfo.Name, fInfo.Extension, intCount, strLine, null, gv2Check);
                                                }
                                                lock (engineLocal.objCount)
                                                {
                                                    engineLocal.intFinds++;
                                                    if (engineLocal.intFinds == Properties.Settings.Default.MaxResults)
                                                    {
                                                        MessageBox.Show("Maximum results found stopping scan", "Stopping scan", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            else if (bRegex == true && strAPIs == null && lstChecks == null) // Standard term search but with regex
                            {
                                Match regexMatch = null;
                                if (bCase == false)
                                {
                                    regexMatch = Regex.Match(strLine, strTerm, RegexOptions.IgnoreCase);
                                }
                                else
                                {
                                    regexMatch = Regex.Match(strLine, strTerm);
                                }


                                if (regexMatch != null && regexMatch.Success)
                                {
                                    // Update the GUI
                                    frmSearch.UpdateList(fInfo.DirectoryName, fInfo.Name, fInfo.Extension, intCount, strLine);
                                    lock (engineLocal.objCount)
                                    {
                                        engineLocal.intFinds++;
                                        if (engineLocal.intFinds == Properties.Settings.Default.MaxResults)
                                        {
                                            MessageBox.Show("Maximum results found stopping scan", "Stopping scan", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                        }
                                    }
                                }
                            }
                            else if (strAPIs == null) // Standard term search without regex
                            {
                                if (bCase == true && strLine.Contains(strTerm) && frmSearch.IsDisposed == false)
                                {
                                    frmSearch.UpdateList(fInfo.DirectoryName, fInfo.Name, fInfo.Extension, intCount, strLine);
                                    lock (engineLocal.objCount)
                                    {
                                        engineLocal.intFinds++;
                                        if (engineLocal.intFinds == Properties.Settings.Default.MaxResults)
                                        {
                                            MessageBox.Show("Maximum results found stopping scan", "Stopping scan", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                        }
                                    }
                                }
                                else if (bCase == false && strLine.ToLower().Contains(strTerm.ToLower()) && frmSearch.IsDisposed == false)
                                {
                                    frmSearch.UpdateList(fInfo.DirectoryName, fInfo.Name, fInfo.Extension, intCount, strLine);
                                    lock (engineLocal.objCount)
                                    {
                                        engineLocal.intFinds++;
                                        if (engineLocal.intFinds == Properties.Settings.Default.MaxResults)
                                        {
                                            MessageBox.Show("Maximum results found stopping scan", "Stopping scan", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                        }
                                    }
                                }
                                else if (frmSearch.IsDisposed == true)
                                {
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                    }
                }

                fInfo = null;
            }
            catch (Exception)
            {
            }
            finally
            {
                engineLocal.LowerQueueCount();
            }

            return(true);
        }