简体   繁体   中英

How do I query via php spamhaus ip blacklist

Trying to query spamhaus.org for blacklisted ip. An example is 69.35.160.59 If I go to https://www.spamhaus.org/lookup/ and enter it, it shows on blacklist same with 112.198.83.17 , however the following code returns an empty array.

I print out the url and it looks correctly formatted in reverse ip 17.83.198.112.zen.spamhaus.org .

Any ideas?

<?php

    $ip = "112.198.83.17";
    $blacklist = "zen.spamhaus.org";
    $url = implode(".", array_reverse(explode(".", $ip))) . ".". $blacklist;
    echo "$url<br>";
    $record = dns_get_record($url);
    print_r ($record);

?>

You doing right. See here for result codes http://www.spamhaus.org/faq/section/DNSBL%20Usage#366

Here is my output for ip 69.35.160.59

Array (
    [0] => Array
        (
            [host] => 59.160.35.69.zen.spamhaus.org
            [type] => A
            [ip] => 127.0.0.4
            [class] => IN
            [ttl] => 900
        )

[1] => Array
    (
        [host] => 59.160.35.69.zen.spamhaus.org
        [type] => TXT
        [txt] => http://www.spamhaus.org/query/bl?ip=69.35.160.59
        [entries] => Array
            (
                [0] => http://www.spamhaus.org/query/bl?ip=69.35.160.59
            )

        [class] => IN
        [ttl] => 900
    )

Your code is correct. Just make sure you are not using public dns resolvers like 8.8.8.8 and 8.8.4.4 because the code will not work if you do so. You can check what resolvers you are using by viewing the file /etc/resolv.conf

I created a simple function that returns true if listed with SpamHaus. Any incoming IPs are checked and either allowed or blocked:

function callSpamhaus($UserIP) {
    // NON-INFECTED IP FOR TESTING
    // 192.99.150.120
    // INFECTED IP FOR TESTING
    // 216.145.14.142
    $blacklist = "zen.spamhaus.org";
    $url = implode(".", array_reverse(explode(".", $UserIP))) . ".". $blacklist;
    $record = dns_get_record($url);
    if (is_array($record) && !empty($record)) :
        return TRUE;
    endif;
}

A conditional is run upon each access to the site that runs the function, using whatever method you choose to fetch the IP and create a variable for it:

$SpamHaus = callSpamhaus($IPAddress);
if ($SpamHaus === TRUE) :
     Redirect("/administration/accessblocked.php");
endif;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM