-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmtp-test.ps1
198 lines (189 loc) · 6.66 KB
/
smtp-test.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# This is just a generic test message; get's split into an array of commands
##$Global:defaultBody = Get-Content .\EmailMessage.txt
<#
A DNS Name resolving function
Designed to be flexible, and work with a list of hostnames
Can use a specific DNS server/s, or use the one for local interfaces
Also allows for Type/s, or ALL (either text or * character)
#>
function Get-DNSResolved {
[cmdletbinding()]
param(
[Parameter(Mandatory=$true, Position=0, ValueFromPipeLine)]
[ValidateNotNullOrEmpty()]
[string[]]
$Hostname,
[Parameter(Mandatory=$false, Position=1)]
[ValidateNotNullOrEmpty()]
[string[]]
$Server,
[Parameter(Mandatory=$false, Position=2)]
[ValidateSet("UNKNOWN", "A_AAAA", "A", "NS", "MD", "MF", "CNAME", "SOA", "MB", "MG", "MR", "NULL", "WKS", "PTR", "HINFO", "MINFO", "MX", "TXT", "RP",
"AFSDB", "X25", "ISDN", "RT", "AAAA", "SRV", "DNAME", "OPT", "DS", "RRSIG", "NSEC", "DNSKEY", "DHCID", "NSEC3", "NSEC3PARAM", "ANY", "ALL", "WINS",
"*", ignorecase=$true)]
[string[]]
$Type = @("A")
)
begin{
if($type -contains "*"){$Type = @("ALL")}
$results = @()
}
process{
foreach($hn in $hostname){
foreach($typ in $type){
if($null -eq $server -or $server -eq @()){
$results += Resolve-DNSName -Name $hn -Type $Typ
}
else{
foreach($serv in $server){
$cnt = $results.count
$results += Resolve-DNSName -Name $hn -Type $Typ -Server $serv
$results[(($results.count - $cnt) * -1)..-1] | ForEach-Object{$_.Name += " ($($serv))"}
}
}
}
}
}
end{
return $results
}
}
# No TLS / SSL support yet...
function Run-SMTPTest {
[cmdletbinding()]
param(
[Parameter(Mandatory=$true, Position=0, ValueFromPipeLine)]
[ValidateNotNullOrEmpty()]
[string[]]
$Recipient,
[Parameter(Mandatory=$false, Position=1)]
[ValidateNotNullOrEmpty()]
[string]
$Subject = "SMTP Testing - IT Administration",
[Parameter(Mandatory=$false, Position=2)]
[ValidateNotNullOrEmpty()]
[string]
$Sender = "[email protected]",
[Parameter(Mandatory=$false, Position=3)]
[ValidateNotNullOrEmpty()]
[string]
$Server = "",
[Parameter(Mandatory=$false, Position=4)]
[ValidateNotNullOrEmpty()]
[int32]
$ServerPort = 25,
[Parameter(Mandatory=$false, Position=5)]
[ValidateSet("Inbound","INB","IN","OUT","OUTB","OUTBOUND", ignorecase=$true)]
[string]
$Direction = "Inbound"
)
# Need to figure out the possible mode we are going for...
begin{
# Figure out whether inbound / outbound (Inbound meaning you focus on recipient end / sending as a server, or Outbound as in connecting
# to the desired server for sending - I.e. Exchange Online)
# Here is the list of commands - this is a skeleton for testing.DESCRIPTION
# Going to have to set this with replace portions - can rebuild the proper message during each process
#$(if($spfcommands){$spfcommands}else{""})
$commandsTemplate = @"
EHLO [DOMAIN][SPF]
MAIL FROM:<[SENDER]>
RCPT TO:<[RECIPIENT]>
DATA
<MESSAGE>
escape
"@
$messageTemplate = @"
Date: [DATE]
To: [RECIPIENT]
From: SMTP Testing <[SENDER]>
Subject: [SUBJECT]
Message-ID: <b43b694073f6450e95dc92d566f11ce7@_>
X-Mailer: SMTP-Tester
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
[BODY]
.
"@
}
process{
## Process will involve handling the connection, and going through a specific series of commands up until the attempt
## Of actually sending the email
foreach($recip in $Recipient){
$SenderDomain = ($Sender -split "@")[-1]
$RecipientDomain = ($recip -split "@")[-1]
if($direction.ToUpper() -in @("INBOUND","INB","IN")){
$domain = $RecipientDomain
}else{
$domain = $SenderDomain
}
# Since a custom server is not defined, let's just set it to what the MX record of the Sender's domain
# NOTE: it will likely return as multiple IPs depending on the server; so logic should be set to try one, and then the next...
# until either one works or all fail
if($Server -eq "" -or $null -eq $Server){
$Server = (Get-DNSResolved (Get-DNSResolved $domain -Type MX).NameExchange).IPAddress[0]
}
## Format for Header
$DateF = ((Get-DAte -Format "ddd, d MMM yyyy H:mm:ss ") + (((Get-Date -Format "zzz") -split ":") -join ""))
$commands = $commandsTemplate
$message = $MessageTemplate
$commands = $commands -replace "\[DOMAIN\]",$SenderDomain
$commands = $commands -replace "\[SPF\]",$null
$commands = $commands -replace "\[SENDER\]",$Sender
$commands = $commands -replace "\[RECIPIENT\]",$recip
$message = $message -replace "\[SENDER\]",$Sender
$message = $message -replace "\[RECIPIENT\]",$recip
$message = $message -replace "\[DATE\]",$DateF
$message = $message -replace "\[SUBJECT\]",$subject
$message = $message -replace "\[BODY\]","Testing email"
$commands = $commands.Split([Environment]::NewLine)
$message = $message.Split([Environment]::NewLine)
## Get TCP connection Going
$commandCount = 0
Write-Host $server -ForegroundColor Green
$tcp = New-Object System.Net.Sockets.TcpClient($server,$serverport)
$tcpstream = $tcp.GetStream()
$writer = New-Object System.IO.StreamWriter($tcpstream)
$reader = New-Object System.IO.StreamReader($tcpstream)
while($tcp.connected)
{
$reply = ""
$reply += ([char]$Reader.Read())
while(($reader.Peek() -ne -1) -or ($tcp.Available)){
$reply += ([char]$reader.Read())
}
switch -Wildcard ($reply){
"5*" {Write-Host $reply -ForegroundColor Red -NoNewLine; Break}
"2*" {Write-Host $reply -ForegroundColor Green -NoNewLine; Break}
Default{
Write-Host $reply -NoNewLine
}
}
# Let's just completely close connection if error
if($reply -like "5*"){break}
if($tcp.connected)
{
$command = $commands[$commandCount]
if($command -eq "escape"){break}
if($command -eq "<MESSAGE>"){
foreach($msg in $message){
if($tcpstream.DataAvailable){$reader.ReadLine()}
Write-Host $msg -ForegroundColor Yellow
$writer.WriteLine($msg)
$Writer.Flush()
}
}else{
Write-Host $command -ForegroundColor Yellow
$writer.WriteLine($command)|Out-Null
$Writer.Flush()
}
$commandCount++
}
}
$reader.Close()
$writer.close()
$tcp.Close()
}
}
end{
}
}