#!/usr/bin/perl
use strict;
use warnings;
# version 20120426
#
# Copyright (C) 2012  Glen Pitt-Pladdy
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
#
# See: http://www.pitt-pladdy.com/blog/_20120427-075628_0100_Are_you_blacklisted_DNSRBL_checker_script_/


# hosts to check - replace these with yours
my @IP = (
	'127.0.0.1',
	'::1',
);

# blacklists to use
my @BLACKLISTS = (
	# from http://www.anti-abuse.org/multi-rbl-check/ - most complete list
	'cbl.abuseat.org',
	'b.barracudacentral.org',
	'dnsbl.invaluement.com',
	'dnsbl.sorbs.net',
	'http.dnsbl.sorbs.net',
	'dul.dnsbl.sorbs.net',
	'misc.dnsbl.sorbs.net',
	'smtp.dnsbl.sorbs.net',
	'socks.dnsbl.sorbs.net',
	'spam.dnsbl.sorbs.net',
	'web.dnsbl.sorbs.net',
	'zombie.dnsbl.sorbs.net',
	'dnsbl-1.uceprotect.net',
	'dnsbl-2.uceprotect.net',
	'dnsbl-3.uceprotect.net',
	'pbl.spamhaus.org',
	'sbl.spamhaus.org',
	'xbl.spamhaus.org',
	'zen.spamhaus.org',
	'bl.spamcannibal.org',
	'psbl.surriel.com',
	'ubl.unsubscore.com',
	'dnsbl.njabl.org',
	'combined.njabl.org',
	'rbl.spamlab.com',
	'dnsbl.ahbl.org',
	'ircbl.ahbl.org',
	'dyna.spamrats.com',
	'noptr.spamrats.com',
	'spam.spamrats.com',
	'cbl.anti-spam.org.cn',
	'cdl.anti-spam.org.cn',
	'dnsbl.inps.de',
	'drone.abuse.ch',
	'httpbl.abuse.ch',
	'dul.ru',
	'korea.services.net',
	'short.rbl.jp',
	'virus.rbl.jp',
	'spamrbl.imp.ch',
	'wormrbl.imp.ch',
	'virbl.bit.nl',
	'rbl.suresupport.com',
	'dsn.rfc-ignorant.org',
	'ips.backscatterer.org',
	'spamguard.leadmon.net',
	'opm.tornevall.org',
	'netblock.pedantic.org',
	'multi.surbl.org',
	'ix.dnsbl.manitu.net',
	'tor.dan.me.uk',
	'rbl.efnetrbl.org',
	'relays.mail-abuse.org',
	'blackholes.mail-abuse.org',
	'rbl-plus.mail-abuse.org',
	'dnsbl.dronebl.org',
	'access.redhawk.org',
	'db.wpbl.info',
	'rbl.interserver.net',
	'query.senderbase.org',
	'bogons.cymru.com',
	# others
	'dun.dnsrbl.net',
	'bl.spamcop.net',
);


use Net::IP;
use Socket;

# walk the lists
foreach my $ip (@IP) {
	# prep ip for lookup (dice up and reverse)
	my $lookuphost;
	if ( $ip =~ /:/ and $ip !~ /\./ ) {
		# IPv6
		my $expandedip = new Net::IP ( $ip );
		$lookuphost = join '.', reverse ( split '', $expandedip->ip() );
		$lookuphost =~ s/\.:\././g;
	} elsif ( $ip =~ /\./ and $ip !~ /:/ ) {
		$lookuphost = join '.', reverse ( split /\./, $ip );
	} else {
		die "FATAL - invalid host \"$ip\"\n";
	}
	# lookup from blacklists
	foreach my $blacklist (@BLACKLISTS) {
		my $lookup = "$lookuphost.$blacklist";
		my $result = gethostbyname ( $lookup );
		if ( ! defined $result ) { next; }
		$result = inet_ntoa ( $result );
		print "$ip ($lookup) => $result\n";
	}
}
