telegram.go 4.4 KB

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