Exemple #1
0
        static void Main(string[] args)
        {
            string path       = "";
            string pattern    = "";
            string searchTerm = "";

            // lazy assignment
            bool grep = args.Contains("-V");

            grepGlobal = grep;

            string year          = "";
            string whitelistPath = "";
            string blacklistPath = "";

            // argument parsing

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

            try
            {
                foreach (var argument in args)
                {
                    var idx = argument.IndexOf(':');
                    if (idx > 0)
                    {
                        arguments[argument.Substring(0, idx)] = argument.Substring(idx + 1);
                    }
                    else
                    {
                        arguments[argument] = string.Empty;
                    }
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("[-] Exception parsing arguments:");
                Console.WriteLine(ex);
            }

            if (!arguments.ContainsKey("path"))
            {
                Console.WriteLine("[-] Error: No path was given.");
                Usage();
                //Environment.Exit(1);
            }

            path = arguments["path"];
            if (!Directory.Exists(path))
            {
                Console.WriteLine("[X] Error: Directory {0} does not exist.", path);
                //Environment.Exit(1);
                Console.ReadLine();
            }

            if (arguments.ContainsKey("pattern"))
            {
                pattern = arguments["pattern"];
            }
            if (arguments.ContainsKey("searchterm"))
            {
                searchTerm = arguments["searchterm"];
            }
            if (arguments.ContainsKey("year"))
            {
                year = arguments["year"];
            }
            if (arguments.ContainsKey("ext_whitelist"))
            {
                whitelistPath = arguments["ext_whitelist"];
                List <string> lines = new List <string>();
                using (StreamReader reader = File.OpenText(whitelistPath))
                {
                    while (!reader.EndOfStream)
                    {
                        lines.Add(reader.ReadLine());
                    }
                }
                ext_whitelist = lines;
            }
            if (arguments.ContainsKey("ext_blacklist"))
            {
                blacklistPath = arguments["ext_blacklist"];
                List <string> lines = new List <string>();
                using (StreamReader reader = File.OpenText(blacklistPath))
                {
                    while (!reader.EndOfStream)
                    {
                        lines.Add(reader.ReadLine());
                    }
                }
                ext_blacklist = lines;
            }
            if (arguments.ContainsKey("search_whitelist"))
            {
                whitelistPath = arguments["search_whitelist"];
                List <string> lines = new List <string>();
                using (StreamReader reader = File.OpenText(whitelistPath))
                {
                    while (!reader.EndOfStream)
                    {
                        lines.Add(reader.ReadLine());
                    }
                }
                search_whitelist = lines;
            }
            if (arguments.ContainsKey("searchterms"))
            {
                String[] split = arguments["searchterms"].Split(',');
                search_whitelist = split.ToList <string>();
            }



            string[] files = GetAllFiles(path, pattern);
            if (files.Length > 0)
            {
                Dictionary <string, string[]> sorted = new Dictionary <string, string[]>();
                string lastDir = "";
                // total size of all files listed
                double allFileSizes      = 0;
                int    allDirectoryCount = 0;
                int    allFilesCount     = 0;
                // directory specific file count
                double dirFileSizes = 0;
                int    dirFileCount = 0;
                FileExtensionHandler fileExtHandler = new FileExtensionHandler();
                foreach (string f in files)
                {
                    FileInfo fileInfo       = new FileInfo(f);
                    string   fileStatusLine = GetFileStatusString(fileInfo);
                    string   size           = HumanBytes(fileInfo.Length);

                    string ext = Path.GetExtension(f);
                    if (ext_whitelist.Count() != 0) // then we have a whitelist
                    {
                        if (!ext_whitelist.Contains(ext))
                        {
                            continue; // extension not in whitelist
                        }
                    }
                    if (ext_blacklist.Count() != 0)
                    {
                        if (ext_blacklist.Contains(ext))
                        {
                            continue;
                        }
                    }



                    string lastWrite = File.GetLastWriteTime(fileInfo.FullName).Date.ToString();
                    if (year != "")
                    {
                        if (!lastWrite.Contains(year))
                        {
                            continue;
                        }
                    }

                    if (searchTerm == "" && search_whitelist.Count() == 0)
                    {
                        try
                        {
                            PrintResults(ref allFilesCount,
                                         ref allFileSizes,
                                         ref allDirectoryCount,
                                         ref lastDir,
                                         fileInfo,
                                         ref dirFileSizes,
                                         ref dirFileCount,
                                         fileStatusLine);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("[-] ERROR printing results.");
                            Console.WriteLine(ex);
                        }
                    }
                    else
                    {
                        bool   hasSearchTerm = false;
                        string entireLine    = "";

                        if (search_whitelist.Count() > 0)
                        {
                            foreach (string eachSearchTerm in search_whitelist)
                            {
                                if (FileContainsString(f, eachSearchTerm))
                                {
                                    hasSearchTerm = true;
                                }
                            }
                        }

                        if (pattern == "")
                        {
                            // Ensure this is not a bad file format.
                            if (fileExtHandler.HasCleanExtension(f))
                            {
                                if (!hasSearchTerm)
                                {
                                    hasSearchTerm = FileContainsString(f, searchTerm);
                                }
                                //entireLine = FileContainsString(f, searchTerm);
                            }
                            else
                            {
                                //Console.WriteLine("[-] Skipping reading {0}. To force this, specify the pattern of files to read with --pattern.", f);
                            }
                        }
                        else
                        {
                            if (!hasSearchTerm)
                            {
                                hasSearchTerm = FileContainsString(f, searchTerm);
                            }
                            //entireLine = FileContainsString(f, searchTerm);
                        }

                        if (hasSearchTerm)
                        //if (entireLine != "")
                        {
                            //if (grep)
                            //{
                            //    Console.WriteLine(entireLine);
                            //}

                            PrintResults(ref allFilesCount,
                                         ref allFileSizes,
                                         ref allDirectoryCount,
                                         ref lastDir,
                                         fileInfo,
                                         ref dirFileSizes,
                                         ref dirFileCount,
                                         fileStatusLine);
                            Console.WriteLine();
                        }
                    }
                }
                if (dirFileCount > 0)
                {
                    Console.WriteLine();
                    string statusString = GetDirectoryStatusString(dirFileCount, dirFileSizes);
                    Console.WriteLine(statusString);
                    dirFileSizes = 0;
                    dirFileCount = 0;
                }
                Console.WriteLine();
                Console.WriteLine("\tTotal Files Listed:");
                string line_1 = GetSpaces(allFilesCount) + allFilesCount.ToString() +
                                " File(s)" + GetSpaces(allFileSizes) + HumanBytes(allFileSizes);
                string line_2 = GetSpaces(allDirectoryCount) + allDirectoryCount.ToString() + " Dir(s)";
                Console.WriteLine(line_1);
                Console.WriteLine(line_2);
            }
            else
            {
                Console.WriteLine("[-] No files found in {0} with pattern {1}", path, pattern);
            }
        }
Exemple #2
0
        static void Main(string[] args)
        {
            string path       = "";
            string pattern    = "";
            string searchTerm = "";

            // argument parsing

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

            try
            {
                foreach (var argument in args)
                {
                    var idx = argument.IndexOf(':');
                    if (idx > 0)
                    {
                        arguments[argument.Substring(0, idx)] = argument.Substring(idx + 1);
                    }
                    else
                    {
                        arguments[argument] = string.Empty;
                    }
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("[-] Exception parsing arguments:");
                Console.WriteLine(ex);
            }

            if (!arguments.ContainsKey("path"))
            {
                Console.WriteLine("[-] Error: No path was given.");
                Usage();
                Environment.Exit(1);
            }

            path = arguments["path"];
            if (!Directory.Exists(path))
            {
                Console.WriteLine("[X] Error: Directory {0} does not exist.", path);
                Environment.Exit(1);
            }

            if (arguments.ContainsKey("pattern"))
            {
                pattern = arguments["pattern"];
            }
            if (arguments.ContainsKey("searchterm"))
            {
                searchTerm = arguments["searchterm"];
            }

            string[] files = GetAllFiles(path, pattern);
            if (files.Length > 0)
            {
                Dictionary <string, string[]> sorted = new Dictionary <string, string[]>();
                string lastDir = "";
                // total size of all files listed
                double allFileSizes      = 0;
                int    allDirectoryCount = 0;
                int    allFilesCount     = 0;
                // directory specific file count
                double dirFileSizes = 0;
                int    dirFileCount = 0;
                FileExtensionHandler fileExtHandler = new FileExtensionHandler();
                foreach (string f in files)
                {
                    FileInfo fileInfo       = new FileInfo(f);
                    string   fileStatusLine = GetFileStatusString(fileInfo);
                    string   size           = HumanBytes(fileInfo.Length);

                    string lastWrite = File.GetLastWriteTime(fileInfo.FullName).Date.ToString();

                    if (searchTerm == "")
                    {
                        try
                        {
                            PrintResults(ref allFilesCount,
                                         ref allFileSizes,
                                         ref allDirectoryCount,
                                         ref lastDir,
                                         fileInfo,
                                         ref dirFileSizes,
                                         ref dirFileCount,
                                         fileStatusLine);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("[-] ERROR printing results.");
                            Console.WriteLine(ex);
                        }
                    }
                    else
                    {
                        bool hasSearchTerm = false;
                        if (pattern == "")
                        {
                            // Ensure this is not a bad file format.
                            if (fileExtHandler.HasCleanExtension(f))
                            {
                                hasSearchTerm = FileContainsString(f, searchTerm);
                            }
                            else
                            {
                                //Console.WriteLine("[-] Skipping reading {0}. To force this, specify the pattern of files to read with --pattern.", f);
                            }
                        }
                        else
                        {
                            hasSearchTerm = FileContainsString(f, searchTerm);
                        }
                        if (hasSearchTerm)
                        {
                            PrintResults(ref allFilesCount,
                                         ref allFileSizes,
                                         ref allDirectoryCount,
                                         ref lastDir,
                                         fileInfo,
                                         ref dirFileSizes,
                                         ref dirFileCount,
                                         fileStatusLine);
                        }
                    }
                }
                if (dirFileCount > 0)
                {
                    Console.WriteLine();
                    string statusString = GetDirectoryStatusString(dirFileCount, dirFileSizes);
                    Console.WriteLine(statusString);
                    dirFileSizes = 0;
                    dirFileCount = 0;
                }
                Console.WriteLine();
                Console.WriteLine("\tTotal Files Listed:");
                string line_1 = GetSpaces(allFilesCount) + allFilesCount.ToString() +
                                " File(s)" + GetSpaces(allFileSizes) + HumanBytes(allFileSizes);
                string line_2 = GetSpaces(allDirectoryCount) + allDirectoryCount.ToString() + " Dir(s)";
                Console.WriteLine(line_1);
                Console.WriteLine(line_2);
            }
            else
            {
                Console.WriteLine("[-] No files found in {0} with pattern {1}", path, pattern);
            }
        }
        private Task <string[]> ParseFiles(string[] files)
        {
            return(new Task <string[]>(() =>
            {
                List <string> validFiles = new List <string>(files);
                Mutex mtx = new Mutex();
                if (FilterExtensions.Length > 0)
                {
                    Parallel.ForEach(validFiles.ToArray(), fName =>
                    {
                        if (!FileExtensionHandler.EndsWithExtension(fName, FilterExtensions))
                        {
                            mtx.WaitOne();
                            validFiles.Remove(fName);
                            mtx.ReleaseMutex();
                        }
                    });
                }

                if (BlockExtensions.Length > 0)
                {
                    Parallel.ForEach(validFiles.ToArray(), fName =>
                    {
                        if (FileExtensionHandler.EndsWithExtension(fName, BlockExtensions))
                        {
                            mtx.WaitOne();
                            validFiles.Remove(fName);
                            mtx.ReleaseMutex();
                        }
                    });
                }

                if (!string.IsNullOrEmpty(Year))
                {
                    Parallel.ForEach(validFiles.ToArray(), fName =>
                    {
                        FileInfo fInfo = new FileInfo(fName);
                        string lastWrite = File.GetLastWriteTime(fInfo.FullName).Date.ToString();
                        if (!lastWrite.Contains(Year))
                        {
                            mtx.WaitOne();
                            validFiles.Remove(fName);
                            mtx.ReleaseMutex();
                        }
                    });
                }

                if (SearchTerms.Length > 0)
                {
                    Parallel.ForEach(validFiles.ToArray(), fName =>
                    {
                        if (FileExtensionHandler.HasCleanExtension(fName) &&
                            !FileContainsStrings(fName))
                        {
                            mtx.WaitOne();
                            validFiles.Remove(fName);
                            mtx.ReleaseMutex();
                        }
                        else if (!FileExtensionHandler.HasCleanExtension(fName))
                        {
                            Console.WriteLine($"[-] Removing file {fName} as it cannot be parsed for search terms.");
                            mtx.WaitOne();
                            validFiles.Remove(fName);
                            mtx.ReleaseMutex();
                        }
                    });
                }

                return validFiles.ToArray();
            }));
        }
        static void ParseDirectory(string path,
                                   string pattern         = null,
                                   string[] ext_whitelist = null,
                                   string[] ext_blacklist = null,
                                   string[] searchterms   = null,
                                   string year            = null)
        {
            string[] files       = null;
            string[] directories = null;
            // Get all the directories in the passed path
            directories = GetDirectoriesInDirectory(path);
            if (directories.Length > 0)
            {
                // For each directory, do the same file parsing and add to the thread queue.
                //Console.WriteLine("There's {0} directoreis to comb through", directories.Length);
                foreach (string dir in directories)
                {
                    //Console.WriteLine("Searching {0}", dir);
                    Thread t = new Thread(() => ParseDirectory(dir, pattern, ext_whitelist, ext_blacklist, searchterms, year));
                    t.Start();
                    //Console.WriteLine("Started thread");
                    _pool.WaitOne(500);
                    //Console.WriteLine("Waited");
                    runningThreads.Add(t);
                    _pool.Release(1);
                    //Console.WriteLine("released");
                }
            }
            files = GetFilesInDirectory(path, pattern);

            List <string> validExtensionFiles = new List <string>();

            if (ext_whitelist != null)
            {
                foreach (string fName in files)
                {
                    //Console.WriteLine("Checking {0}", fName);
                    if (FileExtensionHandler.EndsWithExtension(fName, ext_whitelist))
                    {
                        //Console.WriteLine("{0} looks valid", fName);
                        validExtensionFiles.Add(fName);
                    }
                }
            }
            else if (ext_blacklist != null)
            {
                foreach (string fName in files)
                {
                    if (!FileExtensionHandler.EndsWithExtension(fName, ext_blacklist))
                    {
                        //Console.WriteLine("{0} looks valid");
                        validExtensionFiles.Add(fName);
                    }
                }
            }
            else
            {
                foreach (string f in files)
                {
                    validExtensionFiles.Add(f);
                }
            }

            if (year != null && year != "")
            {
                foreach (string fname in validExtensionFiles.ToArray())
                {
                    FileInfo fileInfo  = new FileInfo(fname);
                    string   lastWrite = File.GetLastWriteTime(fileInfo.FullName).Date.ToString();
                    if (!lastWrite.Contains(year))
                    {
                        validExtensionFiles.Remove(fname);
                    }
                }
            }

            if (searchterms != null)
            {
                foreach (string fName in validExtensionFiles)
                {
                    if (FileExtensionHandler.HasCleanExtension(fName) && FileContainsStrings(fName, searchterms))
                    {
                        //Console.WriteLine("Waiting for pool thread");
                        _pool.WaitOne(500);
                        //Console.WriteLine("added {0}", fName);
                        matchingFiles.Add(fName);
                        _pool.Release(1);
                    }
                }
            }
            else
            {
                _pool.WaitOne(500);
                foreach (string fName in validExtensionFiles)
                {
                    //Console.WriteLine("yolo {0}", fName);
                    matchingFiles.Add(fName);
                }
                _pool.Release(1);
            }
            //Console.WriteLine("Done searching {0}", path);
        }