34 lines
5.3 KiB
PHP
34 lines
5.3 KiB
PHP
<?php
|
|
// Read-only review of actual owners and authenticated/pending session bindings. Payloads never leave memory.
|
|
ini_set('zend.exception_ignore_args','1');set_exception_handler(function(Throwable $e){fwrite(STDERR,json_encode(['error_type'=>$e::class,'line'=>$e->getLine(),'sensitive_values_recorded'=>false]));exit(1);});
|
|
$root=dirname(__DIR__,2);require $root.'/vendor/autoload.php';$app=require $root.'/bootstrap/app.php';(new Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables)->bootstrap($app);(new Illuminate\Foundation\Bootstrap\LoadConfiguration)->bootstrap($app);
|
|
$c=config('database.connections.'.config('database.default'));if($c['database']!=='investemoz'||(int)$c['port']!==5432||!empty($c['url']))throw new RuntimeException('Target differs');
|
|
$key=config('app.key');$crypt=new Illuminate\Encryption\Encrypter(str_starts_with($key,'base64:')?base64_decode(substr($key,7),true):$key,config('app.cipher'));
|
|
$rotation=json_decode(file_get_contents(__DIR__.'/C132-rotation-active.json'),true,512,JSON_THROW_ON_ERROR);$cutoff=strtotime($rotation['at']);$ids=[14,15,16,17];
|
|
$backup=json_decode(file_get_contents(__DIR__.'/C132-backup-restoration.json'),true,512,JSON_THROW_ON_ERROR);
|
|
$location=json_decode(ltrim(file_get_contents(__DIR__.'/C132-private-location.json'),"\xEF\xBB\xBF"),true,512,JSON_THROW_ON_ERROR)['private_directory'];
|
|
$report=['at'=>gmdate('c'),'rotation_at'=>$rotation['at'],'read_only'=>true,'sensitive_payloads_in_report'=>false,'targets'=>[]];
|
|
foreach(['active'=>[...$c],'restored_snapshot'=>[...$c,'host'=>'127.0.0.1','port'=>55441,'database'=>$backup['restore_target']['database'],'username'=>'postgres','password'=>'']]as$label=>$conn){
|
|
$db=new PDO('pgsql:host='.$conn['host'].';port='.$conn['port'].';dbname='.$conn['database'],$conn['username'],$conn['password'],[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);$db->exec('BEGIN READ ONLY');
|
|
$identity=$db->query('SELECT current_database() AS database,inet_server_port() AS port')->fetch(PDO::FETCH_ASSOC);if($identity['database']!==$conn['database']||(int)$identity['port']!==(int)$conn['port'])throw new RuntimeException('Actual identity differs');
|
|
$accounts=$db->query('SELECT id,name,email,email_verified_at,two_factor_confirmed_at,portal_access_revision,remember_token,two_factor_secret FROM users WHERE id IN(14,15,16,17) ORDER BY id')->fetchAll(PDO::FETCH_ASSOC);
|
|
$sessionRows=$db->query('SELECT user_id,payload,last_activity FROM sessions')->fetchAll(PDO::FETCH_ASSOC);$counts=array_fill_keys($ids,['authenticated_rows'=>0,'authenticated_payloads'=>0,'pending_MFA'=>0,'stale_before_rotation'=>0,'revision_mismatch'=>0]);$unreadable=0;$newLegitimate=0;
|
|
foreach($sessionRows as$row){$payload=base64_decode($row['payload'],true);try{if(config('session.encrypt'))$payload=$crypt->decrypt($payload,false);$state=config('session.serialization','php')==='json'?json_decode($payload,true,512,JSON_THROW_ON_ERROR):@unserialize($payload,['allowed_classes'=>false]);}catch(Throwable){$unreadable++;continue;}
|
|
if(!is_array($state)){$unreadable++;continue;}$bound=[];
|
|
if(in_array((int)$row['user_id'],$ids,true)){$counts[(int)$row['user_id']]['authenticated_rows']++;$bound[(int)$row['user_id']]=true;}
|
|
foreach($state as$k=>$v)if(str_starts_with($k,'login_web_')&&in_array((int)$v,$ids,true)){$counts[(int)$v]['authenticated_payloads']++;$bound[(int)$v]=true;}
|
|
if(isset($state['login.id'])&&in_array((int)$state['login.id'],$ids,true)){$counts[(int)$state['login.id']]['pending_MFA']++;$bound[(int)$state['login.id']]=true;}
|
|
foreach(array_keys($bound)as$id){if((int)$row['last_activity']<$cutoff)$counts[$id]['stale_before_rotation']++;else$newLegitimate++;
|
|
if(isset($state['portal.access_revision'])&&(int)$state['portal.access_revision']!==1)$counts[$id]['revision_mismatch']++;
|
|
}unset($state,$payload);
|
|
}
|
|
$safeAccounts=[];foreach($accounts as$u){$id=(int)$u['id'];$matched=null;if($label==='active'){$html=file_get_contents($location.'/handoff-affected-users/conta-'.$id.'-2fas-local.html');if(preg_match('/<code>([A-Z2-7]{16,})<\/code>/',$html,$m)!==1)throw new RuntimeException('Handoff format');$matched=hash_equals($crypt->decrypt($u['two_factor_secret']),$m[1]);unset($html,$m);}
|
|
$safeAccounts[]=['id'=>$id,'name'=>$u['name'],'email'=>$u['email'],'contact_verified'=>$u['email_verified_at']!==null,'MFA_required'=>$u['two_factor_confirmed_at']!==null&&$u['two_factor_secret']!==null,'access_revision'=>(int)$u['portal_access_revision'],'remember_token_null'=>$u['remember_token']===null,'handoff_matches_current_MFA'=>$matched,'sessions'=>$counts[$id]];
|
|
}
|
|
$audits=$db->query("SELECT id,source_system,actor_user_id,entity_type,action,occurred_at,created_at FROM audit_events WHERE created_at >= '2026-10-04 00:00:00+00' ORDER BY created_at,id")->fetchAll(PDO::FETCH_ASSOC);
|
|
$report['targets'][$label]=['identity'=>$identity,'accounts'=>$safeAccounts,'all_session_rows_examined'=>count($sessionRows),'unreadable_payloads'=>$unreadable,'post_rotation_bindings_not_deleted'=>$newLegitimate,'audit_events_examined'=>count($audits)];
|
|
if($label==='active')file_put_contents(__DIR__.'/C132-audit-timeline-safe.json',json_encode(['at'=>gmdate('c'),'events'=>$audits,'raw_data_fields_exported'=>false],JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR));
|
|
$db->exec('ROLLBACK');
|
|
}
|
|
file_put_contents(__DIR__.'/C132-session-review.json',json_encode($report,JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR));echo json_encode($report,JSON_THROW_ON_ERROR);
|