Skip to content

Commit bafd208

Browse files
committed
Fixes to enroll nodes, freebsd and lots of docker nodes
1 parent 293fe29 commit bafd208

File tree

20 files changed

+394
-47
lines changed

20 files changed

+394
-47
lines changed

cmd/tls/scripts/quick-add.ps1

Lines changed: 81 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ $projectName = "{{ .Project }}"
1717
$projectSecret = "{{ .Environment.Secret }}"
1818
$progFiles = [System.Environment]::GetEnvironmentVariable('ProgramFiles')
1919
$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")
2224
$secretFile = (Join-Path $osqueryPath "osquery.secret")
2325
$flagsFile = (Join-Path $osqueryPath "osquery.flags")
2426
$certFile = (Join-Path $osqueryPath "{{ .Project }}.crt")
@@ -66,8 +68,43 @@ function Test-IsAdmin {
6668
)
6769
}
6870

69-
# A helper function to set "safe" permissions for osquery binaries
7071
# 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
71108
function Set-SafePermissions {
72109
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = "Medium")]
73110
[OutputType('System.Boolean')]
@@ -80,7 +117,13 @@ function Set-SafePermissions {
80117
# First, to ensure success, we remove the entirety of the ACL
81118
$acl.SetAccessRuleProtection($true, $false)
82119
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+
}
84127
}
85128
Set-Acl $target $acl
86129

@@ -169,9 +212,19 @@ function QuickAdd-Node {
169212
Write-Host "[+] osquery is installed"
170213
}
171214

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+
172225
# 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
175228

176229
# Stop osquery service
177230
$osquerydService = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'"
@@ -204,13 +257,29 @@ function QuickAdd-Node {
204257
}
205258
$osqueryCertificate | Out-File -FilePath $certFile -Encoding ASCII
206259

