-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmalware-filter.lua
More file actions
606 lines (534 loc) · 14.6 KB
/
malware-filter.lua
File metadata and controls
606 lines (534 loc) · 14.6 KB
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
-- File: malware-filter.lua
-- Author: Dylan Blanqué
local re -- Regex Module
if f.isModuleAvailable("rex_pcre") then
re = require"rex_pcre"
elseif f.isModuleAvailable("rex_pcre2") then
re = require"rex_pcre2"
else
mainlog(
"pdns-recursor-scripts malware-filter.lua requires rex_pcre or rex_pcre2 to be installed",
pdns.loglevels.Error
)
return false
end
-- Pre-compiled Regex Patterns
-- Matches Adblock Format
local re_pat_adblock=re.new(
"^(\\|){2}(.*)\\^$"
)
-- Matches PCRE Format
local re_pat_pcre=re.new(
"(.*)[$^|](.*)"
)
-- Matches Wildcard Format
local re_pat_wild=re.new(
"^\\*\\..*"
)
-- Matches HOSTS Format
local re_pat_hosts=re.new(
"^(0\\.0\\.0\\.0|127\\.0\\.0\\.1|::|2001:1::1)\\s+.*"
)
-- Matches RFC for TLDs
local re_pat_domain=re.new(
"^(?:(?!-)[A-Za-z0-9-]{1,63}(?<!-)\\.)+[A-Za-z]{2,63}$"
)
-- Blocklist Objects (DS, CAS, NMG, Tables, etc.)
local mf_dnsbl = newDS()
local mf_rebl = {}
local mf_ipbl = newCAS()
local mf_cidrbl = newNMG()
local mf_whitelist_ds = newDS()
local mf_nxdomain_ds = newDS()
-- DNSBL Metrics
local dnsbl_hits = getMetric("dnsbl-hits")
local dnsbl_parsed_loaded_metric = getMetric("dnsbl-parsed-success")
local dnsbl_parsed_ignored_metric = getMetric("dnsbl-parsed-ignored")
local dnsbl_parsed_errors_metric = getMetric("dnsbl-parsed-errors")
local dnsbl_loaded_adblock = getMetric("dnsbl-loaded-adblock")
local dnsbl_loaded_pcre = getMetric("dnsbl-loaded-pcre")
local dnsbl_loaded_wildcard = getMetric("dnsbl-loaded-wildcard")
local dnsbl_loaded_hosts = getMetric("dnsbl-loaded-hosts")
local dnsbl_loaded_normal = getMetric("dnsbl-loaded-normal")
-- IPBL / CIDRBL Metrics
local ipbl_loaded_metric = getMetric("ipbl-loaded")
local cidrbl_loaded_metric = getMetric("cidrbl-loaded")
local ipbl_hits = getMetric("ipbl-hits")
-- For debugging
local whitelist_files_loaded = getMetric("whitelist-files-loaded")
local dnsbl_files_loaded = getMetric("dnsbl-files-loaded")
local ipbl_files_loaded = getMetric("ipbl-files-loaded")
-- Set NXDOMAIN Hosts
local nxdomain_hosts={
"localhost",
"localhost.localdomain",
"local",
"broadcasthost",
"ip6-localhost",
"ip6-loopback",
"ip6-localnet",
"ip6-mcastprefix",
"ip6-allnodes",
"ip6-allrouters",
"ip6-allhosts",
}
for _, host in ipairs(nxdomain_hosts) do
mf_nxdomain_ds:add(host)
end
local function get_list_files_in_dir(search_dir)
local files = {}
for dir in io.popen("ls -pa " .. search_dir .. " | grep -v / | grep -E \".*\\.(list|txt|hosts)$\""):lines()
do
table.insert(files, search_dir .. "/" .. dir)
end
return files
end
-- Get relevant files
local dnsbl_file_table = get_list_files_in_dir(g.pdns_scripts_path.."/dnsbl.d")
local ipbl_file_table = get_list_files_in_dir(g.pdns_scripts_path.."/ipbl.d")
local function is_dq_internal(dq)
pdnslog(
"is_dq_internal(): checking if internal record for " .. tostring(dq.qname),
pdns.loglevels.Debug
)
if mf_nxdomain_ds:check(dq.qname) then
pdnslog(
"is_dq_internal(): " .. tostring(dq.qname) .. " is internal",
pdns.loglevels.Debug
)
return true
end
return false
end
local function is_dq_whitelisted(dq)
local qname_no_dot = dq.qname:toStringNoDot()
pdnslog(
"is_dq_whitelisted(): checking whitelist for " .. qname_no_dot,
pdns.loglevels.Debug
)
-- Check in DNSSuffixMatchGroup
if mf_whitelist_ds:check(dq.qname) then
pdnslog(
"is_dq_whitelisted(): " .. qname_no_dot .. " is whitelisted",
pdns.loglevels.Debug
)
return true
end
return false
end
local function is_qname_whitelisted(qname_no_dot)
if not qname_no_dot or f.empty_str(qname_no_dot) then
return false
end
-- Check in DNSSuffixMatchGroup
if mf_whitelist_ds:check(newDN(qname_no_dot)) then
mainlog(
"is_qname_whitelisted(): " .. qname_no_dot .. " is whitelisted",
pdns.loglevels.Debug
)
return true
end
return false
end
-- Precompile regex patterns for better performance
local comment_or_empty = function(line)
return f.is_comment(line) or f.empty_str(line)
end
-- loads contents of a file line by line with the given processing function
local function load_list_file(fn_name, fn_process, filename)
if not f.fileExists(filename) then
mainlog(
fn_name .. "(): could not open file " .. filename,
pdns.loglevels.Warning
)
return
end
-- Process each line
local line_errors = 0
local line_idx = 1
for line in io.lines(filename) do
local ok, result = pcall(function()
return fn_process(line)
end)
if not ok then
line_errors = line_errors + 1
local _filename = f.string_split(filename, "/")
mainlog(
string.format(
"%s(): could not parse line %d for file %s",
fn_name,
line_idx,
_filename[#_filename]
),
pdns.loglevels.Error
)
end
line_idx = line_idx + 1
end
mainlog(
string.format(
"%s(): %s successfully loaded",
fn_name,
filename
),
pdns.loglevels.Notice
)
return line_errors
end
-- loads contents of a file line by line into the whitelist table
local function load_whitelist_file(filename, whitelist_ds)
-- Validate input parameters
if type(filename) ~= "string" or not whitelist_ds then
mainlog("load_whitelist_file(): invalid parameters", pdns.loglevels.Error)
return false
end
local function process_line(line)
-- Trim whitespace from line
line = line:match("^%s*(.-)%s*$")
-- Ignore commented lines
if comment_or_empty(line) then
return
end
if re_pat_wild:match(line) then
local trimmed_dn = line:gsub('*%.', "")
whitelist_ds:add(newDN(trimmed_dn))
else
whitelist_ds:add(newDN(line))
end
mainlog(
line .. " is whitelisted",
pdns.loglevels.Notice
)
end
if getRecursorThreadId() == 1 then
whitelist_files_loaded:inc()
end
return load_list_file("load_whitelist_file", process_line, filename)
end
-- Loads contents of a file line by line into the dnsbl table
local function load_dnsbl_file(filename, dnsbl_ds, rebl_list)
-- Validate input parameters
if type(filename) ~= "string" or not dnsbl_ds or not rebl_list then
mainlog("load_dnsbl_file(): invalid parameters", pdns.loglevels.Error)
return false
end
local ignored_lines = 0
local loaded_lines = 0
local adblock_lines = 0
local pcre_lines = 0
local wildcard_lines = 0
local hosts_lines = 0
local normal_lines = 0
local start_time = os.time()
local function process_line(line)
-- Trim whitespace from line
line = line:match("^%s*(.-)%s*$")
if comment_or_empty(line) then
ignored_lines = ignored_lines + 1
return
end
-- ADBLOCK FORMAT
if re_pat_adblock:match(line) then
local stripped = line:gsub("||", ""):gsub("%^", "")
if is_qname_whitelisted(stripped) then
return
end
dnsbl_ds:add(stripped)
loaded_lines = loaded_lines + 1
adblock_lines = adblock_lines + 1
-- PCRE FORMAT
elseif re_pat_pcre:match(line) then
-- Insert pre-compiled regex into table
table.insert(rebl_list, re.new(line))
loaded_lines = loaded_lines + 1
pcre_lines = pcre_lines + 1
-- WILDCARD FORMAT
elseif re_pat_wild:match(line) then
local wilded = line:gsub('*%.', "")
if is_qname_whitelisted(wilded) then
return
end
dnsbl_ds:add(wilded)
loaded_lines = loaded_lines + 1
wildcard_lines = wildcard_lines + 1
-- HOSTS FORMAT
elseif re_pat_hosts:match(line) then
local host = f.extract_hosts_domain(line)
host = f.trim_hosts_comment(host)
if is_qname_whitelisted(host) then
return
end
dnsbl_ds:add(host)
loaded_lines = loaded_lines + 1
hosts_lines = hosts_lines + 1
-- STANDARD FORMAT
elseif re_pat_domain:match(line) then
dnsbl_ds:add(line)
loaded_lines = loaded_lines + 1
normal_lines = normal_lines + 1
else
error("Could not parse line.")
end
end
-- Load file
local error_lines = load_list_file("load_dnsbl_file", process_line, filename)
local duration = os.time() - start_time
-- Set metrics if main thread only
if getRecursorThreadId() == 1 then
-- Set general metrics
dnsbl_parsed_loaded_metric:incBy(loaded_lines)
-- Set detailed metrics
dnsbl_loaded_adblock:incBy(adblock_lines)
dnsbl_loaded_pcre:incBy(pcre_lines)
dnsbl_loaded_wildcard:incBy(wildcard_lines)
dnsbl_loaded_hosts:incBy(hosts_lines)
dnsbl_loaded_normal:incBy(normal_lines)
-- Set ignored metric
dnsbl_parsed_ignored_metric:incBy(ignored_lines)
dnsbl_parsed_errors_metric:incBy(error_lines)
end
dnsbl_files_loaded:inc()
return true
end
-- loads contents of a file line by line into the ipbl table
local function load_ipbl_file(filename, ipbl_cas, cidrbl_nmg)
-- Validate input parameters
if type(filename) ~= "string" or not ipbl_cas or not cidrbl_nmg then
mainlog("load_ipbl_file(): invalid parameters", pdns.loglevels.Error)
return false
end
local function process_line(line)
-- Ignore commented lines
if comment_or_empty(line) then
return
end
-- Check if it's a CIDR
line = f.trim_hosts_comment(line)
if string.find(line, "/") then
mainlog(
"load_ipbl_file(): loaded CIDR " .. line,
pdns.loglevels.Debug
)
cidrbl_nmg:addMask(line)
if getRecursorThreadId() == 1 then
cidrbl_loaded_metric:inc()
end
else -- Assume IP Address
mainlog(
"load_ipbl_file(): loaded IP " .. line,
pdns.loglevels.Debug
)
ipbl_cas:add(line)
if getRecursorThreadId() == 1 then
ipbl_loaded_metric:inc()
end
end
end
-- Load file
load_list_file("load_ipbl_file", process_line, filename)
if getRecursorThreadId() == 1 then
ipbl_files_loaded:inc()
end
return true
end
local function get_sinkhole(dq)
if dq.qtype == pdns.A or dq.qtype == pdns.ANY then
return {
["0.0.0.0"]=pdns.A
}
elseif dq.qtype == pdns.AAAA or dq.qtype == pdns.ANY then
return {
["2001:1::1"]=pdns.AAAA
}
-- elseif dq.qtype == pdns.CNAME or dq.qtype == pdns.ANY then
-- return {
-- ["0.0.0.0"]=pdns.A,
-- ["::"]=pdns.AAAA
-- }
end
if dq.qtype == pdns.HTTPS then
return {
["1 . alpn=h3,h3-29,h2 ipv4hint=0.0.0.0 ipv6hint=2001:1::1"]=pdns.HTTPS
}
end
return nil
end
-- this function is hooked before resolving starts
local function preresolve_mf(dq)
if not g.initialized_mf then
dq.appliedPolicy.policyKind = pdns.policykinds.Drop
return false
end
if is_dq_internal(dq) then
dq.appliedPolicy.policyKind = pdns.policykinds.NXDOMAIN
return false
end
if is_dq_whitelisted(dq) then
return false
end
-- If the last char is a dot, remove it (for non-canonical format)
local qname_no_trailing_dot = f.qname_remove_trailing_dot(dq)
local resolve_sinkhole = mf_dnsbl:check(dq.qname) -- NORMAL BL Check
-- PCRE BL Check
if not resolve_sinkhole then
for k, p in pairs(mf_rebl) do
if resolve_sinkhole then
break
end
-- Log PCRE Pattern
pdnslog(
"PCRE Pattern: ".. qname_no_trailing_dot .." | " .. tostring(p),
pdns.loglevels.Debug
)
resolve_sinkhole = p:match(qname_no_trailing_dot)
end
end
pdnslog("Pre-Resolving " .. tostring(dq.qname), pdns.loglevels.Debug)
pdnslog("PCRE Check: " .. tostring(re_check), pdns.loglevels.Debug)
pdnslog("DNSBL Check: " .. tostring(ds_check), pdns.loglevels.Debug)
-- check blocklist
if resolve_sinkhole then
local sinkhole = get_sinkhole(dq)
if not sinkhole then
dq.appliedPolicy.policyKind = pdns.policykinds.Drop
return false
end
for dq_val, dq_qtype in pairs(sinkhole) do
dq:addAnswer(dq_qtype, dq_val, g.options.default_ttl)
end
dnsbl_hits:inc()
return true
end
-- default, do not rewrite this response
pdnslog("Did not re-write " .. tostring(dq.qname), pdns.loglevels.Debug)
return false
end
local function postresolve_mf(dq)
if not g.initialized_mf then
dq.appliedPolicy.policyKind = pdns.policykinds.Drop
return false
end
if is_dq_whitelisted(dq) then
return false
end
if dq.qtype ~= pdns.A and dq.qtype ~= pdns.AAAA then
pdnslog(
"Skipping postresolve_mf for ".. tostring(dq.qname),
pdns.loglevels.Debug
)
return false
end
local dq_records = dq:getRecords()
local blocked_records = {}
local result_dq = {}
local update_dq = false
for dr_index, dr in ipairs(dq_records) do
local dr_content = dr:getContent()
local ip_check = false
local cidr_check = false
if not dr_content then
pdnslog(
"No DNSR Content for ".. tostring(dq.qname),
pdns.loglevels.Debug
)
goto continue
end
-- Call function without raising exception to parent process
local ok, dr_ca = pcall(newCA, dr_content)
if not ok then
goto continue
end
pdnslog("DNSR Content: " .. dr_ca:toString(), pdns.loglevels.Debug)
if not ip_check and not cidr_check then
cidr_check = mf_cidrbl:match(dr_ca)
ip_check = mf_ipbl:check(dr_ca)
end
pdnslog("IP Check: " .. tostring(ip_check), pdns.loglevels.Debug)
pdnslog("CIDR Check: " .. tostring(cidr_check), pdns.loglevels.Debug)
if ip_check or cidr_check then
update_dq = true
table.insert(blocked_records, dr_index)
for dq_val, dq_qtype in pairs(get_sinkhole(dq)) do
dr:changeContent(dq_val)
end
pdnslog(
string.format("Blocked Post-Resolving %s", tostring(dq.qname)),
pdns.loglevels.Debug
)
else
pdnslog(
string.format("Allowed Post-Resolving %s", tostring(dq.qname)),
pdns.loglevels.Debug
)
end
::continue::
end
if update_dq then
for i,v in ipairs(dq_records) do
if not f.table_contains(blocked_records, i) then
table.insert(result_dq, v)
end
end
local use_result
if f.table_len(result_dq) > 1 then
use_result = result_dq
else
use_result = dq_records
end
dq:setRecords(use_result)
pdnslog(
string.format("Query Result %s", tostring(use_result)),
pdns.loglevels.Debug
)
ipbl_hits:inc()
return true
end
return false
end
local function main_mf()
if g.initialized_mf then
return
end
mainlog(
"Executing main_mf().",
pdns.loglevels.Debug
)
-- Load Whitelist
if f.fileExists(g.pdns_scripts_path.."/conf.d/whitelist.txt") then
load_whitelist_file(
g.pdns_scripts_path.."/conf.d/whitelist.txt",
mf_whitelist_ds
)
end
-- Load DNSBL
if g.options.use_dnsbl then
for key, filename in pairs(dnsbl_file_table) do
load_dnsbl_file(filename, mf_dnsbl, mf_rebl)
end
-- Add preresolve function to table
mainlog(
"Loading preresolve_mf into pre-resolve functions.",
pdns.loglevels.Debug
)
f.addHookFunction("pre", "preresolve_mf", preresolve_mf)
else
mainlog("DNSBL Function not enabled. Set overrides in file overrides.lua", pdns.loglevels.Notice)
end
-- Load IPBL
if g.options.use_ipbl then
for key, filename in pairs(ipbl_file_table) do
load_ipbl_file(filename, mf_ipbl, mf_cidrbl)
end
-- Add postresolve function to table
mainlog(
"Loading postresolve_mf into post-resolve functions.",
pdns.loglevels.Debug
)
f.addHookFunction("post", "postresolve_mf", postresolve_mf)
else
mainlog("IPBL Function not enabled. Set overrides in file overrides.lua", pdns.loglevels.Notice)
end
g.initialized_mf = true
end
-- Execute malware-filter main logic
main_mf()