Skip to main content

Exchange SMTP Log file DNS Test tool Powershell script

This powershell script is a combination of some of my past scripts (in particular the DNS util script) wrapped up in a nice little GUI with buttons to make it easy to use on a daily basis. What this script does is allows you to open a SMTP log file (or if you don’t want to read the whole log file just a certain number of lines from a file) and it will then parse that log file into a DataGrid on a Winform. You can then select a line in the datagrid and use one of the Buttons provided to perform different DNS test on that log file entry. The tests it can do are

Reverse DNS query on the Source Mail server IP Address (from the log file)

MX lookup of the parsed domain name from a RCPT or MAIL SMTP command

SPF lookup of the parsed domain name from a RCPT or MAIL SMTP command

A Record lookup of the parsed domain name from a RCPT or MAIL SMTP command

RBL lookup of Source Mail Server IP Address (Using SORB’s or any other RBL you like). To configure which RBL server to use you need to modify the following line in the RBLLookup() function.

$RBLService = "dnsbl.sorbs.net"

Helo Banner Check does a connection to the Mail server source IP address and reports on the banner. To do this it creates a connection on Port 25 of the source mail server IP address then reads the response and then issues a Quit to end the connection.

Whois Lookup of the Source Mail Server IP Address using whois.arin.net (or any other whois server you want to configure). This will only return results for IP’s and domains this whois server is authorative for or it will return a referral to the whois server you should be using.

I’ve created two versions of the script the second version does Geolocationing of the Source Mail server IP address and gives the option to resolve every IP address in the log file to a geographical location or just do single queries on the location of certain ip addresses. This is based on my previous post of Geolocationing Message tracking logs .The reason for creating two versions is that the second script is quire complex and blows out to the size to around 800 lines so I wanted to have a simple script for anyone who doesn’t care about geolocatiion. Personally I think it’s really useful if you’re trying to diagnose something with a SMTP log file.

The parsing section of the script uses the get-content commandlet to read the log file you select this commandlet allows you to pass in the number of lines you want it to read from a file or pass -1 and it will read everything. The one thing I wasn’t 100 % sure about is the file locking operation of this commandlet. If it does lock the file when its retrieving the content then this could be a bad thing if you where to open a live log file. If anyone knows the answer to this one please let me know I haven’t come across any issues thus far.

To use the script just run it from powershell and you should see it build and popup a Winfrom. Use the select file button which will popup a file browser to allow you to select the log file you want to use. To use the geolocation version please see my other post about where you need to obtain the download from the iptocountry.csv file and where to place it to make the script work correctly. If you’re using the Geolocation version and you want the ipaddress resolved to a county location make sure you select the Show Cnty check box.

I’ve put a download of this script here the whole script is a bit too large to post but here are some highlights.

function openLog{
$logTable.clear()
$exFileName = new-object System.Windows.Forms.openFileDialog
$exFileName.ShowDialog()
$fnFileNamelableBox.Text = $exFileName.FileName
$tcountline = -1
if ($rbVeiwAllOnlyRadioButton.Checked -eq $true){$tcountline = $lnLogfileLineNum.value}
get-content $exFileName.FileName -totalCount $tcountline | %{
$linarr = $_.split(" ")
$lfDate = ""
$lfTime = ""
$lfSourceIP = ""
$lfHostName = ""
$lfDestIP = ""
$lfSMTPVerb = ""
$lfCommandText = ""
if ($linarr[0].substring(0, 1) -ne "#"){
if ($linarr.Length -gt 0){$lfDate = $linarr[0]}
if ($linarr.Length -gt 1){$lfTime = $linarr[1]}
if ($linarr.Length -gt 2){$lfSourceIP= $linarr[2]}
if ($linarr.Length -gt 3){$lfHostName = $linarr[3]}
if ($linarr.Length -gt 6){$lfDestIP = $linarr[6]}
if ($linarr.Length -gt 8){$lfSMTPVerb = $linarr[8]}
if ($linarr.Length -gt 10){$lfCommandText = $linarr[10]}
$logTable.Rows.Add($lfDate,$lfTime,$lfSourceIP,$lfHostName,$lfDestIP,$lfSMTPVerb,$lfCommandText)
}
}

$dgDataGrid.DataSource = $logTable
}

Compile-Csharp $code
$form = new-object System.Windows.Forms.form
$form.Text = "SMTP Log Test Tool"
$Dataset = New-Object System.Data.DataSet
$logTable = New-Object System.Data.DataTable
$logTable.TableName = "SMTPLogs"
$logTable.Columns.Add("Date");
$logTable.Columns.Add("Time");
$logTable.Columns.Add("SourceIPAddress");
$logTable.Columns.Add("HostName");
$logTable.Columns.Add("DestIPAddress");
$logTable.Columns.Add("SMTPVerb");
$logTable.Columns.Add("CommandText");

# Content
$cmClickMenu = new-object System.Windows.Forms.ContextMenuStrip
$cmClickMenu.Items.add("test122")

# Add Open Log file Button

$olButton = new-object System.Windows.Forms.Button
$olButton.Location = new-object System.Drawing.Size(20,19)
$olButton.Size = new-object System.Drawing.Size(75,23)
$olButton.Text = "Select file"
$olButton.Add_Click({openLog})
$form.Controls.Add($olButton)

# Add Reverse DNS Lookup Button

$rdnsbutton = new-object System.Windows.Forms.Button
$rdnsbutton.Location = new-object System.Drawing.Size(500,19)
$rdnsbutton.Size = new-object System.Drawing.Size(85,23)
$rdnsbutton.Text = "Reverse DNS"
$rdnsbutton.Add_Click({ptrLookup})
$form.Controls.Add($rdnsbutton)

# Add MX Lookup Button

$mxbutton = new-object System.Windows.Forms.Button
$mxbutton.Location = new-object System.Drawing.Size(500,44)
$mxbutton.Size = new-object System.Drawing.Size(85,23)
$mxbutton.Text = "MX Lookup"
$mxbutton.Add_Click({mxLookup})
$form.Controls.Add($mxbutton)


# Add SPF Lookup Button

$spfbutton = new-object System.Windows.Forms.Button
$spfbutton.Location = new-object System.Drawing.Size(590,19)
$spfbutton.Size = new-object System.Drawing.Size(85,23)
$spfbutton.Text = "SPF Lookup"
$spfbutton.Add_Click({spfLookup})
$form.Controls.Add($spfbutton)

# Add A Lookup Button

$Abutton = new-object System.Windows.Forms.Button
$Abutton.Location = new-object System.Drawing.Size(590,44)
$Abutton.Size = new-object System.Drawing.Size(85,23)
$Abutton.Text = "A Rec Lookup"
$Abutton.Add_Click({ALookup})
$form.Controls.Add($Abutton)

# Add RBL Single Lookup Button

$RBLButton = new-object System.Windows.Forms.Button
$RBLButton.Location = new-object System.Drawing.Size(680,19)
$RBLButton.Size = new-object System.Drawing.Size(85,23)
$RBLButton.Text = "RBL Lookup"
$RBLButton.Add_Click({RBLLookup})
$form.Controls.Add($RBLButton)

# Add HELO chk Button

$HelochkButton = new-object System.Windows.Forms.Button
$HelochkButton.Location = new-object System.Drawing.Size(680,44)
$HelochkButton.Size = new-object System.Drawing.Size(85,23)
$HelochkButton.Text = "HELO Banner Check"
$HelochkButton.Add_Click({HELOChk})
$form.Controls.Add($HelochkButton)

# Add WhoIS chk Button

$Whois = new-object System.Windows.Forms.Button
$Whois.Location = new-object System.Drawing.Size(770,19)
$Whois.Size = new-object System.Drawing.Size(85,23)
$Whois.Text = "Whois Check"
$Whois.Add_Click({whoischk})
$form.Controls.Add($Whois)


# Add FileName Lable
$fnFileNamelableBox = new-object System.Windows.Forms.Label
$fnFileNamelableBox.Location = new-object System.Drawing.Size(110,25)
$fnFileNamelableBox.forecolor = "MenuHighlight"
$fnFileNamelableBox.size = new-object System.Drawing.Size(200,20)
$form.Controls.Add($fnFileNamelableBox)

# Add Veiw RadioButtons
$rbVeiwAllRadioButton = new-object System.Windows.Forms.RadioButton
$rbVeiwAllRadioButton.Location = new-object System.Drawing.Size(310,19)
$rbVeiwAllRadioButton.size = new-object System.Drawing.Size(69,17)
$rbVeiwAllRadioButton.Checked = $true
$rbVeiwAllRadioButton.Text = "View All"
$rbVeiwAllRadioButton.Add_Click({if ($rbVeiwAllRadioButton.Checked -eq $true){$lnLogfileLineNum.Enabled = $false}})
$form.Controls.Add($rbVeiwAllRadioButton)

