From 3e1beb999423bfc83159f1888992788c6ca52f0a Mon Sep 17 00:00:00 2001 From: Zebreus Date: Sat, 14 Nov 2026 03:40:32 +0100 Subject: [PATCH] Skip the permission pass when it is right, and log command lines The cleanup job wrote the user's grant on C:\msys2 on every run, which goes through all 30000 files and kept the only processor busy for half a minute every half hour. It is now only written when it is not there yet; a run takes 17 seconds instead of up to two minutes. Process creation events now carry the command line, so what a visitor ran is known even when they deleted their shell history along with the installation. Co-Authored-By: Claude Opus 5.5 (1M context) --- server/README.md | 2 +- server/scripts/install_restore_task.ps1 | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/server/README.md b/server/README.md index c8bcb30..fa107ab 100644 --- a/server/README.md +++ b/server/README.md @@ -106,7 +106,7 @@ made bigger as well, the security log to a gigabyte: the scanners of the remote megabytes of it in a week. A start of the server that fails leaves nothing in that log. So windows records the history of the scheduled tasks -and the start and end of every process, and when the server does not come up, the cleanup job writes the processes +and the start and end of every process, with its command line, and when the server does not come up, the cleanup job writes the processes and the state of the server's task to `C:\ProgramData\kik-restore\failed-starts.log`. Only keys may log in over SSH. The box stands in the open internet and both of its accounts have a password. diff --git a/server/scripts/install_restore_task.ps1 b/server/scripts/install_restore_task.ps1 index 013f39a..f89af14 100644 --- a/server/scripts/install_restore_task.ps1 +++ b/server/scripts/install_restore_task.ps1 @@ -314,9 +314,16 @@ foreach ($copy in Get-ChildItem -LiteralPath $copies -Recurse -File -Force) { # Modify rather than full control, which would also let them delete files whose own permissions do not allow it - the # protected ones below. The folders that lead to those belong to the administrators and cannot be renamed either: a # folder put in the place of one would hand the server files the user wrote. The home directory keeps its own -# protected permissions because they are set without inheritance. +# protected permissions because they are set without inheritance. The grant is only written when it is not there yet: +# writing it goes through all 30000 files of the installation, which takes the only processor half a minute. if ($sid) { - icacls $msys /grant:r "*${sid}:(OI)(CI)M" | Out-Null + $user = New-Object System.Security.Principal.SecurityIdentifier($sid) + $granted = @((Get-Acl -LiteralPath $msys).GetAccessRules($true, $false, [System.Security.Principal.SecurityIdentifier]) | + Where-Object { $_.AccessControlType -eq 'Allow' -and $_.IdentityReference -eq $user }) + if ($granted.Count -ne 1 -or $granted[0].FileSystemRights -ne 'Modify, Synchronize' -or + $granted[0].InheritanceFlags -ne 'ContainerInherit, ObjectInherit') { + icacls $msys /grant:r "*${sid}:(OI)(CI)M" | Out-Null + } foreach ($folder in $msys, "$msys\etc", "$msys\usr", "$msys\usr\bin", "$msys\opt") { New-Item -ItemType Directory -Force -Path $folder | Out-Null Set-Undeletable $folder @@ -629,6 +636,11 @@ wevtutil sl Security /ms:1073741824 | Out-Null # launched and how each process ended: the history of the scheduled tasks, and the start and end of every process. wevtutil sl Microsoft-Windows-TaskScheduler/Operational /e:true /ms:104857600 | Out-Null auditpol /set '/subcategory:{0CCE922B-69AE-11D9-BED3-505054503030},{0CCE922C-69AE-11D9-BED3-505054503030}' /success:enable | Out-Null +# The start of a process is recorded with its command line, so what a visitor ran is known even when they deleted their +# shell history along with everything else. +$audit = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\Audit' +if (-not (Test-Path $audit)) { New-Item -Path $audit | Out-Null } +Set-ItemProperty -Path $audit -Name ProcessCreationIncludeCmdLine_Enabled -Value 1 -Type DWord # The installation's own cygrunsrv sshd service would fight for port 22; the scheduled task owns the sshd instead. Set-Service $service -StartupType Disabled -ErrorAction SilentlyContinue