This repository was archived by the owner on Dec 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGet-RemoteSmtpServersTls.ps1
207 lines (139 loc) · 6.27 KB
/
Get-RemoteSmtpServersTls.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
<#
.SYNOPSIS
Fetch all remote SMTP servers from Exchange receive connector logs, establishing a TLS connection
Thomas Stensitzki
THIS CODE IS MADE AVAILABLE AS IS, WITHOUT WARRANTY OF ANY KIND. THE ENTIRE
RISK OF THE USE OR THE RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.
Version 1.0, 2021-06-04
Ideas, comments and suggestions to [email protected]
.LINK
http://scripts.granikos.eu
.DESCRIPTION
This scripts fetches remote SMTP servers by searching the Exchange receive connector logs for successful TLS connections.
Fetched servers can be exported to a single CSV file for all receive connectors across Exchange Servers or
exported to a separate CSV file per Exchange Server.
You can use this script to identify remote servers connecting using TLS 1.0 or TLS 1.1.
.NOTES
Requirements
- Exchange Server 2013+
Revision History
--------------------------------------------------------------------------------
1.0 Initial community release
.PARAMETER Servers
List of Exchange servers, modern and legacy Exchange servers cannot be mixed
.PARAMETER Backend
Search backend transport (aka hub transport) log files, instead of frontend transport, which is the default
.PARAMETER ToCsv
Export search results to a single CSV file for all servers
.PRAMATER ToCsvPerServer
Export search results to a separate CSV file per servers
.PARAMETER AddDays
File selection filter, -5 will select log files changed during the last five days. Default: -10
.EXAMPLE
.\Get-RemoteSmtpServers.ps1 -Servers SRV01,SRV02 -LegacyExchange -AddDays -4 -ToCsv
Search legacy Exchange servers SMTP receive log files for the last 4 days and save search results in a single CSV file
#>
[CmdletBinding()]
param(
$Servers = @('MYEXCHANGE'),
[switch]$Backend,
[switch]$ToCsv,
[switch]$ToCsvPerServer,
[int]$AddDays = -10
)
$ScriptDir = Split-Path -Path $script:MyInvocation.MyCommand.Path
$CsvFileName = ('RemoteSMTPServersTls-%SERVER%-%ROLE%-%TLS%-{0}.csv' -f ((Get-Date).ToString('s').Replace(':','-')))
# ToDo: Update to Get-TransportServer/Get-TransportService
# Currently pretty static
$BackendPath = '\\%SERVER%\d$\Program Files\Microsoft\Exchange Server\V15\TransportRoles\Logs\Hub\ProtocolLog\SmtpReceive'
$FrontendPath = '\\%SERVER%\d$\Program Files\Microsoft\Exchange Server\V15\TransportRoles\Logs\FrontEnd\ProtocolLog\SmtpReceive'
# The TLS version to search
$TlsProtocols = @('TLS1_2','TLS1_1','TLS1_0')
# The SMTP receive log search pattern
$Pattern = '(.)*SP_PROT_%TLS%(.)*succeeded'
# An empty array for storing remote servers
$RemoteServers = @()
# Create an empty array
$RemoteServersOutput = @()
function Write-RemoteServers {
[CmdletBinding()]
param(
[string]$FilePath = ''
)
# sort servers
$RemoteServers = $RemoteServers | Select-Object -Unique | Sort-Object
if(($RemoteServers| Measure-Object).Count -gt 0) {
# Create an empty array
$RemoteServersOutput = @()
foreach($Server in $RemoteServers) {
if($Server.Trim() -ne '') {
$obj = New-Object -TypeName PSObject
$obj | Add-Member -MemberType NoteProperty -Name 'Remote Server' -Value $Server
$RemoteServersOutput += $obj
}
}
if($ToCsv -or $ToCsvPerServer) {
# save remote servers list as csv
$null = $RemoteServersOutput | Export-Csv -Path $FilePath -Encoding UTF8 -NoTypeInformation -Force -Confirm:$false
Write-Verbose -Message ('Remote server list written to: {0}' -f $FilePath)
}
$RemoteServersOutput
}
else {
Write-Host 'No remote servers found!'
}
}
## MAIN ###########################################
$LogPath = $FrontendPath
# Adjust CSV file name to reflect either HUB or FRONTEND transport
if($Backend) {
$LogPath = $BackendPath
$CsvFileName = $CsvFileName.Replace('%ROLE%','HUB')
}
else {
$CsvFileName = $CsvFileName.Replace('%ROLE%','FE')
}
Write-Verbose -Message ('CsvFileName: {0}' -f ($CsvFileName))
# Fetch each Exchange Server server
foreach($Server in $Servers) {
$Server = $Server.ToUpper()
$Path = $LogPath.Replace('%SERVER%', $Server)
Write-Verbose -Message ('Working on Server {0} | {1}' -f $Server, $Path)
# fetching log files requires an account w/ administrative access to the target server
$LogFiles = Get-ChildItem -Path $Path -File | Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays($AddDays)} | Select-Object -First 2
$LogFileCount = ($LogFiles | Measure-Object).Count
foreach($Tls in $TlsProtocols) {
Write-Host ('Working on: {0}' -f $Tls)
$FileCount = 1
foreach($File in $LogFiles) {
Write-Progress -Activity ('{3} | {4} | File [{0}/{1}] : {2}' -f $FileCount, $LogFileCount, $File.Name, $Server, $Tls) -PercentComplete(($FileCount/$LogFileCount)*100)
# find results in selected log files
$SearchPattern = $Pattern.Replace('%TLS%', $Tls)
$results = (Select-String -Path $File.FullName -Pattern $SearchPattern)
Write-Verbose -Message ('Results {0} : {1}' -f $File.FullName, ($results | Measure-Object).Count)
# Get remote server information from search string result
foreach($record in $results) {
# Fetch remote hostname
# $HostName = ($record.line -replace $Pattern,'').Replace(',','').Trim().ToUpper()
$HostIp = (($record.Line).Split(',')[5]).Split(':')[0]
# Try to resolve remote IP address as the line does not contain a server name
$HostName = Resolve-DnsName $HostIp -ErrorAction Ignore |Select-Object -ExpandProperty NameHost
if(-not $RemoteServers.Contains($HostName)) {
$RemoteServers += $HostName
}
}
$FileCount++
}
if($ToCsvPerServer) {
$CsvFile = $CsvFileName.Replace('%SERVER%',$Server).Replace('%TLS%',$Tls)
Write-Verbose -Message $CsvFile
Write-RemoteServers -FilePath $CsvFile
$RemoteServers = @()
}
}
}
if($ToCsv) {
$CsvFile = $CsvFileName.Replace('%SERVER%','ALL')
Write-Verbose -Message $CsvFile
Write-RemoteServers -FilePath $CsvFile
}