71 lines
6.2 KiB
PowerShell
71 lines
6.2 KiB
PowerShell
$ErrorActionPreference = 'Stop'
|
|
$root = [IO.Path]::GetFullPath('C:\laragon\www\portal-investidor')
|
|
$out = Join-Path $root 'outputs\investmoz-audit-current-20261012'
|
|
$preflight = Get-Content -LiteralPath (Join-Path $out 'C132-preflight.json') -Raw -Encoding UTF8 | ConvertFrom-Json
|
|
if ($preflight.target.database -ne 'investemoz' -or $preflight.target.port -ne 5432 -or $preflight.cached_config -or $preflight.route_cache) { throw 'C132 target/cache differs' }
|
|
$active = Join-Path $root 'config\inertia.php'
|
|
$prepared = Join-Path $out 'correcoes-preparadas\config\inertia.php'
|
|
$oldHash = '45994a9035e8e10d2fbb82f9fc18756cfb1ba403c9597d334122181b6b29839b'
|
|
$newHash = '80c68e12bf6cb64e39931e2467b43909893cea3eb6d04c3c1e68bdcb1236dfb7'
|
|
if ((Get-FileHash -LiteralPath $active -Algorithm SHA256).Hash.ToLowerInvariant() -ne $oldHash -or (Get-FileHash -LiteralPath $prepared -Algorithm SHA256).Hash.ToLowerInvariant() -ne $newHash) { throw 'C132 source differs; stopped before application' }
|
|
$privateBase = [IO.Path]::GetFullPath('C:\Users\macbook\.config\investmoz\incidents')
|
|
$incident = [IO.Path]::GetFullPath((Join-Path $privateBase ('C132-' + (Get-Date).ToUniversalTime().ToString('yyyyMMdd_HHmmss'))))
|
|
if (-not $incident.StartsWith($privateBase + [IO.Path]::DirectorySeparatorChar,[StringComparison]::OrdinalIgnoreCase)) { throw 'Incident path escaped' }
|
|
New-Item -ItemType Directory -Path $incident | Out-Null
|
|
$sid = [Security.Principal.WindowsIdentity]::GetCurrent().User
|
|
$acl = New-Object Security.AccessControl.DirectorySecurity
|
|
$acl.SetAccessRuleProtection($true,$false)
|
|
$acl.SetOwner($sid)
|
|
$inherit = [Security.AccessControl.InheritanceFlags]'ContainerInherit,ObjectInherit'
|
|
$prop = [Security.AccessControl.PropagationFlags]::None
|
|
$acl.AddAccessRule((New-Object Security.AccessControl.FileSystemAccessRule($sid,'FullControl',$inherit,$prop,'Allow')))
|
|
$acl.AddAccessRule((New-Object Security.AccessControl.FileSystemAccessRule((New-Object Security.Principal.SecurityIdentifier('S-1-5-18')),'FullControl',$inherit,$prop,'Allow')))
|
|
Set-Acl -LiteralPath $incident -AclObject $acl
|
|
$actualAcl = Get-Acl -LiteralPath $incident
|
|
if (-not $actualAcl.AreAccessRulesProtected -or $actualAcl.Access.Count -ne 2) { throw 'Evidence ACL not restricted' }
|
|
Copy-Item -LiteralPath $active -Destination (Join-Path $incident 'inertia-before.php')
|
|
Copy-Item -LiteralPath $prepared -Destination (Join-Path $incident 'inertia-contained.php')
|
|
# A local restore round trip validates the exact one-file recovery. Never restore exposure to the live portal.
|
|
$test = Join-Path $incident 'restore-test.php'
|
|
Copy-Item -LiteralPath (Join-Path $incident 'inertia-before.php') -Destination $test
|
|
if ((Get-FileHash -LiteralPath $test -Algorithm SHA256).Hash.ToLowerInvariant() -ne $oldHash) { throw 'One-file restore verification failed' }
|
|
& 'C:\laragon\bin\php\php-8.5.11-Win32-vs17-x64\php.exe' -l $prepared
|
|
if ($LASTEXITCODE -ne 0) { throw 'Prepared config syntax invalid' }
|
|
$beforeAcl = (Get-Acl -LiteralPath $active).Sddl
|
|
$stage = Join-Path $root 'config\inertia.C132.pending.php'
|
|
if (Test-Path -LiteralPath $stage) {
|
|
if ((Get-FileHash -LiteralPath $stage -Algorithm SHA256).Hash.ToLowerInvariant() -ne $newHash) { throw 'Unexpected staging bytes' }
|
|
} else { Copy-Item -LiteralPath $prepared -Destination $stage }
|
|
$applicationMethod = 'atomic-replace'
|
|
try {
|
|
[IO.File]::Replace($stage,$active,$null,$true)
|
|
} catch {
|
|
# This Windows destination disallows file replacement but permits writing the existing file.
|
|
# Use the same exclusive, DACL-preserving method previously used for the authorized portal configuration.
|
|
if ((Get-FileHash -LiteralPath $active -Algorithm SHA256).Hash.ToLowerInvariant() -ne $oldHash) { throw 'Atomic failure altered active bytes; stop' }
|
|
$bytes = [IO.File]::ReadAllBytes($prepared)
|
|
$stream = [IO.File]::Open($active,[IO.FileMode]::Open,[IO.FileAccess]::Write,[IO.FileShare]::None)
|
|
try { $stream.Position=0; $stream.Write($bytes,0,$bytes.Length); $stream.SetLength($bytes.Length); $stream.Flush($true) } finally { $stream.Dispose() }
|
|
$applicationMethod = 'exclusive-existing-file-write-preserving-DACL'
|
|
}
|
|
if ((Get-FileHash -LiteralPath $active -Algorithm SHA256).Hash.ToLowerInvariant() -ne $newHash) { throw 'Applied config digest mismatch' }
|
|
$containedAt = (Get-Date).ToUniversalTime().ToString('o')
|
|
# Recording is disabled before the evidence snapshot so automatic pruning cannot resume on new requests.
|
|
$diagnostics = Join-Path $root 'storage\inertia-devtools'
|
|
$snapshot = Join-Path $incident 'diagnostics'
|
|
New-Item -ItemType Directory -Path $snapshot | Out-Null
|
|
$records = @()
|
|
foreach ($file in (Get-ChildItem -LiteralPath $diagnostics -File -Force)) {
|
|
$dest = Join-Path $snapshot $file.Name
|
|
Copy-Item -LiteralPath $file.FullName -Destination $dest
|
|
$hash = (Get-FileHash -LiteralPath $dest -Algorithm SHA256).Hash.ToLowerInvariant()
|
|
if ($hash -ne (Get-FileHash -LiteralPath $file.FullName -Algorithm SHA256).Hash.ToLowerInvariant()) { throw 'Diagnostic snapshot mismatch' }
|
|
$records += @{ name=$file.Name; bytes=$file.Length; sha256=$hash; lastWriteUTC=$file.LastWriteTimeUtc.ToString('o') }
|
|
}
|
|
$manifest = @{ contained_at=$containedAt; private_evidence_directory=$incident; preservation='Snapshot restricted to current Windows operator and SYSTEM, outside DocumentRoot'; files=$records }
|
|
$manifest | ConvertTo-Json -Depth 6 | Set-Content -LiteralPath (Join-Path $incident 'evidence-manifest-private.json') -Encoding UTF8
|
|
$proof = @{ case='C132'; active_config_applied=$true; contained_file_at=$containedAt; application_method=$applicationMethod; only_changed_application_file='config/inertia.php'; before_sha256=$oldHash; after_sha256=$newHash; config_DACL_preserved=((Get-Acl -LiteralPath $active).Sddl -eq $beforeAcl); config_restore_round_trip=$true; preserved_diagnostic_files=$records.Count; evidence_outside_DocumentRoot=$true; evidence_inheritance_disabled=$true; authorized_principals_count=2; C131_not_applied=$true; migrations=0; business_data_writes=0; services_restarted=0; config_cache_absent=$true; root_directory=$root }
|
|
$proof | ConvertTo-Json -Depth 5 | Set-Content -LiteralPath (Join-Path $out 'C132-containment.json') -Encoding UTF8
|
|
@{ private_directory=$incident } | ConvertTo-Json | Set-Content -LiteralPath (Join-Path $out 'C132-private-location.json') -Encoding UTF8
|
|
$proof | ConvertTo-Json -Compress
|