示例#1
0
        /// <summary>
        /// Main search entry point
        /// </summary>
        private void DoSearch()
        {
            // Error checking
            if (Directory.Exists(txtCodePath.Text) == false) // does the directory exist?
            {
                MessageBox.Show("You need to specify a valid path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else  if (txtSearch.Text.Length == 0) // if the search term > 0
            {
                MessageBox.Show("You need to specify a valid search term", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if (txtCodePath.Text.ToLower().Contains("test") && optIgnoreTest.Checked == true)
            {
                MessageBox.Show("Your patch contains test and you have the ignore directories and files with test option selected." + Environment.NewLine + "No results will be found, cancelling search!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string[] strExts = txtExt.Text.Split (';'); // is the defined extensions list the correct format?
            foreach (string strExt in strExts)
            {
                if (strExt.StartsWith("*.") == false)
                {
                    MessageBox.Show(txtExt.Text + " is not in the correct format\nPlease use *.ext1;*.ext2\n", "Incorrect format", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            AddAndSaveCodePath();

            txtSearch.Items.Insert(0, txtSearch.Text);
            if (txtSearch.Items.Count > 20)
            {
                while (txtSearch.Items.Count > 20)
                {
                    txtSearch.Items.RemoveAt(20);
                }
            }

            // This saves the search strings so they are persistent over runs
            StringCollection strSearchStringCollection = new StringCollection();
            foreach (String strSearchString in txtSearch.Items)
            {
                strSearchStringCollection.Add(strSearchString);
            }
            Properties.Settings.Default.SearchStrings = strSearchStringCollection;
            Properties.Settings.Default.Extensions = txtExt.Text;

            Properties.Settings.Default.Save();

            // ISSUE 1: https://github.com/nccgroup/ncccodenavi/issues/1
            // Prompt the user to automatically escape
            string strSearchText = null;
            if (opRegexSearch.Checked == true)
            {

                //if (MessageBox.Show("You have regex search enabled. Do you want me to escape your search term automatically to result in a literal search?", "Regex escape?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                //{
                    //if (opRegexSearch.Checked == true)
                    //{
                        strSearchText = Regex.Escape(txtSearch.Text);
                    //}
                //}
                //else
                //{
                    strSearchText = txtSearch.Text;
                //}
            }
            else
            {
                strSearchText = txtSearch.Text;
            }

            // Now initalize a search form
            frmSearch frmSearch = new frmSearch(strSearchText + " in " + txtCodePath.Text + " (Regex:"+opRegexSearch.Checked+",Case:"+opCaseSearch.Checked+",Ignore Test:" + optIgnoreTest.Checked+",Ignore Comments:"+ optIgnoreComments.Checked+") - " + txtExt.Text, this);
            frmSearch.MdiParent = this;
            frmSearch.Visible = true;

            // Now initialize the object and start a scan
            Scanner scanYoink = new Scanner(frmSearch, txtCodePath.Text, strSearchText, optIgnoreComments.Checked, opRegexSearch.Checked, opCaseSearch.Checked, optIgnoreTest.Checked, txtExt.Text, richExclusions.Lines);
            frmSearch.SetScanEngine(scanYoink);
            scanYoink.Start(this, frmSearch);
        }
示例#2
0
 public void SetScanEngine(Scanner scanEngine)
 {
     this.scanEngine = scanEngine;
 }
示例#3
0
        private void cmdGrepifyScan_Click(object sender, EventArgs e)
        {
            Grepifyv2 grepifyV2 = null;
            List<String> lstV2Filenames = new List<String>();
            List<Grepifyv2Check> grepifyV2Checks = null;

            // Error checking
            if (Directory.Exists(txtCodePath.Text) == false) // does the directory exist?
            {
                MessageBox.Show("You need to specify a valid path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (txtCodePath.Text.ToLower().Contains("test") && optIgnoreTest.Checked == true)
            {
                MessageBox.Show("Your patch contains test and you have the ignore directories and files with test option selected." + Environment.NewLine + "No results will be found, cancelling search!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string[] strExts = txtExt.Text.Split(';'); // is the defined extensions list the correct format?
            foreach (string strExt in strExts)
            {
                if (strExt.StartsWith("*.") == false)
                {
                    MessageBox.Show(txtExt.Text + " is not in the correct format\nPlease use *.ext1;*.ext2\n", "Incorrect format", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            profileLines = null; // reset
            int intProfileCount=0;

            foreach (ToolStripMenuItem tsiTarget in optGrepify.DropDownItems)
            {
                string[] strNewLines = null;
                bool bError = false;
                List<string> strRegexs = new List<string>();

                if (tsiTarget.CheckState == CheckState.Checked)
                {
                    intProfileCount++;

                    string strFilename = AssemblyDirectory + "\\Grepify.Profiles\\" + tsiTarget.Text + ".txt";
                    string strv2Filename = AssemblyDirectory + "\\Grepify.Profiles\\" + tsiTarget.Text + ".grepifyv2";

                    // Load the file
                    if (!File.Exists(strFilename) && !File.Exists(strv2Filename))
                    {
                        MessageBox.Show("File " + strFilename + " or " + strv2Filename + " does not exist. Aborting scan!", "File does not exist", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    else if(File.Exists(strFilename)) // v1 fileformat
                    {
                        try
                        {
                            strNewLines = File.ReadAllLines(strFilename);
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("File " + strFilename + " could not be read. Aborting scan!", "Could not read file", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return;
                        }

                        // Sanity check the regex of v1 files
                        int intCount = 0;
                        foreach (string strRegex in strNewLines)
                        {

                            intCount++;

                            if (strRegex.StartsWith("#")) continue;

                            try
                            {
                                Match regexMatch = Regex.Match("Mooo", strRegex);
                                strRegexs.Add(strRegex);
                            }
                            catch (ArgumentException rExcp)
                            {
                                MessageBox.Show("Regex looks broken on line " + intCount + ". Regex is '" + strRegex + "'. Error is '" + rExcp.Message + "' in file " + strFilename + ".", "Regex error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                profileLines = null;
                                bError = true;
                                break;
                            }
                        }

                        if (profileLines == null && bError == false)
                        {
                            profileLines = strRegexs.ToArray();
                        }
                        else if (bError == false)
                        {
                            profileLines = profileLines.Concat(strRegexs).ToArray();
                        }
                    }
                    else if (File.Exists(strv2Filename))
                    {
                        lstV2Filenames.Add(strv2Filename);
                    }

                    if (lstV2Filenames.Count > 0)
                    {
                        grepifyV2 = new Grepifyv2(lstV2Filenames);
                    }

                }
            }

            if ((intProfileCount == 0 || profileLines == null) && (grepifyV2 == null || grepifyV2.CheckCount() == 0))
            {
                if (intProfileCount == 0)
                {
                    MessageBox.Show("No Grepify profiles selected for scan. Aborting scan!", "No Grepify profiles", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    Console.WriteLine(grepifyV2.CheckCount());
                    MessageBox.Show("No Grepify regexes in selected files. Aborting scan!", "No Grepify regexes in selected files", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                return;
            }

            // Get the grepify V2 checks if needed
            try
            {
                grepifyV2Checks = grepifyV2.GetChecks();
            }
            catch (Exception)
            {

            }

            // Now try concatinate the extensions that the user supplied and that are required to satisfy any v2 profiles
            string strv2Exts = null;

            try
            {
                strv2Exts = grepifyV2.GetExts();
            }
            catch (Exception)
            {

            }

            string strv1Exts = txtExt.Text;
            if (strv2Exts != null)
            {
                strv1Exts = new StringBuilder().Append(strv1Exts + ";" + strv2Exts).ToString();
            }

            // Now initalize a search (aka results) form
            frmSearch frmSearch = new frmSearch("Grepify scan of " + txtCodePath.Text + " (Regex:True,Case:" + opCaseSearch.Checked + ",Ignore Test:" + optIgnoreTest.Checked + ",Ignore Comments:" + optIgnoreComments.Checked + ") - " + strv1Exts, this, true);
            frmSearch.AddRegexColumns(); // Adds the extra columns
            frmSearch.MdiParent = this;
            frmSearch.Visible = true;

            // Now initialize the object and start a scan
            Scanner scanYoink = new Scanner(frmSearch, txtCodePath.Text, profileLines, grepifyV2Checks, optIgnoreComments.Checked, true, opCaseSearch.Checked, optIgnoreTest.Checked, strv1Exts, richExclusions.Lines);
            frmSearch.SetScanEngine(scanYoink);
            scanYoink.Start(this, frmSearch);
        }
示例#4
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="strFile"></param>
 public FileToScan(string strFile, Scanner scanEngine)
 {
     this.strTerm = scanEngine.strTerm;
     this.strAPIs = scanEngine.strAPIs;
     this.strFile = strFile;
     this.bCase = scanEngine.bCase;
     this.bRegex = scanEngine.bRegex;
     this.bComments = scanEngine.bComments;
     this.frmMaster = scanEngine.frmMain;
     this.frmSearch = scanEngine.frmSearch;
     this.engineLocal = scanEngine;
     this.strCommentsRegex = scanEngine.strCommentsRegex;
 }
示例#5
0
        /// <summary>
        /// Main search entry point
        /// </summary>
        private void DoSearch()
        {
            // Error checking
            if (Directory.Exists(txtCodePath.Text) == false) // does the directory exist?
            {
                MessageBox.Show("You need to specify a valid path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            } else  if (txtSearch.Text.Length == 0) // if the search term > 0
            {
                MessageBox.Show("You need to specify a valid search term", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            string[] strExts = txtExt.Text.Split (';'); // is the defined extensions list the correct format?
            foreach (string strExt in strExts)
            {
                if (strExt.StartsWith("*.") == false)
                {
                    MessageBox.Show(txtExt.Text + " is not in the correct format\nPlease use *.ext1;*.ext2\n", "Incorrect format", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            // So we first the new path and search term to the history
            // we then prune the oldist
            txtCodePath.Items.Insert(0,txtCodePath.Text);
            if (txtCodePath.Items.Count > 20)
            {
                while (txtCodePath.Items.Count > 20)
                {
                    txtCodePath.Items.RemoveAt(20);
                }
            }
            txtSearch.Items.Insert(0,txtSearch.Text);
            if (txtSearch.Items.Count > 20)
            {
                while (txtSearch.Items.Count > 20)
                {
                    txtSearch.Items.RemoveAt(20);
                }
            }

            // This saves the paths so they are persistent over runs
            StringCollection strCodePathCollection = new StringCollection();
            foreach (String strPath in txtCodePath.Items)
            {
                strCodePathCollection.Add(strPath);
            }
            Properties.Settings.Default.CodeFolders = strCodePathCollection;

            // This saves the search strings so they are persistent over runs
            StringCollection strSearchStringCollection = new StringCollection();
            foreach (String strSearchString in txtSearch.Items)
            {
                strSearchStringCollection.Add(strSearchString);
            }
            Properties.Settings.Default.SearchStrings = strSearchStringCollection;

            Properties.Settings.Default.Extensions = txtExt.Text;

            Properties.Settings.Default.Save();

            // Now initalize a search form
            frmSearch frmSearch = new frmSearch(txtSearch.Text + " in " + txtCodePath.Text + " (Regex:"+opRegexSearch.Checked+",Case:"+opCaseSearch.Checked+",Ignore Test:" + optIgnoreTest.Checked+",Ignore Comments:"+ optIgnoreComments.Checked+") - " + txtExt.Text, this);
            frmSearch.MdiParent = this;
            frmSearch.Visible = true;

            // Now initialize the object and start a scan
            Scanner scanYoink = new Scanner(frmSearch, txtCodePath.Text, txtSearch.Text, optIgnoreComments.Checked, opRegexSearch.Checked, opCaseSearch.Checked, optIgnoreTest.Checked, txtExt.Text,richExclusions.Lines);
            frmSearch.SetScanEngine(scanYoink);
            scanYoink.Start(this, frmSearch);
        }
示例#6
0
        private void cmdGrepifyScan_Click(object sender, EventArgs e)
        {
            // Error checking
            if (Directory.Exists(txtCodePath.Text) == false) // does the directory exist?
            {
                MessageBox.Show("You need to specify a valid path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string[] strExts = txtExt.Text.Split(';'); // is the defined extensions list the correct format?
            foreach (string strExt in strExts)
            {
                if (strExt.StartsWith("*.") == false)
                {
                    MessageBox.Show(txtExt.Text + " is not in the correct format\nPlease use *.ext1;*.ext2\n", "Incorrect format", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            profileLines = null; // reset
            int intProfileCount=0;

            foreach (ToolStripMenuItem tsiTarget in optGrepify.DropDownItems)
            {
                string[] strNewLines = null;
                bool bError = false;
                List<string> strRegexs = new List<string>();

                if (tsiTarget.CheckState == CheckState.Checked)
                {
                    intProfileCount++;

                    string strFilename = AssemblyDirectory + "\\Grepify.Profiles\\" + tsiTarget.Text + ".txt";

                    // Load the file
                    if (!File.Exists(strFilename))
                    {
                        MessageBox.Show("File " + strFilename + " does not exist. Aborting scan!", "File does not exist", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    else
                    {
                        try
                        {
                            strNewLines = File.ReadAllLines(strFilename);
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("File " + strFilename + " could not be read. Aborting scan!", "Could not read file", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return;
                        }
                    }

                    // Sanity check the regex
                    int intCount = 0;
                    foreach (string strRegex in strNewLines)
                    {

                        intCount++;

                        if (strRegex.StartsWith("#")) continue;

                        try
                        {
                            Match regexMatch = Regex.Match("Mooo", strRegex);
                            strRegexs.Add(strRegex);
                        }
                        catch (ArgumentException rExcp)
                        {
                            MessageBox.Show("Regex looks broken on line " + intCount + ". Regex is '" + strRegex + "'. Error is '" + rExcp.Message + "' in file " + strFilename + ".", "Regex error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            profileLines = null;
                            bError = true;
                            break;
                        }
                    }

                    if (profileLines == null && bError == false)
                    {
                        profileLines = strRegexs.ToArray();
                    }
                    else if (bError == false)
                    {
                        profileLines = profileLines.Concat(strRegexs).ToArray();
                    }
                }
            }

            if (intProfileCount == 0 || profileLines == null)
            {
                if (intProfileCount == 0)
                {
                    MessageBox.Show("No Grepify profiles selected for scan. Aborting scan!", "No Grepify profiles", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    MessageBox.Show("No Grepify regexes in selected files. Aborting scan!", "No Grepify regexes in selected files", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                return;
            }

            // Now do the scan

            // Now initalize a search (aka results) form
            frmSearch frmSearch = new frmSearch("Grepify scan of " + txtCodePath.Text + " (Regex:True,Case:" + opCaseSearch.Checked + ",Ignore Test:" + optIgnoreTest.Checked + ",Ignore Comments:"+ optIgnoreComments.Checked+") - " + txtExt.Text, this);
            frmSearch.AddRegexColumn(); // Adds the extra column
            frmSearch.MdiParent = this;
            frmSearch.Visible = true;

            // Now initialize the object and start a scan
            Scanner scanYoink = new Scanner(frmSearch, txtCodePath.Text, profileLines, optIgnoreComments.Checked, true, opCaseSearch.Checked, optIgnoreTest.Checked, txtExt.Text,richExclusions.Lines);
            frmSearch.SetScanEngine(scanYoink);
            scanYoink.Start(this, frmSearch);
        }
示例#7
0
 public void SetScanEngine(Scanner scanEngine)
 {
     this.scanEngine = scanEngine;
 }