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
|
--[[
Copyright (c) 2018, Vsevolod Stakhov <vsevolod@highsecure.ru>
Copyright (c) 2018, Carsten Rosenberg <c.rosenberg@heinlein-support.de>
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.
]]--
--[[[
-- @module oletools
-- This module contains oletools access functions.
-- Olefy is needed: https://github.com/HeinleinSupport/olefy
--]]
local lua_util = require "lua_util"
local tcp = require "rspamd_tcp"
local upstream_list = require "rspamd_upstream_list"
local rspamd_logger = require "rspamd_logger"
local ucl = require "ucl"
local common = require "lua_scanners/common"
local N = 'oletools'
local function oletools_check(task, content, digest, rule)
local function oletools_check_uncached ()
local upstream = rule.upstreams:get_upstream_round_robin()
local addr = upstream:get_addr()
local retransmits = rule.retransmits
local protocol = 'OLEFY/1.0\nMethod: oletools\nRspamd-ID: ' .. task:get_uid() .. '\n\n'
local function oletools_callback(err, data)
local function oletools_requery(error)
-- set current upstream to fail because an error occurred
upstream:fail()
-- retry with another upstream until retransmits exceeds
if retransmits > 0 then
retransmits = retransmits - 1
lua_util.debugm(rule.name, task,
'%s: Request Error: %s - retries left: %s',
rule.log_prefix, error, retransmits)
-- Select a different upstream!
upstream = rule.upstreams:get_upstream_round_robin()
addr = upstream:get_addr()
lua_util.debugm(rule.name, task, '%s: retry IP: %s:%s',
rule.log_prefix, addr, addr:get_port())
tcp.request({
task = task,
host = addr:to_string(),
port = addr:get_port(),
timeout = rule.timeout,
shutdown = true,
data = { protocol, content },
callback = oletools_callback,
})
else
rspamd_logger.errx(task, '%s: failed to scan, maximum retransmits '..
'exceed - err: %s', rule.log_prefix, error)
task:insert_result(rule.symbol_fail, 0.0, 'failed - err: ' .. error)
end
end
if err then
oletools_requery(err)
else
-- Parse the response
if upstream then upstream:ok() end
data = tostring(data)
local ucl_parser = ucl.parser()
local ok, ucl_err = ucl_parser:parse_string(tostring(data))
if not ok then
rspamd_logger.errx(task, "%s: error parsing json response: %s",
rule.log_prefix, ucl_err)
return
end
local result = ucl_parser:get_object()
local oletools_rc = {
[0] = 'RETURN_OK',
[1] = 'RETURN_WARNINGS',
[2] = 'RETURN_WRONG_ARGS',
[3] = 'RETURN_FILE_NOT_FOUND',
[4] = 'RETURN_XGLOB_ERR',
[5] = 'RETURN_OPEN_ERROR',
[6] = 'RETURN_PARSE_ERROR',
[7] = 'RETURN_SEVERAL_ERRS',
[8] = 'RETURN_UNEXPECTED',
[9] = 'RETURN_ENCRYPTED',
}
if result[1].error ~= nil then
rspamd_logger.errx(task, '%s: ERROR found: %s', rule.log_prefix,
result[1].error)
if result[1].error == 'File too small' then
common.save_av_cache(task, digest, rule, 'OK')
common.log_clean(task, rule, 'File too small to be scanned for macros')
else
oletools_requery(result[1].error)
end
elseif result[3]['return_code'] == 9 then
rspamd_logger.warnx(task, '%s: File is encrypted.', rule.log_prefix)
elseif result[3]['return_code'] > 6 then
rspamd_logger.errx(task, '%s: Error Returned: %s',
rule.log_prefix, oletools_rc[result[3]['return_code']])
rspamd_logger.errx(task, '%s: Error message: %s',
rule.log_prefix, result[2]['message'])
task:insert_result(rule.symbol_fail, 0.0, 'failed - err: ' .. oletools_rc[result[3]['return_code']])
elseif result[3]['return_code'] > 1 then
rspamd_logger.errx(task, '%s: Error message: %s',
rule.log_prefix, result[2]['message'])
oletools_requery(oletools_rc[result[3]['return_code']])
elseif #result[2]['analysis'] == 0 and #result[2]['macros'] == 0 then
rspamd_logger.warnx(task, '%s: maybe unhandled python or oletools error', rule.log_prefix)
task:insert_result(rule.symbol_fail, 0.0, 'oletools unhandled error')
elseif result[2]['analysis'] == 'null' and #result[2]['macros'] == 0 then
common.save_av_cache(task, digest, rule, 'OK')
common.log_clean(task, rule, 'No macro found')
elseif #result[2]['macros'] > 0 then
-- M=Macros, A=Auto-executable, S=Suspicious keywords, I=IOCs,
-- H=Hex strings, B=Base64 strings, D=Dridex strings, V=VBA strings
local m_exist = 'M'
local m_autoexec = '-'
local m_suspicious = '-'
local m_iocs = '-'
local m_hex = '-'
local m_base64 = '-'
local m_dridex = '-'
local m_vba = '-'
lua_util.debugm(rule.name, task,
'%s: filename: %s', rule.log_prefix, result[2]['file'])
lua_util.debugm(rule.name, task,
'%s: type: %s', rule.log_prefix, result[2]['type'])
for _,m in ipairs(result[2]['macros']) do
lua_util.debugm(rule.name, task, '%s: macros found - code: %s, ole_stream: %s, '..
'vba_filename: %s', rule.log_prefix, m.code, m.ole_stream, m.vba_filename)
end
local analysis_keyword_table = {}
for _,a in ipairs(result[2]['analysis']) do
lua_util.debugm(rule.name, task, '%s: threat found - type: %s, keyword: %s, '..
'description: %s', rule.log_prefix, a.type, a.keyword, a.description)
if a.type == 'AutoExec' then
m_autoexec = 'A'
table.insert(analysis_keyword_table, a.keyword)
elseif a.type == 'Suspicious' then
m_suspicious = 'S'
if a.keyword ~= 'Base64 Strings' and a.keyword ~= 'Hex Strings'
then
table.insert(analysis_keyword_table, a.keyword)
end
elseif a.type == 'IOCs' then
m_iocs = 'I'
elseif a.type == 'Hex strings' then
m_hex = 'H'
elseif a.type == 'Base64 strings' then
m_base64 = 'B'
elseif a.type == 'Dridex strings' then
m_dridex = 'D'
elseif a.type == 'VBA strings' then
m_vba = 'V'
end
end
--lua_util.debugm(N, task, '%s: analysis_keyword_table: %s', rule.log_prefix, analysis_keyword_table)
if rule.extended == false and m_autoexec == 'A' and m_suspicious == 'S' then
-- use single string as virus name
local threat = 'AutoExec + Suspicious (' .. table.concat(analysis_keyword_table, ',') .. ')'
lua_util.debugm(rule.name, task, '%s: threat result: %s', rule.log_prefix, threat)
common.yield_result(task, rule, threat, rule.default_score)
common.save_av_cache(task, digest, rule, threat, rule.default_score)
elseif rule.extended == true and #analysis_keyword_table > 0 then
-- report any flags (types) and any most keywords as individual virus name
local flags = m_exist ..
m_autoexec ..
m_suspicious ..
m_iocs ..
m_hex ..
m_base64 ..
m_dridex ..
m_vba
table.insert(analysis_keyword_table, 1, flags)
lua_util.debugm(rule.name, task, '%s: extended threat result: %s',
rule.log_prefix, table.concat(analysis_keyword_table, ','))
common.yield_result(task, rule, analysis_keyword_table, rule.default_score)
common.save_av_cache(task, digest, rule, analysis_keyword_table, rule.default_score)
else
common.save_av_cache(task, digest, rule, 'OK')
common.log_clean(task, rule, 'Scanned Macro is OK')
end
else
rspamd_logger.warnx(task, '%s: unhandled response', rule.log_prefix)
task:insert_result(rule.symbol_fail, 0.0, 'unhandled response')
end
end
end
tcp.request({
task = task,
host = addr:to_string(),
port = addr:get_port(),
timeout = rule.timeout,
shutdown = true,
data = { protocol, content },
callback = oletools_callback,
})
end
if common.need_av_check(task, content, rule) then
if common.check_av_cache(task, digest, rule, oletools_check_uncached) then
return
else
oletools_check_uncached()
end
end
end
local function oletools_config(opts)
local oletools_conf = {
name = N,
scan_mime_parts = false,
scan_text_mime = false,
scan_image_mime = false,
default_port = 5954,
timeout = 15.0,
log_clean = false,
retransmits = 2,
cache_expire = 86400, -- expire redis in 1d
message = '${SCANNER}: Oletools threat message found: "${VIRUS}"',
detection_category = "office macro",
default_score = 1,
action = false,
extended = false,
}
oletools_conf = lua_util.override_defaults(oletools_conf, opts)
if not oletools_conf.prefix then
oletools_conf.prefix = 'rs_' .. oletools_conf.name .. '_'
end
if not oletools_conf.log_prefix then
if oletools_conf.name:lower() == oletools_conf.type:lower() then
oletools_conf.log_prefix = oletools_conf.name
else
oletools_conf.log_prefix = oletools_conf.name .. ' (' .. oletools_conf.type .. ')'
end
end
if not oletools_conf.servers then
rspamd_logger.errx(rspamd_config, 'no servers defined')
return nil
end
oletools_conf.upstreams = upstream_list.create(rspamd_config,
oletools_conf.servers,
oletools_conf.default_port)
if oletools_conf.upstreams then
lua_util.add_debug_alias('external_services', oletools_conf.name)
return oletools_conf
end
rspamd_logger.errx(rspamd_config, 'cannot parse servers %s',
oletools_conf.servers)
return nil
end
return {
type = {N, 'attachment scanner', 'hash', 'scanner'},
description = 'oletools office macro scanner',
configure = oletools_config,
check = oletools_check,
name = N
}
|