-
Notifications
You must be signed in to change notification settings - Fork 2
/
CL_Detection.ps1
312 lines (268 loc) · 10.2 KB
/
CL_Detection.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# Copyright © 2008, Microsoft Corporation. All rights reserved.
. .\CL_Service.ps1
. .\CL_INetwork.ps1
. .\CL_NativeMethods.ps1
. .\CL_WscApi.ps1
Import-LocalizedData -BindingVariable LocalizedStrings -FileName CL_LocalizationData
[string]$Global:ExplorerPublishingPath = "HKLM:\SOFTWARE\Microsoft\Function Discovery\RegistryStore\Publication\Explorer"
[string]$Global:HomegroupPublishingPath = "HKLM:\SOFTWARE\Microsoft\Function Discovery\RegistryStore\Publication\HomeGroup"
[string]$Global:MachineKeysPath = "$ENV:SystemDrive\ProgramData\Microsoft\Crypto\RSA\MachineKeys"
# NET_FW_PROFILE_TYPE2 Enumeration
$Global:NET_FW_PROFILE2_DOMAIN = 0x0001
$Global:NET_FW_PROFILE2_PRIVATE = 0x0002
$Global:NET_FW_PROFILE2_PUBLIC = 0x0004
$Global:NET_FW_PROFILE2_ALL = 0x7FFFFFFF
$Global:FirewallRulesHomeNetwork = @{"Core Networking" = "@FirewallAPI.dll,-25000"; # IDS_FW_CORENET_GROUP
"Network Discovery" = "@FirewallAPI.dll,-32752"} # IDS_FW_NETDIS_GROUP
$Global:FirewallRuleHomeGroup = "@%systemroot%\system32\provsvc.dll,-202" # IDS_FIREWALL_GROUP
$Global:FirewallRulesSharing = @{"File and Printer Sharing" = "@FirewallAPI.dll,-28502"; # IDS_FW_FPS_GROUP
"Windows Media Player" = "@FirewallAPI.dll,-31002"; # IDS_FW_WMP_GROUP
"Windows Media Player Network Sharing Service" = "@FirewallAPI.dll,-31252"} # IDS_FW_WMPNSS_GROUP
function Get-HomeGroupId()
{
[string]$hgid = $null;
$regKey= Get-Item -path "HKLM:\SYSTEM\CurrentControlSet\Services\HomeGroupProvider\ServiceData"
if ($regKey)
{
if ($regKey.SubKeyCount -eq 1)
{
$hgid = $regKey.GetSubKeyNames()[0];
}
}
return $hgid;
}
function Get-HomeGroupName()
{
$name = $null;
[string]$hgid = Get-HomeGroupId;
if ($hgid)
{
[string]$path = "HKLM:\SYSTEM\CurrentControlSet\Services\HomeGroupProvider\ServiceData\" + $hgid;
[string]$item = "PeerGroupName";
$name = (Get-ItemProperty -Path $path -Name $item).$item
}
return $name;
}
function Test-HomeGroupName()
{
return [bool](Get-HomeGroupName)
}
function Check-IPv6()
{
[bool]$disabled = $false
$nics = [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces()
foreach ($adapter in $nics)
{
if (($adapter.NetworkInterfaceType -ne "Tunnel") -and ($adapter.NetworkInterfaceType -ne "Loopback"))
{
if ($adapter.OperationalStatus -eq "Up")
{
if (-not($adapter.Supports([System.Net.NetworkInformation.NetworkInterfaceComponent]::IPv6)))
{
# One of the connected adapters doesn't support IPv6
$disabled = $true
break
}
}
}
}
return $disabled
}
function Check-RegKeys()
{
# If we are not joined to a homegroup yet, these registry keys must be empty
return ((-not(Test-HomeGroupName)) -and
((-not(((Get-Item -Path $Global:ExplorerPublishingPath).GetValueNames()).Count -eq 0)) -or
(-not(((Get-Item -Path $Global:HomegroupPublishingPath).GetValueNames()).Count -eq 0))))
}
function Check-Permissions()
{
[bool]$issueDetected = $true
$everyone = (New-Object System.Security.Principal.SecurityIdentifier("S-1-1-0")).Translate([System.Security.Principal.NTAccount])
$acl = Get-Acl -Path $Global:MachineKeysPath
foreach ($ace in $acl.Access)
{
if ($ace.IdentityReference -eq $everyone)
{
if ($ace.AccessControlType -eq "Allow")
{
$issueDetected = ($ace.FileSystemRights -ne "Write, Read, Synchronize")
}
if ($issueDetected)
{
break
}
}
}
return $issueDetected
}
function Check-GroupMembership()
{
[bool]$script:issueDetected = $true
$ad = [ADSI]"WinNT://$ENV:ComputerName"
&{
$script:group = $ad.Children.Find("HomeUsers", "group")
}
trap [Exception]
{
# If the HomeUsers group doesn't exist, then there is something seriously wrong
# Bail out and let the catch all interaction handle the issue
$script:issueDetected = $false
continue
}
if ($script:issueDetected)
{
$user = $ad.Children.Find("$ENV:UserName", "user")
foreach ($item in $user.Groups())
{
$grp = New-Object System.DirectoryServices.DirectoryEntry($item)
if ($grp.Name.Value -eq $script:group.Name.Value)
{
$script:issueDetected = $false
break
}
}
}
return $script:issueDetected
}
function IsWindowsFirewallEnabled()
{
$firewall = New-Object -COM HNetCfg.FwPolicy2
return $firewall.FirewallEnabled($Global:NET_FW_PROFILE2_PRIVATE)
}
function Check-WindowsFirewallRules()
{
[bool]$issueDetected = $false
$firewall = New-Object -COM HNetCfg.FwPolicy2
foreach ($RuleGroup in $Global:FirewallRulesHomeNetwork.Values)
{
if (-not($firewall.IsRuleGroupEnabled($Global:NET_FW_PROFILE2_PRIVATE, $RuleGroup)))
{
$issueDetected = $true
break
}
}
# Certain firewall rules are only enabled when we are joined to a HomeGroup
if (-not($issueDetected) -and (Test-HomeGroupName))
{
$issueDetected = (-not($firewall.IsRuleGroupEnabled($Global:NET_FW_PROFILE2_PRIVATE, $Global:FirewallRuleHomeGroup)))
# File sharing firewall rules are only enabled on non-domain-joined machines and non-ARM machines
if (-not($issueDetected) -and -not(IsDomainJoined) -and (env:PROCESSOR_ARCHITECTURE -ne "ARM"))
{
foreach ($RuleGroup in $Global:FirewallRulesSharing.Values)
{
if (-not($firewall.IsRuleGroupEnabled($Global:NET_FW_PROFILE2_PRIVATE, $RuleGroup)))
{
$issueDetected = $true
break
}
}
}
}
return $issueDetected
}
function Check-RootCause($RootCause, [switch]$ShowDialog)
{
$params = @{"SBSData" = "Rerun"}
[bool]$checked = $true
[bool]$detected = $false
switch ($RootCause)
{
"PeerNetworkingGrouping" {
Write-DiagProgress -activity $LocalizedStrings.TS_PeerNetworkingGrouping
$checked = ((CheckForHomeNetwork) -and (Test-HomeGroupName) -and (ServiceRunning "HomeGroupProvider"))
if ($checked)
{
$detected = (-not(ServiceRunning "p2psvc"))
}
}
"HomeGroupProvider" {
Write-DiagProgress -activity $LocalizedStrings.TS_HomeGroupProvider
$checked = (CheckForHomeNetwork)
if ($checked)
{
$detected = ((-not(ServiceRunning "HomeGroupProvider")) -or
(-not(ServiceRunning "netprofm")) -or
(-not(ServiceRunning "fdPHost")) -or
(-not(ServiceRunning "FDResPub")))
}
}
"MultipleNetworks" {
Write-DiagProgress -activity $LocalizedStrings.TS_MultipleNetworks
$detected = ((NumNetworks) -gt 1)
}
"NetworkIsPublic" {
Write-DiagProgress -activity $LocalizedStrings.TS_NetworkIsPublic
$checked = ((NumNetworks) -gt 0)
if ($checked)
{
$detected = (-not(CheckForHomeNetwork))
}
}
"IPv6" {
Write-DiagProgress -activity $LocalizedStrings.TS_IPv6
$detected = (Check-IPv6)
}
"InformationCorruption" {
Write-DiagProgress -activity $LocalizedStrings.TS_InformationCorruption
$detected = (Check-RegKeys)
}
"Permissions" {
Write-DiagProgress -activity $LocalizedStrings.TS_Permissions
$detected = (Check-Permissions)
}
"GroupMembership" {
Write-DiagProgress -activity $LocalizedStrings.TS_GroupMembership
$checked = (Test-HomeGroupName)
if ($checked)
{
$detected = (Check-GroupMembership)
}
}
"Firewall" {
Write-DiagProgress -activity $LocalizedStrings.TS_Firewall
$params.Add("FirewallRootcauseName", $LocalizedStrings.DSP_RC_FirewallTitle)
$params.Add("FirewallResolverName", $LocalizedStrings.DSP_RS_FirewallTitle)
[string]$FirewallName = ""
$detected = Check-Firewall([ref]$FirewallName)
if ($detected)
{
if ($FirewallName.Length -gt 0)
{
$params.Item("FirewallRootcauseName") = ($LocalizedStrings.DSP_RC_FirewallTitle_Name -f $FirewallName)
$params.Item("FirewallResolverName") = ($LocalizedStrings.DSP_RS_FirewallTitle_Name -f $FirewallName)
}
if ($ShowDialog.isPresent)
{
$params.Item("SBSData") = "End"
Get-DiagInput -id IT_Firewall -Parameter @{"FirewallInteractionName" = $params.Item("FirewallResolverName")}
}
}
}
"FirewallConfiguration" {
Write-DiagProgress -activity $LocalizedStrings.TS_FirewallConfiguration
$checked = ((CheckForHomeNetwork) -and (IsWindowsFirewallEnabled))
if ($checked)
{
$detected = (Check-WindowsFirewallRules)
}
}
"HomeGroupNotConnected" {
Write-DiagProgress -activity $LocalizedStrings.TS_HomeGroupNotConnected
$checked = (CheckForHomeNetwork)
if ($checked)
{
$detected = (-not(Test-HomeGroupName))
}
}
default {
$checked = $false
throw "RootCause not found. `$RootCause = $RootCause"
}
}
if ($checked)
{
Update-DiagRootCause -id "RC_$RootCause" -Detected $detected -Parameter $params
}
return [bool]$detected
}