hashicorp.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package rules
  2. import (
  3. "fmt"
  4. "github.com/zricethezav/gitleaks/v8/cmd/generate/config/utils"
  5. "github.com/zricethezav/gitleaks/v8/cmd/generate/secrets"
  6. "github.com/zricethezav/gitleaks/v8/config"
  7. "github.com/zricethezav/gitleaks/v8/regexp"
  8. )
  9. func HashiCorpTerraform() *config.Rule {
  10. // define rule
  11. r := config.Rule{
  12. RuleID: "hashicorp-tf-api-token",
  13. Description: "Uncovered a HashiCorp Terraform user/org API token, which may lead to unauthorized infrastructure management and security breaches.",
  14. Regex: regexp.MustCompile(`(?i)[a-z0-9]{14}\.(?-i:atlasv1)\.[a-z0-9\-_=]{60,70}`),
  15. Entropy: 3.5,
  16. Keywords: []string{"atlasv1"},
  17. }
  18. // validate
  19. tps := utils.GenerateSampleSecrets("hashicorpToken", secrets.NewSecret(utils.Hex("14"))+".atlasv1."+secrets.NewSecret(utils.AlphaNumericExtended("60,70")))
  20. tps = append(tps,
  21. `#token = "hE1hlYILrSqpqh.atlasv1.ARjZuyzl33F71WR55s6ln5GQ1HWIwTDDH3MiRjz7OnpCfaCb1RCF5zGaSncCWmJdcYA"`,
  22. )
  23. fps := []string{
  24. `token = "xxxxxxxxxxxxxx.atlasv1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"`, // low entropy
  25. }
  26. return utils.Validate(r, tps, fps)
  27. }
  28. func HashicorpField() *config.Rule {
  29. keywords := []string{"administrator_login_password", "password"}
  30. // define rule
  31. r := config.Rule{
  32. RuleID: "hashicorp-tf-password",
  33. Description: "Identified a HashiCorp Terraform password field, risking unauthorized infrastructure configuration and security breaches.",
  34. Regex: utils.GenerateSemiGenericRegex(keywords, fmt.Sprintf(`"%s"`, utils.AlphaNumericExtended("8,20")), true),
  35. Entropy: 2,
  36. Path: regexp.MustCompile(`(?i)\.(?:tf|hcl)$`),
  37. Keywords: keywords,
  38. }
  39. tps := map[string]string{
  40. // Example from: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/sql_server.html
  41. "file.tf": "administrator_login_password = " + `"thisIsDog11"`,
  42. // https://registry.terraform.io/providers/petoju/mysql/latest/docs
  43. "file.hcl": "password = " + `"rootpasswd"`,
  44. }
  45. fps := map[string]string{
  46. "file.tf": "administrator_login_password = var.db_password",
  47. "file.hcl": `password = "${aws_db_instance.default.password}"`,
  48. "unrelated.js": "password = " + `"rootpasswd"`,
  49. }
  50. return utils.ValidateWithPaths(r, tps, fps)
  51. }