-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInvoke-SMTP.ps1
294 lines (251 loc) · 12 KB
/
Invoke-SMTP.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#Requires -Version 5.1
#Requires -Modules PoShLog
#Requires -Modules PSSQLite
$Settings = ([xml](Get-Content ./config.xml)).configuration
$AppDetails = @{
Name = "PoshSMTP"
Version = "1.0.0"
}
# *******************************************************
# *******************************************************
# ** **
# ** DO NOT EDIT BELOW THIS BLOCK **
# ** **
# ** INITIALISATION BLOCK **
# ** **
# *******************************************************
# *******************************************************
$CurrentModules = Get-Module
If ($Settings.Logging.EventLogs.enabled -eq 1) { Import-Module PoShLog.Sinks.EventLog }
$LogLevel = $Settings.Logging.Defaults.Verbosity.ToString()
$LoggingOutput = 'Starting Logging. Initialised the Default'
$LoggingCommand = "New-Logger | Set-MinimumLevel -Value $LogLevel | "
If ($Settings.Logging.File.enabled -eq 1) {
$LogLevel = $Settings.Logging.File.Verbosity.ToString()
$LogFormat = '"' + $Settings.Logging.File.Format.ToString() + '"'
$LogFile = $Settings.Logging.File.Logfile.ToString()
$LogRollover = $Settings.Logging.File.Rollover.When.ToString()
$LogRetain = $Settings.Logging.File.Rollover.Retains.ToInt32($null)
$LoggingOutput += ", File"
$LoggingCommand = $LoggingCommand + "Add-SinkFile -RestrictedToMinimumLevel $LogLevel -OutputTemplate $LogFormat -Path $LogFile -RollingInterval $LogRollover -RetainedFileCountLimit $LogRetain | "
}
If ($Settings.Logging.Console.enabled -eq 1) {
$LogLevel = $Settings.Logging.Console.Verbosity.ToString()
$LogFormat = '"' + $Settings.Logging.Console.Format.ToString() + '"'
$LoggingOutput += ", Console"
$LoggingCommand = $LoggingCommand + "Add-SinkConsole -RestrictedToMinimumLevel $LogLevel -OutputTemplate $LogFormat | "
}
If ($Settings.Logging.EventLogs.enabled -eq 1) {
$LogLevel = $Settings.Logging.EventLogs.Verbosity.ToString()
$LogFormat = '"' + $Settings.Logging.EventLogs.Format.ToString() + '"'
$LogName = '"' + $Settings.Logging.EventLogs.EventLog.ToString() + '"'
$LoggingOutput += ", EventLogs"
$LoggingCommand = $LoggingCommand + "Add-SinkEventLog -RestrictedToMinimumLevel $LogLevel -OutputTemplate $LogFormat -Source $LogName | "
}
If ($Settings.Logging.PowerShell.enabled -eq 1) {
$LogLevel = $Settings.Logging.PowerShell.Verbosity.ToString()
$LogFormat = '"' + $Settings.Logging.PowerShell.Format.ToString() + '"'
$LoggingOutput += ", PowerShell"
$LoggingCommand = $LoggingCommand + "Add-SinkPowerShell -RestrictedToMinimumLevel $LogLevel -OutputTemplate $LogFormat | "
}
$LoggingCommand = $LoggingCommand + "Start-Logger"
Invoke-Expression $LoggingCommand
$LoggingOutput += ' modules.'
Write-InformationLog $LoggingOutput
$LoggingOutput = 'Loading core functions module.'
Write-InformationLog $LoggingOutput
Import-Module ./functions/CoreFunctions.psm1
$LoggingOutput = 'Loading misc functions module.'
Write-InformationLog $LoggingOutput
Import-Module ./functions/MiscFunctions.psm1
$LoggingOutput = 'Loading SMTP HELO functions module.'
Write-InformationLog $LoggingOutput
Import-Module ./functions/SMTPD_HeloFunctions.psm1
$LoggingOutput = 'Loading SMTP EHLO functions module.'
Write-InformationLog $LoggingOutput
Import-Module ./functions/SMTPD_EhloFunctions.psm1
# *******************************************************
# *******************************************************
# ** **
# ** INTERNAL VARIABLES BLOCK **
# ** **
# *******************************************************
# *******************************************************
Write-DebugLog 'Setting the IP address to listen on'
If ($Settings.Server.Listening.IPaddress -eq '0.0.0.0') {
$LoggingOutput = 'Listening on all IP addresses - ' + $Settings.Server.Listening.IPaddress
Write-VerboseLog $LoggingOutput
$IPAddressToUse = [System.Net.IPAddress]::Any
}
else {
$LoggingOutput = 'Listening on the IP Address - ' + $Settings.Server.Listening.IPaddress
Write-VerboseLog $LoggingOutput
$IPAddressToUse = [System.Net.IPAddress]::Parse($Settings.Server.Listening.IPaddress)
}
$MyRunspaceJobs = $null
$MyStatus = [Hashtable]::Synchronized(@{
Continue = $true
Emails = @{
Received = 0
Sent = 0
Rejected = 0
Failed = 0
SoftFail = 0
}
})
# *******************************************************
# *******************************************************
# ** **
# ** FUNCTIONS BLOCK **
# ** **
# *******************************************************
# *******************************************************
Function Invoke-EndCleanup {
If ($MyRunspace) {
Write-DebugLog 'Closing the MyRunspace'
$MyRunspace.Close()
Write-DebugLog 'Disposing the MyRunspace'
$MyRunspace.Dispose()
}
If ($TCPListener) {
Write-DebugLog 'Stopping the TCPListener'
$TCPListener.Stop()
}
Write-DebugLog 'Doing cleanup tasks (Closing logging file, removing used modules)'
Close-Logger
$LoadedModules = Get-Module
ForEach ($ModuleLoaded in (Compare-Object -ReferenceObject $CurrentModules -DifferenceObject $LoadedModules)) { Remove-Module $ModuleLoaded.InputObject }
exit
}
Function FakeTelnet {
param (
[Parameter(Mandatory)][System.Net.Sockets.TcpClient]$TCPClient
)
Write-Output "Incoming connection logged from $($TCPClient.Client.RemoteEndPoint.Address):$($TCPClient.Client.RemoteEndPoint.Port)"
$Stream = $TCPClient.GetStream()
$Timer = 10; $Ticks = 0; $Continue = $true
$Response = [System.Text.Encoding]::UTF8.GetBytes("I see you. I will die in $($Timer.ToString()) seconds.`r`nHit <space> to add another 10 seconds.`r`nType q to quit now.`r`nType x to terminate listener.`r`n`r`n")
$Stream.Write($Response, 0, $Response.Length)
$StartTimer = (Get-Date).Ticks
while (($Timer -gt 0) -and $Continue) {
if ($Stream.DataAvailable) {
$Buffer = $Stream.ReadByte()
Write-Output "Received Data: $($Buffer.ToString())"
if ($Buffer -eq 113) {
$Continue = $false
$Response = [System.Text.Encoding]::UTF8.GetBytes("`r`nI am terminating this session. Bye!`r`n")
}
elseif ($Buffer -eq 32) {
$Timer += 10
$Response = [System.Text.Encoding]::UTF8.GetBytes("`r`nAdding another 10 seconds.`r`nI will die in $($Timer.ToString()) seconds.`r`n")
}
elseif ($Buffer -eq 120) {
$Continue = $false
$Response = [System.Text.Encoding]::UTF8.GetBytes("`r`nI am terminating the listener. :-(`r`n")
}
else { $Response = [System.Text.Encoding]::UTF8.GetBytes("`r`nI see you. I will die in $($Timer.ToString()) seconds.`r`nHit <space> to add another 10 seconds.`r`nType q to quit this session.`r`nType x to terminate listener.`r`n`r`n") }
$Stream.Write($Response, 0, $Response.Length)
}
$EndTimer = (Get-Date).Ticks
$Ticks = $EndTimer - $StartTimer
if ($Ticks -gt 10000000) { $Timer--; $StartTimer = (Get-Date).Ticks }
}
$TCPClient.Close()
}
# *******************************************************
# *******************************************************
# ** **
# ** SYSTEM INITIALISATION BLOCK **
# ** **
# *******************************************************
# *******************************************************
If ($true){
Write-DebugLog 'Attempting to create the IP Endpoint for listening'
$IPEndPoint = try {
[System.Net.IPEndPoint]::new($IPAddressToUse, $Settings.Server.Listening.Port)
}
catch {
$LoggingOutput = "An exception was caught... '" + $_.Exception.Message + "'"
Write-ErrorLog $LoggingOutput
Write-Error 'Unable to create required IP endpoint. Check logs for more details'
Write-ErrorLog 'Unable to create required IP endpoint. Check logs for more details'
Invoke-EndCleanup
}
If ($IPEndPoint) {
Write-DebugLog 'Attempting to create the listener'
$TCPListener = try {
[System.Net.Sockets.TcpListener]::new($IPEndPoint)
}
catch {
$LoggingOutput = "An exception was caught... '" + $_.Exception.Message + "'"
Write-ErrorLog $LoggingOutput
Write-Error 'Unable to create required listener. Check logs for more details'
Write-ErrorLog 'Unable to create required listener. Check logs for more details'
Invoke-EndCleanup
}
}
Write-DebugLog 'Successfully created the listener & IP Endpoint'
$LoggingOutput = 'Initialising the new Runspace'
Write-DebugLog $LoggingOutput
$MyRunspaceInitialisation = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
$MyRunspaceInitialisation.Variables.Add((New-Object -TypeName System.Management.Automation.Runspaces.SessionStateVariableEntry -ArgumentList 'MyStatus', $MyStatus, ''))
$MyRunspaceInitialisation.Variables.Add((New-Object -TypeName System.Management.Automation.Runspaces.SessionStateVariableEntry -ArgumentList 'LogSettings', $Settings.Logging, ''))
$LoggingOutput = 'Attempting to open a Runspace pool with a max of ' + [uint32]$Settings.Server.MaxThreads + " threads"
Write-DebugLog $LoggingOutput
$MyRunspace = try {
[System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspacePool(1, [uint32]$Settings.Server.MaxThreads, $MyRunspaceInitialisation, $Host)
}
catch {
$LoggingOutput = "An exception was caught... '" + $_.Exception.Message + "'"
Write-ErrorLog $LoggingOutput
Write-Error 'Unable to create required the Runspace pool. Check logs for more details'
Write-ErrorLog 'Unable to create required the Runspace pool. Check logs for more details'
Invoke-EndCleanup
}
try {
$MyRunspace.Open()
}
catch {
$LoggingOutput = "An exception was caught... '" + $_.Exception.Message + "'"
Write-ErrorLog $LoggingOutput
Write-Error 'Unable to open the Runspace pool. Check logs for more details'
Write-ErrorLog 'Unable to open the Runspace pool. Check logs for more details'
Invoke-EndCleanup
}
Write-DebugLog 'Successfully created the Runspace pool & opened it.'
}
# *******************************************************
# *******************************************************
# ** **
# ** MAIN CODE BLOCK **
# ** **
# *******************************************************
# *******************************************************
Write-VerboseLog 'Starting the listener'
$TCPListener.Start()
while ($MyStatus.Continue) {
Write-VerboseLog 'Polling every 100ms to see if another connection come in'
while (!$TCPListener.Pending()) {
Start-Sleep -Milliseconds 100
}
#FakeTelnet -TCPClient $TCPListener.AcceptTcpClient()
# Write-VerboseLog 'Creating a PowerShell instance'
# $NewRunspaceJob = [powershell]::Create()
# Write-VerboseLog ' * Setting the runspace'
# $NewRunspaceJob.RunspacePool = $MyRunspace
# Write-VerboseLog ' * Adding the script'
# [void] $NewRunspaceJob.AddScript({New-SMTPD}).AddParameter("TCPClient", $TCPListener.AcceptTcpClient())
# Write-VerboseLog ' * Saving the details to the Job Tracker'
# $MyRunspaceJobs += $NewRunspaceJob
# Write-VerboseLog 'Invoking PowerShell instance'
# [void] $NewRunspaceJob.BeginInvoke()
New-SMTPD -TCPClient $TCPListener.AcceptTcpClient()
}
# *******************************************************
# *******************************************************
# ** **
# ** CLEAN UP CODE BLOCK **
# ** **
# *******************************************************
# *******************************************************
Invoke-EndCleanup