Jun 072009

Ever notice how the public RBL databases aren’t enough? spamcop and spamhaus are great, but there are spammers still getting through. Did you ever want to do it yourself?

This procedure explains how to run your own RBL DNS Blacklist. It uses a mysql table to store the IP address you want to blacklist and whitelist. Based on this data, it rebuilds a flatfile that the dns server uses on a regular basis. I prefer every 5 minutes. I run it on a Blue Quartz server which is CentOS Linux (Red Hat EL4) based. You will need a local mysql server.
Step 1:Download the RBL DNS Daemon

We use rbldnsd from here
Download the rbldns server:
RHEL 4 / CentOS 4 rbldnsd RPM ver. 0.995
RHEL 5 / CentOS 5 rbldnsd RPM ver 0.995
Source rbldnsd RPM ver 0.996b
Step 2: Turn off any existing DNS server

Make sure you are not already running a DNS server on this machine. Turn off “named” if its on.

service named stop

Step 3:Install the RPM

useradd rbldns
rpm -Uvh rbldnsd*.rpm

Step 4: Create a mysql table
Make sure the MySQL server is running.

`ipaddress` varchar(15) NOT NULL default ”,
`dateadded` datetime NOT NULL default ‘0000-00-00 00:00:00’,
`reportedby` varchar(40) default NULL,
`updated` datetime default NULL,
`attacknotes` text,
`b_or_w` char(1) NOT NULL default ‘b’,
PRIMARY KEY (`ipaddress`),
KEY `dateadded` (`dateadded`),
KEY `b_or_w` (`b_or_w`)

You may want to create a mysql user just for this purpose with limited permissions.
Step 5: Download the perl script that rebuilds the flat file from a mysql database
rebuild_rbldns.pl script
Put this script in /usr/local/bin

wget -O /usr/local/bin/rebuild_rbldns.pl http://www.blue-quartz.com/rbl/rebuild_rbldns.txt
chmod 750 /usr/local/bin/rebuild_rbldns.pl

You will want to put this in the root cron and run it every 5 minutes

crontab -e
*/5 * * * * /usr/local/bin/rebuild_rbldns.pl

Please edit lines 25-27 of this perl script to change your mysql user and password.
Step 6: Edit the /etc/sysconfig/rbldnsd config file

# My boot rbldnsd options
# —————————————–
# TTL 35m, check files every 60s for changes, -f = smooth reloads
# -l logfilepath
# Please change to your real public IP that you want the dns daemon to listen on
# Please change mydomain.com to your real domain name.
RBLDNSD=”dsbl -l /var/lib/rbldns/log/rbl.log -f -r/var/lib/rbldns/dsbl -b \
rbl.mydomain.com:ip4set:spammerlist,whitelist \

Step 7: Create directory structure for flat file

mkdir /var/lib/rbldns/dsbl
touch /var/lib/rbldns/dsbl/forward
touch /var/lib/rbldns/dsbl/spammerlist
touch /var/lib/rbldns/dsbl/whitelist
touch /var/lib/rbldns/dsbl/rbl.log
chown -R rbldns:rbldns dsbl

Step 8: Add some records to the MySQL database you have of known spammers

attacknotes=’dictionary attack from badboy.com’,

To help in diagnosing problems, add these entries in the “/var/lib/rbldns/dsbl/forward” file:

@ A
test A

And please replace with the ip address of your rbl server.

Step 9: Run the script to build the flat file
/usr/local/bin/rebuild_rbldns.pl and if you want to see if it actually created the file type this:

cat /var/lib/rbldns/dsbl/spammerlist

Step 10: Start the rbldns service

service rbldnsd start

Step 11: Create a DNS subdomain zone for rbl.mydomain.com
You must create a DNS zone (subdomain) in your main DNS server for rbl.mydomain.com and point it to your rbldnsd server.

; subdomain delegation
rbl.mydomain.com. in ns rbl.mydomain.com.
rbl.mydomain.com. in a

Step 12: test rbl.mydomain.com lookups
If a blacklisted IP address is in your rbl database it will “exist” in the DNS system.

For example:

if you blacklisted IP
then doing a regular DNS lookup like this:

nslookup test.rbl.mydomain.com

should result in a match of

nslookup test.rbl.mydomain.com

should result in a match for (your public ip address of your rbl server). If this works then the file /var/lib/rbldns/dsbl/forward is working.

Every entry in your RBL database will return a match of

If an IP address is not in your RBL database it will fail to find an entry. This is how mail servers know how to block relays of email from known spammers.
Step 13: Having Your Mail Servers Use This RBL database
If you are using sendmail, and want it to use this database, do this:

cd /etc/mail
vi sendmail.mc

add this line right below the “blacklist_recipients” line:

FEATURE(dnsbl, `rbl.mydomain.com’, `Rejected – known spammer’)dnl

Now sendmail will reject messages from bad IP addresses in your database. You can monitor your /var/log/maillog file to see if sendmail really did block a specific IP.
Step 14: Filling your database with known spammers
Now you need to decide how you are going to add records to your MySQL table. I suggest you write a script that monitors mailboxes or mail server logs. This is a great way to discover those spammers that are getting through the system.

I also wrote some PHP web pages with forms to allow me to quickly add IP’s to my blacklist. You might want to try that.

In my dictionary attack monitoring scripts, I use this command to update the rbl database:

wget -q -O /dev/null ‘http://rbl.domain.com/drop.php?ipaddress= attack’

This way all my servers can add to the database. Of course, only approved IPs in my network are allowed to submit rbl data. I ignore all others.

 Posted by at 8:42 am