aboutsummaryrefslogtreecommitdiff
path: root/rules/content.lua
blob: 8270aaebbbaed111e3041f93bb3a779c940097ce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
--[[
Copyright (c) 2022, Vsevolod Stakhov <vsevolod@rspamd.com>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
]]--

local lua_content = require "lua_content"

local function process_pdf_specific(task, part, specific)
  local suspicious_factor = 0
  if specific.encrypted then
    task:insert_result('PDF_ENCRYPTED', 1.0, part:get_filename() or 'unknown')
    suspicious_factor = suspicious_factor + 0.1
    if specific.openaction then
      suspicious_factor = suspicious_factor + 0.5
    end
  end

  if specific.scripts then
    task:insert_result('PDF_JAVASCRIPT', 1.0, part:get_filename() or 'unknown')
    suspicious_factor = suspicious_factor + 0.1
  end

  if specific.suspicious then
    suspicious_factor = suspicious_factor + specific.suspicious
  end

  if suspicious_factor > 0.5 then
    if suspicious_factor > 1.0 then
      suspicious_factor = 1.0
    end
    task:insert_result('PDF_SUSPICIOUS', suspicious_factor, part:get_filename() or 'unknown')
  end

  if specific.long_trailer then
    task:insert_result('PDF_LONG_TRAILER', 1.0, string.format('%s:%d',
        part:get_filename() or 'unknown', specific.long_trailer))
  end
  if specific.many_objects then
    task:insert_result('PDF_MANY_OBJECTS', 1.0, string.format('%s:%d',
        part:get_filename() or 'unknown', specific.many_objects))
  end
  if specific.timeout_processing then
    task:insert_result('PDF_TIMEOUT', 1.0, string.format('%s:%.3f',
        part:get_filename() or 'unknown', specific.timeout_processing))
  end

  -- What the parser could not or did not look at: none of that is a proof of
  -- anything on its own, but a file that is not fully seen is not a clean one
  local flags = specific.flags
  if type(flags) == 'table' then
    local fname = part:get_filename() or 'unknown'

    if flags.no_objects then
      task:insert_result('PDF_NO_OBJECTS', 1.0, fname)
    end
    if flags.decode_limit then
      task:insert_result('PDF_DECODE_LIMIT', 1.0, fname)
    end
    if flags.undecoded_streams then
      task:insert_result('PDF_UNDECODED_STREAMS', 1.0, string.format('%s:%d',
          fname, flags.undecoded_streams))
    end
    if flags.unparsed_objects or flags.object_errors then
      task:insert_result('PDF_PARSE_ERRORS', 1.0, string.format('%s:%d',
          fname, (flags.unparsed_objects or 0) + (flags.object_errors or 0)))
    end
    if flags.text_truncated then
      task:insert_result('PDF_TEXT_TRUNCATED', 1.0, fname)
    end
  end
end

local ooxml_prefixes = {
  docx = 'DOCX',
  xlsx = 'XLSX',
  pptx = 'PPTX',
}
local max_external_data_options = 8
local max_target_option_length = 128

local function trim_target(target)
  if #target > max_target_option_length then
    return target:sub(1, max_target_option_length) .. '...'
  end
  return target
end

