Debian Codegraph @

src/modules/secfilter/README

raw
1 Secfilter Module
2
3 Jose Luis Verdeguer
4
5 Zoon Suite
6 <[email protected]>
7
8 Copyright © 2018
9 __________________________________________________________________
10
11 Table of Contents
12
13 1. Admin Guide
14
15 1. Overview
16 2. Dependencies
17
18 2.1. Kamailio Modules
19 2.2. External Libraries or Applications
20
21 3. Parameters
22
23 3.1. db_url (string)
24 3.2. table_name (string)
25 3.3. action_col (string)
26 3.4. type_col (string)
27 3.5. data_col (string)
28 3.6. dst_exact_match (integer)
29 3.7. reload_delta (integer)
30 3.8. cleanup_interval (integer)
31
32 4. Functions
33
34 4.1. secf_check_ip ()
35 4.2. secf_check_ua ()
36 4.3. secf_check_country (string)
37 4.4. secf_check_from_hdr ()
38 4.5. secf_check_to_hdr ()
39 4.6. secf_check_contact_hdr ()
40 4.7. secf_check_dst (string)
41 4.8. secf_check_sqli_hdr (string)
42 4.9. secf_check_sqli_all ()
43
44 5. RPC commands
45
46 5.1. secfilter.reload
47 5.2. secfilter.print
48 5.3. secfilter.stats
49 5.4. secfilter.stats_reset
50 5.5. secfilter.add_dst
51 5.6. secfilter.del_dst
52 5.7. secfilter.add_bl
53 5.8. secfilter.del_bl
54 5.9. secfilter.add_wl
55 5.10. secfilter.del_wl
56
57 6. Installation
58
59 6.1. Database setup
60
61 7. Some examples
62
63 7.1. Print data
64 7.2. Statistics
65
66 List of Examples
67
68 1.1. Set db_url parameter
69 1.2. Set table_name parameter
70 1.3. Set action_col parameter
71 1.4. Set type_col parameter
72 1.5. Set data_col parameter
73 1.6. Set dst_exact_match parameter
74 1.7. Set reload_delta parameter
75 1.8. Set cleanup_interval parameter
76 1.9. secf_check_ip usage
77 1.10. secf_check_ua usage
78 1.11. secf_check_country usage
79 1.12. secf_check_from_hdr usage
80 1.13. secf_check_to_hdr usage
81 1.14. secf_check_contact_hdr usage
82 1.15. secf_check_dst usage
83 1.16. secf_check_sqli_hdr usage
84 1.17. secf_check_sqli_all usage
85 1.18. secfilter.reload usage
86 1.19. secfilter.print usage
87 1.20. secfilter.stats usage
88 1.21. secfilter.stats_reset usage
89 1.22. secfilter.add_dst usage
90 1.23. secfilter.del_dst usage
91 1.24. secfilter.add_bl usage
92 1.25. secfilter.del_bl usage
93 1.26. secfilter.add_wl usage
94 1.27. secfilter.del_wl usage
95 1.28. Example database content - secfilter table
96 1.29. kamctl rpc secfilter.print ua
97 1.30. kamctl rpc secfilter.stats
98
99 Chapter 1. Admin Guide
100
101 Table of Contents
102
103 1. Overview
104 2. Dependencies
105
106 2.1. Kamailio Modules
107 2.2. External Libraries or Applications
108
109 3. Parameters
110
111 3.1. db_url (string)
112 3.2. table_name (string)
113 3.3. action_col (string)
114 3.4. type_col (string)
115 3.5. data_col (string)
116 3.6. dst_exact_match (integer)
117 3.7. reload_delta (integer)
118 3.8. cleanup_interval (integer)
119
120 4. Functions
121
122 4.1. secf_check_ip ()
123 4.2. secf_check_ua ()
124 4.3. secf_check_country (string)
125 4.4. secf_check_from_hdr ()
126 4.5. secf_check_to_hdr ()
127 4.6. secf_check_contact_hdr ()
128 4.7. secf_check_dst (string)
129 4.8. secf_check_sqli_hdr (string)
130 4.9. secf_check_sqli_all ()
131
132 5. RPC commands
133
134 5.1. secfilter.reload
135 5.2. secfilter.print
136 5.3. secfilter.stats
137 5.4. secfilter.stats_reset
138 5.5. secfilter.add_dst
139 5.6. secfilter.del_dst
140 5.7. secfilter.add_bl
141 5.8. secfilter.del_bl
142 5.9. secfilter.add_wl
143 5.10. secfilter.del_wl
144
145 6. Installation
146
147 6.1. Database setup
148
149 7. Some examples
150
151 7.1. Print data
152 7.2. Statistics
153
154 1. Overview
155
156 This module has been designed to offer an additional layer of security
157 over our communications. To achieve this, the following features are
158 available:
159
160 - Blacklist to block user agents, IP addresses, countries, domains and
161 users.
162
163 - Whitelist to allow user agents, IP addresses, countries, domains and
164 users.
165
166 - Blacklist of destinations where the called number is not allowed.
167
168 - SQL injection attacks prevention.
169
170 When a function is called, it will be searched in the whitelist. If the
171 value is not found, then the blacklist will be searched.
172
173 All data will be loaded into memory when the module is started. There
174 is an RPC reload command to update all the data from database. It is
175 also possible to add new data to the blacklist or whitelist using other
176 RPC commands.
177
178 2. Dependencies
179
180 2.1. Kamailio Modules
181 2.2. External Libraries or Applications
182
183 2.1. Kamailio Modules
184
185 The following modules must be loaded before this module:
186 * database -- Any db_* database module
187
188 2.2. External Libraries or Applications
189
190 The following libraries or applications must be installed before
191 running Kamailio with this module loaded:
192 * none
193
194 3. Parameters
195
196 3.1. db_url (string)
197 3.2. table_name (string)
198 3.3. action_col (string)
199 3.4. type_col (string)
200 3.5. data_col (string)
201 3.6. dst_exact_match (integer)
202 3.7. reload_delta (integer)
203 3.8. cleanup_interval (integer)
204
205 3.1. db_url (string)
206
207 Database URL.
208
209 Default value is ""
210
211 Example 1.1. Set db_url parameter
212 ...
213 modparam("secfilter", "db_url", "mysql://user:pass@localhost/kam
214 ailio")
215 ...
216
217 3.2. table_name (string)
218
219 Name of the table used to store the blacklisted and whitelisted values.
220
221 Default value is secfilter
222
223 Example 1.2. Set table_name parameter
224 ...
225 modparam("secfilter", "table_name", "secfilter")
226 ...
227
228 3.3. action_col (string)
229
230 Name of database column containing the type of list. The possible
231 values are:
232
233 * 0 = blacklisted data
234 * 1 = whitelisted data
235 * 2 = blacklisted destination number
236
237 Default value is action
238
239 Example 1.3. Set action_col parameter
240 ...
241 modparam("secfilter", "action_col", "action")
242 ...
243
244 3.4. type_col (string)
245
246 Name of database column containing the type of values. The possible
247 values are:
248
249 * 0 = user-agent (if action=0 or action=1)
250 * 0 = destination number (if action=2)
251 * 1 = country
252 * 2 = domain
253 * 3 = IP address
254 * 4 = user
255
256 Default value is type
257
258 Example 1.4. Set type_col parameter
259 ...
260 modparam("secfilter", "type_col", "type")
261 ...
262
263 3.5. data_col (string)
264
265 Name of database column containing blacklisted and whitelisted values.
266
267 Default value is data
268
269 Example 1.5. Set data_col parameter
270 ...
271 modparam("secfilter", "data_col", "data")
272 ...
273
274 3.6. dst_exact_match (integer)
275
276 This value is used in the destinations blacklist and corresponds to the
277 numbers that we want to prevent calling. If the value is 1, the call
278 will appear as blacklisted if the destination is exactly the same. If
279 the value is 0, every destination whose number begins with a number
280 appearing on the destination blacklist will be rejected.
281
282 Default value is 1
283
284 Example 1.6. Set dst_exact_match parameter
285 ...
286 modparam("secfilter", "dst_exact_match", 1)
287 ...
288
289 3.7. reload_delta (integer)
290
291 The number of seconds that have to be waited before executing a new RPC
292 reload. By default there is a rate limiting of maximum one reload in
293 five seconds. If set to 0, no rate limit is configured.
294
295 Default value is 5
296
297 Example 1.7. Set reload_delta parameter
298 ...
299 modparam("secfilter", "reload_delta", 1)
300 ...
301
302 3.8. cleanup_interval (integer)
303
304 The number of seconds that have to be wait before cleanup the previous
305 values from memory after a RPC reload.
306
307 Default value is 60
308
309 Example 1.8. Set cleanup_interval parameter
310 ...
311 modparam("secfilter", "cleanup_interval", 120)
312 ...
313
314 4. Functions
315
316 4.1. secf_check_ip ()
317 4.2. secf_check_ua ()
318 4.3. secf_check_country (string)
319 4.4. secf_check_from_hdr ()
320 4.5. secf_check_to_hdr ()
321 4.6. secf_check_contact_hdr ()
322 4.7. secf_check_dst (string)
323 4.8. secf_check_sqli_hdr (string)
324 4.9. secf_check_sqli_all ()
325
326 4.1. secf_check_ip ()
327
328 It checks if the source IP address is blacklisted. The search is
329 approximate and data stored in the database will be compared as a
330 prefix. For example, if we have blacklisted IP address 192.168.1. all
331 messages from IPs like 192.168.1.% will be rejected.
332
333 Return values are:
334 * 2 = the value is whitelisted
335 * 1 = the value is not found
336 * -2 = the value is blacklisted
337
338 Example 1.9. secf_check_ip usage
339 ...
340 secf_check_ip();
341 if ($? == -2) {
342 xlog("L_ALERT", "$rm from $si blocked because IP address is blac
343 klisted");
344 exit;
345 }
346 ...
347
348 4.2. secf_check_ua ()
349
350 It checks if the user-agent is blacklisted. The search is approximate
351 and the comparison will be made using the values of the database as a
352 prefix. If we add to the user-agent blacklist the word sipcli, every
353 message whose user-agent is named, for example, sipcli/1.6 or
354 sipcli/1.8 will be blocked. It is very useful to block different
355 versions of the same program.
356
357 Return values are:
358 * 2 = the value is whitelisted
359 * 1 = the value is not found
360 * -1 = error
361 * -2 = the value is blacklisted
362
363 Example 1.10. secf_check_ua usage
364 ...
365 secf_check_ua();
366 if ($? == -2) {
367 xlog("L_ALERT", "$rm from $si blocked because UserAgent '$ua' is
368 blacklisted");
369 exit;
370 }
371 ...
372
373 4.3. secf_check_country (string)
374
375 Similar to secf_check_ua. It checks if the country (IP address) is
376 blacklisted. Geoip module must be loaded to get the country code.
377
378 Return values are:
379 * 2 = the value is whitelisted
380 * 1 = the value is not found
381 * -1 = error
382 * -2 = the value is blacklisted
383
384 You can filter by blocking the IPs of messages coming from countries
385 that are on the blacklist. Alternatively, you can block everything that
386 is not explicitly on the whitelist.
387
388 Example 1.11. secf_check_country usage
389 ...
390 if (geoip2_match("$si", "src")) {
391 secf_check_country($gip2(src=>cc));
392 if ($? == -2) {
393 xdbg("$rm from $si blocked because Country '$gip2(src=>c
394 c)' is blacklisted");
395 exit;
396 }
397 }
398
399 ...
400
401 if (geoip2_match("$si", "src")) {
402 secf_check_country($gip2(src=>cc));
403 if ($? != 2) {
404 xdbg("$rm from $si blocked because Country '$gip2(src=>c
405 c)' is not whitelisted");
406 exit;
407 }
408 }
409 ...
410
411 4.4. secf_check_from_hdr ()
412
413 It checks if any value of from header is blacklisted. It checks if from
414 name or from user are in the users blacklist or whitelist. It also
415 checks if the from domain is in the domains blacklist or whitelist. The
416 blacklisted value will be used as a prefix and if we block, for
417 example, the user sipvicious, all users whose name starts with this
418 word will be considered as blacklisted.
419
420 Return values are:
421 * 4 = from name is whitelisted
422 * 3 = from domain is whitelisted
423 * 2 = from user is whitelisted
424 * 1 = from header not found
425 * -1 = error
426 * -2 = from user is blacklisted
427 * -3 = from domain is blacklisted
428 * -4 = from name is blacklisted
429
430 Example 1.12. secf_check_from_hdr usage
431 ...
432 secf_check_from_hdr();
433 switch ($?) {
434 case -2:
435 xlog("L_ALERT", "$rm to $si blocked because From user '$
436 fU' is blacklisted");
437 exit;
438 case -3:
439 xlog("L_ALERT", "$rm to $si blocked because From domain
440 '$fd' is blacklisted");
441 exit;
442 case -4:
443 xlog("L_ALERT", "$rm to $si blocked because From name '$
444 fn' is blacklisted");
445 exit;
446 };
447 ...
448
449 4.5. secf_check_to_hdr ()
450
451 Do the same as secf_check_from_hdr function but with the to header.
452
453 Return values are:
454 * 4 = to name is whitelisted
455 * 3 = to domain is whitelisted
456 * 2 = to user is whitelisted
457 * 1 = to header not found
458 * -1 = error
459 * -2 = to user is blacklisted
460 * -3 = to domain is blacklisted
461 * -4 = to name is blacklisted
462
463 Example 1.13. secf_check_to_hdr usage
464 ...
465 secf_check_to_hdr();
466 switch ($?) {
467 case -2:
468 xlog("L_ALERT", "$rm to $si blocked because To user '$tU
469 ' is blacklisted");
470 exit;
471 case -3:
472 xlog("L_ALERT", "$rm to $si blocked because To domain '$
473 td' is blacklisted");
474 exit;
475 case -4:
476 xlog("L_ALERT", "$rm to $si blocked because To name '$tn
477 ' is blacklisted");
478 exit;
479 };
480 ...
481
482 4.6. secf_check_contact_hdr ()
483
484 Do the same as secf_check_from_hdr function but with the contact
485 header.
486
487 Return values are:
488 * 3 = contact domain is whitelisted
489 * 2 = contact user is whitelisted
490 * 1 = contact header not found
491 * -1 = error
492 * -2 = contact user is blacklisted
493 * -3 = contact domain is blacklisted
494
495 Example 1.14. secf_check_contact_hdr usage
496 ...
497 secf_check_contact_hdr();
498 switch ($?) {
499 case -2:
500 xlog("L_ALERT", "$rm to $si blocked because Contact user
501 '$ct' is blacklisted");
502 exit;
503 case -3:
504 xlog("L_ALERT", "$rm to $si blocked because Contact doma
505 in '$ct' is blacklisted");
506 exit;
507 };
508 ...
509
510 4.7. secf_check_dst (string)
511
512 It checks if the destination number is blacklisted. It must be user for
513 INVITE messages. If the value of dst_exact_match is 1, the call will
514 appear as blacklisted if the destination is exactly the same. If the
515 value is 0, every destination whose number begins with a number
516 appearing on the destination blacklist will be rejected.
517
518 Return values are:
519 * 2 (if the value is whitelisted)
520 * 1 (if the value not found)
521 * -2 (if the value is blacklisted)
522
523 Example 1.15. secf_check_dst usage
524 ...
525 if (is_method("INVITE")) {
526 secf_check_dst($rU);
527 if ($? == -2) {
528 xlog("L_ALERT", "$rm from $si blocked because de
529 stination $rU is blacklisted");
530 send_reply("403", "Forbidden");
531 exit;
532 }
533 }
534 ...
535
536 4.8. secf_check_sqli_hdr (string)
537
538 Search for illegal characters in the given value.
539
540 Example 1.16. secf_check_sqli_hdr usage
541 ...
542 secf_check_sqli_hdr($ua);
543 if ($? == -1) {
544 xlog("L_ALERT", "$rm from $si blocked because possible SQLi foun
545 d in the user-agent header ($ua)");
546 exit;
547 }
548
549 ...
550
551 4.9. secf_check_sqli_all ()
552
553 Search for illegal characters in several headers (user-agent, from, to
554 and contact). If illegal characters are found the message will be
555 dropped.
556
557 Example 1.17. secf_check_sqli_all usage
558 ...
559 secf_check_sqli_all();
560 ...
561
562 5. RPC commands
563
564 5.1. secfilter.reload
565 5.2. secfilter.print
566 5.3. secfilter.stats
567 5.4. secfilter.stats_reset
568 5.5. secfilter.add_dst
569 5.6. secfilter.del_dst
570 5.7. secfilter.add_bl
571 5.8. secfilter.del_bl
572 5.9. secfilter.add_wl
573 5.10. secfilter.del_wl
574
575 5.1. secfilter.reload
576
577 Reload all blacklisted and whitelisted values from database.
578
579 Example 1.18. secfilter.reload usage
580 ...
581 kamctl rpc secfilter.reload
582 ...
583
584 5.2. secfilter.print
585
586 Print blacklisted and whitelisted values. Without parameters it will
587 print all values. If you enter a type it will print this type values
588 only.
589
590 Possible values are:
591 * (none) (show all data)
592 * ua (show blacklisted and whitelisted user-agents)
593 * country (show blacklisted and whitelisted countries)
594 * domain (show blacklisted and whitelisted domains)
595 * user (show blacklisted and whitelisted users)
596 * ip (show blacklisted and whitelisted IP addresses)
597 * dst (show blacklisted destinations)
598
599 Example 1.19. secfilter.print usage
600 ...
601 kamctl rpc secfilter.print
602 kamctl rpc secfilter.print ua
603 kamctl rpc secfilter.print country
604 kamctl rpc secfilter.print dst
605 ...
606
607 5.3. secfilter.stats
608
609 Print statistics of blocked and allowed messages.
610
611 Example 1.20. secfilter.stats usage
612 ...
613 kamctl rpc secfilter.stats
614 ...
615
616 5.4. secfilter.stats_reset
617
618 Reset all statistics.
619
620 Example 1.21. secfilter.stats_reset usage
621 ...
622 kamctl rpc secfilter.stats_reset
623 ...
624
625 5.5. secfilter.add_dst
626
627 Insert values into destination blacklist. These values will be checked
628 with the function secf_check_dst to verify if the destination number
629 can be called.
630
631 Parameters:
632 * number (number to add to the destination blacklist)
633
634 Example 1.22. secfilter.add_dst usage
635 ...
636 kamctl rpc secfilter.add_dst 555123123
637 ...
638
639 5.6. secfilter.del_dst
640
641 Delete value from destination blacklist.
642
643 Parameters:
644 * number (number to remove from the destination blacklist)
645
646 Example 1.23. secfilter.del_dst usage
647 ...
648 kamctl rpc secfilter.del_dst 555123123
649 ...
650
651 5.7. secfilter.add_bl
652
653 Insert values into blacklist.
654
655 Parameters:
656 * type (must be: ua, country, domain, user or ip)
657 * value (value to add to the blacklist)
658
659 Example 1.24. secfilter.add_bl usage
660 ...
661 kamctl rpc secfilter.add_bl ua friendly-scanner
662 kamctl rpc secfilter.add_bl user sipvicious
663 ...
664
665 5.8. secfilter.del_bl
666
667 Delete value from blacklist.
668
669 Parameters:
670 * type (must be: ua, country, domain, user or ip)
671 * value (value to remove from the blacklist)
672
673 Example 1.25. secfilter.del_bl usage
674 ...
675 kamctl rpc secfilter.del_bl ua friendly-scanner
676 kamctl rpc secfilter.del_bl user sipvicious
677 ...
678
679 5.9. secfilter.add_wl
680
681 Insert values into whitelist.
682
683 Parameters:
684 * type (must be: ua, country, domain, user or ip)
685 * value (value to add to the whitelist)
686
687 Example 1.26. secfilter.add_wl usage
688 ...
689 kamctl rpc secfilter.add_wl country es
690 kamctl rpc secfilter.add_wl user trusted_user
691 ...
692
693 5.10. secfilter.del_wl
694
695 Delete value from whitelist.
696
697 Parameters:
698 * type (must be: ua, country, domain, user or ip)
699 * value (value to remove from the blacklist)
700
701 Example 1.27. secfilter.del_wl usage
702 ...
703 kamctl rpc secfilter.del_wl country es
704 kamctl rpc secfilter.del_wl user trusted_user
705 ...
706
707 6. Installation
708
709 6.1. Database setup
710
711 6.1. Database setup
712
713 Before running Kamailio with the secfilter module, it is necessary to
714 set up the database table where the module will read the blacklist data
715 from. In order to do that, if the table was not created by the
716 installation script or you choose to install everything by yourself you
717 can use the secfilter-create.sql SQL script in the database directories
718 in the kamailio/scripts folder as a template. Database and table name
719 can be set with module parameters so they can be changed, but the name
720 of the columns must match the ones in the SQL script. You can also find
721 the complete database documentation on the project webpage,
722 https://www.kamailio.org/docs/db-tables/kamailio-db-devel.html.
723
724 Example 1.28. Example database content - secfilter table
725 ...
726 +----+-----------+-----------+------------------+
727 | id | action | type | data |
728 +----+-----------+-----------+------------------+
729 | 1 | 0 | 2 | 1.1.1.1 |
730 | 2 | 0 | 0 | friendly-scanner |
731 | 3 | 0 | 0 | pplsip |
732 | 4 | 0 | 0 | sipcli |
733 | 5 | 0 | 4 | sipvicious |
734 | 6 | 0 | 1 | ps |
735 | 7 | 0 | 3 | 5.56.57.58 |
736 | 8 | 1 | 0 | asterisk pbx |
737 | 9 | 1 | 2 | sip.mydomain.com |
738 | 10 | 2 | 0 | 555123123 |
739 | 11 | 2 | 0 | 555998776 |
740 +----+-----------+-----------+------------------+
741 ...
742
743 Action values are:
744 * 0 (blacklist)
745 * 1 (whitelist)
746 * 2 (destination)
747
748 Type values are:
749 * 0 (user-agent)
750 * 1 (country)
751 * 2 (domain)
752 * 3 (IP address)
753 * 4 (user)
754
755 7. Some examples
756
757 7.1. Print data
758 7.2. Statistics
759
760 7.1. Print data
761
762 Example 1.29. kamctl rpc secfilter.print ua
763 ...
764 {
765 User-Agent: {
766 Blacklisted: {
767 Value: friendly-scanner
768 Value: pplsip
769 Value: sipcli
770 Value: sundayddr
771 Value: iWar
772 Value: sipsak
773 Value: VaxSIPUserAgent
774 Value: SimpleSIP
775 Value: SIP Call
776 Value: Ozeki
777 Value: VoIPSec
778 Value: SIPScan
779 Value: Conaito
780 Value: UsaAirport
781 Value: PortSIP VoIP SDK
782 Value: zxcvfdf11
783 Value: fdgddfg546df4g8d5f
784 Value: siptest
785 Value: Nmap NSE
786 }
787 Whitelisted: {
788 Value: my custom ua
789 }
790 }
791 }
792 ...
793
794 7.2. Statistics
795
796 Example 1.30. kamctl rpc secfilter.stats
797 ...
798 {
799 Blacklist: {
800 User-Agent: 1256
801 Country: 45
802 From-Domain: 0
803 To-Domain: 0
804 Contact-Domain: 1
805 IP-Address: 2552
806 From-Name: 0
807 To-Name: 0
808 Contact-Name: 0
809 From-User: 316
810 To-User: 0134
811 Contact-User: 0
812 }
813 Whitelist: {
814 User-Agent: 0
815 Country: 478
816 From-Domain: 0
817 To-Domain: 0
818 Contact-Domain: 0
819 IP-Address: 0
820 From-Name: 0
821 To-Name: 0
822 Contact-Name: 0
823 From-User: 0
824 To-User: 0
825 Contact-User: 0
826 }
827 Other: {
828 Destination: 0
829 SQL-Injection: 213
830 }
831 }
832 ...