예제 #1
0
 public DirClassifier(ClassifierRule inRule)
 {
     this.ClassifierRule = inRule;
 }
예제 #2
0
        public bool ClassifyFile(FileInfo fileInfo)
        {
            BlockingMq Mq = BlockingMq.GetMq();
            // figure out what part we gonna look at
            string stringToMatch = null;

            switch (ClassifierRule.MatchLocation)
            {
            case MatchLoc.FileExtension:
                stringToMatch = fileInfo.Extension;
                // special handling to treat files named like 'thing.kdbx.bak'
                if (stringToMatch == ".bak")
                {
                    // strip off .bak
                    string subName = fileInfo.Name.Replace(".bak", "");
                    stringToMatch = Path.GetExtension(subName);
                    // if this results in no file extension, put it back.
                    if (stringToMatch == "")
                    {
                        stringToMatch = ".bak";
                    }
                }
                // this is insane that i have to do this but apparently files with no extension return
                // this bullshit
                if (stringToMatch == "")
                {
                    return(false);
                }
                break;

            case MatchLoc.FileName:
                stringToMatch = fileInfo.Name;
                break;

            case MatchLoc.FilePath:
                stringToMatch = fileInfo.FullName;
                break;

            case MatchLoc.FileLength:
                if (!SizeMatch(fileInfo))
                {
                    return(false);
                }
                else
                {
                    break;
                }

            default:
                Mq.Error("You've got a misconfigured file classifier rule named " + ClassifierRule.RuleName + ".");
                return(false);
            }

            TextResult textResult = null;

            if (!String.IsNullOrEmpty(stringToMatch))
            {
                TextClassifier textClassifier = new TextClassifier(ClassifierRule);
                // check if it matches
                textResult = textClassifier.TextMatch(stringToMatch);
                if (textResult == null)
                {
                    // if it doesn't we just bail now.
                    return(false);
                }
            }

            FileResult fileResult;

            // if it matches, see what we're gonna do with it
            switch (ClassifierRule.MatchAction)
            {
            case MatchAction.Discard:
                // chuck it.
                return(true);

            case MatchAction.Snaffle:
                // snaffle that bad boy
                fileResult = new FileResult(fileInfo)
                {
                    MatchedRule = ClassifierRule,
                    TextResult  = textResult
                };
                // if the file was list-only, don't bother sending a result back to the user.
                if (!fileResult.RwStatus.CanRead && !fileResult.RwStatus.CanModify && !fileResult.RwStatus.CanWrite)
                {
                    return(false);
                }
                ;
                Mq.FileResult(fileResult);
                return(false);

            case MatchAction.CheckForKeys:
                // do a special x509 dance
                List <string> x509MatchReason = x509Match(fileInfo);
                if (x509MatchReason.Count >= 0)
                {
                    // if there were any matchreasons, cat them together...
                    string matchContext = String.Join(",", x509MatchReason);
                    // and sling the results on the queue
                    fileResult = new FileResult(fileInfo)
                    {
                        MatchedRule = ClassifierRule,
                        TextResult  = new TextResult()
                        {
                            MatchContext   = matchContext,
                            MatchedStrings = new List <string>()
                            {
                                ""
                            }
                        }
                    };

                    if (!fileResult.RwStatus.CanRead && !fileResult.RwStatus.CanModify && !fileResult.RwStatus.CanWrite)
                    {
                        return(false);
                    }
                    ;

                    Mq.FileResult(fileResult);
                }
                return(false);

            case MatchAction.Relay:
                // bounce it on to the next ClassifierRule
                try
                {
                    bool fLoggedContentSizeWarning = false;

                    foreach (string relayTarget in ClassifierRule.RelayTargets)
                    {
                        ClassifierRule nextRule =
                            MyOptions.ClassifierRules.First(thing => thing.RuleName == relayTarget);

                        if (nextRule.EnumerationScope == EnumerationScope.ContentsEnumeration)
                        {
                            if (fileInfo.Length > MyOptions.MaxSizeToGrep)
                            {
                                if (!fLoggedContentSizeWarning)
                                {
                                    // Just log once per relay rule, no need to fill up the log with one for each relay target
                                    Mq.Trace("The following file was bigger than the MaxSizeToGrep config parameter:" + fileInfo.FullName);
                                    fLoggedContentSizeWarning = true;
                                }

                                continue;
                            }

                            ContentClassifier nextContentClassifier = new ContentClassifier(nextRule);
                            nextContentClassifier.ClassifyContent(fileInfo);
                        }
                        else if (nextRule.EnumerationScope == EnumerationScope.FileEnumeration)
                        {
                            FileClassifier nextFileClassifier = new FileClassifier(nextRule);
                            nextFileClassifier.ClassifyFile(fileInfo);
                        }
                        else
                        {
                            Mq.Error("You've got a misconfigured file ClassifierRule named " + ClassifierRule.RuleName + ".");
                        }
                    }
                    return(false);
                }
                catch (IOException e)
                {
                    Mq.Trace(e.ToString());
                }
                catch (Exception e)
                {
                    Mq.Error("You've got a misconfigured file ClassifierRule named " + ClassifierRule.RuleName + ".");
                    Mq.Trace(e.ToString());
                }
                return(false);

            case MatchAction.EnterArchive:
                // do a special looking inside archive files dance using
                // https://github.com/adamhathcock/sharpcompress
                // TODO FUUUUUCK
                throw new NotImplementedException("Haven't implemented walking dir structures inside archives.");

            default:
                Mq.Error("You've got a misconfigured file ClassifierRule named " + ClassifierRule.RuleName + ".");
                return(false);
            }
        }
예제 #3
0
 public FileClassifier(ClassifierRule inRule)
 {
     this.ClassifierRule = inRule;
 }
예제 #4
0
 public ContentClassifier(ClassifierRule inRule)
 {
     this.ClassifierRule = inRule;
 }
예제 #5
0
 public ShareClassifier(ClassifierRule inRule)
 {
     this.ClassifierRule = inRule;
 }
예제 #6
0
 public TextClassifier(ClassifierRule inRule)
 {
     this.ClassifierRule = inRule;
 }