You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bitcoin.lua 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. --[[
  2. Copyright (c) 2019, Vsevolod Stakhov <vsevolod@highsecure.ru>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ]]--
  13. -- Bitcoin filter rules
  14. local fun = require "fun"
  15. local off = 0
  16. local base58_dec = fun.tomap(fun.map(
  17. function(c)
  18. off = off + 1
  19. return c,(off - 1)
  20. end,
  21. "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"))
  22. rspamd_config:register_symbol{
  23. name = 'BITCOIN_ADDR',
  24. description = 'Message has a valid bitcoin wallet address',
  25. callback = function(task)
  26. local rspamd_re = require "rspamd_regexp"
  27. local hash = require "rspamd_cryptobox_hash"
  28. local wallet_re = rspamd_re.create_cached('^[13][1-9A-Za-z]{25,34}$')
  29. local words_matched = {}
  30. local valid_wallets = {}
  31. for _,part in ipairs(task:get_text_parts() or {}) do
  32. local pw = part:filter_words(wallet_re, 'raw', 3)
  33. if pw and #pw > 0 then
  34. for _,w in ipairs(pw) do
  35. words_matched[#words_matched + 1] = w
  36. end
  37. end
  38. end
  39. for _,word in ipairs(words_matched) do
  40. local bytes = {}
  41. for i=1,25 do bytes[i] = 0 end
  42. -- Base58 decode loop
  43. fun.each(function(ch)
  44. local acc = base58_dec[ch] or 0
  45. for i=25,1,-1 do
  46. acc = acc + (58 * bytes[i]);
  47. bytes[i] = acc % 256
  48. acc = math.floor(acc / 256);
  49. end
  50. end, word)
  51. -- Now create a validation tag
  52. local sha256 = hash.create_specific('sha256')
  53. for i=1,21 do
  54. sha256:update(string.char(bytes[i]))
  55. end
  56. sha256 = hash.create_specific('sha256', sha256:bin()):bin()
  57. -- Compare tags
  58. local valid = true
  59. for i=1,4 do
  60. if string.sub(sha256, i, i) ~= string.char(bytes[21 + i]) then
  61. valid = false
  62. end
  63. end
  64. if valid then
  65. valid_wallets[#valid_wallets + 1] = word
  66. end
  67. end
  68. if #valid_wallets > 0 then
  69. return true,1.0,valid_wallets
  70. end
  71. end,
  72. score = 0.0,
  73. group = 'scams'
  74. }