curl.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package rules
  2. import (
  3. "fmt"
  4. "github.com/zricethezav/gitleaks/v8/cmd/generate/config/utils"
  5. "github.com/zricethezav/gitleaks/v8/config"
  6. "github.com/zricethezav/gitleaks/v8/regexp"
  7. )
  8. // https://curl.se/docs/manpage.html#-u
  9. func CurlBasicAuth() *config.Rule {
  10. r := config.Rule{
  11. RuleID: "curl-auth-user",
  12. Description: "Discovered a potential basic authorization token provided in a curl command, which could compromise the curl accessed resource.",
  13. Regex: regexp.MustCompile(`\bcurl\b(?:.*|.*(?:[\r\n]{1,2}.*){1,5})[ \t\n\r](?:-u|--user)(?:=|[ \t]{0,5})(?:"([^:"]{3,}:[^"]{3,})"|'([^:']{3,}:[^']{3,})'|((?:"[^"]{3,}"|'[^']{3,}'|[\w$@.-]+):(?:"[^"]{3,}"|'[^']{3,}'|[\w${}@.-]+)))(?:\s|\z)`),
  14. Keywords: []string{"curl"},
  15. Entropy: 2,
  16. Allowlists: []config.Allowlist{
  17. {
  18. Regexes: []*regexp.Regexp{
  19. regexp.MustCompile(`[^:]+:(change(it|me)|pass(word)?|pwd|test|token|\*+|x+)`), // common placeholder passwords
  20. regexp.MustCompile(`['"]?<[^>]+>['"]?:['"]?<[^>]+>|<[^:]+:[^>]+>['"]?`), // <placeholder>
  21. regexp.MustCompile(`[^:]+:\[[^]]+]`), // [placeholder]
  22. regexp.MustCompile(`['"]?[^:]+['"]?:['"]?\$(\d|\w+|\{(\d|\w+)})['"]?`), // $1 or $VARIABLE
  23. regexp.MustCompile(`\$\([^)]+\):\$\([^)]+\)`), // $(cat login.txt)
  24. regexp.MustCompile(`['"]?\$?{{[^}]+}}['"]?:['"]?\$?{{[^}]+}}['"]?`), // ${{ secrets.FOO }} or {{ .Values.foo }}
  25. },
  26. },
  27. },
  28. }
  29. // validate
  30. tps := []string{
  31. // short
  32. `curl --cacert ca.crt -u elastic:P@ssw0rd$1 https://localhost:9200`, // same lines, no quotes
  33. `sh-5.0$ curl -k -X POST https://infinispan:11222/rest/v2/caches/default/hello \
  34. -H 'Content-type: text/plain' \
  35. -d 'world' \
  36. -u developer:yqDVtkqPECriaLRi`, // different line
  37. // long
  38. `curl -sw '%{http_code}' -X POST --user 'johns:h0pk1ns~21s' $GItHUB_API_URL/$GIT_COMMIT --data`,
  39. `curl --user roger23@gmail.com:pQ9wTxu4Fg https://www.dropbox.com/cli_link?host_id=abcdefg -v`, // same line, no quotes
  40. `curl -s --user 'api:d2LkV78zLx!t' \
  41. https://api.mailgun.net/v2/sandbox91d3515882ecfaa1c65be642.mailgun.org/messages`, // same line, single quotes
  42. `curl -s -v --user "j.smith:dB2yF6@qL9vZm1P#4J" "https://api.contoso.org/user/me"`, // same line, double quotes
  43. `curl -X POST --user "{acd3c08b-74e8-4f44-a2d0-80694le24f46}":"{ZqL5kVrX1n8tA2}" --header "Accept: application/json" --data "{\"text\":\"Hello, world\",\"source\":\"en\",\"target\":\"es\"}" https://gateway.watsonplatform.net/language-translator/api`,
  44. `curl --user kevin:'pRf7vG2h1L8nQkW9' -iX PATCH -H "Content-Type: application/json" -d`, // same line, mixed quoting
  45. `$ curl https://api.dropbox.com/oauth2/token \
  46. --user c28wlsosanujy2z:qgsnai0xokrw4j1 --data grant_type=authorization_code`, // different line
  47. // TODO
  48. //` curl -s --insecure --url "imaps://whatever.imap.server" --user\
  49. //"myuserid:mypassword" --request "STATUS INBOX (UNSEEN)"`,
  50. }
  51. fps := []string{
  52. // short
  53. `curl -i -u 'test:test'`,
  54. ` curl -sL --user "$1:$2" "$3" > "$4"`, // environment variable
  55. `curl -u <user:password> https://test.com/endpoint`, // placeholder
  56. `curl --user neo4j:[PASSWORD] http://[IP]:7474/db/data/`, // placeholder
  57. `curl -u "myusername" http://localhost:15130/api/check_user/`, // no password
  58. `curl -u username:token`,
  59. `curl -u "${_username}:${_password}"`,
  60. `curl -u "${username}":"${password}"`,
  61. `curl -k -X POST -I -u "SRVC_JENKINS:${APPID}"`,
  62. // long
  63. `curl -sw '%{http_code}' -X POST --user '$USERNAME:$PASSWORD' $GItHUB_API_URL/$GIT_COMMIT --data`,
  64. `curl --user "xxx:yyy"`,
  65. ` curl -sL --user "$GITHUB_USERNAME:$GITHUB_PASSWORD" "$GITHUB_URL" > "$TESTS_PATH"`, // environment variable
  66. // variable interpolation
  67. `curl --silent --fail {{- if and $.Values.username $.Values.password }} --user "{{ $.Values.username }}:{{ $.Values.password }}"`,
  68. `curl -XGET -i -u "${{ env.ELK_ID }}:${{ build.env.ELK_PASS }}"`,
  69. `curl -XGET -i -u "${{needs.vault.outputs.account_id}}:${{needs.vault.outputs.account_password}}"`,
  70. `curl -XGET -i -u "${{ steps.vault.outputs.account_id }}:${{ steps.vault.outputs.account_password }}"`,
  71. `curl -X POST --user "$(cat ./login.txt):$(cat ./password.txt)"`, // command
  72. `curl http://127.0.0.1:5000/file --user user:pass --digest # digest auth`, // placeholder
  73. ` curl -X GET --insecure --user "username:password" \`, // placeholder
  74. `curl --silent --insecure --user ${f5user}:${f5pass} \`, // placeholder
  75. `curl --insecure --ssl-reqd "smtps://smtp.gmail.com" --mail-from "src@gmail.com" --mail-rcpt "dst@gmail.com" --user "src@gmail.com" --upload-file out.txt`, // no password
  76. // different command
  77. `#HTTP command line test
  78. curl -X POST -H "Content-Type: application/json" -d '{"id":12345,"geo":{"latitude":28.50,"longitude":-81.14}}' http://<ip>:8080/serve
  79. #UDP command line test
  80. echo -n '{"type":"serve","channel":"/","data":{"site_id":8,"post_id":12345,"geo":{"lat":28.50,"long":-81.14}}}' >/dev/udp/127.0.0.1/41234
  81. #UDP Listener (for confirmation)
  82. nc -u -l 41234`,
  83. }
  84. return utils.Validate(r, tps, fps)
  85. }
  86. // https://curl.se/docs/manpage.html#-H
  87. func CurlHeaderAuth() *config.Rule {
  88. // language=regexp
  89. authPat := `(?i)(?:Authorization:[ \t]{0,5}(?:Basic[ \t]([a-z0-9+/]{8,}={0,3})|(?:Bearer|(?:Api-)?Token)[ \t]([\w=~@.+/-]{8,})|([\w=~@.+/-]{8,}))|(?:(?:X-(?:[a-z]+-)?)?(?:Api-?)?(?:Key|Token)):[ \t]{0,5}([\w=~@.+/-]{8,}))`
  90. r := config.Rule{
  91. RuleID: "curl-auth-header",
  92. Description: "Discovered a potential authorization token provided in a curl command header, which could compromise the curl accessed resource.",
  93. Regex: regexp.MustCompile(
  94. // language=regexp
  95. fmt.Sprintf(`\bcurl\b(?:.*?|.*?(?:[\r\n]{1,2}.*?){1,5})[ \t\n\r](?:-H|--header)(?:=|[ \t]{0,5})(?:"%s"|'%s')(?:\B|\s|\z)`, authPat, authPat)),
  96. Entropy: 2.75,
  97. Keywords: []string{"curl"},
  98. //Allowlists: []config.Allowlist{
  99. // {
  100. // Regexes: []*regexp.Regexp{},
  101. // },
  102. //},
  103. }
  104. tps := []string{
  105. `curl --header 'Authorization: 5eb4223e-5008-46e5-be67-c7b8f2732305'`,
  106. // Short flag.
  107. `curl -H 'Authorization: Basic YnJvd3Nlcjo=' \`, // same line, single quotes
  108. // TODO: Handle short flags combined.
  109. //`TOKEN=$(curl -sH "Authorization: Basic $BASIC_TOKEN" "https://$REGISTRY/oauth2/token?service=$REGISTRY&scope=repository:$REPO:pull" | jq -r .access_token)`,
  110. // Long flag.
  111. `curl -k -X POST --header "Authorization: Basic djJlNEpYa0NJUHZ5a2FWT0VRXzRqZmZUdDkwYTp2emNBZGFzZWpmlWZiUDc2VUJjNDNNVDExclVh" "https://api-qa.example.com:8243/token" -d "grant_type=client_credentials"`, // same line, double quotes
  112. // Basic auth.
  113. `curl -X POST -H "Content-Type: application/json" \
  114. -H "Authorization: Basic MzUzYjMwMmM0NDU3NGY1NjUwNDU2ODdlNTM0ZTdkNmE6Mjg2OTI0Njk3ZTYxNWE2NzJhNjQ2YTQ5MzU0NTY0NmM=" \
  115. -d '{"user":{"emailAddress":"test@example.com"}, "password":"password"}' \
  116. 'http://localhost:8080/oauth2-provider/v1.0/users'`, // different line, double quotes
  117. `#curl -X POST \
  118. # https://api.mailgun.net/v3/sandbox7dbcabccd4314c123e8b23599d35f5b6.mailgun.org/messages \
  119. # -H 'Authorization: Basic YXBpOmtleS1hN2MzNDJ3MzNhNWQxLTU2M2U3MjlwLTZhYjI3YzYzNzM0Ng==' \
  120. # -F from='Excited User <mailgun@sandbox7dbc123bccd4314c0aae8b23599d35f5b6.mailgun.org>' \
  121. # -F to='joe@example.com' \
  122. # -F subject='Hello' \
  123. # -F text='Testing some Mailgun awesomness!'`, // different line, single quotes
  124. // Bearer auth
  125. `# curl -X GET "http://localhost:3000/api/cron/status" -H "Authorization: Bearer cfcabd11c7ed9a41b1a3e063c32d5114"`, // same line, double quotes
  126. `curl -X PUT -H 'Authorization: Bearer jC+6TUUjCNHcVtAXpcqBCgxnA8r+qD6MatnYaf/+289y7HWpK0BWPyLHv/K4DMN32fufwmeVVjlo8zjgBh8kx3GfS6IqO70w1DVMSCTwX7fhEpiXaxzv0mhSMHDX9Kw63Q6DkavUWUV+MDNhCF5wGQrcdQNncVRF3YkuDHDT/xw2YWyZ/DX8k+gAYiC8gcD8Ueg0ljBVS1IDwPjuGoFPESJVxYr0MDPF2D8Pn2S5rq692U4D9ZLuluS46VA4DK6ig5P7QM5XVXi4V7vXM8qpN/zqneyz+w4PUh6NIX7QG6JczMhYd9maWRWVat5jDdyII63P6sNAy9QZjw+ClW211Q==' -d 'user={"account":"user@domain.com", "roles":["user"]}' http://127.0.0.1:8443/desks/1/occupy`, // same line, single quotes
  127. `curl https://api.openai.com/v1/chat/completions \
  128. -H "Content-Type: application/json" \
  129. -H "Authorization: Bearer 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" \`, // different line, double quotes
  130. `curl -X GET "https://api.cloudflare.com/client/v4/user/tokens/verify" \
  131. -H "Authorization: Bearer _FXNljbSRYMWx3TWrd7lgKhLtVZX6iskC8Wcbb4b" \
  132. -H "Content-Type:application/json"`,
  133. `curl -H "Authorization: Bearer sha256~bRLFnzd59Z3XpZH5_seJPHALOuvbWiKwbFKSsoALkgp"`,
  134. // Token auth
  135. `curl -H "Authorization: Api-Token 22cb987851bc5659l29114c62e60c79abd0d2c08" --request PUT https://appsecclass.report/api/use/635`, // token
  136. `curl -H "Authorization: Token 22cb987851bc5659229114c62e60c79abd0d2c08" --request PUT https://appsecclass.report/api/use/635`, // token
  137. // Nothing
  138. `curl -L -H "Authorization:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjb25maWRlbmNlIjowLjh9.kvRPfjAhLhtRTczoRgctVGp7KY1QVH3UBZM-gM0x8ec" service1.local -> correct jwt `, // no prefix
  139. `curl -L -H "Authorization: sha256~bRLFnzd5@=-.a+/hgdS"`, // no prefix
  140. // Non-authorization headers.
  141. `curl -X GET \
  142. -H "apikey: c4ed6c21-9dd5-4a05-8e3f-c56d1151cce8" \
  143. -H "Accept: application/json" \`, // apikey
  144. `curl -X POST --header "Api-Token: Sk94HG7f6KB"`, // api-token
  145. `curl -XPOST http://localhost:8080/api/tasks -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" -H "Token: 3fea6af1349166ea" -d "content=hello-curl"`, // token
  146. `curl -X GET https://octopus.corp.net/
  147. -H "X-Octopus-ApiKey: 3a16750d-d363-41a4-8ebd-035408f7730f" \`, // X-$thing-ApiKey
  148. }
  149. fps := []string{
  150. // Placeholders
  151. `curl https://example.com/micropub -d h=entry -d "content=Hello World" -H "Authorization: Bearer XXXXXXXXXXXX"`,
  152. `curl -X POST https://accounts.spotify.com/api/token -d grant_type=client_credentials --header "Authorization: Basic ..."`,
  153. `curl \
  154. -H "Authorization: Bearer <Openverse API token>" \
  155. "https://api.openverse.org/v1/audio/?q=test"`,
  156. `curl -v -v -v -X POST https://domain/api/v1/authentication/sso/login-url/ \
  157. -H 'Content-Type: application/json' \
  158. -H "Authorization: Token **********" \
  159. -d '{"username": "test", "next": "/luna/"}'`,
  160. // Variables
  161. `curl -XPOST http://localhost:8080/api/token -H "Authorization: basic {base64(email:password[\n])}" => token`, // same line, invalid base64
  162. `curl -X GET \
  163. -H "apikey: $API_KEY" \
  164. -H "Accept: $FORMAT" \
  165. "$API_URL/rest/v1/stats_derniere_labellisation"`, // API Key placeholder
  166. `$ curl -X POST "http://localhost:8000/v1/chat/completions" \
  167. -H "Content-Type: application/json" \
  168. -H "Authorization: Bearer $API_KEY" \
  169. -d '{
  170. "model": "chatglm3-6b-32k",
  171. "messages": [{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Hello!"}]
  172. }'`, // different line, placeholder
  173. `curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $(gcloud auth print-access-token)" https://workflowexecutions.googleapis.com/v1/projects/244283331594/locations/us-central1/workflows/sample-workflow/executions/43c925aa-514a-44c1-a0a4-a9f8f26fd2cb/callbacks/1705791f-d446-4e92-a6d0-a13622422e80_31864a51-8c13-4b03-ad4d-945cdc8d0631`, // script
  174. // Not valid BASIC
  175. `curl -X POST -H "Content-Type: application/json" -H "Authorization: Basic eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjb25maWRlbmNlIjowLjh9.kvRPfjAhLhtRTczoRgctVGp7KY1QVH3UBZM-gM0x8ec" \`,
  176. }
  177. return utils.Validate(r, tps, fps)
  178. }