generic.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package rules
  2. import (
  3. regexp "github.com/wasilibs/go-re2"
  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. )
  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. "access",
  15. "auth",
  16. `(?-i:[Aa]pi|API)`,
  17. "credential",
  18. "creds",
  19. "key",
  20. "passwd",
  21. "password",
  22. "secret",
  23. "token",
  24. }, `[\w.=-]{10,150}`, true),
  25. Keywords: []string{
  26. "access",
  27. "api",
  28. "auth",
  29. "key",
  30. "credential",
  31. "creds",
  32. "passwd",
  33. "password",
  34. "secret",
  35. "token",
  36. },
  37. Entropy: 3.5,
  38. Allowlists: []config.Allowlist{
  39. {
  40. // NOTE: this is a goofy hack to get around the fact there golang's regex engine does not support positive lookaheads.
  41. // Ideally we would want to ensure the secret contains both numbers and alphabetical characters, not just alphabetical characters.
  42. Regexes: []*regexp.Regexp{
  43. regexp.MustCompile(`^[a-zA-Z_.-]+$`),
  44. },
  45. },
  46. {
  47. Description: "Allowlist for Generic API Keys",
  48. MatchCondition: config.AllowlistMatchOr,
  49. RegexTarget: "match",
  50. Regexes: []*regexp.Regexp{
  51. regexp.MustCompile(`(?i)(` +
  52. // Access
  53. `accessor` +
  54. `|access[_.-]?id` +
  55. // API
  56. `|api[_.-]?(version|id)` + // version/id -> not a secret
  57. `|rapid|capital` + // common words containing "api"
  58. `|[a-z0-9-]*?api[a-z0-9-]*?:jar:` + // Maven META-INF dependencies that contain "api" in the name.
  59. // Auth
  60. `|author` +
  61. `|X-MS-Exchange-Organization-Auth` + // email header
  62. `|Authentication-Results` + // email header
  63. // Credentials
  64. `|(credentials?[_.-]?id|withCredentials)` + // Jenkins plugins
  65. // Key
  66. `|(bucket|foreign|hot|natural|primary|schema|sequence)[_.-]?key` +
  67. `|key[_.-]?(alias|board|code|ring|selector|size|stone|storetype|word|up|down|left|right)` +
  68. `|key(store|tab)[_.-]?(file|path)` +
  69. `|issuerkeyhash` + // part of ssl cert
  70. `|(?-i:[DdMm]onkey|[DM]ONKEY)|keying` + // common words containing "key"
  71. // Secret
  72. `|(secret)[_.-]?name` + // name of e.g. env variable
  73. `|UserSecretsId` + // https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-8.0&tabs=linux
  74. // Token
  75. // General
  76. `|(api|credentials|token)[_.-]?(endpoint|ur[il])` +
  77. `|public[_.-]?(key|token)` + // public key -> not a secret
  78. `|(key|token)[_.-]?file` +
  79. `)`),
  80. },
  81. StopWords: DefaultStopWords,
  82. },
  83. },
  84. }
  85. // validate
  86. tps := utils.GenerateSampleSecrets("generic", "CLOJARS_34bf0e88955ff5a1c328d6a7491acc4f48e865a7b8dd4d70a70749037443") //gitleaks:allow
  87. tps = append(tps, utils.GenerateSampleSecrets("generic", "Zf3D0LXCM3EIMbgJpUNnkRtOfOueHznB")...)
  88. tps = append(tps,
  89. // Access
  90. `'access_token': 'eyJ0eXAioiJKV1slS3oASx=='`,
  91. // API
  92. `some_api_token_123 = "`+newPlausibleSecret(`[a-zA-Z0-9]{60}`)+`"`,
  93. // Auth
  94. // Credentials
  95. `"credentials" : "0afae57f3ccfd9d7f5767067bc48b30f719e271ba470488056e37ab35d4b6506"`,
  96. `creds = `+newPlausibleSecret(`[a-zA-Z0-9]{30}`),
  97. // Key
  98. `private-key: `+newPlausibleSecret(`[a-zA-Z0-9\-_.=]{100}`),
  99. // Password
  100. `passwd = `+newPlausibleSecret(`[a-zA-Z0-9\-_.=]{30}`),
  101. // TODO: `ID=dbuser;password=` + newPlausibleSecret(`[a-zA-Z0-9+/]{30}={0,3}`) + `;"`,
  102. // Secret
  103. `"client_secret" : "6da89121079f83b2eb6acccf8219ea982c3d79bccc3e9c6a85856480661f8fde",`,
  104. `mySecretString=`+newPlausibleSecret(`[a-zA-Z0-9]{30}`),
  105. `todo_secret_do_not_commit = `+newPlausibleSecret(`[a-zA-Z0-9]{30}`),
  106. // Token
  107. utils.GenerateSampleSecret("generic", "CLOJARS_34bf0e88955ff5a1c328d6a7491acc4f48e865a7b8dd4d70a70749037443"), //gitleaks:allow
  108. utils.GenerateSampleSecret("generic", "Zf3D0LXCM3EIMbgJpUNnkRtOfOueHznB"),
  109. )
  110. fps := []string{
  111. // Access
  112. `"accessor":"rA1wk0Y45YCufyfq",`,
  113. `report_access_id: e8e4df51-2054-49b0-ab1c-516ac95c691d`,
  114. // API
  115. `this.ultraPictureBox1.Name = "ultraPictureBox1";`,
  116. `rapidstring:marm64-uwp=fail`,
  117. `event-bus-message-api:rc0.15.0_20231217_1420-SNAPSHOT'`,
  118. `COMMUNICATION_API_VERSION=rc0.13.0_20230412_0712-SNAPSHOT`,
  119. `MantleAPI_version=9a038989604e8da62ecddbe2094b16ce1b778be1`,
  120. `[DEBUG] org.slf4j.slf4j-api:jar:1.7.8.:compile (version managed from default)`,
  121. `[DEBUG] org.neo4j.neo4j-graphdb-api:jar:3.5.12:test`,
  122. `apiUrl=apigee.corpint.com`,
  123. // TODO: Jetbrains IML files (requires line-level allowlist).
  124. // `<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.directory.api:api-asn1-api:1.0.0-M20" level="projcet" />`
  125. // Auth
  126. `author = "james.fake@ymail.com",`,
  127. `X-MS-Exchange-Organization-AuthSource: sm02915.int.contoso.com`,
  128. `Authentication-Results: 5h.ca.iphmx.com`,
  129. // Credentials
  130. `withCredentials([usernamePassword(credentialsId: '29f63271-dc2f-4734-8221-5b31b5169bac', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {`,
  131. `credentialsId: 'ff083f76-7804-4ef1-80e4-fe975bb9141b'`,
  132. `jobCredentialsId: 'f4aeb6bc-2a25-458a-8111-9be9e502c0e7'`,
  133. ` "credentialId": "B9mTcFSck2LzJO2S3ols63",`,
  134. `environment {
  135. CREDENTIALS_ID = "K8S_CRED"
  136. }`,
  137. `dev.credentials.url=dev-lb1.api.f4ke.com:5215`,
  138. // Key
  139. `keyword: "Befaehigung_P2"`,
  140. `public_key = "9Cnzj4p4WGeKLs1Pt8QuKUpRKfFLfRYC9AIKjbJTWit"`,
  141. `monkeys-audio:mx64-uwp=fail`,
  142. `primaryKey=` + newPlausibleSecret(`[a-zA-Z0-9\-_.=]{30}`),
  143. `foreignKey=` + newPlausibleSecret(`[a-zA-Z0-9\-_.=]{30}`),
  144. `key_down_event=` + newPlausibleSecret(`[a-zA-Z0-9\-_.=]{30}`),
  145. `issuerKeyHash=` + newPlausibleSecret(`[a-zA-Z0-9\-_.=]{30}`),
  146. `<entry key="jetbrains.mps.v8_elimination" value="executed" />`,
  147. `minisat-master-keying:x64-uwp=fail`,
  148. `IceSSL.KeyFile=s_rsa1024_priv.pem`,
  149. `"bucket_key": "SalesResults-1.2"`,
  150. // `<TAR key="REF_ID_923.properties" value="/opts/config/alias/"/>`,
  151. `<key tag="SecurityIdentifier" name="SecurityIdentifier" type="STRING" />`,
  152. // `packageKey":` + newPlausibleSecret(`[a-zA-Z0-9\-_.=]{30}`),
  153. `schemaKey = 'DOC_Vector_5_32'`,
  154. `sequenceKey = "18"`,
  155. `app.keystore.file=env/cert.p12`,
  156. `-DKEYTAB_FILE=/tmp/app.keytab`,
  157. ` doc.Security.KeySize = PdfEncryptionKeySize.Key128Bit;`,
  158. `o.keySelector=n,o.haKey=!1,`,
  159. // TODO: Requires line-level allowlists.
  160. // `<add key="SchemaTable" value="G:\SchemaTable.xml" />`,
  161. // `secret:
  162. // secretName: app-decryption-secret
  163. // items:
  164. // - key: app-k8s.yml
  165. // path: app-k8s.yml`,
  166. // TODO: https://learn.microsoft.com/en-us/windows/apps/design/style/xaml-theme-resources
  167. // `<Color x:Key="NormalBrushGradient1">#FFBAE4FF</Color>`,
  168. // Password
  169. `password combination.
  170. R5: Regulatory--21`,
  171. `PuttyPassword=0`,
  172. // Secret
  173. `LLM_SECRET_NAME = "NEXUS-GPT4-API-KEY"`,
  174. ` <UserSecretsId>79a3edd0-2092-40a2-a04d-dcb46d5ca9ed</UserSecretsId>`,
  175. // Token
  176. ` access_token_url='https://github.com/login/oauth/access_token',`,
  177. `publicToken = "9Cnzj4p4WGeKLs1Pt8QuKUpRKfFLfRYC9AIKjbJTWit"`,
  178. `<SourceFile SourceLocation="F:\Extracts\" TokenFile="RTL_INST_CODE.cer">`,
  179. // TODO: `TOKEN_AUDIENCE = "25872395-ed3a-4703-b647-22ec53f3683c"`,
  180. // General
  181. `clientId = "73082700-1f09-405b-80d0-3131bfd6272d"`,
  182. }
  183. return utils.Validate(r, tps, fps)
  184. }
  185. func newPlausibleSecret(regex string) string {
  186. allowList := config.Allowlist{StopWords: DefaultStopWords}
  187. // attempt to generate a random secret,
  188. // retrying until it contains at least one digit and no stop words
  189. // TODO: currently the DefaultStopWords list contains many short words,
  190. // so there is a significant chance of generating a secret that contains a stop word
  191. for {
  192. secret := secrets.NewSecret(regex)
  193. if !regexp.MustCompile(`[1-9]`).MatchString(secret) {
  194. continue
  195. }
  196. if allowList.ContainsStopWord(secret) {
  197. continue
  198. }
  199. return secret
  200. }
  201. }