1*5f4c09ddSEd Maste# $NetBSD: README,v 1.8 2017/04/13 17:59:34 christos Exp $ 2*5f4c09ddSEd Maste 3*5f4c09ddSEd MasteThis package contains library that can be used by network daemons to 4*5f4c09ddSEd Mastecommunicate with a packet filter via a daemon to enforce opening and 5*5f4c09ddSEd Masteclosing ports dynamically based on policy. 6*5f4c09ddSEd Maste 7*5f4c09ddSEd MasteThe interface to the packet filter is in libexec/blacklistd-helper 8*5f4c09ddSEd Maste(this is currently designed for npf) and the configuration file 9*5f4c09ddSEd Maste(inspired from inetd.conf) is in etc/blacklistd.conf. 10*5f4c09ddSEd Maste 11*5f4c09ddSEd MasteOn NetBSD you can find an example npf.conf and blacklistd.conf in 12*5f4c09ddSEd Maste/usr/share/examples/blacklistd; you need to adjust the interface 13*5f4c09ddSEd Mastein npf.conf and copy both files to /etc; then you just enable 14*5f4c09ddSEd Masteblacklistd=YES in /etc/rc.conf, start it up, and you are all set. 15*5f4c09ddSEd Maste 16*5f4c09ddSEd MasteThere is also a startup file in etc/rc.d/blacklistd 17*5f4c09ddSEd Maste 18*5f4c09ddSEd MastePatches to various daemons to add blacklisting capabilitiers are in the 19*5f4c09ddSEd Maste"diff" directory: 20*5f4c09ddSEd Maste - OpenSSH: diff/ssh.diff [tcp socket example] 21*5f4c09ddSEd Maste - Bind: diff/named.diff [both tcp and udp] 22*5f4c09ddSEd Maste - ftpd: diff/ftpd.diff [tcp] 23*5f4c09ddSEd Maste 24*5f4c09ddSEd MasteThese patches have been applied to NetBSD-current. 25*5f4c09ddSEd Maste 26*5f4c09ddSEd MasteThe network daemon (for example sshd) communicates to blacklistd, via 27*5f4c09ddSEd Mastea unix socket like syslog. The library calls are simple and everything 28*5f4c09ddSEd Masteis handled by the library. In the simplest form the only thing the 29*5f4c09ddSEd Mastedaemon needs to do is to call: 30*5f4c09ddSEd Maste 31*5f4c09ddSEd Maste blacklist(action, acceptedfd, message); 32*5f4c09ddSEd Maste 33*5f4c09ddSEd MasteWhere: 34*5f4c09ddSEd Maste action = 0 -> successful login clear blacklist state 35*5f4c09ddSEd Maste 1 -> failed login, add to the failed count 36*5f4c09ddSEd Maste acceptedfd -> the file descriptor where the server is 37*5f4c09ddSEd Maste connected to the remote client. It is used 38*5f4c09ddSEd Maste to determine the listening socket, and the 39*5f4c09ddSEd Maste remote address. This allows any program to 40*5f4c09ddSEd Maste contact the blacklist daemon, since the verification 41*5f4c09ddSEd Maste if the program has access to the listening 42*5f4c09ddSEd Maste socket is done by virtue that the port 43*5f4c09ddSEd Maste number is retrieved from the kernel. 44*5f4c09ddSEd Maste message -> an optional string that is used in debugging logs. 45*5f4c09ddSEd Maste 46*5f4c09ddSEd MasteUnfortunately there is no way to get information about the "peer" 47*5f4c09ddSEd Mastefrom a udp socket, because there is no connection and that information 48*5f4c09ddSEd Masteis kept with the server. In that case the daemon can provide the 49*5f4c09ddSEd Mastepeer information to blacklistd via: 50*5f4c09ddSEd Maste 51*5f4c09ddSEd Maste blacklist_sa(action, acceptedfd, sockaddr, sockaddr_len, message); 52*5f4c09ddSEd Maste 53*5f4c09ddSEd MasteThe configuration file contains entries of the form: 54*5f4c09ddSEd Maste 55*5f4c09ddSEd Maste# Blacklist rule 56*5f4c09ddSEd Maste# host/Port type protocol owner name nfail disable 57*5f4c09ddSEd Maste192.168.1.1:ssh stream tcp * -int 10 1m 58*5f4c09ddSEd Maste8.8.8.8:ssh stream tcp * -ext 6 60m 59*5f4c09ddSEd Mastessh stream tcp6 * * 6 60m 60*5f4c09ddSEd Mastehttp stream tcp * * 6 60m 61*5f4c09ddSEd Maste 62*5f4c09ddSEd MasteHere note that owner is * because the connection is done from the 63*5f4c09ddSEd Mastechild ssh socket which runs with user privs. We treat ipv4 connections 64*5f4c09ddSEd Mastedifferently by maintaining two different rules one for the external 65*5f4c09ddSEd Masteinterface and one from the internal We also register for both tcp 66*5f4c09ddSEd Masteand tcp6 since those are different listening sockets and addresses; 67*5f4c09ddSEd Mastewe don't bother with ipv6 and separate rules. We use nfail = 6, 68*5f4c09ddSEd Mastebecause ssh allows 3 password attempts per connection, and this 69*5f4c09ddSEd Mastewill let us have 2 connections before blocking. Finally we block 70*5f4c09ddSEd Mastefor an hour; we could block forever too by specifying * in the 71*5f4c09ddSEd Masteduration column. 72*5f4c09ddSEd Maste 73*5f4c09ddSEd Masteblacklistd and the library use syslog(3) to report errors. The 74*5f4c09ddSEd Masteblacklist filter state is persisted automatically in /var/db/blacklistd.db 75*5f4c09ddSEd Masteso that if the daemon is restarted, it remembers what connections 76*5f4c09ddSEd Masteis currently handling. To start from a fresh state (if you restart 77*5f4c09ddSEd Mastenpf too for example), you can use -f. To watch the daemon at work, 78*5f4c09ddSEd Masteyou can use -d. 79*5f4c09ddSEd Maste 80*5f4c09ddSEd MasteThe current control file is designed for npf, and it uses the 81*5f4c09ddSEd Mastedynamic rule feature. You need to create a dynamic rule in your 82*5f4c09ddSEd Maste/etc/npf.conf on the group referring to the interface you want to block 83*5f4c09ddSEd Mastecalled blacklistd as follows: 84*5f4c09ddSEd Maste 85*5f4c09ddSEd Masteext_if=bge0 86*5f4c09ddSEd Masteint_if=sk0 87*5f4c09ddSEd Maste 88*5f4c09ddSEd Mastegroup "external" on $ext_if { 89*5f4c09ddSEd Maste ... 90*5f4c09ddSEd Maste ruleset "blacklistd-ext" 91*5f4c09ddSEd Maste ruleset "blacklistd" 92*5f4c09ddSEd Maste ... 93*5f4c09ddSEd Maste} 94*5f4c09ddSEd Maste 95*5f4c09ddSEd Mastegroup "internal" on $int_if { 96*5f4c09ddSEd Maste ... 97*5f4c09ddSEd Maste ruleset "blacklistd-int" 98*5f4c09ddSEd Maste ... 99*5f4c09ddSEd Maste} 100*5f4c09ddSEd Maste 101*5f4c09ddSEd MasteYou can use 'blacklistctl dump -a' to list all the current entries 102*5f4c09ddSEd Mastein the database; the ones that have nfail <c>/<t> where <c>urrent 103*5f4c09ddSEd Maste>= <t>otal, should have an id assosiated with them; this means that 104*5f4c09ddSEd Mastethere is a packet filter rule added for that entry. For npf, you 105*5f4c09ddSEd Mastecan examine the packet filter dynamic rule entries using 'npfctl 106*5f4c09ddSEd Masterule <rulename> list'. The number of current entries can exceed 107*5f4c09ddSEd Mastethe total. This happens because entering packet filter rules is 108*5f4c09ddSEd Masteasynchronous; there could be other connection before the rule 109*5f4c09ddSEd Mastebecomes activated. 110*5f4c09ddSEd Maste 111*5f4c09ddSEd MasteEnjoy, 112*5f4c09ddSEd Maste 113*5f4c09ddSEd Mastechristos 114