207-
# Start osquery
260+
# Start osqueryd service
208261
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+
}
214283
Start-Service $serviceName
215284
Write-Host "[+] '$serviceName' system service is started." -foregroundcolor Cyan
216285
} else {

cmd/tls/scripts/quick-add.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,28 @@
66

77
_PROJECT="{{ .Project }}"
88
_SECRET="{{ .Environment.Secret }}"
9+
910
_SECRET_LINUX=/etc/osquery/osquery.secret
1011
_FLAGS_LINUX=/etc/osquery/osquery.flags
1112
_CERT_LINUX=/etc/osquery/certs/${_PROJECT}.crt
13+
1214
_SECRET_OSX=/private/var/osquery/osquery.secret
1315
_FLAGS_OSX=/private/var/osquery/osquery.flags
1416
_CERT_OSX=/private/var/osquery/certs/${_PROJECT}.crt
1517
_PLIST_OSX=/Library/LaunchDaemons/com.facebook.osqueryd.plist
1618
_OSQUERY_PLIST=/private/var/osquery/com.facebook.osqueryd.plist
19+
20+
_SECRET_FREEBSD=/usr/local/etc/osquery.secret
21+
_FLAGS_FREEBSD=/usr/local/etc/osquery.flags
22+
_CERT_FREEBSD=/usr/local/etc/certs/${_PROJECT}.crt
23+
1724
_OSQUERY_PKG="https://osquery-packages.s3.amazonaws.com/darwin/osquery-3.3.2.pkg"
1825
_OSQUERY_DEB="https://osquery-packages.s3.amazonaws.com/deb/osquery_3.3.2_1.linux.amd64.deb"
1926
_OSQUERY_RPM="https://osquery-packages.s3.amazonaws.com/rpm/osquery-3.3.2-1.linux.x86_64.rpm"
27+
2028
_OSQUERY_SERVICE_LINUX="osqueryd"
2129
_OSQUERY_SERVICE_OSX="com.facebook.osqueryd"
30+
_OSQUERY_SERVICE_FREEBSD="osqueryd"
2231

2332
_SECRET_FILE=""
2433
_FLAGS=""
@@ -37,6 +46,7 @@ log() {
3746
installOsquery() {
3847
log "Installing osquery for $OS"
3948
if [ "$OS" = "linux" ]; then
49+
log "Installing osquery in Linux"
4050
distro=$(/usr/bin/rpm -q -f /usr/bin/rpm >/dev/null 2>&1)
4151
if [ "$?" = "0" ]; then
4252
log "RPM based system detected"
@@ -51,10 +61,15 @@ installOsquery() {
5161
fi
5262
fi
5363
if [ "$OS" = "darwin" ]; then
64+
log "Installing osquery in OSX"
5465
_PKG="$(echo $_OSQUERY_PKG | cut -d"/" -f5)"
5566
sudo curl -# "$_OSQUERY_PKG" -o "/tmp/$_PKG"
5667
sudo installer -pkg "/tmp/$_PKG" -target /
5768
fi
69+
if [ "$OS" = "freebsd" ]; then
70+
log "Installing osquery in FreeBSD"
71+
sudo ASSUME_ALWAYS_YES=YES pkg install osquery
72+
fi
5873
}
5974

6075
verifyOsquery() {
@@ -88,6 +103,12 @@ whatOS() {
88103
_CERT="$_CERT_OSX"
89104
_SERVICE="$_OSQUERY_SERVICE_OSX"
90105
fi
106+
if [ "$OS" = "freebsd" ]; then
107+
_SECRET_FILE="$_SECRET_FREEBSD"
108+
_FLAGS="$_FLAGS_FREEBSD"
109+
_CERT="$_CERT_FREEBSD"
110+
_SERVICE="$_OSQUERY_SERVICE_FREEBSD"
111+
fi
91112
log "_SECRET_FILE=$_SECRET_FILE"
92113
log "_FLAGS=$_FLAGS"
93114
log "_CERT=$_CERT"
@@ -109,6 +130,12 @@ stopOsquery() {
109130
sudo launchctl unload "$_PLIST_OSX"
110131
fi
111132
fi
133+
if [ "$OS" = "freebsd" ]; then
134+
log "Stopping $_OSQUERY_SERVICE_FREEBSD"
135+
if [ "$(service osqueryd onestatus)" = "osqueryd is running." ]; then
136+
sudo service "$_OSQUERY_SERVICE_FREEBSD" onestop
137+
fi
138+
fi
112139
}
113140

114141
prepareSecret() {
@@ -173,6 +200,11 @@ startOsquery() {
173200
sudo cp "$_OSQUERY_PLIST" "$_PLIST_OSX"
174201
sudo launchctl load "$_PLIST_OSX"
175202
fi
203+
if [ "$OS" = "freebsd" ]; then
204+
log "Starting $_OSQUERY_SERVICE_FREEBSD"
205+
echo 'osqueryd_enable="YES"' | sudo tee -a /etc/rc.conf
206+
sudo service "$_OSQUERY_SERVICE_FREEBSD" start
207+
fi
176208
}
177209

178210
bye() {

cmd/tls/scripts/quick-remove.sh

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@
66

77
_PROJECT="{{ .Project }}"
88
_SECRET_LINUX=/etc/osquery/osquery.secret
9-
_SECRET_OSX=/private/var/osquery/osquery.secret
109
_FLAGS_LINUX=/etc/osquery/osquery.flags
1110
_CERT_LINUX=/etc/osquery/certs/${_PROJECT}.crt
11+
12+
_SECRET_OSX=/private/var/osquery/osquery.secret
1213
_FLAGS_OSX=/private/var/osquery/osquery.flags
1314
_CERT_OSX=/private/var/osquery/certs/${_PROJECT}.crt
1415
_PLIST_OSX=/Library/LaunchDaemons/com.facebook.osqueryd.plist
16+
17+
_SECRET_FREEBSD=
18+
_FLAGS_FREEBSD=
19+
_CERT_FREEBSD=
20+
1521
_OSQUERY_SERVICE_LINUX="osqueryd"
1622
_OSQUERY_SERVICE_OSX="com.facebook.osqueryd"
23+
_OSQUERY_SERVICE_FREEBSD="osqueryd"
1724

1825
_SECRET_FILE=""
1926
_FLAGS=""
@@ -69,6 +76,13 @@ stopOsquery() {
6976
sudo rm -f "$_PLIST_OSX"
7077
fi
7178
fi
79+
if [ "$OS" = "freebsd" ]; then
80+
log "Stopping $_OSQUERY_SERVICE_FREEBSD"
81+
if [ "$(service osqueryd onestatus)" = "osqueryd is running." ]; then
82+
sudo service "$_OSQUERY_SERVICE_FREEBSD" onestop
83+
fi
84+
cat /etc/rc.conf | grep "osqueryd_enable" | sed 's/YES/NO/g' | sudo tee /etc/rc.conf
85+
fi
7286
}
7387

7488
removeSecret() {

deploy/osquery/osquery-dev.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"options": {
3-
"schedule_splay_percent" : 0
3+
"schedule_splay_percent": 0
44
},
55
"schedule": {
6-
"osquery_info" : {
7-
"query" : "SELECT * FROM uptime;",
8-
"interval" : 30
6+
"osquery_info": {
7+
"query": "SELECT * FROM uptime;",
8+
"interval": 60
99
}
1010
},
1111
"decorators": {
@@ -16,4 +16,4 @@
1616
"SELECT version AS osquery_version, config_hash FROM osquery_info WHERE config_valid = 1;"
1717
]
1818
}
19-
}
19+
}

deploy/provision.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ DEST="$DEST_PATH" make install_cli
543543
if [[ "$MODE" == "dev" ]]; then
544544
log "Creating environment for dev"
545545
__db_conf="$DEST_PATH/config/$DB_CONF"
546-
__osquery_dev="$SOURCE_PATH/deploy/osquery/osquery-dev.conf"
546+
__osquery_dev="$SOURCE_PATH/deploy/osquery/osquery-dev.json"
547547
__osctrl_crt="/etc/nginx/certs/osctrl.crt"
548548
"$DEST_PATH"/osctrl-cli -D "$__db_conf" environment add -n "dev" -host "$_T_HOST" -conf "$__osquery_dev" -crt "$__osctrl_crt"
549549

docker/admin/wait.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ CONFIG="config"
1010
CERTS="certs"
1111
DB_JSON="$CONFIG/db.json"
1212
CRT_FILE="$CERTS/osctrl.crt"
13-
OSQUERY_JSON="$CONFIG/osquery-dev.json"
13+
OSQUERY_JSON="$CONFIG/osquery/osquery-dev.json"
1414

1515
# Check if database is ready, otherwise commands will fail
1616
until $(./bin/osctrl-cli -D "$DB_JSON" check); do
@@ -31,7 +31,9 @@ fi
3131
# Generate flag and secret file for enrolling nodes
3232
FLAGS_FILE="$CONFIG/docker.flags"
3333
SECRET_FILE="$CONFIG/docker.secret"
34-
./bin/osctrl-cli -D "$DB_JSON" environment flags -n dev -crt "$CRT_FILE" -secret "$SECRET_FILE" > "$FLAGS_FILE"
34+
# Generating flags and rewriting UUID as identifier for ephemeral, otherwise all the containers
35+
# will have the same UUID and it will mess things up
36+
./bin/osctrl-cli -D "$DB_JSON" environment flags -n dev -crt "$CRT_FILE" -secret "$SECRET_FILE" | sed 's/=uuid/=ephemeral/g' > "$FLAGS_FILE"
3537
./bin/osctrl-cli -D "$DB_JSON" environment secret -n dev > "$SECRET_FILE"
3638

3739
# Create admin user

0 commit comments

Comments
 (0)