The answer to the question is yes, you can do you own blacklist checker automation, all your need to figure out is the frequency of these checks.
The script will only output to the console, then we need another script to provide the HTML content file generation and call that:
If you adopt this method you have console only output script, and then another script that has a pretty management, friendly overview of the technical status of your monitored endpoints.
That means when the scripts are run you have a way to monitor your blacklist status, but this does not offer any automation,
Unlike many Blacklist check websites that will check your listing every 15 minutes and notify you the moment you’re on one of these blacklists……
If you review both the scripts, you now have neither of these are fit for automation, The first script only outputs to a console, and the second will indeed generate to HTML file which theoretically could be emailed……
The downside here is if you set the schedule for every 15 minutes you’re going to get an email every 15 minutes with a report telling you “you’re not on a blacklist” - leave that running for a couple of hours, and you will be completely desensitized to any kind of alert that comes from this inbox
# Define the list of hostnames and IP addresses to check
$hostList = @(
"<host>",
"<ip1>",
"<ip2>"
)
# Define the list of blacklist servers to check against
$blacklists = @(
"b.barracudacentral.org",
"bl.spamcop.net",
"blacklist.woody.ch",
"cbl.abuseat.org",
"dnsbl.sorbs.net",
"zen.spamhaus.org"
)
# SMTP server settings
$smtpServer = "smtprelay.bear.local"
$fromAddress = "blacklist@croucher.cloud”
$toAddress = "lee@croucher.cloud"
$subject = "Alert : Blacklist Check Report"
# Function to check if an IP address is blacklisted
function Check-Blacklist {
param (
[string]$hostname,
[string]$reversedIp
)
$results = @()
foreach ($blacklist in $blacklists) {
try {
$lookup = "$($reversedIp).$blacklist"
$resolved = [System.Net.Dns]::GetHostAddresses($lookup)
if ($resolved) {
$results += [pscustomobject]@{
Hostname = $hostname
Blacklist = $blacklist
Status = "Listed"
}
}
} catch {
$results += [pscustomobject]@{
Hostname = $hostname
Blacklist = $blacklist
Status = "Not Listed"
}
}
}
return $results
}
# Flag to determine if any host is listed
$anyListed = $false
# HTML report initialization
$htmlReport = @"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blacklist Check Report</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
.logo {
display: block;
margin-left: auto;
margin-right: auto;
width: 150px;
}
table {
width: 50%;
border-collapse: collapse;
margin: 20px auto;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
.status-listed {
color: red;
font-weight: bold;
}
.status-not-listed {
color: green;
font-weight: bold;
}
.status-listed::before {
content: "❌ ";
}
.status-not-listed::before {
content: "✅ ";
}
</style>
</head>
<body>
<img src="logo.png" alt="Logo" class="logo">
<h1 style="text-align: center;">Blacklist Check Report</h1>
<table>
<thead>
<tr>
<th>Hostname</th>
<th>Blacklist</th>
<th>Status</th>
</tr>
</thead>
<tbody>
"@
# Process each host in the list
foreach ($hostname in $hostList) {
$ipAddresses = try {
[System.Net.Dns]::GetHostAddresses($hostname) | Where-Object { $_.AddressFamily -eq 'InterNetwork' }
} catch {
Write-Host "Could not resolve $hostname"
continue
}
if ($ipAddresses.Count -eq 0) {
Write-Host "No IPv4 addresses found for $hostname"
continue
}
$ipAddresses = $ipAddresses | ForEach-Object { $_.IPAddressToString }
foreach ($ipAddress in $ipAddresses) {
$reversedIp = ($ipAddress -split '\.')[-1..0] -join '.'
$results = Check-Blacklist -hostname $hostname -reversedIp $reversedIp
$results | Format-Table -AutoSize
foreach ($result in $results) {
$statusClass = if ($result.Status -eq "Listed") {
$anyListed = $true
"status-listed"
} else {
"status-not-listed"
}
$htmlReport += @"
<tr>
<td>$($result.Hostname)</td>
<td>$($result.Blacklist)</td>
<td class="$statusClass">$($result.Status)</td>
</tr>
"@
}
}
}
# Complete the HTML report if any host is listed
if ($anyListed) {
$htmlReport += @"
</tbody>
</table>
</body>
</html>
"@
# Output the HTML report to a file
$outputPath = "BlacklistCheckReport.html"
$htmlReport | Out-File -FilePath $outputPath -Encoding UTF8
# Send the email
$message = New-Object system.net.mail.mailmessage
$message.from = $fromAddress
$message.To.Add($toAddress)
$message.Subject = $subject
$message.Body = "Please find the attached blacklist check report."
$message.IsBodyHtml = $false
# Attach the HTML report
$attachment = New-Object system.net.mail.attachment($outputPath)
$message.Attachments.Add($attachment)
# Configure the SMTP client and send the email
$smtpClient = New-Object Net.Mail.SmtpClient($smtpServer)
$smtpClient.Send($message)
# Clean up
$message.Dispose()
$attachment.Dispose()
$smtpClient.Dispose()
Write-Host "Report generated and emailed to $toAddress"
} else {
Write-Host "No hosts were listed in any blacklists. No report generated."
}