Example #1
0
        public void TestLongPaths()
        {
            var LongDirectories = new List <string> {
                Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + @"\testfiles\ininfafasf ienflaflieflanfeifnalnfae\ininfafasf ienflaflieflanfeifnalnfae\ininfafasf ienflaflieflanfeifnalnfae\ininfafasf ienflaflieflanfeifnalnfae\ienflaflieflanfeifnalnfae\dfiwnfwnfwnfownefinewnf.txt",
            };
            var Regex = new SauronEye.RegexSearch(new List <string> {
                "pass"
            });
            var Keywords = new List <string> {
                "pass"
            };
            var ContentSearcher = new SauronEye.ContentsSearcher(LongDirectories, Keywords, Regex, 1024);

            ContentSearcher.Search();

            var currentConsoleOut = Console.Out;

            var outputPath  = @"ininfafasf ienflaflieflanfeifnalnfae\ininfafasf ienflaflieflanfeifnalnfae\ininfafasf ienflaflieflanfeifnalnfae\ininfafasf ienflaflieflanfeifnalnfae\ienflaflieflanfeifnalnfae\dfiwnfwnfwnfownefinewnf.txt";
            var outputMatch = @"this is pass";

            using (var consoleOutput = new ConsoleOutput())
            {
                ContentSearcher.Search();
                Assert.IsTrue(consoleOutput.GetOuput().Contains(outputPath));
                Assert.IsTrue(consoleOutput.GetOuput().Contains(outputMatch));
            }

            Assert.AreEqual(currentConsoleOut, Console.Out);
        }
Example #2
0
        public void TestHasKeywordInLargeString()
        {
            var Directories = new List <string> {
                ""
            };
            var Regex = new SauronEye.RegexSearch(new List <string> {
                "pass"
            });
            var Keywords = new List <string> {
                "pass"
            };
            var ContentSearcher = new SauronEye.ContentsSearcher(Directories, Keywords, Regex, 1024);

            var testCases = new Dictionary <string, string>();

            testCases.Add("this is a long string containing a password", "...g containing a password... ");
            testCases.Add("password is this yet another", "...password is this ye... ");
            testCases.Add("password", "...password... ");
            testCases.Add("not a wachtwoord", "");
            testCases.Add("another password in this test makes it to password twice", "...another password in this te... ...st makes it to password twice... ");
            testCases.Add("password begin and at the end password", "...password begin and ... ...and at the end password... ");
            testCases.Add("long path before the password=wetiife", "...ath before the password=wetiife... ");
            testCases.Add("this is anther password testcase that is a normal one", "...this is anther password testcase t... ");

            foreach (KeyValuePair <string, string> kvp in testCases)
            {
                Assert.AreEqual(kvp.Value, ContentSearcher.HasKeywordInLargeString(kvp.Key));
            }
        }
Example #3
0
        private static void setDefaultArgs()
        {
            if (Directories.Count == 0)
            {
                foreach (DriveInfo d in DriveInfo.GetDrives())
                {
                    Directories.Add(d.Name);
                }
            }

            if (Keywords.Count == 0)
            {
                foreach (string s in DefaultKeywords)
                {
                    Keywords.Add(s);
                }
            }

            if (FileTypes.Count == 0)
            {
                foreach (string s in DefaultFileTypes)
                {
                    FileTypes.Add(s);
                }
            }

            regexSearcher = new RegexSearch(Keywords);
        }
Example #4
0
 public ContentsSearcher(IEnumerable <string> directories, List <string> keywords, RegexSearch regex, UInt64 maxFileSizeInKB)
 {
     this.Directories   = directories;
     this.Keywords      = keywords;
     this.RegexSearcher = regex;
     this.MAX_FILE_SIZE = maxFileSizeInKB;
 }
