Metadata-Version: 2.5
Name: lupaxa-dns-blacklist-checker
Version: 0.1.4
Summary: Check whether an IPv4 address or hostname is listed on DNS blacklists.
Project-URL: Homepage, https://github.com/lupaxa-sre-toolbox/dns-blacklist-checker
Project-URL: Repository, https://github.com/lupaxa-sre-toolbox/dns-blacklist-checker
Project-URL: Issues, https://github.com/lupaxa-sre-toolbox/dns-blacklist-checker/issues
Author: The Lupaxa Project
License: MIT License
        
        Copyright (c) The Lupaxa Project
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENCE
Keywords: blacklist,cli,dns,dnsbl,rbl,sre
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Console
Classifier: Intended Audience :: System Administrators
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Internet
Classifier: Topic :: System :: Networking
Classifier: Topic :: System :: Systems Administration
Classifier: Topic :: Utilities
Requires-Python: >=3.13
Requires-Dist: dnspython>=2.6
Provides-Extra: dev
Requires-Dist: bump-my-version>=1.2.0; extra == 'dev'
Requires-Dist: hatch>=1.12.0; extra == 'dev'
Requires-Dist: mypy>=1.12.0; extra == 'dev'
Requires-Dist: pip-audit>=2.7.0; extra == 'dev'
Requires-Dist: pytest-cov>=5.0; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Requires-Dist: ruff>=0.6.0; extra == 'dev'
Provides-Extra: test
Requires-Dist: pytest-cov>=5.0; extra == 'test'
Requires-Dist: pytest>=8.0; extra == 'test'
Description-Content-Type: text/markdown

<p align="center">
  <a href="https://github.com/lupaxa-sre-toolbox">
    <img src="https://raw.githubusercontent.com/the-lupaxa-project/brand-assets/master/logos/organisations/sre-toolbox/readme-logo.png" alt="SRE Toolbox" />
  </a>
</p>

<h1 align="center">DNS Blacklist Checker</h1>

Check whether an IP address or hostname is listed on DNS blacklists.

Each hostname is resolved to every IPv4 and IPv6 address. Each address is
reversed and queried against the built-in DNSBL zones in parallel. A loopback
answer is a listing, and the line includes the return code and TXT reason when
the zone provides them. Spamhaus `127.255.255.0/24` answers and Abusix
`127.0.0.11` / `127.0.0.12` are notes, not listings. A zone timeout is not a
listing.

## Install

Requires Python 3.13 or newer. `dnspython` is installed with the package.

```bash
pip install lupaxa-dns-blacklist-checker
dns-blacklist-checker --help
```

## CLI

`dns-blacklist-checker` is the command. `dnsbl` is an alias for that same
command: same flags, same output. `python -m lupaxa.dns_blacklist_checker`
runs that same program. Examples use the full command name.

```bash
dns-blacklist-checker 192.0.2.1 example.com
dns-blacklist-checker 2001:db8::1
dns-blacklist-checker 203.0.113.9 --zone zen.spamhaus.org --timeout 2
dns-blacklist-checker example.com --mx
dns-blacklist-checker 203.0.113.9 --spamhaus-key "$SPAMHAUS_DQS_KEY"
```

| Flag              | Default              | Description                                      |
| :---------------- | :------------------- | :----------------------------------------------- |
| `TARGET`          | required, repeatable | IPv4 address, IPv6 address, or hostname          |
| `--timeout`, `-t` | `5`                  | Per-zone DNS timeout in seconds                  |
| `--zone`, `-z`    | built-in zone list   | DNSBL zone; repeat to replace the built-in list  |
| `--mx`            | off                  | Check every address of each mail exchanger       |
| `--spamhaus-key`  | `SPAMHAUS_DQS_KEY`   | Spamhaus Data Query Service key                  |
| `--version`       | —                    | Print the package version and exit               |

`--timeout` must be greater than `0`. It bounds each zone query, and hostname
and MX lookups.

`--zone` replaces the built-in list. Repeat the flag for each zone. Whitespace
and a trailing dot are ignored, and duplicate names are queried once.

`--mx` requires a domain. Exchangers are ordered by preference, and every
address of each one is checked. An IP address with `--mx` is an error.

`--spamhaus-key` rewrites Spamhaus zones to `<key>.<zone>.dq.spamhaus.net`.
The printed zone name stays the public name. When the flag is omitted, the
command reads `SPAMHAUS_DQS_KEY`.

### Exit Codes

| Code | When                                                             |
| :--- | :--------------------------------------------------------------- |
| `0`  | Help, version, or every address resolved and none were listed    |
| `1`  | At least one address is listed, or library input was invalid     |
| `2`  | Usage failed, a target could not be resolved, or no zone applied |

When one target is listed and another cannot be resolved, the command exits
`1`.

## Output

Each address is one line.

```text
192.0.2.1 (192.0.2.1) is not blacklisted.
203.0.113.9 (203.0.113.9) is blacklisted on: zen.spamhaus.org [127.0.0.2] SBL123
example.com (192.0.2.10) is not blacklisted.
example.com (2001:db8::10) is not blacklisted.
example.com mx=mail.example (192.0.2.10) is not blacklisted.
missing.example error: name resolution failed
203.0.113.9 (203.0.113.9) is not blacklisted. (zen.spamhaus.org: query via public resolver)
192.0.2.1 (192.0.2.1) is not blacklisted. (1 zone query failed)
2001:db8::1 (2001:db8::1) error: no zones support IPv6
```

IPv6 queries use nibble names. Built-in zones that publish IPv4 data only are
skipped for IPv6. A zone passed with `--zone` is queried for both families.
If none of the selected zones apply, that address is an error.

## Zones

The built-in list starts with `zen.spamhaus.org` and
`combined.mail.abusix.zone`. It does not include SORBS, the separate Spamhaus
SBL, XBL, and PBL zones, CBL, or UCEPROTECT levels 2 and 3.
`b.barracudacentral.org` answers after the querying address is registered
with Barracuda.

## Library

```python
from lupaxa.dns_blacklist_checker import check

result = check("203.0.113.9", zones=("zen.spamhaus.org",), timeout=5.0)
if result.error:
    print(result.query, result.error)
else:
    for item in result.addresses:
        print(item.address, item.listed)
        for zone in item.zones:
            if zone.listed:
                print(zone.zone, zone.return_codes, zone.reason)
```

`check` returns a result when a name does not resolve. It raises `ValueError`
for an empty target, a non-positive timeout, or a zone list that is empty
after normalisation. `listed` is true when any address was listed.
`check_mail_exchangers` checks each MX host. `dnsbl_qname` builds the
reversed query name.

## Development

```bash
make init
make python-install-dev
make python-check
```

<a href="https://github.com/the-lupaxa-project">
    <img src="https://raw.githubusercontent.com/the-lupaxa-project/brand-assets/master/logos/components/footer-for-child-orgs.svg" alt="The Lupaxa Project Footer" width="100%" />
</a>
