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"
|
|
|
|
"regexp"
|
2017-04-26 16:08:46 +01:00
|
|
|
"strconv"
|
2017-01-14 21:45:34 +00:00
|
|
|
|
2017-04-26 16:08:46 +01:00
|
|
|
"github.com/GoASTScanner/gas"
|
2017-01-14 21:45:34 +00:00
|
|
|
"github.com/nbutton23/zxcvbn-go"
|
2016-07-20 11:02:01 +01:00
|
|
|
)
|
|
|
|
|
2017-12-13 12:35:47 +00:00
|
|
|
type credentials struct {
|
2016-07-20 11:02:01 +01:00
|
|
|
gas.MetaData
|
2017-01-14 21:45:34 +00:00
|
|
|
pattern *regexp.Regexp
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-12-13 12:35:47 +00:00
|
|
|
func (r *credentials) Match(n ast.Node, ctx *gas.Context) (*gas.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)
|
2016-11-13 20:57:59 +00:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-12-13 12:35:47 +00:00
|
|
|
func (r *credentials) matchAssign(assign *ast.AssignStmt, ctx *gas.Context) (*gas.Issue, error) {
|
2016-11-13 20:57:59 +00:00
|
|
|
for _, i := range assign.Lhs {
|
|
|
|
if ident, ok := i.(*ast.Ident); ok {
|
|
|
|
if r.pattern.MatchString(ident.Name) {
|
|
|
|
for _, e := range assign.Rhs {
|
2017-01-14 21:45:34 +00:00
|
|
|
if val, err := gas.GetString(e); err == nil {
|
|
|
|
if r.ignoreEntropy || (!r.ignoreEntropy && r.isHighEntropyString(val)) {
|
|
|
|
return gas.NewIssue(ctx, assign, r.What, r.Severity, r.Confidence), nil
|
|
|
|
}
|
2016-11-13 20:57:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-03-08 23:49:49 +00:00
|
|
|
func (r *credentials) matchValueSpec(valueSpec *ast.ValueSpec, ctx *gas.Context) (*gas.Issue, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if val, err := gas.GetString(valueSpec.Values[index]); err == nil {
|
|
|
|
if r.ignoreEntropy || (!r.ignoreEntropy && r.isHighEntropyString(val)) {
|
|
|
|
return gas.NewIssue(ctx, valueSpec, r.What, r.Severity, r.Confidence), nil
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-13 20:57:59 +00:00
|
|
|
return nil, nil
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
|
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.
|
2017-10-05 22:32:03 +01:00
|
|
|
func NewHardcodedCredentials(id string, conf gas.Config) (gas.Rule, []ast.Node) {
|
2016-11-13 20:57:59 +00:00
|
|
|
pattern := `(?i)passwd|pass|password|pwd|secret|token`
|
2017-01-14 21:45:34 +00:00
|
|
|
entropyThreshold := 80.0
|
|
|
|
perCharThreshold := 3.0
|
|
|
|
ignoreEntropy := false
|
2017-12-13 12:35:47 +00:00
|
|
|
var truncateString = 16
|
2016-11-13 20:57:59 +00:00
|
|
|
if val, ok := conf["G101"]; ok {
|
2017-01-14 21:45:34 +00:00
|
|
|
conf := val.(map[string]string)
|
|
|
|
if configPattern, ok := conf["pattern"]; ok {
|
|
|
|
pattern = configPattern
|
|
|
|
}
|
|
|
|
if configIgnoreEntropy, ok := conf["ignore_entropy"]; ok {
|
|
|
|
if parsedBool, err := strconv.ParseBool(configIgnoreEntropy); err == nil {
|
|
|
|
ignoreEntropy = parsedBool
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if configEntropyThreshold, ok := conf["entropy_threshold"]; ok {
|
|
|
|
if parsedNum, err := strconv.ParseFloat(configEntropyThreshold, 64); err == nil {
|
|
|
|
entropyThreshold = parsedNum
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if configCharThreshold, ok := conf["per_char_threshold"]; ok {
|
|
|
|
if parsedNum, err := strconv.ParseFloat(configCharThreshold, 64); err == nil {
|
|
|
|
perCharThreshold = parsedNum
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if configTruncate, ok := conf["truncate"]; ok {
|
2017-01-14 22:39:22 +00:00
|
|
|
if parsedInt, err := strconv.Atoi(configTruncate); err == nil {
|
2017-01-14 21:45:34 +00:00
|
|
|
truncateString = parsedInt
|
|
|
|
}
|
|
|
|
}
|
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),
|
|
|
|
entropyThreshold: entropyThreshold,
|
|
|
|
perCharThreshold: perCharThreshold,
|
|
|
|
ignoreEntropy: ignoreEntropy,
|
|
|
|
truncate: truncateString,
|
2016-07-20 11:02:01 +01:00
|
|
|
MetaData: gas.MetaData{
|
2017-10-05 22:32:03 +01:00
|
|
|
ID: id,
|
2016-07-20 11:02:01 +01:00
|
|
|
What: "Potential hardcoded credentials",
|
|
|
|
Confidence: gas.Low,
|
|
|
|
Severity: gas.High,
|
|
|
|
},
|
2018-03-08 23:49:49 +00:00
|
|
|
}, []ast.Node{(*ast.AssignStmt)(nil), (*ast.ValueSpec)(nil)}
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|