| 1 |
--[[ |
| 2 |
Copyright (c) 2022, Vsevolod Stakhov <[email protected]> |
| 3 |
Copyright (c) 2019, Denis Paavilainen <[email protected]> |
| 4 |
|
| 5 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
you may not use this file except in compliance with the License. |
| 7 |
You may obtain a copy of the License at |
| 8 |
|
| 9 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
|
| 11 |
Unless required by applicable law or agreed to in writing, software |
| 12 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
See the License for the specific language governing permissions and |
| 15 |
limitations under the License. |
| 16 |
]]-- |
| 17 |
|
| 18 |
--[[[ |
| 19 |
-- @module p0f |
| 20 |
-- This module contains p0f access functions |
| 21 |
--]] |
| 22 |
|
| 23 |
local tcp = require "rspamd_tcp" |
| 24 |
local rspamd_util = require "rspamd_util" |
| 25 |
local rspamd_logger = require "rspamd_logger" |
| 26 |
local lua_redis = require "lua_redis" |
| 27 |
local lua_util = require "lua_util" |
| 28 |
local common = require "lua_scanners/common" |
| 29 |
|
| 30 |
-- SEE: https://github.com/p0f/p0f/blob/v3.06b/docs/README#L317 |
| 31 |
local S = { |
| 32 |
BAD_QUERY = 0x0, |
| 33 |
OK = 0x10, |
| 34 |
NO_MATCH = 0x20 |
| 35 |
} |
| 36 |
|
| 37 |
local N = 'p0f' |
| 38 |
|
| 39 |
local function p0f_check(task, ip, rule) |
| 40 |
|
| 41 |
local function ip2bin(addr) |
| 42 |
addr = addr:to_table() |
| 43 |
|
| 44 |
for k, v in ipairs(addr) do |
| 45 |
addr[k] = rspamd_util.pack('B', v) |
| 46 |
end |
| 47 |
|
| 48 |
return table.concat(addr) |
| 49 |
end |
| 50 |
|
| 51 |
local function trim(...) |
| 52 |
local vars = { ... } |
| 53 |
|
| 54 |
for k, v in ipairs(vars) do |
| 55 |
-- skip numbers, trim only strings |
| 56 |
if tonumber(vars[k]) == nil then |
| 57 |
vars[k] = string.gsub(v, '[^%w-_\\.\\(\\) ]', '') |
| 58 |
end |
| 59 |
end |
| 60 |
|
| 61 |
return lua_util.unpack(vars) |
| 62 |
end |
| 63 |
|
| 64 |
local function parse_p0f_response(data) |
| 65 |
--[[ |
| 66 |
p0f_api_response[232]: magic, status, first_seen, last_seen, total_conn, |
| 67 |
uptime_min, up_mod_days, last_nat, last_chg, distance, bad_sw, os_match_q, |
| 68 |
os_name, os_flavor, http_name, http_flavor, link_type, language |
| 69 |
]]-- |
| 70 |
|
| 71 |
data = tostring(data) |
| 72 |
|
| 73 |
-- API response must be 232 bytes long |
| 74 |
if #data ~= 232 then |
| 75 |
rspamd_logger.errx(task, 'malformed response from p0f on %s, %s bytes', |
| 76 |
rule.socket, #data) |
| 77 |
|
| 78 |
common.yield_result(task, rule, 'Malformed Response: ' .. rule.socket, |
| 79 |
0.0, 'fail') |
| 80 |
return |
| 81 |
end |
| 82 |
|
| 83 |
local _, status, _, _, _, uptime_min, _, _, _, distance, _, _, os_name, |
| 84 |
os_flavor, _, _, link_type, _ = trim(rspamd_util.unpack( |
| 85 |
'I4I4I4I4I4I4I4I4I4hbbc32c32c32c32c32c32', data)) |
| 86 |
|
| 87 |
if status ~= S.OK then |
| 88 |
if status == S.BAD_QUERY then |
| 89 |
rspamd_logger.errx(task, 'malformed p0f query on %s', rule.socket) |
| 90 |
common.yield_result(task, rule, 'Malformed Query: ' .. rule.socket, |
| 91 |
0.0, 'fail') |
| 92 |
end |
| 93 |
|
| 94 |
return |
| 95 |
end |
| 96 |
|
| 97 |
local os_string = #os_name == 0 and 'unknown' or os_name .. ' ' .. os_flavor |
| 98 |
|
| 99 |
task:get_mempool():set_variable('os_fingerprint', os_string, link_type, |
| 100 |
uptime_min, distance) |
| 101 |
|
| 102 |
if link_type and #link_type > 0 then |
| 103 |
common.yield_result(task, rule, { |
| 104 |
os_string, |
| 105 |
'link=' .. link_type, |
| 106 |
'distance=' .. distance }, |
| 107 |
0.0) |
| 108 |
else |
| 109 |
common.yield_result(task, rule, { |
| 110 |
os_string, |
| 111 |
'link=unknown', |
| 112 |
'distance=' .. distance }, |
| 113 |
0.0) |
| 114 |
end |
| 115 |
|
| 116 |
return data |
| 117 |
end |
| 118 |
|
| 119 |
local function make_p0f_request() |
| 120 |
|
| 121 |
local function check_p0f_cb(err, data) |
| 122 |
|
| 123 |
local function redis_set_cb(redis_set_err) |
| 124 |
if redis_set_err then |
| 125 |
rspamd_logger.errx(task, 'redis received an error: %s', redis_set_err) |
| 126 |
end |
| 127 |
end |
| 128 |
|
| 129 |
if err then |
| 130 |
rspamd_logger.errx(task, 'p0f received an error: %s', err) |
| 131 |
common.yield_result(task, rule, 'Error getting result: ' .. err, |
| 132 |
0.0, 'fail') |
| 133 |
return |
| 134 |
end |
| 135 |
|
| 136 |
data = parse_p0f_response(data) |
| 137 |
|
| 138 |
if rule.redis_params and data then |
| 139 |
local key = rule.prefix .. ip:to_string() |
| 140 |
local ret = lua_redis.redis_make_request(task, |
| 141 |
rule.redis_params, |
| 142 |
key, |
| 143 |
true, |
| 144 |
redis_set_cb, |
| 145 |
'SETEX', |
| 146 |
{ key, tostring(rule.expire), data } |
| 147 |
) |
| 148 |
|
| 149 |
if not ret then |
| 150 |
rspamd_logger.warnx(task, 'error connecting to redis') |
| 151 |
end |
| 152 |
end |
| 153 |
end |
| 154 |
|
| 155 |
local query = rspamd_util.pack('I4 I1 c16', 0x50304601, |
| 156 |
ip:get_version(), ip2bin(ip)) |
| 157 |
|
| 158 |
tcp.request({ |
| 159 |
host = rule.socket, |
| 160 |
callback = check_p0f_cb, |
| 161 |
data = { query }, |
| 162 |
task = task, |
| 163 |
timeout = rule.timeout |
| 164 |
}) |
| 165 |
end |
| 166 |
|
| 167 |
local function redis_get_cb(err, data) |
| 168 |
if err or type(data) ~= 'string' then |
| 169 |
make_p0f_request() |
| 170 |
else |
| 171 |
parse_p0f_response(data) |
| 172 |
end |
| 173 |
end |
| 174 |
|
| 175 |
local ret = nil |
| 176 |
if rule.redis_params then |
| 177 |
local key = rule.prefix .. ip:to_string() |
| 178 |
ret = lua_redis.redis_make_request(task, |
| 179 |
rule.redis_params, |
| 180 |
key, |
| 181 |
false, |
| 182 |
redis_get_cb, |
| 183 |
'GET', |
| 184 |
{ key } |
| 185 |
) |
| 186 |
end |
| 187 |
|
| 188 |
if not ret then |
| 189 |
make_p0f_request() -- fallback to directly querying p0f |
| 190 |
end |
| 191 |
end |
| 192 |
|
| 193 |
local function p0f_config(opts) |
| 194 |
local p0f_conf = { |
| 195 |
name = N, |
| 196 |
timeout = 5, |
| 197 |
symbol = 'P0F', |
| 198 |
symbol_fail = 'P0F_FAIL', |
| 199 |
patterns = {}, |
| 200 |
expire = 7200, |
| 201 |
prefix = 'p0f', |
| 202 |
detection_category = 'fingerprint', |
| 203 |
message = '${SCANNER}: fingerprint matched: "${VIRUS}"' |
| 204 |
} |
| 205 |
|
| 206 |
p0f_conf = lua_util.override_defaults(p0f_conf, opts) |
| 207 |
p0f_conf.patterns = common.create_regex_table(p0f_conf.patterns) |
| 208 |
|
| 209 |
if not p0f_conf.log_prefix then |
| 210 |
p0f_conf.log_prefix = p0f_conf.name |
| 211 |
end |
| 212 |
|
| 213 |
if not p0f_conf.socket then |
| 214 |
rspamd_logger.errx(rspamd_config, 'no servers defined') |
| 215 |
return nil |
| 216 |
end |
| 217 |
|
| 218 |
return p0f_conf |
| 219 |
end |
| 220 |
|
| 221 |
return { |
| 222 |
type = { N, 'fingerprint', 'scanner' }, |
| 223 |
description = 'passive OS fingerprinter', |
| 224 |
configure = p0f_config, |
| 225 |
check = p0f_check, |
| 226 |
name = N |
| 227 |
} |