コード例 #1
0
ファイル: Program.cs プロジェクト: KasperWittrup/CPR-Scanner
        static void Main(string[] args)
        {
            if (1 == 1)
            {
                MailHandler MH = new MailHandler();
                DataManager DM = new DataManager();
                if (1 == 1)
                {
                    using (SqlConnection SC = DM.getStatsConnection())
                    {
                        Console.WriteLine("Approve validated files...");
                        SC.Open();
                        SqlDataReader DFReader  = null;
                        SqlCommand    DFCommand = new SqlCommand("select FilePath from FileScannerDangerousFiles where Validated is null", SC);
                        DFReader = DFCommand.ExecuteReader();
                        while (DFReader.Read())
                        {
                            AlreadyDangerous.Add(DFReader[0].ToString());
                        }


                        SqlDataReader approveFileReader  = null;
                        SqlCommand    approveFileCommand = new SqlCommand("select FilePath from FileScannerDangerousFiles where Validated = 1 and filePath not like 'http%'", SC);
                        approveFileReader = approveFileCommand.ExecuteReader();
                        while (approveFileReader.Read())
                        {
                            try
                            {
                                FileInfo fi = new System.IO.FileInfo(@approveFileReader[0].ToString());

                                SqlCommand cmd = new SqlCommand("insert into FileScanner (FilePath,UnixLastEdit) VALUES ('" + approveFileReader[0].ToString().Replace("'", "''") + "','" + getUnixTime(fi.LastWriteTime) + "')", SC);
                                cmd.ExecuteNonQuery();

                                SqlCommand cmd1 = new SqlCommand("delete from FileScannerDangerousFiles where FilePath ='" + approveFileReader[0].ToString().Replace("'", "''") + "'", SC);
                                cmd1.ExecuteNonQuery();
                            }
                            catch { }
                        }



                        SqlDataReader approveWebReader  = null;
                        SqlCommand    approveWebCommand = new SqlCommand("select FilePath from FileScannerDangerousFiles where Validated = 1 and filePath like 'http%'", SC);
                        approveWebReader = approveWebCommand.ExecuteReader();
                        while (approveWebReader.Read())
                        {
                            try
                            {
                                SqlCommand cmd = new SqlCommand("insert into FileScanner (FilePath,UnixLastEdit) VALUES ('" + approveWebReader[0].ToString().Replace("'", "''") + "','0')", SC);
                                cmd.ExecuteNonQuery();

                                SqlCommand cmd1 = new SqlCommand("delete from FileScannerDangerousFiles where FilePath ='" + approveWebReader[0].ToString().Replace("'", "''") + "'", SC);
                                cmd1.ExecuteNonQuery();
                            }
                            catch { }
                        }



                        SqlDataReader itemReader  = null;
                        SqlCommand    itemCommand = new SqlCommand("select BasePath, OwnerEmail from FileScannerPaths", SC);
                        itemReader = itemCommand.ExecuteReader();
                        while (itemReader.Read())
                        {
                            string basePath = itemReader[0].ToString();
                            Console.WriteLine("Load Checksums");
                            CheckSums = GetChecks(basePath);

                            List <FileHit> temp      = new List <FileHit>();
                            List <FileHit> Documents = LoadDirectories(basePath, temp);
                            Console.WriteLine("Documents found: " + Documents.Count);
                        }
                    }
                }
                Console.WriteLine("All Done!");
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: KasperWittrup/CPR-Scanner
        public static List <FileHit> LoadDirectories(string DirectoryPath, List <FileHit> T)
        {
            List <FileHit> r = T;

            DataManager DM = new DataManager();

            try
            {
                foreach (string fileName in Directory.GetFiles(@DirectoryPath))
                {
                    try
                    {
                        if (!CheckSums.Contains(fileName) && !AlreadyDangerous.Contains(fileName))
                        {
                            FileInfo fi = null;
                            fi = new System.IO.FileInfo(@fileName);
                            //Do check
                            Console.WriteLine("TEST FILE: " + fileName);
                            string FileContent = "";
                            switch (fi.Extension.ToLower())
                            {
                            case ".pdf":
                                Console.WriteLine("PDF CHECK");
                                FileContent = ReadPDFFile(fileName);
                                break;

                            case ".xls":
                                Console.WriteLine("EXCEL CHECK");
                                FileContent = ReadExcelFile(fileName);
                                break;

                            case ".xlsx":
                                Console.WriteLine("EXCEL CHECK");
                                FileContent = ReadExcelFile(fileName);
                                break;

                            case ".html":
                                Console.WriteLine("HTML CHECK");
                                FileContent = ReadTextFile(fileName);
                                break;

                            case ".htm":
                                Console.WriteLine("HTML CHECK");
                                FileContent = ReadTextFile(fileName);
                                break;

                            case ".txt":
                                Console.WriteLine("TXT CHECK");
                                FileContent = ReadTextFile(fileName);
                                break;

                            case ".xml":
                                Console.WriteLine("XML CHECK");
                                FileContent = ReadTextFile(fileName);
                                break;

                            case ".xsl":
                                Console.WriteLine("XSL CHECK");
                                FileContent = ReadTextFile(fileName);
                                break;

                            case ".doc":
                                Console.WriteLine("DOC CHECK");
                                FileContent = ReadDocFile(fileName);
                                break;

                            case ".docx":
                                Console.WriteLine("DOCX CHECK");
                                FileContent = ReadDocFile(fileName);
                                break;
                            }

                            string CPRF     = "";
                            bool   HasCPR   = false;
                            int    CPRCount = 0;
                            if (FileContent.Length > 1)
                            {
                                foreach (string Word in FileContent.Split(new Char[] { '.', ',', ' ', '\n' }))
                                {
                                    string FixWord = Word.Trim();
                                    if (CPRValid(FixWord))
                                    {
                                        CPRF = FixWord;
                                        if (FixWord.Contains('-'))
                                        {
                                            HasCPR = true;
                                            Console.WriteLine("CPR Found: " + FixWord);
                                            FileHit FH = new FileHit();
                                            FH.FilePath = fileName;
                                            FH.FileID   = AddDangerousFile(fileName);
                                            r.Add(FH);
                                        }
                                        else
                                        {
                                            HasCPR = true;
                                            CPRCount++;
                                        }
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                if (fi.Extension.ToLower().Equals(".pdf"))
                                {
                                    try
                                    {
                                        Console.WriteLine("OCR Read document");
                                        FileContent = OCRPDF(fileName);
                                        if (FileContent.Length > 1)
                                        {
                                            foreach (string Word in FileContent.Split(new Char[] { '.', ',', ' ', '\n' }))
                                            {
                                                string FixWord = Word.Trim();
                                                if (CPRValid(FixWord))
                                                {
                                                    CPRF = FixWord;
                                                    if (FixWord.Contains('-'))
                                                    {
                                                        HasCPR = true;
                                                        Console.WriteLine("CPR Found: " + FixWord);
                                                        FileHit FH1 = new FileHit();
                                                        FH1.FilePath = fileName;
                                                        FH1.FileID   = AddDangerousFile(fileName);
                                                        r.Add(FH1);
                                                    }
                                                    else
                                                    {
                                                        HasCPR = true;
                                                        CPRCount++;
                                                    }
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                    catch
                                    {
                                        FileContent = "ERROR - DISREGARD";
                                        HasCPR      = false;
                                    }
                                }
                                else
                                {
                                    Console.WriteLine("Nothing in file...");
                                }
                            }

                            if (CPRCount > 0 && CPRCount < 5000)
                            {
                                Console.WriteLine("CPR Found: " + CPRF);
                                FileHit FH = new FileHit();
                                FH.FilePath = fileName;
                                FH.FileID   = AddDangerousFile(fileName);
                                r.Add(FH);
                                HasCPR = true;
                            }



                            if (!HasCPR)
                            {
                                using (SqlConnection SC = DM.getStatsConnection())
                                {
                                    SC.Open();
                                    SqlCommand cmd = new SqlCommand("insert into FileScanner (FilePath,UnixLastEdit) VALUES ('" + fileName.Replace("'", "''") + "','" + getUnixTime(fi.LastWriteTime) + "')", SC);
                                    cmd.ExecuteNonQuery();
                                    Console.WriteLine("File Cleared...");
                                }
                            }
                            GC.Collect();
                        }
                        else
                        {
                            Console.WriteLine("DO NOT TEST FILE: " + fileName);
                        }
                    }
                    catch
                    {
                        Console.WriteLine("ERROR IN FILE: " + fileName);
                    }
                }
            }
            catch { }
            try
            {
                foreach (string SubDirectories in Directory.GetDirectories(DirectoryPath))
                {
                    foreach (FileHit FH in LoadDirectories(SubDirectories, T))
                    {
                        if (!r.Contains(FH))
                        {
                            r.Add(FH);
                        }
                    }
                }
            }
            catch { }

            return(r);
        }