45 lines
5.4 KiB
PHP
45 lines
5.4 KiB
PHP
<?php
|
|
ini_set('zend.exception_ignore_args','1');set_exception_handler(function(Throwable $e){fwrite(STDERR,'Assessment stopped: '.$e::class.' '.basename($e->getFile()).':'.$e->getLine());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['driver']!=='pgsql'||!empty($c['url'])||(int)$c['port']!==5432||$c['database']!=='investemoz'||!in_array($c['host'],['localhost','127.0.0.1'],true))throw new RuntimeException('Target mismatch');
|
|
$db=new PDO('pgsql:host='.$c['host'].';port='.$c['port'].';dbname='.$c['database'],$c['username'],$c['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']!=='investemoz'||(int)$identity['port']!==5432)throw new RuntimeException('Identity mismatch');
|
|
$where=json_decode(ltrim(file_get_contents(__DIR__.'/C132-private-location.json'),"\xEF\xBB\xBF"),true,512,JSON_THROW_ON_ERROR)['private_directory'];
|
|
$candidates=json_decode(file_get_contents($where.'/secret-candidates-private.json'),true,512,JSON_THROW_ON_ERROR);
|
|
$key=config('app.key');$key=str_starts_with($key,'base64:')?base64_decode(substr($key,7),true):$key;$crypt=new Illuminate\Encryption\Encrypter($key,config('app.cipher'));
|
|
$plainString=static function(string $encrypted)use($crypt):string{
|
|
$plain=$crypt->decrypt($encrypted,false);
|
|
// Fortify encrypt() serializes strings; legacy encryptString() does not. Do not instantiate objects.
|
|
if(preg_match('/^s:\d+:"/',$plain)){$decoded=unserialize($plain,['allowed_classes'=>false]);if(!is_string($decoded))throw new RuntimeException('Unexpected envelope type');return $decoded;}
|
|
if(!is_string($plain))throw new RuntimeException('Unexpected plaintext type');return $plain;
|
|
};
|
|
$beforeSnapshot=($argv[1]??'')==='--before-snapshot';
|
|
$users=$beforeSnapshot?json_decode(file_get_contents($where.'/recovery/users-before-response-private.json'),true,512,JSON_THROW_ON_ERROR):$db->query('SELECT id,email,password,two_factor_secret,two_factor_recovery_codes,two_factor_confirmed_at,remember_token FROM users ORDER BY id')->fetchAll(PDO::FETCH_ASSOC);
|
|
$sessions=$db->query('SELECT id,user_id,last_activity FROM sessions')->fetchAll(PDO::FETCH_ASSOC);
|
|
$impacted=[];$detail=[];$types=[];
|
|
foreach($candidates as $candidate){
|
|
$kind=$candidate['kind'];$types[$kind]=($types[$kind]??0)+1;$values=is_array($candidate['value'])?$candidate['value']:[$candidate['value']];
|
|
if($kind==='otpauth_URI'){
|
|
$uri=$candidate['value'];$uri=str_replace('&','&',$uri);parse_str(parse_url($uri,PHP_URL_QUERY)??'', $qs);$values=isset($qs['secret'])?[$qs['secret']]:[];
|
|
}
|
|
foreach($values as $value){if(!is_string($value))continue;
|
|
foreach($users as $user){
|
|
$matches=false;$effect=null;
|
|
if(in_array($kind,['MFA_or_other_secret','otpauth_URI'],true)&&$user['two_factor_secret']){
|
|
$secret=$plainString($user['two_factor_secret']);$matches=hash_equals(strtoupper($secret),strtoupper($value));unset($secret);$effect='current_TOTP_seed';
|
|
}elseif($kind==='password'){$matches=password_verify($value,$user['password']);$effect='current_password';}
|
|
elseif($kind==='MFA_recovery_code'&&$user['two_factor_recovery_codes']){$codes=json_decode($plainString($user['two_factor_recovery_codes']),true);$matches=is_array($codes)&&in_array($value,$codes,true);unset($codes);$effect='current_recovery_code';}
|
|
if($matches){$id=(int)$user['id'];$impacted[$id][$effect]=true;$detail[]=['user_id'=>$id,'kind'=>$effect,'entry_id'=>$candidate['entry_id'],'timestamp'=>$candidate['timestamp']];}
|
|
}
|
|
}
|
|
}
|
|
$db->exec('ROLLBACK');$detailsPrivate=['at'=>gmdate('c'),'matches'=>$detail];$privateName=$beforeSnapshot?'usability-before-snapshot-matches-private.json':(($argv[1]??'')==='--after-rotation'?'usability-after-rotation-matches-private.json':'usability-matches-private.json');file_put_contents($where.'/'.$privateName,json_encode($detailsPrivate,JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR));
|
|
$known=[];foreach($impacted as $id=>$effects)$known[]=['user_id'=>(int)$id,'usable_exposed_credentials'=>array_keys($effects),'active_session_count'=>count(array_filter($sessions,fn($s)=>(int)$s['user_id']===$id))];
|
|
$summary=['at'=>gmdate('c'),'target'=>$identity,'read_only'=>true,'current_credential_matches'=>$known,'current_seed_affected_user_ids'=>array_map(fn($x)=>$x['user_id'],array_values(array_filter($known,fn($x)=>in_array('current_TOTP_seed',$x['usable_exposed_credentials'],true)))),
|
|
'source_state'=>$beforeSnapshot?'Verified pre-response backup snapshot':'Current active credentials, READ ONLY',
|
|
'recovery_code_candidate_occurrences'=>count(array_filter($candidates,fn($x)=>$x['kind']==='MFA_recovery_code')),
|
|
'cookies_and_token_values_redacted_in_retained_records'=>true,'password_values_redacted_in_retained_records'=>true,'one_time_codes_expired_by_age'=>true,'secrets_or_values_exported'=>false,'MFA_not_disabled'=>true];
|
|
$output=$beforeSnapshot?'C132-usability-before-snapshot.json':(($argv[1]??'')==='--after-rotation'?'C132-usability-after-rotation.json':'C132-usability.json');
|
|
file_put_contents(__DIR__.'/'.$output,json_encode($summary,JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES|JSON_THROW_ON_ERROR));echo json_encode($summary,JSON_THROW_ON_ERROR);
|