Example #5
0
 public FSSearcher(string d, List <string> f, List <string> k, bool s, UInt64 maxfs, bool systemdirs, RegexSearch regex, DateTime beforedate, DateTime afterdate, bool CheckForMacro, string ossstring)
 {
     this.SearchDirectory = d;
     this.Filetypes       = f;
     this.Keywords        = k;
     this.Results         = new List <string>();
     this.searchContents  = s;
     this.maxFileSizeInKB = maxfs;
     this.SystemDirs      = systemdirs;
     this.RegexSearcher   = regex;
     if (string.IsNullOrEmpty(ossstring))
     {
         this.ossflag = false;
     }
     else
     {
         this.ossflag   = true;
         this.ossstring = ossstring;
         string[] arr = ossstring.Split(':');
         this.bucketName      = arr[0];
         this.accessKeyId     = arr[1];
         this.accessKeySecret = arr[2];
         this.endpoint        = arr[3];
     }
     if (beforedate != null)
     {
         this.BeforeDate = beforedate;
     }
     if (afterdate != null)
     {
         this.AfterDate = afterdate;
     }
     this.CheckForMacro = CheckForMacro;
     this.OLXExplorer   = null;
 }
Example #6
0
 public FSSearcher(string d, List <string> f, List <string> k, bool s, bool systemdirs, RegexSearch regex)
 {
     this.SearchDirectory = d;
     this.Filetypes       = f;
     this.Keywords        = k;
     this.Results         = new List <string>();
     this.searchContents  = s;
     this.SystemDirs      = systemdirs;
     this.RegexSearcher   = regex;
 }
Example #7
0
 public ContentsSearcher(IEnumerable <string> directories, List <string> keywords, RegexSearch regex, UInt64 maxFileSizeInKB, string ossstring)
 {
     this.Directories   = directories;
     this.Keywords      = keywords;
     this.RegexSearcher = regex;
     this.MAX_FILE_SIZE = maxFileSizeInKB;
     if (string.IsNullOrEmpty(ossstring))
     {
         this.ossflag = false;
     }
     else
     {
         this.ossflag   = true;
         this.ossstring = ossstring;
         string[] arr = ossstring.Split(':');
         this.bucketName      = arr[0];
         this.accessKeyId     = arr[1];
         this.accessKeySecret = arr[2];
         this.endpoint        = arr[3];
     }
 }
Example #8
0
 public FSSearcher(string d, List <string> f, List <string> k, bool s, UInt64 maxfs, bool systemdirs, RegexSearch regex, DateTime beforedate, DateTime afterdate, bool CheckForMacro)
 {
     this.SearchDirectory = d;
     this.Filetypes       = f;
     this.Keywords        = k;
     this.Results         = new List <string>();
     this.searchContents  = s;
     this.maxFileSizeInKB = maxfs;
     this.SystemDirs      = systemdirs;
     this.RegexSearcher   = regex;
     if (beforedate != null)
     {
         this.BeforeDate = beforedate;
     }
     if (afterdate != null)
     {
         this.AfterDate = afterdate;
     }
     this.CheckForMacro = CheckForMacro;
     this.OLXExplorer   = null;
 }
        // Parses the arguments passed to SauronEye, using Mono.Options
        public void ParseArgumentsOptions(string[] args)
        {
            var    shouldShowHelp   = false;
            string currentParameter = "";
            var    options          = new OptionSet()
            {
                { "d|directories=", "Directories to search", v => {
                      Directories.Add(v);
                      currentParameter = "d";
                  } },
                { "f|filetypes=", "Filetypes to search for/in", v => {
                      FileTypes.Add(v);
                      currentParameter = "f";
                  } },
                { "k|keywords=", "Keywords to search for", v => {
                      Keywords.Add(v);
                      currentParameter = "k";
                  } },
                { "o|osskey=", "aliyunOSS key \n format: bucketName:accessKeyId:accessKeySecret:endpoint", v => {
                      ossstring        = v;
                      currentParameter = "o";
                  } },
                { "c|contents", "Search file contents", c => SearchContents = c != null },
                { "m|maxfilesize=", "Max file size to search contents in, in kilobytes", m => { CheckInteger(m); } },
                { "b|beforedate=", "Filter files last modified before this date, \n format: yyyy-MM-dd", b => { CheckDate(b, "before"); } },
                { "a|afterdate=", "Filter files last modified after this date, \n format: yyyy-MM-dd", a => { CheckDate(a, "after"); } },
                { "s|systemdirs", "Search in filesystem directories %APPDATA% and %WINDOWS%", s => SystemDirs = s != null },
                { "v|vbamacrocheck", "Check if 2003 Office files (*.doc and *.xls) contain a VBA macro", s => CheckForMacro = s != null },
                { "h|help", "Show help", h => shouldShowHelp = h != null },
                { "<>", v => {
                      switch (currentParameter)
                      {
                      case "d":
                          Directories.Add(v);
                          break;

                      case "f":
                          FileTypes.Add(v);
                          break;

                      case "k":
                          Keywords.Add(v);
                          break;

                      case "o":
                          ossstring = v;
                          break;

                      case "":
                          break;
                      }
                  } }
            };

            List <string> extra;

            try {
                extra = options.Parse(args);
            } catch (OptionException e) {
                Console.Write("SauronEye.exe: ");
                Console.WriteLine(e.Message);
                Console.WriteLine("Try 'SauronEye.exe --help' for more information.");
                System.Environment.Exit(1);
            }

            if (shouldShowHelp)
            {
                ShowHelp(options);
                System.Environment.Exit(1);
            }
            CheckArgs();
            Directories   = Directories.Where(s => !isNullOrWhiteSpace(s)).Distinct().ToList();
            FileTypes     = FileTypes.Where(s => !isNullOrWhiteSpace(s)).Distinct().ToList();
            Keywords      = Keywords.Where(s => !isNullOrWhiteSpace(s)).Distinct().ToList();
            regexSearcher = new RegexSearch(Keywords);
            return;
        }