local function process_ooxml_relationships(task, filename, specific)
  local relationships = specific.relationships
  if not relationships then return end

  -- One option per finding, prefixed by the file name, so that options
  -- never need to be split on commas
  local macros = {}
  for _, kind in ipairs(relationships.macros or {}) do
    macros[#macros + 1] = string.format('%s:%s', filename, kind)
  end
  for _, name in ipairs(specific.auto_exec_names or {}) do
    macros[#macros + 1] = string.format('%s:%s', filename, name)
  end
  if #macros > 0 then
    task:insert_result('OOXML_MACROS', 1.0, macros)
  end

  if (relationships.ole_objects or 0) > 0 then
    task:insert_result('OOXML_OLE_OBJECT', 1.0,
        string.format('%s:%d', filename, relationships.ole_objects))
  end

  if relationships.remote_template then
    task:insert_result('OOXML_REMOTE_TEMPLATE', 1.0,
        string.format('%s:%s', filename, trim_target(relationships.remote_template)))
  end

  local external_targets = relationships.external_targets or {}
  for i, target in ipairs(external_targets) do
    if i > max_external_data_options then break end
    task:insert_result('OOXML_EXTERNAL_DATA', 1.0,
        string.format('%s:%s=%s', filename, target.type, trim_target(target.target)))
  end
end

local function process_ooxml_specific(task, part, specific)
  local filename = part:get_filename() or 'unknown'
  local prefix = ooxml_prefixes[specific.tag]

  process_ooxml_relationships(task, filename, specific)

  if specific.suspicious then
    task:insert_result(prefix .. '_SUSPICIOUS', 1.0,
        string.format('%s:%s', filename, specific.reason or 'unknown'))
    return
  end

  task:insert_result(prefix .. '_CONTENT', 1.0, filename)
  if specific.truncated or specific.failed_stories then
    -- Not all of the document was read: more parts than are processed, or
    -- parts that could not be parsed
    local options = {}
    if specific.truncated then
      options[#options + 1] = string.format('%s:truncated', filename)
    end
    if specific.failed_stories then
      options[#options + 1] = string.format('%s:failed=%d', filename, specific.failed_stories)
    end
    task:insert_result('OOXML_INCOMPLETE', 1.0, options)
  end
  if specific.urls and #specific.urls > 0 then
    task:insert_result(prefix .. '_EXTERNAL_LINKS', 1.0,
        string.format('%s:%s', filename, #specific.urls))
  end
end

local max_svg_resource_options = 8

local function process_svg_specific(task, part, specific)
  local filename = part:get_filename() or 'unknown'

  if specific.suspicious then
    task:insert_result('SVG_SUSPICIOUS', 1.0,
        string.format('%s:%s', filename, specific.reason or 'unknown'))
    return
  end

  task:insert_result('SVG_CONTENT', 1.0, filename)

  -- Parts of the image that were not read: text, links or embedded documents
  -- over the limits, or an embedded document that could not be read
  local incomplete = {}
  if specific.text_truncated then
    incomplete[#incomplete + 1] = string.format('%s:text', filename)
  end
  if specific.urls_truncated then
    incomplete[#incomplete + 1] = string.format('%s:urls', filename)
  end
  if specific.payloads_truncated then
    incomplete[#incomplete + 1] = string.format('%s:payloads', filename)
  end
  if specific.nested_suspicious then
    incomplete[#incomplete + 1] = string.format('%s:nested=%s', filename, specific.nested_suspicious)
  end
  if #incomplete > 0 then
    task:insert_result('SVG_INCOMPLETE', 1.0, incomplete)
  end

  local scripts = specific.scripts or 0
  local handlers = specific.event_handlers or 0
  local javascript_urls = specific.javascript_urls or 0
  local external_scripts = specific.external_scripts or 0
  if scripts + handlers + javascript_urls + external_scripts > 0 then
    local options = {}
    for _, counter in ipairs({
      { 'scripts', scripts },
      { 'handlers', handlers },
      { 'javascript', javascript_urls },
      { 'external', external_scripts },
    }) do
      if counter[2] > 0 then
        options[#options + 1] = string.format('%s:%s=%d', filename, counter[1], counter[2])
      end
    end
    for _, indicator in ipairs(specific.script_indicators or {}) do
      options[#options + 1] = string.format('%s:%s', filename, indicator)
    end
    task:insert_result('SVG_SCRIPT', 1.0, options)
  end

  if (specific.foreign_objects or 0) > 0 then
    task:insert_result('SVG_FOREIGN_OBJECT', 1.0,
        string.format('%s:%d', filename, specific.foreign_objects))
  end

  -- Embedded raster images are the normal use of data: URIs; anything else
  -- (HTML, scripts, nested SVG, octet streams) is smuggled content
  local smuggled_types = {}
  for _, uri_type in ipairs(specific.data_uri_types or {}) do
    if not uri_type:find('^image/') or uri_type == 'image/svg+xml' then
      smuggled_types[#smuggled_types + 1] = string.format('%s:%s', filename, uri_type)
    end
  end
  if #smuggled_types > 0 then
    task:insert_result('SVG_DATA_URI', 1.0, smuggled_types)
  end

  if (specific.hyperlinks or 0) > 0 then
    task:insert_result('SVG_EXTERNAL_LINKS', 1.0,
        string.format('%s:%d', filename, specific.hyperlinks))
  end

  for i, resource in ipairs(specific.resources or {}) do
    if i > max_svg_resource_options then break end
    task:insert_result('SVG_EXTERNAL_RESOURCES', 1.0,
        string.format('%s:%s=%s', filename, resource.kind, trim_target(resource.url)))
  end

  local forms = specific.forms or 0
  local passwords = specific.password_inputs or 0
  if forms + passwords > 0 then
    local options = {}
    if forms > 0 then
      options[#options + 1] = string.format('%s:forms=%d', filename, forms)
    end
    if passwords > 0 then
      options[#options + 1] = string.format('%s:passwords=%d', filename, passwords)
    end
    task:insert_result('SVG_FORM', 1.0, options)
  end

  local redirects = {}
  if (specific.meta_refresh or 0) > 0 then
    redirects[#redirects + 1] = filename .. ':meta_refresh'
  end
  if (specific.embedded_documents or 0) > 0 then
    redirects[#redirects + 1] = filename .. ':embedded_documents'
  end
  for _, indicator in ipairs(specific.script_indicators or {}) do
    if indicator == 'location' then
      redirects[#redirects + 1] = filename .. ':location'
    end
  end
  if #redirects > 0 then
    task:insert_result('SVG_REDIRECT', 1.0, redirects)
  end
end

local tags_processors = {
  pdf = process_pdf_specific,
  docx = process_ooxml_specific,
  xlsx = process_ooxml_specific,
  pptx = process_ooxml_specific,
  svg = process_svg_specific,
}

local function process_specific_cb(task)
  local parts = task:get_parts() or {}

  for _, p in ipairs(parts) do
    local data = lua_content.get_specific(p, task)

    if data and type(data) == 'table' and data.tag then
      if tags_processors[data.tag] then
        tags_processors[data.tag](task, p, data)
      end
    end
  end
end

local id = rspamd_config:register_symbol {
  type = 'callback',
  name = 'SPECIFIC_CONTENT_CHECK',
  callback = process_specific_cb
}

rspamd_config:register_symbol {
  type = 'virtual',
  name = 'PDF_ENCRYPTED',
  parent = id,
  groups = { "content", "pdf" },
}

for tag, prefix in pairs(ooxml_prefixes) do
  for _, suffix in ipairs({ 'CONTENT', 'EXTERNAL_LINKS', 'SUSPICIOUS' }) do
    rspamd_config:register_symbol {
      type = 'virtual',
      name = prefix .. '_' .. suffix,
      parent = id,
      groups = { "content", tag },
    }
  end
end

for _, name in ipairs({
  'OOXML_MACROS', 'OOXML_OLE_OBJECT', 'OOXML_REMOTE_TEMPLATE', 'OOXML_EXTERNAL_DATA',
  'OOXML_INCOMPLETE',
}) do
  rspamd_config:register_symbol {
    type = 'virtual',
    name = name,
    parent = id,
    groups = { "content", "ooxml" },
  }
end

for _, name in ipairs({
  'SVG_CONTENT', 'SVG_SUSPICIOUS', 'SVG_SCRIPT', 'SVG_FOREIGN_OBJECT', 'SVG_DATA_URI',
  'SVG_EXTERNAL_LINKS', 'SVG_EXTERNAL_RESOURCES', 'SVG_FORM', 'SVG_REDIRECT',
  'SVG_INCOMPLETE',
}) do
  rspamd_config:register_symbol {
    type = 'virtual',
    name = name,
    parent = id,
    groups = { "content", "svg" },
  }
end
rspamd_config:register_symbol {
  type = 'virtual',
  name = 'PDF_JAVASCRIPT',
  parent = id,
  groups = { "content", "pdf" },
}
rspamd_config:register_symbol {
  type = 'virtual',
  name = 'PDF_SUSPICIOUS',
  parent = id,
  groups = { "content", "pdf" },
}
rspamd_config:register_symbol {
  type = 'virtual',
  name = 'PDF_LONG_TRAILER',
  parent = id,
  groups = { "content", "pdf" },
}
rspamd_config:register_symbol {
  type = 'virtual',
  name = 'PDF_MANY_OBJECTS',
  parent = id,
  groups = { "content", "pdf" },
}
rspamd_config:register_symbol {
  type = 'virtual',
  name = 'PDF_TIMEOUT',
  parent = id,
  groups = { "content", "pdf" },
}
for _, name in ipairs({
  'PDF_NO_OBJECTS', 'PDF_DECODE_LIMIT', 'PDF_UNDECODED_STREAMS', 'PDF_PARSE_ERRORS',
  'PDF_TEXT_TRUNCATED',
}) do
  rspamd_config:register_symbol {
    type = 'virtual',
    name = name,
    parent = id,
    groups = { "content", "pdf" },
  }
end