telegram.go 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. )
  7. func TelegramBotToken() *config.Rule {
  8. // define rule
  9. r := config.Rule{
  10. Description: "Detected a Telegram Bot API Token, risking unauthorized bot operations and message interception on Telegram.",
  11. RuleID: "telegram-bot-api-token",
  12. Regex: utils.GenerateSemiGenericRegex([]string{"telegr"}, "[0-9]{5,16}:(?-i:A)[a-z0-9_\\-]{34}", true),
  13. Keywords: []string{
  14. "telegr",
  15. },
  16. }
  17. // validate
  18. var (
  19. validToken = secrets.NewSecret(utils.Numeric("8") + ":A" + utils.AlphaNumericExtendedShort("34"))
  20. minToken = secrets.NewSecret(utils.Numeric("5") + ":A" + utils.AlphaNumericExtendedShort("34"))
  21. maxToken = secrets.NewSecret(utils.Numeric("16") + ":A" + utils.AlphaNumericExtendedShort("34"))
  22. // xsdWithToken = secrets.NewSecret(`<xsd:element name="AgencyIdentificationCode" type="` + Numeric("5") + `:A` + AlphaNumericExtendedShort("34") + `"/>`)
  23. )
  24. // variable assignment
  25. tps := utils.GenerateSampleSecrets("telegram", validToken)
  26. // Token with min bot_id
  27. tps = append(tps, utils.GenerateSampleSecrets("telegram", minToken)...)
  28. // Token with max bot_id
  29. tps = append(tps, utils.GenerateSampleSecrets("telegram", maxToken)...)
  30. tps = append(tps,
  31. // URL containing token TODO add another url based rule
  32. // GenerateSampleSecret("url", "https://api.telegram.org/bot"+validToken+"/sendMessage"),
  33. // object constructor
  34. //TODO: `const bot = new Telegraf("`+validToken+`")`,
  35. // .env
  36. `TELEGRAM_API_TOKEN = `+validToken,
  37. // YAML
  38. `telegram bot: `+validToken,
  39. // Valid token in XSD document TODO separate rule for this
  40. // generateSampleSecret("telegram", xsdWithToken),
  41. )
  42. var (
  43. tooSmallToken = secrets.NewSecret(utils.Numeric("4") + ":A" + utils.AlphaNumericExtendedShort("34"))
  44. tooBigToken = secrets.NewSecret(utils.Numeric("17") + ":A" + utils.AlphaNumericExtendedShort("34"))
  45. xsdAgencyIdentificationCode1 = secrets.NewSecret(`<xsd:element name="AgencyIdentificationCode" type="clm`+utils.Numeric("5")+":AgencyIdentificationCodeContentType") + `"/>`
  46. xsdAgencyIdentificationCode2 = secrets.NewSecret(`token:"clm` + utils.Numeric("5") + `:AgencyIdentificationCodeContentType"`)
  47. xsdAgencyIdentificationCode3 = secrets.NewSecret(`<xsd:element name="AgencyIdentificationCode" type="clm` + utils.Numeric("8") + `:AgencyIdentificationCodeContentType"/>`)
  48. prefixedToken1 = secrets.NewSecret(`telegram_api_token = \"` + utils.Numeric("8") + `:Ahello` + utils.AlphaNumericExtendedShort("34") + `\"`)
  49. prefixedToken2 = secrets.NewSecret(`telegram_api_token = \"` + utils.Numeric("8") + `:A-some-other-thing-` + utils.AlphaNumericExtendedShort("34") + `\"`)
  50. prefixedToken3 = secrets.NewSecret(`telegram_api_token = \"` + utils.Numeric("8") + `:A_` + utils.AlphaNumericExtendedShort("34") + `\"`)
  51. suffixedToken1 = secrets.NewSecret(`telegram_api_token = \"` + utils.Numeric("8") + `:A` + utils.AlphaNumericExtendedShort("34") + `hello\"`)
  52. suffixedToken2 = secrets.NewSecret(`telegram_api_token = \"` + utils.Numeric("8") + `:A` + utils.AlphaNumericExtendedShort("34") + `-some-other-thing\"`)
  53. suffixedToken3 = secrets.NewSecret(`telegram_api_token = \"` + utils.Numeric("8") + `:A_` + utils.AlphaNumericExtendedShort("34") + `_\"`)
  54. )
  55. fps := []string{
  56. // Token with too small bot_id
  57. utils.GenerateSampleSecret("telegram", tooSmallToken),
  58. // Token with too big bot_id
  59. utils.GenerateSampleSecret("telegram", tooBigToken),
  60. // XSD file containing the string AgencyIdentificationCodeContentType
  61. utils.GenerateSampleSecret("telegram", xsdAgencyIdentificationCode1),
  62. utils.GenerateSampleSecret("telegram", xsdAgencyIdentificationCode2),
  63. utils.GenerateSampleSecret("telegram", xsdAgencyIdentificationCode3),
  64. // Prefix and suffix variations that shouldn't match
  65. utils.GenerateSampleSecret("telegram", prefixedToken1),
  66. utils.GenerateSampleSecret("telegram", prefixedToken2),
  67. utils.GenerateSampleSecret("telegram", prefixedToken3),
  68. utils.GenerateSampleSecret("telegram", suffixedToken1),
  69. utils.GenerateSampleSecret("telegram", suffixedToken2),
  70. utils.GenerateSampleSecret("telegram", suffixedToken3),
  71. }
  72. return utils.Validate(r, tps, fps)
  73. }