2026-10-04 22:43:18 +08:00
|
|
|
# patch_login_hash.ps1 - patch login.elf .text SHA256 into .hashseg
|
|
|
|
|
# usage: powershell -File patch_login_hash.ps1 [login.elf]
|
|
|
|
|
param([string]$ElfPath = "..\..\SYSTEM\system\user\use\login.elf")
|
2026-10-02 21:07:05 +08:00
|
|
|
|
2026-10-04 22:43:18 +08:00
|
|
|
if (-not (Test-Path $ElfPath)) { Write-Error ("not found: " + $ElfPath); exit 1 }
|
2026-10-02 21:07:05 +08:00
|
|
|
|
|
|
|
|
$bytes = [System.IO.File]::ReadAllBytes($ElfPath)
|
|
|
|
|
|
|
|
|
|
# ELF64 header: e_shoff @ 0x28 (u64), e_shentsize @ 0x3A (u16), e_shnum @ 0x3C (u16)
|
2026-10-04 22:43:18 +08:00
|
|
|
function U64([byte[]]$b, [int]$o) { [UInt64]$b[$o] -bor ([UInt64]$b[$o+1] -shl 8) -bor ([UInt64]$b[$o+2] -shl 16) -bor ([UInt64]$b[$o+3] -shl 24) -bor ([UInt64]$b[$o+4] -shl 32) -bor ([UInt64]$b[$o+5] -shl 40) -bor ([UInt64]$b[$o+6] -shl 48) -bor ([UInt64]$b[$o+7] -shl 56) }
|
|
|
|
|
function U16([byte[]]$b, [int]$o) { [UInt32]$b[$o] -bor ([UInt32]$b[$o+1] -shl 8) }
|
|
|
|
|
function U32([byte[]]$b, [int]$o) { [UInt32]$b[$o] -bor ([UInt32]$b[$o+1] -shl 8) -bor ([UInt32]$b[$o+2] -shl 16) -bor ([UInt32]$b[$o+3] -shl 24) }
|
2026-10-02 21:07:05 +08:00
|
|
|
|
|
|
|
|
$shoff = U64 $bytes 0x28
|
|
|
|
|
$shentsize = U16 $bytes 0x3A
|
|
|
|
|
$shnum = U16 $bytes 0x3C
|
|
|
|
|
|
|
|
|
|
$textOff = 0; $textSize = 0; $hashOff = 0
|
2026-10-04 22:43:18 +08:00
|
|
|
$shstr = $null
|
2026-10-02 21:07:05 +08:00
|
|
|
for ($i = 0; $i -lt $shnum; $i++) {
|
|
|
|
|
$sh = $shoff + $i * $shentsize
|
|
|
|
|
$shType = U32 $bytes ($sh + 4)
|
|
|
|
|
$shOffset = U64 $bytes ($sh + 24)
|
|
|
|
|
$shSize = U64 $bytes ($sh + 32)
|
2026-10-04 22:43:18 +08:00
|
|
|
if ($shType -eq 3 -and -not $shstr) { $shstr = $bytes[[int]$shOffset..[int]($shOffset+$shSize-1)] }
|
2026-10-02 21:07:05 +08:00
|
|
|
}
|
|
|
|
|
|
2026-10-04 22:43:18 +08:00
|
|
|
function Get-SectName([byte[]]$strtab, [int]$idx) {
|
2026-10-02 21:07:05 +08:00
|
|
|
$end = $idx
|
2026-10-04 22:43:18 +08:00
|
|
|
while ($strtab[$end] -ne 0) { $end++ }
|
|
|
|
|
[System.Text.Encoding]::ASCII.GetString($strtab, $idx, $end - $idx)
|
2026-10-02 21:07:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for ($i = 0; $i -lt $shnum; $i++) {
|
|
|
|
|
$sh = $shoff + $i * $shentsize
|
|
|
|
|
$nameIdx = U32 $bytes $sh
|
|
|
|
|
$shType = U32 $bytes ($sh + 4)
|
|
|
|
|
$shOffset = U64 $bytes ($sh + 24)
|
|
|
|
|
$shSize = U64 $bytes ($sh + 32)
|
2026-10-04 22:43:18 +08:00
|
|
|
$nm = Get-SectName $shstr $nameIdx
|
2026-10-02 21:07:05 +08:00
|
|
|
if ($nm -eq '.text' -and $shType -eq 1) { $textOff = $shOffset; $textSize = $shSize }
|
|
|
|
|
if ($nm -eq '.hashseg') { $hashOff = $shOffset }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($textOff -eq 0 -or $hashOff -eq 0) { Write-Error 'section .text/.hashseg not found'; exit 1 }
|
|
|
|
|
|
|
|
|
|
$sha = [System.Security.Cryptography.SHA256]::Create()
|
|
|
|
|
$textBytes = New-Object byte[] $textSize
|
|
|
|
|
[Array]::Copy($bytes, [int]$textOff, $textBytes, 0, $textSize)
|
|
|
|
|
$hash = $sha.ComputeHash($textBytes)
|
|
|
|
|
|
|
|
|
|
[Array]::Copy($hash, 0, $bytes, [int]$hashOff, 32)
|
|
|
|
|
[System.IO.File]::WriteAllBytes($ElfPath, $bytes)
|
2026-10-04 22:43:18 +08:00
|
|
|
$hex = ($hash[0..3] | ForEach-Object { $_.ToString('x2') }) -join ''
|
|
|
|
|
Write-Host (".text(" + $textSize + " B) SHA256 -> .hashseg @ " + $hashOff + " : " + $hex + "...")
|
2026-10-02 21:07:05 +08:00
|
|
|
exit 0
|