@@ -17,8 +17,10 @@ $projectName = "{{ .Project }}"
17
17
$projectSecret = " {{ .Environment.Secret }}"
18
18
$progFiles = [System.Environment ]::GetEnvironmentVariable(' ProgramFiles' )
19
19
$osqueryPath = (Join-Path $progFiles " osquery" )
20
- $osqueryDaemonPath = (Join-Path $osqueryPath " osqueryd" )
21
- $osqueryDaemon = (Join-Path $osqueryDaemonPath " osqueryd.exe" )
20
+ $daemonFolder = (Join-Path $osqueryPath " osqueryd" )
21
+ $extensionsFolder = (Join-Path $osqueryPath " extensions" )
22
+ $logFolder = (Join-Path $osqueryPath " log" )
23
+ $osqueryDaemon = (Join-Path $daemonFolder " osqueryd.exe" )
22
24
$secretFile = (Join-Path $osqueryPath " osquery.secret" )
23
25
$flagsFile = (Join-Path $osqueryPath " osquery.flags" )
24
26
$certFile = (Join-Path $osqueryPath " {{ .Project }}.crt" )
@@ -66,8 +68,43 @@ function Test-IsAdmin {
66
68
)
67
69
}
68
70
69
- # A helper function to set "safe" permissions for osquery binaries
70
71
# From https://github.com/facebook/osquery/blob/master/tools/provision/chocolatey/osquery_utils.ps1
72
+ # Helper function to add an explicit Deny-Write ACE for the Everyone group
73
+ function Set-DenyWriteAcl {
74
+ [CmdletBinding (SupportsShouldProcess = $true , ConfirmImpact = " Medium" )]
75
+ [OutputType (' System.Boolean' )]
76
+ param (
77
+ [string ] $targetDir = ' ' ,
78
+ [string ] $action = ' '
79
+ )
80
+ if (($action -ine ' Add' ) -and ($action -ine ' Remove' )) {
81
+ Write-Debug ' [-] Invalid action in Set-DenyWriteAcl.'
82
+ return $false
83
+ }
84
+ if ($PSCmdlet.ShouldProcess ($targetDir )) {
85
+ $acl = Get-Acl $targetDir
86
+ $inheritanceFlag = [System.Security.AccessControl.InheritanceFlags ]::ContainerInherit -bor [System.Security.AccessControl.InheritanceFlags ]::ObjectInherit
87
+ $propagationFlag = [System.Security.AccessControl.PropagationFlags ]::None
88
+ $permType = [System.Security.AccessControl.AccessControlType ]::Deny
89
+
90
+ $worldSIDObj = New-Object System.Security.Principal.SecurityIdentifier (' S-1-1-0' )
91
+ $worldUser = $worldSIDObj.Translate ([System.Security.Principal.NTAccount ])
92
+ $permission = $worldUser.Value , " write" , $inheritanceFlag , $propagationFlag , $permType
93
+ $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
94
+ # We only support adding or removing the ACL
95
+ if ($action -ieq ' add' ) {
96
+ $acl.SetAccessRule ($accessRule )
97
+ } else {
98
+ $acl.RemoveAccessRule ($accessRule )
99
+ }
100
+ Set-Acl $targetDir $acl
101
+ return $true
102
+ }
103
+ return $false
104
+ }
105
+
106
+ # From https://github.com/facebook/osquery/blob/master/tools/provision/chocolatey/osquery_utils.ps1
107
+ # A helper function to set "safe" permissions for osquery binaries
71
108
function Set-SafePermissions {
72
109
[CmdletBinding (SupportsShouldProcess = $true , ConfirmImpact = " Medium" )]
73
110
[OutputType (' System.Boolean' )]
@@ -80,7 +117,13 @@ function Set-SafePermissions {
80
117
# First, to ensure success, we remove the entirety of the ACL
81
118
$acl.SetAccessRuleProtection ($true , $false )
82
119
foreach ($access in $acl.Access ) {
83
- $acl.RemoveAccessRule ($access )
120
+ Try {
121
+ $acl.RemoveAccessRule ($access )
122
+ } Catch [System.Management.Automation.MethodInvocationException ] {
123
+ if ($_.FullyQualifiedErrorId -ne ' IdentityNotMappedException' ) {
124
+ Throw " Error trying to remove access ($access )"
125
+ }
126
+ }
84
127
}
85
128
Set-Acl $target $acl
86
129
@@ -169,9 +212,19 @@ function QuickAdd-Node {
169
212
Write-Host " [+] osquery is installed"
170
213
}
171
214
215
+ # Lastly, ensure that the Deny Write ACLs have been removed before modifying
216
+ Write-Host " [+] Setting Deny Write ACLs"
217
+ if (Test-Path $daemonFolder ) {
218
+ Set-DenyWriteAcl $daemonFolder ' Remove'
219
+ }
220
+ if (Test-Path $extensionsFolder ) {
221
+ Set-DenyWriteAcl $extensionsFolder ' Remove'
222
+ }
223
+ Set-DenyWriteAcl $osqueryDaemon ' Remove'
224
+
172
225
# Making sure non-privileged write access is not allowed
173
- Write-Host " [+] Setting osquery safe permissions"
174
- Set-SafePermissions $osqueryDaemonPath
226
+ Write-Host " [+] Setting $daemonFolder safe permissions"
227
+ Set-SafePermissions $daemonFolder
175
228
176
229
# Stop osquery service
177
230
$osquerydService = Get-WmiObject - Class Win32_Service - Filter " Name='$serviceName '"
@@ -204,13 +257,29 @@ function QuickAdd-Node {
204
257
}
205
258
$osqueryCertificate | Out-File - FilePath $certFile - Encoding ASCII
206
259
207
- # Start osquery
260
+ # Start osqueryd service
208
261
if ($osquerydService ) {
209
- New-Service - BinaryPathName " $osqueryDaemon --flagfile=$flagsFile " `
210
- - Name $serviceName `
211
- - DisplayName $serviceName `
212
- - Description $serviceDescription `
213
- - StartupType Automatic
262
+ if (-not (Get-Service $serviceName - ErrorAction SilentlyContinue)) {
263
+ Write-Debug ' Installing osquery daemon service.'
264
+ # If the 'install' parameter is passed, we create a Windows service with
265
+ # the flag file in the default location in \Program Files\osquery\
266
+ # the flag file in the default location in Program Files
267
+ $cmd = ' "{0}" --flagfile="C:\Program Files\osquery\osquery.flags"' -f $osqueryDaemon
268
+
269
+ $svcArgs = @ {
270
+ Name = $serviceName
271
+ BinaryPathName = $cmd
272
+ DisplayName = $serviceName
273
+ Description = $serviceDescription
274
+ StartupType = " Automatic"
275
+ }
276
+ New-Service @svcArgs
277
+
278
+ # If the osquery.flags file doesn't exist, we create a blank one.
279
+ if (-not (Test-Path " $targetFolder \osquery.flags" )) {
280
+ Add-Content " $targetFolder \osquery.flags" $null
281
+ }
282
+ }
214
283
Start-Service $serviceName
215
284
Write-Host " [+] '$serviceName ' system service is started." - foregroundcolor Cyan
216
285
} else {
0 commit comments