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.

p0f.lua 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. --[[
  2. Copyright (c) 2022, Vsevolod Stakhov <vsevolod@rspamd.com>
  3. Copyright (c) 2019, Denis Paavilainen <denpa@denpa.pro>
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. ]]--
  14. -- Detect remote OS via passive fingerprinting
  15. local lua_util = require "lua_util"
  16. local lua_redis = require "lua_redis"
  17. local rspamd_logger = require "rspamd_logger"
  18. local p0f = require("lua_scanners").filter('p0f').p0f
  19. local N = 'p0f'
  20. if confighelp then
  21. rspamd_config:add_example(nil, N,
  22. 'Detect remote OS via passive fingerprinting',
  23. [[
  24. p0f {
  25. # Enable module
  26. enabled = true
  27. # Path to the unix socket that p0f listens on
  28. socket = '/var/run/p0f.sock';
  29. # Connection timeout
  30. timeout = 5s;
  31. # If defined, insert symbol with lookup results
  32. symbol = 'P0F';
  33. # Patterns to match against results returned by p0f
  34. # Symbol will be yielded on OS string, link type or distance matches
  35. patterns = {
  36. WINDOWS = '^Windows.*';
  37. #DSL = '^DSL$';
  38. #DISTANCE10 = '^distance:10$';
  39. }
  40. # Cache lifetime in seconds (default - 2 hours)
  41. expire = 7200;
  42. # Cache key prefix
  43. prefix = 'p0f';
  44. }
  45. ]])
  46. return
  47. end
  48. local rule
  49. local function check_p0f(task)
  50. local ip = task:get_from_ip()
  51. if not (ip and ip:is_valid()) or ip:is_local() then
  52. return
  53. end
  54. p0f.check(task, ip, rule)
  55. end
  56. local opts = rspamd_config:get_all_opt(N)
  57. rule = p0f.configure(opts)
  58. if rule then
  59. rule.redis_params = lua_redis.parse_redis_server(N)
  60. lua_redis.register_prefix(rule.prefix .. '*', N,
  61. 'P0f check cache', {
  62. type = 'string',
  63. })
  64. local id = rspamd_config:register_symbol({
  65. name = 'P0F_CHECK',
  66. type = 'prefilter',
  67. callback = check_p0f,
  68. priority = lua_util.symbols_priorities.medium,
  69. flags = 'empty,nostat',
  70. group = N,
  71. augmentations = {string.format("timeout=%f", rule.timeout or 0.0)},
  72. })
  73. if rule.symbol then
  74. rspamd_config:register_symbol({
  75. name = rule.symbol,
  76. parent = id,
  77. type = 'virtual',
  78. flags = 'empty',
  79. group = N
  80. })
  81. end
  82. for sym in pairs(rule.patterns) do
  83. rspamd_logger.debugm(N, rspamd_config, 'registering: %1', {
  84. type = 'virtual',
  85. name = sym,
  86. parent = id,
  87. group = N
  88. })
  89. rspamd_config:register_symbol({
  90. type = 'virtual',
  91. name = sym,
  92. parent = id,
  93. group = N
  94. })
  95. end
  96. else
  97. lua_util.disable_module(N, 'config')
  98. rspamd_logger.infox('p0f module not configured');
  99. end