$rbVeiwAllOnlyRadioButton = new-object System.Windows.Forms.RadioButton
$rbVeiwAllOnlyRadioButton.Location = new-object System.Drawing.Size(310,42)
$rbVeiwAllOnlyRadioButton.size = new-object System.Drawing.Size(89,17)
$rbVeiwAllOnlyRadioButton.Text = "View Only #"
$rbVeiwAllOnlyRadioButton.Add_Click({if ($rbVeiwAllOnlyRadioButton.Checked -eq $true){$lnLogfileLineNum.Enabled = $true}})
$form.Controls.Add($rbVeiwAllOnlyRadioButton)

# Add Numeric log line number
$lnLogfileLineNum = new-object System.Windows.Forms.numericUpDown
$lnLogfileLineNum.Location = new-object System.Drawing.Size(401,39)
$lnLogfileLineNum.Size = new-object System.Drawing.Size(69,20)
$lnLogfileLineNum.Enabled = $false
$lnLogfileLineNum.Maximum = 10000000000
$form.Controls.Add($lnLogfileLineNum)


# File setting Group Box

$OfGbox = new-object System.Windows.Forms.GroupBox
$OfGbox.Location = new-object System.Drawing.Size(12,0)
$OfGbox.Size = new-object System.Drawing.Size(464,75)
$OfGbox.Text = "Log File Settings"
$form.Controls.Add($OfGbox)

# Add DataGrid View

$dgDataGrid = new-object System.windows.forms.DataGrid
$dgDataGrid.AllowSorting = $True
#$dgDataGrid.Add_Click({MXlookup($logTable.DefaultView[$dgDataGrid.CurrentCell.RowNumber])})
$dgDataGrid.Location = new-object System.Drawing.Size(12,81)
$dgDataGrid.size = new-object System.Drawing.Size(1024,750)
$form.Controls.Add($dgDataGrid)

#
$form.topmost = $true
$form.Add_Shown({$form.Activate()})
$form.ShowDialog()

Popular posts from this blog

Testing and Sending email via SMTP using Opportunistic TLS and oAuth in Office365 with PowerShell

As well as EWS and Remote PowerShell (RPS) other mail protocols POP3, IMAP and SMTP have had OAuth authentication enabled in Exchange Online (Official announcement here ). A while ago I created  this script that used Opportunistic TLS to perform a Telnet style test against a SMTP server using SMTP AUTH. Now that oAuth authentication has been enabled in office365 I've updated this script to be able to use oAuth instead of SMTP Auth to test against Office365. I've also included a function to actually send a Message. Token Acquisition  To Send a Mail using oAuth you first need to get an Access token from Azure AD there are plenty of ways of doing this in PowerShell. You could use a library like MSAL or ADAL (just google your favoured method) or use a library less approach which I've included with this script . Whatever way you do this you need to make sure that your application registration  https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-

The MailboxConcurrency limit and using Batching in the Microsoft Graph API

If your getting an error such as Application is over its MailboxConcurrency limit while using the Microsoft Graph API this post may help you understand why. Background   The Mailbox  concurrency limit when your using the Graph API is 4 as per https://docs.microsoft.com/en-us/graph/throttling#outlook-service-limits . This is evaluated for each app ID and mailbox combination so this means you can have different apps running under the same credentials and the poor behavior of one won't cause the other to be throttled. If you compared that to EWS you could have up to 27 concurrent connections but they are shared across all apps on a first come first served basis. Batching Batching in the Graph API is a way of combining multiple requests into a single HTTP request. Batching in the Exchange Mail API's EWS and MAPI has been around for a long time and its common, for email Apps to process large numbers of smaller items for a variety of reasons.  Batching in the Graph is limited to a m

How to test SMTP using Opportunistic TLS with Powershell and grab the public certificate a SMTP server is using

Most email services these day employ Opportunistic TLS when trying to send Messages which means that wherever possible the Messages will be encrypted rather then the plain text legacy of SMTP.  This method was defined in RFC 3207 "SMTP Service Extension for Secure SMTP over Transport Layer Security" and  there's a quite a good explanation of Opportunistic TLS on Wikipedia  https://en.wikipedia.org/wiki/Opportunistic_TLS .  This is used for both Server to Server (eg MTA to MTA) and Client to server (Eg a Message client like Outlook which acts as a MSA) the later being generally Authenticated. Basically it allows you to have a normal plain text SMTP conversation that is then upgraded to TLS using the STARTTLS verb. Not all servers will support this verb so if its not supported then a message is just sent as Plain text. TLS relies on PKI certificates and the administrative issue s that come around certificate management like expired certificates which is why I wrote th
All sample scripts and source code is provided by for illustrative purposes only. All examples are untested in different environments and therefore, I cannot guarantee or imply reliability, serviceability, or function of these programs.

All code contained herein is provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.