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
|
--[[
Copyright (c) 2019, Vsevolod Stakhov <vsevolod@highsecure.ru>
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 lua_content/pdf
-- This module contains some heuristics for PDF files
--]]
local rspamd_trie = require "rspamd_trie"
local bit = require "bit"
local pdf_trie
local N = "lua_content"
local lua_util = require "lua_util"
local rspamd_regexp = require "rspamd_regexp"
local lpeg = require "lpeg"
local pdf_patterns = {
trailer = {
patterns = {
[[\ntrailer\r?\n]]
}
},
javascript = {
patterns = {
[[\/JS(?:[\s/><])]],
[[\/JavaScript(?:[\s/><])]],
}
},
openaction = {
patterns = {
[[\/OpenAction(?:[\s/><])]],
[[\/AA(?:[\s/><])]],
}
},
suspicious = {
patterns = {
[[netsh\s]],
[[echo\s]],
[[\/[A-Za-z]*#\d\d(?:[#A-Za-z<>/\s])]], -- Hex encode obfuscation
}
},
start_object = {
patterns = {
[[\n\s*\d+ \d+ obj\r?\n]]
}
},
end_object = {
patterns = {
[=[endobj[\r\n]]=]
}
},
start_stream = {
patterns = {
[[>\s*stream\r?\n]],
}
},
end_stream = {
patterns = {
[=[endstream[\r\n]]=]
}
}
}
-- index[n] ->
-- t[1] - pattern,
-- t[2] - key in patterns table,
-- t[3] - value in patterns table
-- t[4] - local pattern index
local pdf_indexes = {}
local exports = {}
-- Used to process patterns found in PDF
-- positions for functional processors should be a iter/table from trie matcher in form
---- [{n1, pat_idx1}, ... {nn, pat_idxn}] where
---- pat_idxn is pattern index and n1 ... nn are match positions
local processors = {}
local pdf_grammar
-- Used to match objects
local object_re = rspamd_regexp.create_cached([=[/(\d+)\s+(\d+)\s+obj\s*/]=])
local function compile_tries()
local default_compile_flags = bit.bor(rspamd_trie.flags.re,
rspamd_trie.flags.dot_all,
rspamd_trie.flags.no_start)
local function compile_pats(patterns, indexes, compile_flags)
local strs = {}
for what,data in pairs(patterns) do
for i,pat in ipairs(data.patterns) do
strs[#strs + 1] = pat
indexes[#indexes + 1] = {what, data, pat, i}
end
end
return rspamd_trie.create(strs, compile_flags or default_compile_flags)
end
if not pdf_trie then
pdf_trie = compile_pats(pdf_patterns, pdf_indexes)
end
end
local function gen_grammar()
local P = lpeg.P
local R = lpeg.R
local S = lpeg.S
local V = lpeg.V
local C = lpeg.C
local D = R'09' -- Digits
-- Helper functions
local function pdf_hexstring_unescape(s)
local function ue(cc)
return string.char(tonumber(cc, 16))
end
if #s % 2 == 0 then
-- Sane hex string
return s:gsub('..', ue)
end
-- WTF hex string
-- Append '0' to it and unescape...
return s:sub(1, #s - 1):gsub('..' , ue) .. (s:sub(#s) .. '0'):gsub('..' , ue)
end
local function pdf_string_unescape(s)
return s
end
local function pdf_id_unescape(s)
return (s:gsub('#%d%d', function (cc)
return string.char(tonumber(cc:sub(2), 16))
end))
end
local delim = S'()<>[]{}/%'
local ws = S'\0 \r\n\t\f'
local hex = R'af' + R'AF' + D
-- Comments.
local eol = P'\r\n' + '\n'
local line = (1 - S'\r\n\f')^0 * eol^-1
local comment = P'%' * line
-- Numbers.
local sign = S'+-'^-1
local decimal = D^1
local float = D^1 * P'.' * D^0 + P'.' * D^1
local number = C(sign * (float + decimal)) / tonumber
-- String
local str = P{ "(" * C(((1 - S"()") + V(1))^0) / pdf_string_unescape * ")" }
local hexstr = P{"<" * C(hex^0) / pdf_hexstring_unescape * ">"}
-- Identifier
local id = P{'/' * C((1-(delim + ws))^1) / pdf_id_unescape}
-- Stupid references
local ref = lpeg.Ct{lpeg.Cc("%REF%") * C(D^1) * " " * C(D^1) * " " * "R"}
return P{
"EXPR";
EXPR = V("ELT")^0,
ELT = V("ARRAY") + V("DICT") + V("ATOM"),
ATOM = ws^0 * (comment + ref + number + V("STRING") + id) * ws^0,
DICT = "<<" * lpeg.Cf(lpeg.Ct("") * V("KV_PAIR")^0, rawset) * ">>",
KV_PAIR = lpeg.Cg(id * ws^0 * V("ELT") * ws^0),
ARRAY = "[" * lpeg.Ct(V("ELT")^0) * "]",
STRING = P{str + hexstr},
}
end
-- Call immediately on require
compile_tries()
pdf_grammar = gen_grammar()
local function extract_text_data(specific)
return nil -- NYI
end
local function postprocess_pdf_objects(task, input, pdf)
local start_pos, end_pos = 1, 1
local objects = {}
local obj_count = 0
while start_pos <= #pdf.start_objects and end_pos <= #pdf.end_objects do
local first = pdf.start_objects[start_pos]
local last = pdf.end_objects[end_pos]
-- 7 is length of `endobj\n`
if first + 7 < last then
local len = last - first - 7
-- Also get the starting span and try to match it versus obj re to get numbers
local obj_line_potential = first - 32
if obj_line_potential < 1 then obj_line_potential = 1 end
if end_pos > 1 and pdf.end_objects[end_pos - 1] >= obj_line_potential then
obj_line_potential = pdf.end_objects[end_pos - 1] + 1
end
local obj_line_span = input:span(obj_line_potential, first - obj_line_potential + 1)
local matches = object_re:search(obj_line_span, true, true)
if matches and matches[1] then
objects[obj_count + 1] = {
start = first,
len = len,
data = input:span(first, len),
major = matches[1][2],
minor = matches[1][3],
}
end
obj_count = obj_count + 1
start_pos = start_pos + 1
end_pos = end_pos + 1
elseif start_pos > end_pos then
end_pos = end_pos + 1
end
end
-- Now we have objects and we need to attach streams that are in bounds
if pdf.start_streams and pdf.end_streams then
start_pos, end_pos = 1, 1
for _,obj in ipairs(objects) do
while start_pos <= #pdf.start_streams and end_pos <= #pdf.end_streams do
local first = pdf.start_streams[start_pos]
local last = pdf.end_streams[end_pos]
last = last - 10 -- Exclude endstream\n pattern
lua_util.debugm(N, task, "start: %s, end: %s; obj: %s-%s",
first, last, obj.start, obj.start + obj.len)
if first > obj.start and last < obj.start + obj.len and last > first then
-- In case if we have fake endstream :(
while pdf.end_streams[end_pos + 1] and pdf.end_streams[end_pos + 1] < obj.start + obj.len do
end_pos = end_pos + 1
last = pdf.end_streams[end_pos]
end
local len = last - first
obj.stream = {
start = first,
len = len,
data = input:span(first, len)
}
start_pos = start_pos + 1
end_pos = end_pos + 1
break
elseif first < obj.start then
start_pos = start_pos + 1
elseif last > obj.start + obj.len then
-- Not this object
break
else
start_pos = start_pos + 1
end_pos = end_pos + 1
end
end
if obj.stream then
lua_util.debugm(N, task, 'found object %s:%s %s start %s len, %s stream start, %s stream length',
obj.major, obj.minor, obj.start, obj.len, obj.stream.start, obj.stream.len)
else
lua_util.debugm(N, task, 'found object %s:%s %s start %s len, no stream',
obj.major, obj.minor, obj.start, obj.len)
end
end
end
pdf.objects = objects
end
local function process_pdf(input, _, task)
local matches = pdf_trie:match(input)
if matches then
local pdf_output = {
tag = 'pdf',
extract_text = extract_text_data,
}
local grouped_processors = {}
for npat,matched_positions in pairs(matches) do
local index = pdf_indexes[npat]
local proc_key,loc_npat = index[1], index[4]
if not grouped_processors[proc_key] then
grouped_processors[proc_key] = {
processor_func = processors[proc_key],
offsets = {},
}
end
local proc = grouped_processors[proc_key]
-- Fill offsets
for _,pos in ipairs(matched_positions) do
proc.offsets[#proc.offsets + 1] = {pos, loc_npat}
end
end
for name,processor in pairs(grouped_processors) do
-- Sort by offset
lua_util.debugm(N, task, "pdf: process group %s with %s matches",
name, #processor.offsets)
table.sort(processor.offsets, function(e1, e2) return e1[1] < e2[1] end)
processor.processor_func(input, task, processor.offsets, pdf_output)
end
if pdf_output.start_objects and pdf_output.end_objects then
-- Postprocess objects
postprocess_pdf_objects(task, input, pdf_output)
end
return pdf_output
end
end
-- Processes the PDF trailer
processors.trailer = function(input, task, positions, output)
local last_pos = positions[#positions]
local last_span = input:span(last_pos[1])
for line in last_span:lines(true) do
if line:find('/Encrypt ') then
lua_util.debugm(N, task, "pdf: found encrypted line in trailer: %s",
line)
output.encrypted = true
end
end
end
processors.javascript = function(_, task, _, output)
lua_util.debugm(N, task, "pdf: found javascript tag")
output.javascript = true
end
processors.openaction = function(_, task, _, output)
lua_util.debugm(N, task, "pdf: found openaction tag")
output.openaction = true
end
processors.suspicious = function(_, task, _, output)
lua_util.debugm(N, task, "pdf: found a suspicious pattern")
output.suspicious = true
end
local function generic_table_inserter(positions, output, output_key)
if not output[output_key] then
output[output_key] = {}
end
local shift = #output[output_key]
for i,pos in ipairs(positions) do
output[output_key][i + shift] = pos[1]
end
end
processors.start_object = function(_, task, positions, output)
generic_table_inserter(positions, output, 'start_objects')
end
processors.end_object = function(_, task, positions, output)
generic_table_inserter(positions, output, 'end_objects')
end
processors.start_stream = function(_, task, positions, output)
generic_table_inserter(positions, output, 'start_streams')
end
processors.end_stream = function(_, task, positions, output)
generic_table_inserter(positions, output, 'end_streams')
end
exports.process = process_pdf
return exports
|