Example #10
0
        // Implemented my own args parser. This is probably a very bad idea...
        // Transforms:
        // -Dirs C:\,D:\,\\NLDOMFS\testing\ -Filetype .txt, .ini,.conf , .bat
        // To:
        // List<string>{ "C:\", "D:\", "\\NLDOMFS\testing\" } and List<string>{ ".txt", ".ini", ".conf", ".bat" }
        private static void parseArguments(string[] args)
        {
            for (var arg = 0; arg < args.Length;)
            {
                if (args[arg].StartsWith("-"))
                {
                    switch (args[arg].ToLower())
                    {
                    case "-dirs": {
                        if (args[arg + 1].ToLower().Equals("-dirs"))
                        {
                            arg++; break;
                        }
                        while (arg + 1 != args.Length && !args[arg + 1].StartsWith("-"))
                        {
                            Directories.AddRange(strimAndSplit(args[arg + 1].ToLower()));
                            arg++;
                        }
                    } break;

                    case "-filetypes": {
                        if (args[arg + 1].ToLower().Equals("-filetype"))
                        {
                            arg++; break;
                        }
                        while (arg + 1 != args.Length && !args[arg + 1].StartsWith("-"))
                        {
                            FileTypes.AddRange(strimAndSplit(args[arg + 1].ToLower()));
                            arg++;
                        }
                    } break;

                    case "-keywords": {
                        if (args[arg + 1].ToLower().Equals("-keywords"))
                        {
                            arg++; break;
                        }
                        while (arg + 1 != args.Length && !args[arg + 1].StartsWith("-"))
                        {
                            Keywords.AddRange(strimAndSplit(args[arg + 1].ToLower()));
                            arg++;
                        }
                    } break;

                    case "-contents": {
                        SearchContents = true;
                        arg++;
                    } break;

                    case "-systemdirs": {
                        SystemDirs = true;
                        arg++;
                    }
                    break;

                    default:
                        // unknown arg, proceed to next
                        arg++;
                        break;
                    }
                }
                else
                {
                    // increment to next arg
                    arg++;
                }
            }
            // remove empty or duplicate args
            Directories   = Directories.Where(s => !isNullOrWhiteSpace(s)).Distinct().ToList();
            FileTypes     = FileTypes.Where(s => !isNullOrWhiteSpace(s)).Distinct().ToList();
            Keywords      = Keywords.Where(s => !isNullOrWhiteSpace(s)).Distinct().ToList();
            regexSearcher = new RegexSearch(Keywords);
            return;
        }
Example #11
0
 public ContentsSearcher(IEnumerable <string> directories, List <string> keywords, RegexSearch regex)
 {
     this.Directories   = directories;
     this.Keywords      = keywords;
     this.RegexSearcher = regex;
 }