[Updated Script to work with RC2 of powershell]
A couple of weeks ago I blogged this script that allowed DNS operations like MX , PTR and SPF queries from a powershell script based on a C# class from Peter Bromberg. Since then I’ve added some more functionality to the code to give the ability to look up DNS RBL lists. A lot of people use DNSBL’s as a way of fighting SPAM but every now and again you may find a legitimate server that has been blacklisted on one of the many lists around for some reason. Wikipedia has a great entry that describes what RBL’s are and how they work essentially they are just another DNS zone that you query using a normal DNS A record lookup. So for example say you want to look up the IP address 192.168.8.2 to see if this is listed in the RBL list from SpamHaus you basically need to first reverse the IP address bytes to 2.8.168.192 and append the name of the RBL list you want to search which in this case would be sbl.spamhaus.org. So you then do an A record lookup on 2.8.168.192. sbl.spamhaus.org. If the record is listed in the RBL then the server will return back if not you will get a normal no record response. So putting this in a script was fairly easy I already had some code that did the IP address reversal so it was just a matter of appending the RBL name you wanted to lookup and then adding some code to process the response. One of the little cool things I like about Powershell is being able to easily change the text color of the text you’re returning in a command window using the –foregroudcolor parameter with write-host.
Going a little bit further is that usually you will use more then one RBL list on your server or possibly you want to search all active RBL’s like those services provided at sites such as http://www.dnsstuff.com/ and http://www.robtex.com/rbls.html . To do this I’ve added in some code that will read a list of RBL’s from a text file and then run though and test a certain IP addresss against each RBL in the file. You can seed the text file using any of the URL’s I previously method by just cutting and pasting the list they use into a normal text file.
The last bit of functionality I added to the script was the STMP telnet test from my other post. So what I did was add an option where you can put in a domain name you want to test and the script will then go out and find all the MX records for that domain and then test each one to see if it will accept email for that domain.
To use these three functions run the code as follows
To Do a RBL check for example of sbl.spamhaus.org on the ipaddress 192.168.1.56
C:\dnsutilv2.ps1 RBL 192.168.1.56 sbl.spamhaus.org
To do a multi RBL check on a list of RBL providers in a text file called rbllist.txt (see download for a example text file)
C:\dnsutilv2.ps1 MULTIRBL 192.168.1.56 c:\rbllist.txt
To do a SMTP telnet test on all MX records in a domain use
C:\dnsutilv2.ps1 SMTPTEST domainname.com
I’ve put a downloadable copy of the code here the code itself looks like.
param([String] $dnsqtype = $(throw "Please specify the DNS Query
Type"),[String] $IpParam = $(throw "Please specify the IPaddress"),[string] $RBLlist)
function readResponse {
while($stream.DataAvailable)
{
$read = $stream.Read($buffer, 0, 1024)
write-host -n -foregroundcolor cyan ($encoding.GetString($buffer, 0, $read))
""
}
}
function Smtptest([string] $remoteHost){
$port = 25
$socket = new-object System.Net.Sockets.TcpClient($remoteHost, $port)
if($socket -eq $null) { return; }
$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter($stream)
$buffer = new-object System.Byte[] 1024
$encoding = new-object System.Text.AsciiEncoding
readResponse($stream)
$command = "HELO "+ $domain
write-host -foregroundcolor DarkGreen $command
""
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 500
readResponse($stream)
$command = "MAIL FROM: <smtpcheck@" + $IpParam + ">"
write-host -foregroundcolor DarkGreen $command
""
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 500
readResponse($stream)
$command = "RCPT TO: <postmaster@" + $IpParam + ">"
write-host -foregroundcolor DarkGreen $command
""
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 500
readResponse($stream)
$command = "QUIT"
write-host -foregroundcolor DarkGreen $command
""
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 500
readResponse($stream)
## Close the streams
$writer.Close()
$stream.Close()
}
function Compile-Csharp ([string] $code, [Array]$References) {
# Get an instance of the CSharp code provider
$cp = New-Object Microsoft.CSharp.CSharpCodeProvider
$refs = New-Object Collections.ArrayList
$refs.AddRange( @("${framework}System.dll",
#"${PsHome}\System.Management.Automation.dll",
#"${PsHome}\Microsoft.PowerShell.ConsoleHost.dll",
"${framework}System.Windows.Forms.dll",
"${framework}System.Data.dll",
"${framework}System.Drawing.dll",
"${framework}System.XML.dll"))
if ($References.Count -ge 1) {
$refs.AddRange($References)
}
# Build up a compiler params object...
$cpar = New-Object System.CodeDom.Compiler.CompilerParameters
$cpar.GenerateInMemory = $true
$cpar.GenerateExecutable = $false
$cpar.IncludeDebugInformation = $false
$cpar.CompilerOptions = "/target:library"
$cpar.ReferencedAssemblies.AddRange($refs)
$cr = $cp.CompileAssemblyFromSource($cpar, $code)
if ( $cr.Errors.Count) {
$codeLines = $code.Split("`n");
foreach ($ce in $cr.Errors) {
write-host "Error: $($codeLines[$($ce.Line - 1)])"
$ce | out-default
}
Throw "INVALID DATA: Errors encountered while compiling code"
}
}
$code = @'
namespace PAB.DnsUtils
{
using System;
using System.Collections;
using System.ComponentModel;
using System.Runtime.InteropServices;
public class Dns
{
public Dns()
{
}
[DllImport("Dnsapi", EntryPoint="DnsQuery_W", CharSet=CharSet.Unicode,
SetLastError=true, ExactSpelling=true)]
private static extern Int32 DnsQuery([MarshalAs(UnmanagedType.VBByRefStr)]ref
string sName, QueryTypes wType, QueryOptions options, UInt32 aipServers, ref
IntPtr ppQueryResults, UInt32 pReserved);
[DllImport("Dnsapi", CharSet=CharSet.Auto, SetLastError=true)]
private static extern void DnsRecordListFree(IntPtr pRecordList, int FreeType);
public enum ErrorReturnCode
{
DNS_ERROR_RCODE_NO_ERROR = 0,
DNS_ERROR_RCODE_FORMAT_ERROR = 9001,
DNS_ERROR_RCODE_SERVER_FAILURE = 9002,
DNS_ERROR_RCODE_NAME_ERROR = 9003,
DNS_ERROR_RCODE_NOT_IMPLEMENTED = 9004,
DNS_ERROR_RCODE_REFUSED = 9005,
DNS_ERROR_RCODE_YXDOMAIN = 9006,
DNS_ERROR_RCODE_YXRRSET = 9007,
DNS_ERROR_RCODE_NXRRSET = 9008,
DNS_ERROR_RCODE_NOTAUTH = 9009,
DNS_ERROR_RCODE_NOTZONE = 9010,
DNS_ERROR_RCODE_BADSIG = 9016,
DNS_ERROR_RCODE_BADKEY = 9017,
DNS_ERROR_RCODE_BADTIME = 9018
}
private enum QueryOptions
{
DNS_QUERY_ACCEPT_TRUNCATED_RESPONSE = 1,
DNS_QUERY_BYPASS_CACHE = 8,
DNS_QUERY_DONT_RESET_TTL_VALUES = 0x100000,
DNS_QUERY_NO_HOSTS_FILE = 0x40,
DNS_QUERY_NO_LOCAL_NAME = 0x20,
DNS_QUERY_NO_NETBT = 0x80,
DNS_QUERY_NO_RECURSION = 4,
DNS_QUERY_NO_WIRE_QUERY = 0x10,
DNS_QUERY_RESERVED = -16777216,
DNS_QUERY_RETURN_MESSAGE = 0x200,
DNS_QUERY_STANDARD = 0,
DNS_QUERY_TREAT_AS_FQDN = 0x1000,
DNS_QUERY_USE_TCP_ONLY = 2,
DNS_QUERY_WIRE_ONLY = 0x100
}
public enum QueryTypes
{
DNS_TYPE_A = 1,
DNS_TYPE_CNAME = 5,
DNS_TYPE_MX = 15,
DNS_TYPE_TEXT = 16,
DNS_TYPE_SRV = 33,
DNS_TYPE_PTR = 12
}
[StructLayout(LayoutKind.Explicit)]
private struct DnsRecord
{
[FieldOffset(0)]
public IntPtr pNext;
[FieldOffset(4)]
public string pName;
[FieldOffset(8)]
public short wType;
[FieldOffset(10)]
public short wDataLength;
[FieldOffset(12)]
public uint flags;
[FieldOffset(16)]
public uint dwTtl;
[FieldOffset(20)]
public uint dwReserved;
// below is a partial list of the unionized members for this struct
// for DNS_TYPE_A records
[FieldOffset(24)]
public uint a_IpAddress;
// for DNS_TYPE_ PTR, CNAME, NS, MB, MD, MF, MG, MR records
[FieldOffset(24)]
public IntPtr ptr_pNameHost;
// for DNS_TXT_ DATA, HINFO, ISDN, TXT, X25 records
[FieldOffset(24)]
public uint data_dwStringCount;
[FieldOffset(28)]
public IntPtr data_pStringArray;
// for DNS_TYPE_MX records
[FieldOffset(24)]
public IntPtr mx_pNameExchange;
[FieldOffset(28)]
public short mx_wPreference;
[FieldOffset(30)]
public short mx_Pad;
// for DNS_TYPE_SRV records
[FieldOffset(24)]
public IntPtr srv_pNameTarget;
[FieldOffset(28)]
public short srv_wPriority;
[FieldOffset(30)]
public short srv_wWeight;
[FieldOffset(32)]
public short srv_wPort;
[FieldOffset(34)]
public short srv_Pad;
}
public static string[] GetRecords(string domain, string dnsqtype)
{
IntPtr ptr1 = IntPtr.Zero ;
IntPtr ptr2 = IntPtr.Zero ;
DnsRecord rec;
Dns.QueryTypes qtype = QueryTypes.DNS_TYPE_PTR;
switch(dnsqtype){
case "MX":
qtype = QueryTypes.DNS_TYPE_MX;
break;
case "PTR":
qtype = QueryTypes.DNS_TYPE_PTR;
break;
case "SPF":
qtype = QueryTypes.DNS_TYPE_TEXT;
break;
case "A":
qtype = QueryTypes.DNS_TYPE_A;
break;
case "RBL":
qtype = QueryTypes.DNS_TYPE_A;
break;
case "MULTIRBL":
qtype = QueryTypes.DNS_TYPE_A;
break;
case "SMTPTEST":
qtype = QueryTypes.DNS_TYPE_MX;
break;
}
if(Environment.OSVersion.Platform != PlatformID.Win32NT)
{
throw new NotSupportedException();
}
ArrayList list1 = new ArrayList();
int num1 = DnsQuery(ref domain, qtype,
QueryOptions.DNS_QUERY_USE_TCP_ONLY|QueryOptions.DNS_QUERY_BYPASS_CACHE, 0, ref
ptr1, 0);
if (num1 != 0)
{
if (num1 == 9003)
{
String[] emErrormessage = new string[1];
emErrormessage.SetValue("No Record Found",0);
return emErrormessage;
}
else
{
String[] emErrormessage = new string[1];
emErrormessage.SetValue("Error During Query Error Number " + num1 , 0);
return emErrormessage;
}
}
for (ptr2 = ptr1; !ptr2.Equals(IntPtr.Zero); ptr2 = rec.pNext)
{
rec = (DnsRecord) Marshal.PtrToStructure(ptr2, typeof(DnsRecord));
if (rec.wType == (short)qtype)
{
string text1 = String.Empty;
switch(qtype)
{
case Dns.QueryTypes.DNS_TYPE_A:
System.Net.IPAddress ip = new System.Net.IPAddress(rec.a_IpAddress);
text1 = ip.ToString();
break;
case Dns.QueryTypes.DNS_TYPE_CNAME:
text1 = Marshal.PtrToStringAuto(rec.ptr_pNameHost);
break;
case Dns.QueryTypes.DNS_TYPE_MX:
text1 = Marshal.PtrToStringAuto(rec.mx_pNameExchange);
if (dnsqtype == "MX") {
string[] mxalookup =
PAB.DnsUtils.Dns.GetRecords(Marshal.PtrToStringAuto(rec.mx_pNameExchange), "A");
text1 = text1 + " : " + rec.mx_wPreference.ToString() + " : " ;
foreach (string st in mxalookup)
{
text1 = text1 + st.ToString() + " ";
}}
break;
case Dns.QueryTypes.DNS_TYPE_SRV:
text1 = Marshal.PtrToStringAuto(rec.srv_pNameTarget);
break;
case Dns.QueryTypes.DNS_TYPE_PTR:
text1 = Marshal.PtrToStringAuto(rec.ptr_pNameHost);
break;
case Dns.QueryTypes.DNS_TYPE_TEXT:
if (Marshal.PtrToStringAuto(rec.data_pStringArray).ToLower().IndexOf("v=spf") ==
0)
{
text1 = Marshal.PtrToStringAuto(rec.data_pStringArray);
}
break;
default:
continue;
}
list1.Add(text1);
}
}
DnsRecordListFree(ptr2, 0);
return (string[]) list1.ToArray(typeof(string));
}
}
}
'@
Compile-Csharp $code
switch ($dnsqtype.ToUpper()){
PTR {$ipIpaddressSplit = $IpParam.Split(".")
$revipaddress = $ipIpaddressSplit.GetValue(3) + "." +
$ipIpaddressSplit.GetValue(2) + "." + $ipIpaddressSplit.GetValue(1) + "." +
$ipIpaddressSplit.GetValue(0) + ".in-addr.arpa"
$qrQueryresults =
[PAB.DnsUtils.DNS]::GetRecords($revipaddress,$dnsqtype.ToUpper())
""
foreach ($qresult in $qrQueryresults) {
$qresult
}
""}
RBL {$ipIpaddressSplit = $IpParam.Split(".")
$revipaddress = $ipIpaddressSplit.GetValue(3) + "." +
$ipIpaddressSplit.GetValue(2) + "." + $ipIpaddressSplit.GetValue(1) + "." +
$ipIpaddressSplit.GetValue(0) + "." + $RBLlist
$qrQueryresults =
[PAB.DnsUtils.DNS]::GetRecords($revipaddress,$dnsqtype.ToUpper())
if ($dnsqtype.ToUpper() -eq "RBL"){
foreach ($qresult in $qrQueryresults) {
$qresultprn = $qresultprn + " " + $qresult}
if ($qresultprn -eq " No Record Found"){
write-host -foregroundcolor DarkGreen "Not Listed"
}
else{
write-host -foregroundcolor Red "Listed : " + $qresultprn
}
}
}
MULTIRBL {
""
$Rblprovders = Get-Content $RBLlist
Foreach ($rblprov in $Rblprovders){
$ipIpaddressSplit = $IpParam.Split(".")
$revipaddress = $ipIpaddressSplit.GetValue(3) + "." +
$ipIpaddressSplit.GetValue(2) + "." + $ipIpaddressSplit.GetValue(1) + "." +
$ipIpaddressSplit.GetValue(0) + "." + $rblprov
$qrQueryresults =
[PAB.DnsUtils.DNS]::GetRecords($revipaddress,$dnsqtype.ToUpper())
$qresultprn = ""
foreach ($qresult in $qrQueryresults) {
$qresultprn = $qresultprn + " " + $qresult
if ($qresultprn -eq " No Record Found"){
write-host -foregroundcolor DarkGreen $rblprov " : Not Listed"
}
else{
write-host -foregroundcolor Red $rblprov " : Listed : " $qresultprn
}
}
}
}
SMTPTEST {
$revipaddress = $IpParam
$qrQueryresults =
[PAB.DnsUtils.DNS]::GetRecords($revipaddress,$dnsqtype.ToUpper())
""
foreach ($qresult in $qrQueryresults) {
write-host "SMTP Test for Host " + $qresult
""
smtptest($qresult)
}
}
default {$revipaddress = $IpParam
$qrQueryresults =
[PAB.DnsUtils.DNS]::GetRecords($revipaddress,$dnsqtype.ToUpper())
""
foreach ($qresult in $qrQueryresults) {
$qresult
}
""
}
}
A couple of weeks ago I blogged this script that allowed DNS operations like MX , PTR and SPF queries from a powershell script based on a C# class from Peter Bromberg. Since then I’ve added some more functionality to the code to give the ability to look up DNS RBL lists. A lot of people use DNSBL’s as a way of fighting SPAM but every now and again you may find a legitimate server that has been blacklisted on one of the many lists around for some reason. Wikipedia has a great entry that describes what RBL’s are and how they work essentially they are just another DNS zone that you query using a normal DNS A record lookup. So for example say you want to look up the IP address 192.168.8.2 to see if this is listed in the RBL list from SpamHaus you basically need to first reverse the IP address bytes to 2.8.168.192 and append the name of the RBL list you want to search which in this case would be sbl.spamhaus.org. So you then do an A record lookup on 2.8.168.192. sbl.spamhaus.org. If the record is listed in the RBL then the server will return back if not you will get a normal no record response. So putting this in a script was fairly easy I already had some code that did the IP address reversal so it was just a matter of appending the RBL name you wanted to lookup and then adding some code to process the response. One of the little cool things I like about Powershell is being able to easily change the text color of the text you’re returning in a command window using the –foregroudcolor parameter with write-host.
Going a little bit further is that usually you will use more then one RBL list on your server or possibly you want to search all active RBL’s like those services provided at sites such as http://www.dnsstuff.com/ and http://www.robtex.com/rbls.html . To do this I’ve added in some code that will read a list of RBL’s from a text file and then run though and test a certain IP addresss against each RBL in the file. You can seed the text file using any of the URL’s I previously method by just cutting and pasting the list they use into a normal text file.
The last bit of functionality I added to the script was the STMP telnet test from my other post. So what I did was add an option where you can put in a domain name you want to test and the script will then go out and find all the MX records for that domain and then test each one to see if it will accept email for that domain.
To use these three functions run the code as follows
To Do a RBL check for example of sbl.spamhaus.org on the ipaddress 192.168.1.56
C:\dnsutilv2.ps1 RBL 192.168.1.56 sbl.spamhaus.org
To do a multi RBL check on a list of RBL providers in a text file called rbllist.txt (see download for a example text file)
C:\dnsutilv2.ps1 MULTIRBL 192.168.1.56 c:\rbllist.txt
To do a SMTP telnet test on all MX records in a domain use
C:\dnsutilv2.ps1 SMTPTEST domainname.com
I’ve put a downloadable copy of the code here the code itself looks like.
param([String] $dnsqtype = $(throw "Please specify the DNS Query
Type"),[String] $IpParam = $(throw "Please specify the IPaddress"),[string] $RBLlist)
function readResponse {
while($stream.DataAvailable)
{
$read = $stream.Read($buffer, 0, 1024)
write-host -n -foregroundcolor cyan ($encoding.GetString($buffer, 0, $read))
""
}
}
function Smtptest([string] $remoteHost){
$port = 25
$socket = new-object System.Net.Sockets.TcpClient($remoteHost, $port)
if($socket -eq $null) { return; }
$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter($stream)
$buffer = new-object System.Byte[] 1024
$encoding = new-object System.Text.AsciiEncoding
readResponse($stream)
$command = "HELO "+ $domain
write-host -foregroundcolor DarkGreen $command
""
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 500
readResponse($stream)
$command = "MAIL FROM: <smtpcheck@" + $IpParam + ">"
write-host -foregroundcolor DarkGreen $command
""
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 500
readResponse($stream)
$command = "RCPT TO: <postmaster@" + $IpParam + ">"
write-host -foregroundcolor DarkGreen $command
""
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 500
readResponse($stream)
$command = "QUIT"
write-host -foregroundcolor DarkGreen $command
""
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 500
readResponse($stream)
## Close the streams
$writer.Close()
$stream.Close()
}
function Compile-Csharp ([string] $code, [Array]$References) {
# Get an instance of the CSharp code provider
$cp = New-Object Microsoft.CSharp.CSharpCodeProvider
$refs = New-Object Collections.ArrayList
$refs.AddRange( @("${framework}System.dll",
#"${PsHome}\System.Management.Automation.dll",
#"${PsHome}\Microsoft.PowerShell.ConsoleHost.dll",
"${framework}System.Windows.Forms.dll",
"${framework}System.Data.dll",
"${framework}System.Drawing.dll",
"${framework}System.XML.dll"))
if ($References.Count -ge 1) {
$refs.AddRange($References)
}
# Build up a compiler params object...
$cpar = New-Object System.CodeDom.Compiler.CompilerParameters
$cpar.GenerateInMemory = $true
$cpar.GenerateExecutable = $false
$cpar.IncludeDebugInformation = $false
$cpar.CompilerOptions = "/target:library"
$cpar.ReferencedAssemblies.AddRange($refs)
$cr = $cp.CompileAssemblyFromSource($cpar, $code)
if ( $cr.Errors.Count) {
$codeLines = $code.Split("`n");
foreach ($ce in $cr.Errors) {
write-host "Error: $($codeLines[$($ce.Line - 1)])"
$ce | out-default
}
Throw "INVALID DATA: Errors encountered while compiling code"
}
}
$code = @'
namespace PAB.DnsUtils
{
using System;
using System.Collections;
using System.ComponentModel;
using System.Runtime.InteropServices;
public class Dns
{
public Dns()
{
}
[DllImport("Dnsapi", EntryPoint="DnsQuery_W", CharSet=CharSet.Unicode,
SetLastError=true, ExactSpelling=true)]
private static extern Int32 DnsQuery([MarshalAs(UnmanagedType.VBByRefStr)]ref
string sName, QueryTypes wType, QueryOptions options, UInt32 aipServers, ref
IntPtr ppQueryResults, UInt32 pReserved);
[DllImport("Dnsapi", CharSet=CharSet.Auto, SetLastError=true)]
private static extern void DnsRecordListFree(IntPtr pRecordList, int FreeType);
public enum ErrorReturnCode
{
DNS_ERROR_RCODE_NO_ERROR = 0,
DNS_ERROR_RCODE_FORMAT_ERROR = 9001,
DNS_ERROR_RCODE_SERVER_FAILURE = 9002,
DNS_ERROR_RCODE_NAME_ERROR = 9003,
DNS_ERROR_RCODE_NOT_IMPLEMENTED = 9004,
DNS_ERROR_RCODE_REFUSED = 9005,
DNS_ERROR_RCODE_YXDOMAIN = 9006,
DNS_ERROR_RCODE_YXRRSET = 9007,
DNS_ERROR_RCODE_NXRRSET = 9008,
DNS_ERROR_RCODE_NOTAUTH = 9009,
DNS_ERROR_RCODE_NOTZONE = 9010,
DNS_ERROR_RCODE_BADSIG = 9016,
DNS_ERROR_RCODE_BADKEY = 9017,
DNS_ERROR_RCODE_BADTIME = 9018
}
private enum QueryOptions
{
DNS_QUERY_ACCEPT_TRUNCATED_RESPONSE = 1,
DNS_QUERY_BYPASS_CACHE = 8,
DNS_QUERY_DONT_RESET_TTL_VALUES = 0x100000,
DNS_QUERY_NO_HOSTS_FILE = 0x40,
DNS_QUERY_NO_LOCAL_NAME = 0x20,
DNS_QUERY_NO_NETBT = 0x80,
DNS_QUERY_NO_RECURSION = 4,
DNS_QUERY_NO_WIRE_QUERY = 0x10,
DNS_QUERY_RESERVED = -16777216,
DNS_QUERY_RETURN_MESSAGE = 0x200,
DNS_QUERY_STANDARD = 0,
DNS_QUERY_TREAT_AS_FQDN = 0x1000,
DNS_QUERY_USE_TCP_ONLY = 2,
DNS_QUERY_WIRE_ONLY = 0x100
}
public enum QueryTypes
{
DNS_TYPE_A = 1,
DNS_TYPE_CNAME = 5,
DNS_TYPE_MX = 15,
DNS_TYPE_TEXT = 16,
DNS_TYPE_SRV = 33,
DNS_TYPE_PTR = 12
}
[StructLayout(LayoutKind.Explicit)]
private struct DnsRecord
{
[FieldOffset(0)]
public IntPtr pNext;
[FieldOffset(4)]
public string pName;
[FieldOffset(8)]
public short wType;
[FieldOffset(10)]
public short wDataLength;
[FieldOffset(12)]
public uint flags;
[FieldOffset(16)]
public uint dwTtl;
[FieldOffset(20)]
public uint dwReserved;
// below is a partial list of the unionized members for this struct
// for DNS_TYPE_A records
[FieldOffset(24)]
public uint a_IpAddress;
// for DNS_TYPE_ PTR, CNAME, NS, MB, MD, MF, MG, MR records
[FieldOffset(24)]
public IntPtr ptr_pNameHost;
// for DNS_TXT_ DATA, HINFO, ISDN, TXT, X25 records
[FieldOffset(24)]
public uint data_dwStringCount;
[FieldOffset(28)]
public IntPtr data_pStringArray;
// for DNS_TYPE_MX records
[FieldOffset(24)]
public IntPtr mx_pNameExchange;
[FieldOffset(28)]
public short mx_wPreference;
[FieldOffset(30)]
public short mx_Pad;
// for DNS_TYPE_SRV records
[FieldOffset(24)]
public IntPtr srv_pNameTarget;
[FieldOffset(28)]
public short srv_wPriority;
[FieldOffset(30)]
public short srv_wWeight;
[FieldOffset(32)]
public short srv_wPort;
[FieldOffset(34)]
public short srv_Pad;
}
public static string[] GetRecords(string domain, string dnsqtype)
{
IntPtr ptr1 = IntPtr.Zero ;
IntPtr ptr2 = IntPtr.Zero ;
DnsRecord rec;
Dns.QueryTypes qtype = QueryTypes.DNS_TYPE_PTR;
switch(dnsqtype){
case "MX":
qtype = QueryTypes.DNS_TYPE_MX;
break;
case "PTR":
qtype = QueryTypes.DNS_TYPE_PTR;
break;
case "SPF":
qtype = QueryTypes.DNS_TYPE_TEXT;
break;
case "A":
qtype = QueryTypes.DNS_TYPE_A;
break;
case "RBL":
qtype = QueryTypes.DNS_TYPE_A;
break;
case "MULTIRBL":
qtype = QueryTypes.DNS_TYPE_A;
break;
case "SMTPTEST":
qtype = QueryTypes.DNS_TYPE_MX;
break;
}
if(Environment.OSVersion.Platform != PlatformID.Win32NT)
{
throw new NotSupportedException();
}
ArrayList list1 = new ArrayList();
int num1 = DnsQuery(ref domain, qtype,
QueryOptions.DNS_QUERY_USE_TCP_ONLY|QueryOptions.DNS_QUERY_BYPASS_CACHE, 0, ref
ptr1, 0);
if (num1 != 0)
{
if (num1 == 9003)
{
String[] emErrormessage = new string[1];
emErrormessage.SetValue("No Record Found",0);
return emErrormessage;
}
else
{
String[] emErrormessage = new string[1];
emErrormessage.SetValue("Error During Query Error Number " + num1 , 0);
return emErrormessage;
}
}
for (ptr2 = ptr1; !ptr2.Equals(IntPtr.Zero); ptr2 = rec.pNext)
{
rec = (DnsRecord) Marshal.PtrToStructure(ptr2, typeof(DnsRecord));
if (rec.wType == (short)qtype)
{
string text1 = String.Empty;
switch(qtype)
{
case Dns.QueryTypes.DNS_TYPE_A:
System.Net.IPAddress ip = new System.Net.IPAddress(rec.a_IpAddress);
text1 = ip.ToString();
break;
case Dns.QueryTypes.DNS_TYPE_CNAME:
text1 = Marshal.PtrToStringAuto(rec.ptr_pNameHost);
break;
case Dns.QueryTypes.DNS_TYPE_MX:
text1 = Marshal.PtrToStringAuto(rec.mx_pNameExchange);
if (dnsqtype == "MX") {
string[] mxalookup =
PAB.DnsUtils.Dns.GetRecords(Marshal.PtrToStringAuto(rec.mx_pNameExchange), "A");
text1 = text1 + " : " + rec.mx_wPreference.ToString() + " : " ;
foreach (string st in mxalookup)
{
text1 = text1 + st.ToString() + " ";
}}
break;
case Dns.QueryTypes.DNS_TYPE_SRV:
text1 = Marshal.PtrToStringAuto(rec.srv_pNameTarget);
break;
case Dns.QueryTypes.DNS_TYPE_PTR:
text1 = Marshal.PtrToStringAuto(rec.ptr_pNameHost);
break;
case Dns.QueryTypes.DNS_TYPE_TEXT:
if (Marshal.PtrToStringAuto(rec.data_pStringArray).ToLower().IndexOf("v=spf") ==
0)
{
text1 = Marshal.PtrToStringAuto(rec.data_pStringArray);
}
break;
default:
continue;
}
list1.Add(text1);
}
}
DnsRecordListFree(ptr2, 0);
return (string[]) list1.ToArray(typeof(string));
}
}
}
'@
Compile-Csharp $code
switch ($dnsqtype.ToUpper()){
PTR {$ipIpaddressSplit = $IpParam.Split(".")
$revipaddress = $ipIpaddressSplit.GetValue(3) + "." +
$ipIpaddressSplit.GetValue(2) + "." + $ipIpaddressSplit.GetValue(1) + "." +
$ipIpaddressSplit.GetValue(0) + ".in-addr.arpa"
$qrQueryresults =
[PAB.DnsUtils.DNS]::GetRecords($revipaddress,$dnsqtype.ToUpper())
""
foreach ($qresult in $qrQueryresults) {
$qresult
}
""}
RBL {$ipIpaddressSplit = $IpParam.Split(".")
$revipaddress = $ipIpaddressSplit.GetValue(3) + "." +
$ipIpaddressSplit.GetValue(2) + "." + $ipIpaddressSplit.GetValue(1) + "." +
$ipIpaddressSplit.GetValue(0) + "." + $RBLlist
$qrQueryresults =
[PAB.DnsUtils.DNS]::GetRecords($revipaddress,$dnsqtype.ToUpper())
if ($dnsqtype.ToUpper() -eq "RBL"){
foreach ($qresult in $qrQueryresults) {
$qresultprn = $qresultprn + " " + $qresult}
if ($qresultprn -eq " No Record Found"){
write-host -foregroundcolor DarkGreen "Not Listed"
}
else{
write-host -foregroundcolor Red "Listed : " + $qresultprn
}
}
}
MULTIRBL {
""
$Rblprovders = Get-Content $RBLlist
Foreach ($rblprov in $Rblprovders){
$ipIpaddressSplit = $IpParam.Split(".")
$revipaddress = $ipIpaddressSplit.GetValue(3) + "." +
$ipIpaddressSplit.GetValue(2) + "." + $ipIpaddressSplit.GetValue(1) + "." +
$ipIpaddressSplit.GetValue(0) + "." + $rblprov
$qrQueryresults =
[PAB.DnsUtils.DNS]::GetRecords($revipaddress,$dnsqtype.ToUpper())
$qresultprn = ""
foreach ($qresult in $qrQueryresults) {
$qresultprn = $qresultprn + " " + $qresult
if ($qresultprn -eq " No Record Found"){
write-host -foregroundcolor DarkGreen $rblprov " : Not Listed"
}
else{
write-host -foregroundcolor Red $rblprov " : Listed : " $qresultprn
}
}
}
}
SMTPTEST {
$revipaddress = $IpParam
$qrQueryresults =
[PAB.DnsUtils.DNS]::GetRecords($revipaddress,$dnsqtype.ToUpper())
""
foreach ($qresult in $qrQueryresults) {
write-host "SMTP Test for Host " + $qresult
""
smtptest($qresult)
}
}
default {$revipaddress = $IpParam
$qrQueryresults =
[PAB.DnsUtils.DNS]::GetRecords($revipaddress,$dnsqtype.ToUpper())
""
foreach ($qresult in $qrQueryresults) {
$qresult
}
""
}
}