generic.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package rules
  2. import (
  3. "github.com/zricethezav/gitleaks/v8/cmd/generate/config/utils"
  4. "github.com/zricethezav/gitleaks/v8/cmd/generate/secrets"
  5. "github.com/zricethezav/gitleaks/v8/config"
  6. "regexp"
  7. )
  8. func GenericCredential() *config.Rule {
  9. // define rule
  10. r := config.Rule{
  11. RuleID: "generic-api-key",
  12. Description: "Detected a Generic API Key, potentially exposing access to various services and sensitive operations.",
  13. Regex: utils.GenerateSemiGenericRegex([]string{
  14. "key",
  15. "api",
  16. "token",
  17. "secret",
  18. "credential",
  19. "creds",
  20. "passwd",
  21. "password",
  22. "auth",
  23. "access",
  24. }, `[0-9a-z\-_.=]{10,150}`, true),
  25. Keywords: []string{
  26. "key",
  27. "api",
  28. "token",
  29. "secret",
  30. "credential",
  31. "creds",
  32. "passwd",
  33. "password",
  34. "auth",
  35. "access",
  36. },
  37. Entropy: 3.5,
  38. Allowlists: []config.Allowlist{
  39. {
  40. Description: "Allowlist for Generic API Keys",
  41. MatchCondition: config.AllowlistMatchOr,
  42. Regexes: []*regexp.Regexp{
  43. regexp.MustCompile(`(?i)(` +
  44. `public[_.-]?(key|token)` + // public key -> not a secret
  45. `|api[_.-]?(version|id)` + // version/id -> not a secret
  46. `|(secret)[_.-]?name` + // name of e.g. env variable
  47. `|issuerkeyhash` + // part of ssl cert
  48. `|(?-i:[DdMm]onkey|[DM]ONKEY)|keying` + // common words containing "key"
  49. `|(primary|foreign|natural|definition|hot)[_.-]?key` +
  50. `|key[_.-]?(alias|board|code|stone|storetype|word|up|down|left|right)` +
  51. `|rapid|capital` + // common words containing "api"
  52. `)`),
  53. },
  54. RegexTarget: "match",
  55. StopWords: DefaultStopWords,
  56. },
  57. },
  58. }
  59. // validate
  60. tps := []string{
  61. utils.GenerateSampleSecret("generic", "CLOJARS_34bf0e88955ff5a1c328d6a7491acc4f48e865a7b8dd4d70a70749037443"), //gitleaks:allow
  62. utils.GenerateSampleSecret("generic", "Zf3D0LXCM3EIMbgJpUNnkRtOfOueHznB"),
  63. `"credentials" : "0afae57f3ccfd9d7f5767067bc48b30f719e271ba470488056e37ab35d4b6506"`,
  64. `"client_secret" : "6da89121079f83b2eb6acccf8219ea982c3d79bccc3e9c6a85856480661f8fde",`,
  65. `passwd = ` + newPlausibleSecret(`[a-zA-Z0-9\-_.=]{30}`),
  66. `private-key: ` + newPlausibleSecret(`[a-zA-Z0-9\-_.=]{100}`),
  67. `mySecretString=` + newPlausibleSecret(`[a-zA-Z0-9]{30}`),
  68. `some_api_token_123 = "` + newPlausibleSecret(`[a-zA-Z0-9]{60}`) + `"`,
  69. `todo_secret_do_not_commit = ` + newPlausibleSecret(`[a-zA-Z0-9]{30}`),
  70. `creds = ` + newPlausibleSecret(`[a-zA-Z0-9]{30}`),
  71. }
  72. fps := []string{
  73. `client_vpn_endpoint_id = aws_ec2_client_vpn_endpoint.client-vpn-endpoint.id`,
  74. `password combination.
  75. R5: Regulatory--21`,
  76. `public_key = "9Cnzj4p4WGeKLs1Pt8QuKUpRKfFLfRYC9AIKjbJTWit"`,
  77. `publicToken = "9Cnzj4p4WGeKLs1Pt8QuKUpRKfFLfRYC9AIKjbJTWit"`,
  78. `clientId = "73082700-1f09-405b-80d0-3131bfd6272d"`,
  79. `MantleAPI_version=9a038989604e8da62ecddbe2094b16ce1b778be1`,
  80. `COMMUNICATION_API_VERSION=rc0.13.0_20230412_0712-SNAPSHOT`,
  81. `LLM_SECRET_NAME = "NEXUS-GPT4-API-KEY"`,
  82. `keyword: "Befaehigung_P2"`,
  83. `minisat-master-keying:x64-uwp=fail`,
  84. `monkeys-audio:mx64-uwp=fail`,
  85. `rapidstring:marm64-uwp=fail`,
  86. `<entry key="jetbrains.mps.v8_elimination" value="executed" />`,
  87. `event-bus-message-api:rc0.15.0_20231217_1420-SNAPSHOT'`,
  88. `primaryKey=` + newPlausibleSecret(`[a-zA-Z0-9\-_.=]{30}`),
  89. `foreignKey=` + newPlausibleSecret(`[a-zA-Z0-9\-_.=]{30}`),
  90. `key_down_event=` + newPlausibleSecret(`[a-zA-Z0-9\-_.=]{30}`),
  91. `issuerKeyHash=` + newPlausibleSecret(`[a-zA-Z0-9\-_.=]{30}`),
  92. }
  93. return utils.Validate(r, tps, fps)
  94. }
  95. func newPlausibleSecret(regex string) string {
  96. allowList := config.Allowlist{StopWords: DefaultStopWords}
  97. // attempt to generate a random secret,
  98. // retrying until it contains at least one digit and no stop words
  99. // TODO: currently the DefaultStopWords list contains many short words,
  100. // so there is a significant chance of generating a secret that contains a stop word
  101. for {
  102. secret := secrets.NewSecret(regex)
  103. if !regexp.MustCompile(`[1-9]`).MatchString(secret) {
  104. continue
  105. }
  106. if allowList.ContainsStopWord(secret) {
  107. continue
  108. }
  109. return secret
  110. }
  111. }