github.com/chainreactors/fingers@v1.2.1/fingers/matcher.go (about)

     1  package fingers
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/chainreactors/fingers/common"
     7  )
     8  
     9  func compiledMatch(reg CompiledRegexp, s []byte) (string, bool) {
    10  	matched := reg.FindSubmatch(s)
    11  	if matched == nil {
    12  		return "", false
    13  	}
    14  	if len(matched) == 1 {
    15  		return "", true
    16  	} else {
    17  		return strings.TrimSpace(string(matched[1])), true
    18  	}
    19  }
    20  
    21  func compiledAllMatch(reg CompiledRegexp, s string) ([]string, bool) {
    22  	matchedes := reg.FindAllString(s, -1)
    23  	if matchedes == nil {
    24  		return nil, false
    25  	}
    26  	return matchedes, true
    27  }
    28  
    29  func RuleMatcher(rule *Rule, content *Content, ishttp bool) (bool, bool, string, *common.MatchDetail) {
    30  	var hasFrame, hasVuln bool
    31  	var version string
    32  	var detail *common.MatchDetail
    33  	if rule.Regexps == nil {
    34  		return false, false, "", nil
    35  	}
    36  
    37  	hasFrame, hasVuln, version, detail = rule.Match(content.Content, content.Header, content.Body)
    38  	if hasFrame || !ishttp {
    39  		return hasFrame, hasVuln, version, detail
    40  	}
    41  
    42  	if content.Cert != "" {
    43  		hasFrame = rule.MatchCert(content.Cert)
    44  		if hasFrame && detail == nil {
    45  			detail = &common.MatchDetail{MatcherType: "cert"}
    46  		}
    47  	}
    48  
    49  	if version == "" && rule.Regexps.CompiledVersionRegexp != nil {
    50  		for _, reg := range rule.Regexps.CompiledVersionRegexp {
    51  			version, _ = compiledMatch(reg, content.Content)
    52  		}
    53  	}
    54  	return hasFrame, hasVuln, version, detail
    55  }