2016-07-20 11:02:01 +01:00
|
|
|
// (c) Copyright 2016 Hewlett Packard Enterprise Development LP
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package rules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go/ast"
|
2020-04-17 14:47:27 +01:00
|
|
|
"go/token"
|
2016-07-20 11:02:01 +01:00
|
|
|
"regexp"
|
2017-04-26 16:08:46 +01:00
|
|
|
"strconv"
|
2017-01-14 21:45:34 +00:00
|
|
|
|
2018-07-19 17:42:25 +01:00
|
|
|
zxcvbn "github.com/nbutton23/zxcvbn-go"
|
2023-03-30 08:31:24 +01:00
|
|
|
|
2020-04-01 21:18:39 +01:00
|
|
|
"github.com/securego/gosec/v2"
|
2023-02-15 19:44:13 +00:00
|
|
|
"github.com/securego/gosec/v2/issue"
|
2016-07-20 11:02:01 +01:00
|
|
|
)
|
|
|
|
|
2017-12-13 12:35:47 +00:00
|
|
|
type credentials struct {
|
2023-02-15 19:44:13 +00:00
|
|
|
issue.MetaData
|
2017-01-14 21:45:34 +00:00
|
|
|
pattern *regexp.Regexp
|
2023-06-15 09:18:03 +01:00
|
|
|
patternValue *regexp.Regexp // Pattern for matching string values (LHS on assign statements)
|
2017-01-14 21:45:34 +00:00
|
|
|
entropyThreshold float64
|
|
|
|
perCharThreshold float64
|
2017-01-14 22:39:22 +00:00
|
|
|
truncate int
|
2017-01-14 21:45:34 +00:00
|
|
|
ignoreEntropy bool
|
|
|
|
}
|
|
|
|
|
2017-10-05 22:32:03 +01:00
|
|
|
func (r *credentials) ID() string {
|
|
|
|
return r.MetaData.ID
|
|
|
|
}
|
|
|
|
|
2017-01-14 22:39:22 +00:00
|
|
|
func truncate(s string, n int) string {
|
|
|
|
if n > len(s) {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return s[:n]
|
|
|
|
}
|
|
|
|
|
2017-12-13 12:35:47 +00:00
|
|
|
func (r *credentials) isHighEntropyString(str string) bool {
|
2017-01-14 22:39:22 +00:00
|
|
|
s := truncate(str, r.truncate)
|
2017-01-14 21:45:34 +00:00
|
|
|
info := zxcvbn.PasswordStrength(s, []string{})
|
|
|
|
entropyPerChar := info.Entropy / float64(len(s))
|
|
|
|
return (info.Entropy >= r.entropyThreshold ||
|
|
|
|
(info.Entropy >= (r.entropyThreshold/2) &&
|
|
|
|
entropyPerChar >= r.perCharThreshold))
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
|
2023-02-15 19:44:13 +00:00
|
|
|
func (r *credentials) Match(n ast.Node, ctx *gosec.Context) (*issue.Issue, error) {
|
2016-11-13 20:57:59 +00:00
|
|
|
switch node := n.(type) {
|
|
|
|
case *ast.AssignStmt:
|
|
|
|
return r.matchAssign(node, ctx)
|
2018-03-08 23:49:49 +00:00
|
|
|
case *ast.ValueSpec:
|
|
|
|
return r.matchValueSpec(node, ctx)
|
2020-04-17 14:47:27 +01:00
|
|
|
case *ast.BinaryExpr:
|
|
|
|
return r.matchEqualityCheck(node, ctx)
|
2016-11-13 20:57:59 +00:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2023-02-15 19:44:13 +00:00
|
|
|
func (r *credentials) matchAssign(assign *ast.AssignStmt, ctx *gosec.Context) (*issue.Issue, error) {
|
2016-11-13 20:57:59 +00:00
|
|
|
for _, i := range assign.Lhs {
|
|
|
|
if ident, ok := i.(*ast.Ident); ok {
|
2023-06-15 09:18:03 +01:00
|
|
|
// First check LHS to find anything being assigned to variables whose name appears to be a cred
|
2016-11-13 20:57:59 +00:00
|
|
|
if r.pattern.MatchString(ident.Name) {
|
|
|
|
for _, e := range assign.Rhs {
|
2018-07-19 17:42:25 +01:00
|
|
|
if val, err := gosec.GetString(e); err == nil {
|
2017-01-14 21:45:34 +00:00
|
|
|
if r.ignoreEntropy || (!r.ignoreEntropy && r.isHighEntropyString(val)) {
|
2023-02-15 19:44:13 +00:00
|
|
|
return ctx.NewIssue(assign, r.ID(), r.What, r.Severity, r.Confidence), nil
|
2017-01-14 21:45:34 +00:00
|
|
|
}
|
2016-11-13 20:57:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-15 09:18:03 +01:00
|
|
|
|
|
|
|
// Now that no names were matched, match the RHS to see if the actual values being assigned are creds
|
|
|
|
for _, e := range assign.Rhs {
|
|
|
|
val, err := gosec.GetString(e)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.patternValue.MatchString(val) {
|
|
|
|
if r.ignoreEntropy || r.isHighEntropyString(val) {
|
|
|
|
return ctx.NewIssue(assign, r.ID(), r.What, r.Severity, r.Confidence), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-13 20:57:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2023-02-15 19:44:13 +00:00
|
|
|
func (r *credentials) matchValueSpec(valueSpec *ast.ValueSpec, ctx *gosec.Context) (*issue.Issue, error) {
|
2023-06-15 09:18:03 +01:00
|
|
|
// Running match against the variable name(s) first. Will catch any creds whose var name matches the pattern,
|
|
|
|
// then will go back over to check the values themselves.
|
2018-03-08 23:49:49 +00:00
|
|
|
for index, ident := range valueSpec.Names {
|
|
|
|
if r.pattern.MatchString(ident.Name) && valueSpec.Values != nil {
|
|
|
|
// const foo, bar = "same value"
|
|
|
|
if len(valueSpec.Values) <= index {
|
|
|
|
index = len(valueSpec.Values) - 1
|
|
|
|
}
|
2018-07-19 17:42:25 +01:00
|
|
|
if val, err := gosec.GetString(valueSpec.Values[index]); err == nil {
|
2018-03-08 23:49:49 +00:00
|
|
|
if r.ignoreEntropy || (!r.ignoreEntropy && r.isHighEntropyString(val)) {
|
2023-02-15 19:44:13 +00:00
|
|
|
return ctx.NewIssue(valueSpec, r.ID(), r.What, r.Severity, r.Confidence), nil
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-15 09:18:03 +01:00
|
|
|
|
|
|
|
// Now that no variable names have been matched, match the actual values to find any creds
|
|
|
|
for _, ident := range valueSpec.Values {
|
|
|
|
if val, err := gosec.GetString(ident); err == nil {
|
|
|
|
if r.patternValue.MatchString(val) {
|
|
|
|
if r.ignoreEntropy || r.isHighEntropyString(val) {
|
|
|
|
return ctx.NewIssue(valueSpec, r.ID(), r.What, r.Severity, r.Confidence), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-13 20:57:59 +00:00
|
|
|
return nil, nil
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
|
2023-02-15 19:44:13 +00:00
|
|
|
func (r *credentials) matchEqualityCheck(binaryExpr *ast.BinaryExpr, ctx *gosec.Context) (*issue.Issue, error) {
|
2020-04-17 14:47:27 +01:00
|
|
|
if binaryExpr.Op == token.EQL || binaryExpr.Op == token.NEQ {
|
2023-01-31 08:52:37 +00:00
|
|
|
ident, ok := binaryExpr.X.(*ast.Ident)
|
|
|
|
if !ok {
|
|
|
|
ident, _ = binaryExpr.Y.(*ast.Ident)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ident != nil && r.pattern.MatchString(ident.Name) {
|
|
|
|
valueNode := binaryExpr.Y
|
|
|
|
if !ok {
|
|
|
|
valueNode = binaryExpr.X
|
|
|
|
}
|
|
|
|
if val, err := gosec.GetString(valueNode); err == nil {
|
|
|
|
if r.ignoreEntropy || (!r.ignoreEntropy && r.isHighEntropyString(val)) {
|
2023-02-15 19:44:13 +00:00
|
|
|
return ctx.NewIssue(binaryExpr, r.ID(), r.What, r.Severity, r.Confidence), nil
|
2020-04-17 14:47:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-15 09:18:03 +01:00
|
|
|
|
|
|
|
// Now that the variable names have been checked, and no matches were found, make sure that
|
|
|
|
// either the left or right operands is a string literal so we can match the value.
|
|
|
|
identStrConst, ok := binaryExpr.X.(*ast.BasicLit)
|
|
|
|
if !ok {
|
|
|
|
identStrConst, ok = binaryExpr.Y.(*ast.BasicLit)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok && identStrConst.Kind == token.STRING {
|
|
|
|
s, _ := gosec.GetString(identStrConst)
|
|
|
|
if r.patternValue.MatchString(s) {
|
|
|
|
if r.ignoreEntropy || r.isHighEntropyString(s) {
|
|
|
|
return ctx.NewIssue(binaryExpr, r.ID(), r.What, r.Severity, r.Confidence), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-17 14:47:27 +01:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-12-13 12:35:47 +00:00
|
|
|
// NewHardcodedCredentials attempts to find high entropy string constants being
|
|
|
|
// assigned to variables that appear to be related to credentials.
|
2018-07-19 17:42:25 +01:00
|
|
|
func NewHardcodedCredentials(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
|
2021-07-19 10:23:39 +01:00
|
|
|
pattern := `(?i)passwd|pass|password|pwd|secret|token|pw|apiKey|bearer|cred`
|
2023-06-15 09:18:03 +01:00
|
|
|
patternValue := "(?i)(^(.*[:;,](\\s)*)?[a-f0-9]{64}$)|(AIza[0-9A-Za-z-_]{35})|(^(.*[:;,](\\s)*)?github_pat_[a-zA-Z0-9]{22}_[a-zA-Z0-9]{59}$)|(^(.*[:;,](\\s)*)?[0-9a-zA-Z-_]{24}$)"
|
2017-01-14 21:45:34 +00:00
|
|
|
entropyThreshold := 80.0
|
|
|
|
perCharThreshold := 3.0
|
|
|
|
ignoreEntropy := false
|
2021-05-31 09:44:12 +01:00
|
|
|
truncateString := 16
|
2022-03-28 19:28:02 +01:00
|
|
|
if val, ok := conf[id]; ok {
|
2020-04-15 15:10:21 +01:00
|
|
|
conf := val.(map[string]interface{})
|
2017-01-14 21:45:34 +00:00
|
|
|
if configPattern, ok := conf["pattern"]; ok {
|
2020-04-15 15:10:21 +01:00
|
|
|
if cfgPattern, ok := configPattern.(string); ok {
|
|
|
|
pattern = cfgPattern
|
|
|
|
}
|
2017-01-14 21:45:34 +00:00
|
|
|
}
|
2023-06-15 09:18:03 +01:00
|
|
|
|
|
|
|
if configPatternValue, ok := conf["patternValue"]; ok {
|
|
|
|
if cfgPatternValue, ok := configPatternValue.(string); ok {
|
|
|
|
patternValue = cfgPatternValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-14 21:45:34 +00:00
|
|
|
if configIgnoreEntropy, ok := conf["ignore_entropy"]; ok {
|
2020-04-15 15:10:21 +01:00
|
|
|
if cfgIgnoreEntropy, ok := configIgnoreEntropy.(bool); ok {
|
|
|
|
ignoreEntropy = cfgIgnoreEntropy
|
2017-01-14 21:45:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if configEntropyThreshold, ok := conf["entropy_threshold"]; ok {
|
2020-04-15 15:10:21 +01:00
|
|
|
if cfgEntropyThreshold, ok := configEntropyThreshold.(string); ok {
|
|
|
|
if parsedNum, err := strconv.ParseFloat(cfgEntropyThreshold, 64); err == nil {
|
|
|
|
entropyThreshold = parsedNum
|
|
|
|
}
|
2017-01-14 21:45:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if configCharThreshold, ok := conf["per_char_threshold"]; ok {
|
2020-04-15 15:10:21 +01:00
|
|
|
if cfgCharThreshold, ok := configCharThreshold.(string); ok {
|
|
|
|
if parsedNum, err := strconv.ParseFloat(cfgCharThreshold, 64); err == nil {
|
|
|
|
perCharThreshold = parsedNum
|
|
|
|
}
|
2017-01-14 21:45:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if configTruncate, ok := conf["truncate"]; ok {
|
2020-04-15 15:10:21 +01:00
|
|
|
if cfgTruncate, ok := configTruncate.(string); ok {
|
|
|
|
if parsedInt, err := strconv.Atoi(cfgTruncate); err == nil {
|
|
|
|
truncateString = parsedInt
|
|
|
|
}
|
2017-01-14 21:45:34 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-13 20:57:59 +00:00
|
|
|
}
|
2017-01-14 21:45:34 +00:00
|
|
|
|
2017-12-13 12:35:47 +00:00
|
|
|
return &credentials{
|
2017-01-14 21:45:34 +00:00
|
|
|
pattern: regexp.MustCompile(pattern),
|
2023-06-15 09:18:03 +01:00
|
|
|
patternValue: regexp.MustCompile(patternValue),
|
2017-01-14 21:45:34 +00:00
|
|
|
entropyThreshold: entropyThreshold,
|
|
|
|
perCharThreshold: perCharThreshold,
|
|
|
|
ignoreEntropy: ignoreEntropy,
|
|
|
|
truncate: truncateString,
|
2023-02-15 19:44:13 +00:00
|
|
|
MetaData: issue.MetaData{
|
2017-10-05 22:32:03 +01:00
|
|
|
ID: id,
|
2016-07-20 11:02:01 +01:00
|
|
|
What: "Potential hardcoded credentials",
|
2023-02-15 19:44:13 +00:00
|
|
|
Confidence: issue.Low,
|
|
|
|
Severity: issue.High,
|
2016-07-20 11:02:01 +01:00
|
|
|
},
|
2020-04-17 14:47:27 +01:00
|
|
|
}, []ast.Node{(*ast.AssignStmt)(nil), (*ast.ValueSpec)(nil), (*ast.BinaryExpr)(nil)}
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|