Unsafe Relocation Fixups in Asset Loaders
Tip
AWS ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training AWS Red Team Expert (ARTE)
GCP ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:HackTricks Training GCP Red Team Expert (GRTE)
Azure ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks ์ง์ํ๊ธฐ
- ๊ตฌ๋ ๊ณํ ํ์ธํ๊ธฐ!
- **๐ฌ ๋์ค์ฝ๋ ๊ทธ๋ฃน ๋๋ ํ ๋ ๊ทธ๋จ ๊ทธ๋ฃน์ ์ฐธ์ฌํ๊ฑฐ๋ ํธ์ํฐ ๐ฆ @hacktricks_live๋ฅผ ํ๋ก์ฐํ์ธ์.
- HackTricks ๋ฐ HackTricks Cloud ๊นํ๋ธ ๋ฆฌํฌ์งํ ๋ฆฌ์ PR์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.
์ asset relocations๊ฐ ์ค์ํ๊ฐ
๋ง์ ๋ ๊ฑฐ์ ๊ฒ์ ์์ง(Granny 3D, Gamebryo ๋ฑ)์ ๋ณต์กํ assets๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ๋ก๋ํ๋ค:
- ํค๋์ ์น์ ํ ์ด๋ธ์ ํ์ฑํ๋ค.
- ์น์ ๋ง๋ค ํ ๋ฒํผ๋ฅผ ํ๋ ํ ๋นํ๋ค.
- ๋ชจ๋ ์น์
์ base pointer๋ฅผ ์ ์ฅํ๋
SectionArray๋ฅผ ๊ตฌ์ถํ๋ค. - ์น์ ๋ฐ์ดํฐ ๋ด๋ถ์ ํฌํจ๋ ํฌ์ธํฐ๋ค์ด ์ฌ๋ฐ๋ฅธ ๋์ ์น์ + ์คํ์ ์ผ๋ก ํจ์น๋๋๋ก relocation tables๋ฅผ ์ ์ฉํ๋ค.
relocation ํธ๋ค๋ฌ๊ฐ ๊ณต๊ฒฉ์๊ฐ ์ ์ดํ๋ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ๋งน์ ํ๋ฉด, ๋ชจ๋ relocation์ ์ ์ฌ์ ์ธ ์์ ์ฝ๊ธฐ/์ฐ๊ธฐ ํ๋ฆฌ๋ฏธํฐ๋ธ๊ฐ ๋๋ค. Anno 1404: Venice์์, granny2.dll์ ๋ค์ ํฌํผ๋ฅผ ์ ๊ณตํ๋ค:
`GrannyGRNFixUp_0` (์๋ต๋จ)
```c int *__cdecl GrannyGRNFixUp_0(DWORD RelocationCount, Relocation *PointerFixupArray, int *SectionArray, char *destination) { while (RelocationCount--) { int target_base = SectionArray[PointerFixupArray->SectionNumber]; // unchecked index int *patch_site = (int *)(destination + PointerFixupArray->SectionOffset); // unchecked offset *patch_site = target_base ; if (target_base) *patch_site = target_base + PointerFixupArray->Offset; ++PointerFixupArray; } return SectionArray; } ```SectionNumber์ ๋ฒ์ ๊ฒ์ฌ๋์ง ์์ผ๋ฉฐ SectionOffset์ ํ์ฌ ์น์
ํฌ๊ธฐ์ ๋ํด ๊ฒ์ฆ๋์ง ์์ต๋๋ค. ์์ ์คํ์
์ด๋ ๊ณผ๋ํ ์ธ๋ฑ์ค๋ฅผ ๊ฐ์ง relocation entries๋ฅผ ์กฐ์ํ๋ฉด ์ ์ดํ๋ ์น์
๋ฐ์ผ๋ก ๋ฒ์ด๋ ์น์
ํฌ์ธํฐ ๋ฐฐ์ด ์์ฒด์ ๊ฐ์ allocator metadata๋ฅผ ์ง๋ฐ์ ์ ์์ต๋๋ค.
Stage 1 โ ๋ก๋ ๋ฉํ๋ฐ์ดํฐ๋ก ์ญ๋ฐฉํฅ ์ฐ๊ธฐ
๋ชฉํ๋ section 0์ relocation table์ด SectionContentArray์ ์ํธ๋ฆฌ๋ค์ ๋ฎ์ด์ฐ๊ฒ ๋ง๋๋ ๊ฒ์
๋๋ค(SectionContentArray๋ SectionArray๋ฅผ ๋ฏธ๋ฌ๋งํ๋ฉฐ ์ฒซ ๋ฒ์งธ ์น์
๋ฒํผ ๋ฐ๋ก ์์ ์ ์ฅ๋ฉ๋๋ค). Grannyโs custom allocator๊ฐ ์์ 0x1F ๋ฐ์ดํธ๋ฅผ ์ถ๊ฐํ๊ณ NT heap์ด ์์ฒด 0x10-๋ฐ์ดํธ ํค๋์ ์ ๋ ฌ์ ๋ํ๊ธฐ ๋๋ฌธ์, ๊ณต๊ฒฉ์๋ ์ฒซ ๋ฒ์งธ ์น์
์ ์์(destination)๊ณผ ์น์
-ํฌ์ธํฐ ๋ฐฐ์ด ์ฌ์ด์ ๊ฑฐ๋ฆฌ๋ฅผ ๋ฏธ๋ฆฌ ๊ณ์ฐํ ์ ์์ต๋๋ค.
ํ
์คํธ๋ ๋น๋์์๋ ๋ก๋๊ฐ ์ ํํ 0x4000 bytes์ธ GrannyFile ๊ตฌ์กฐ๋ฅผ ํ ๋นํ๋๋ก ๊ฐ์ ํ๋ฉด ์น์
-ํฌ์ธํฐ ๋ฐฐ์ด์ด ์ฒซ ๋ฒ์งธ ์น์
๋ฒํผ ๋ฐ๋ก ์์ ์์นํ๊ฒ ๋ฉ๋๋ค. Solving
0x20 (header) + 0x20 (section descriptors)
+ n * 1 (section types) + n * 1 (flags)
+ n * 4 (pointer table) = 0x4000
gives n = 2720 sections. A relocation entry with SectionOffset = -0x3FF0 ( 0x4000 - 0x20 - 0x20 + 0x30 ) now resolves to SectionContentArray[1] even though the destination section thinks it is patching internal pointers.
Stage 2 โ Windows 10์์ ๊ฒฐ์ ๋ก ์ ํ ๋ ์ด์์
Windows 10 NT Heap๋ โค RtlpLargestLfhBlock (0x4000) ์ธ ํ ๋น์ ๋๋คํ๋ LFH๋ก, ๊ทธ๋ณด๋ค ํฐ ํ ๋น์ ๊ฒฐ์ ๋ก ์ ๋ฐฑ์๋ ํ ๋น์๋ก ๋ณด๋ธ๋ค. GrannyFile ๋ฉํ๋ฐ์ดํฐ๋ฅผ ๊ทธ ์๊ณ๊ฐ๋ณด๋ค ์ฝ๊ฐ ํฌ๊ฒ ์ ์ง(2720 ์น์
ํธ๋ฆญ ์ฌ์ฉ)ํ๊ณ ์
์ฑ .gr2 ์์
์ ์ฌ๋ฌ ๊ฐ ๋ฏธ๋ฆฌ ๋ก๋ํ๋ฉด ๋ค์์ ๋ง๋ค ์ ์๋ค:
- Allocation #1 (metadata + section pointer arrays)๊ฐ >0x4000 ๋ฐฑ์๋ ์ฒญํฌ์ ๋ฐฐ์น๋๋ค.
- Allocation #2 (section 0 contents)๊ฐ Allocation #1 ๋ฐ๋ก ๋ค์์ ๋ฐฐ์น๋๋ค.
- Allocation #3 (section 1 contents)๊ฐ Allocation #2 ๋ฐ๋ก ๋ค์ ๋ฐฐ์น๋์ด ์ดํ ์ฌ๋ฐฐ์น(relocations)์ ๋ํด ์์ธก ๊ฐ๋ฅํ ํ๊น์ ์ ๊ณตํ๋ค.
Process Monitor๋ ์์ ์ด ์จ๋๋งจ๋๋ก ์คํธ๋ฆฌ๋ฐ๋๋ค๋ ๊ฒ์ ํ์ธํ๋ค. ๋ฐ๋ผ์ ์กฐ์๋ ์ ๋/๊ฑด๋ฌผ์ ๋ฐ๋ณต์ ์ผ๋ก ์์ฒญํ๋ฉด ์คํ ํ์ผ ์ด๋ฏธ์ง๋ฅผ ๊ฑด๋๋ฆฌ์ง ์๊ณ ๋ ํ ๋ ์ด์์์ โํ๋ผ์โํ๋ ๋ฐ ์ถฉ๋ถํ๋ค.
Stage 3 โ ํ๋ฆฌ๋ฏธํฐ๋ธ๋ฅผ RCE๋ก ์ ํ
- Corrupt
SectionContentArray[1]. Section 0์ ์ฌ๋ฐฐ์น ํ ์ด๋ธ์ด-0x3FF0์คํ์ ์ ์ฌ์ฉํด ์ด๋ฅผ ๋ฎ์ด์ด๋ค. ์ด๋ฅผ ์ ์ด ๊ฐ๋ฅํ ์ฐ๊ธฐ ๊ฐ๋ฅํ ์์ญ(์: ์ดํ ์น์ ๋ฐ์ดํฐ)์ ๊ฐ๋ฆฌํค๊ฒ ํ๋ผ. - Recycle the corrupted pointer. ์ด์ Section 1์ ์ฌ๋ฐฐ์น ํ
์ด๋ธ์
SectionNumber = 1์ ๋น์ ์ด ์ฃผ์ ํ ์์์ ํฌ์ธํฐ๋ก ์ทจ๊ธํ๋ค. ํธ๋ค๋ฌ๋SectionArray[1] + Offset์destination + SectionOffset์ ์ฐ๋ฏ๋ก ๊ฐ ์ฌ๋ฐฐ์น ์ํธ๋ฆฌ๋ง๋ค ์์์ 4๋ฐ์ดํธ ์ฐ๊ธฐ๊ฐ ๊ฐ๋ฅํด์ง๋ค. - Hit reliable dispatchers. Anno 1404์์๋ ๋์ ์ ํ์ผ๋ก
granny2.dll์ allocator ์ฝ๋ฐฑ์ด ์ฌ์ฉ๋์๋ค(ASLR ์์, DEP ๋นํ์ฑ). ๋ค์Malloc/Freeํธ์ถ์granny2.dll์ด ์ฌ์ฉํ๋ ํจ์ ํฌ์ธํฐ๋ฅผ ๋ฎ์ด์ฐ๋ฉด ํธ๋ก์ดํ๋ ์์ ์์ ๋ก๋๋ ๊ณต๊ฒฉ์ ์ ์ด ์ฝ๋๋ก ์ฆ์ ์คํ์ด ์ ํ๋๋ค.
ASLR/DEP๊ฐ ๋นํ์ฑํ๋ ์ํ์์๋ granny2.dll๊ณผ ์ฃผ์
๋ .gr2 ๋ฒํผ๊ฐ ์์ ์ ์ธ ์ฃผ์์ ์กด์ฌํ๋ฏ๋ก, ๊ณต๊ฒฉ์ ์์ ROP ์ฒด์ธ์ด๋ ์์ ์์ฝ๋๋ฅผ ๊ตฌ์ฑํ๊ณ ์ฝ๋ฐฑ์ ๊ทธ์ชฝ์ผ๋ก ๊ฐ๋ฆฌํค๊ฒ ํ๋ ๊ฒ์ผ๋ก ๊ท๊ฒฐ๋๋ค.
์ค์ ์ฒดํฌ๋ฆฌ์คํธ
- SectionArray / ์ฌ๋ฐฐ์น ํ ์ด๋ธ์ ์ ์งํ๋ ์์ ๋ก๋๋ฅผ ์ฐพ์๋ผ.
- ์ธ๋ฑ์ค/์คํ์ ์ ๋ํ ๊ฒฝ๊ณ ๊ฒ์ฌ๊ฐ ๋๋ฝ๋ ์ฌ๋ฐฐ์น ํธ๋ค๋ฌ๋ฅผ diffํ์ฌ ์ฐพ์๋ผ.
- ๊ฒ์์ allocator ๋ํผ์ ๊ธฐ์ OS ํ์ด ์ถ๊ฐํ๋ allocator ํค๋๋ฅผ ์ธก์ ํด ์ญ๋ฐฉํฅ ์คํ์ ์ ์ ํํ ๊ณ์ฐํ๋ผ.
- ๊ฒฐ์ ๋ก ์ ๋ฐฐ์น๋ฅผ ๊ฐ์ ํ๋ ค๋ฉด:
- ๋ฉํ๋ฐ์ดํฐ๋ฅผ ๋๋ ค(์ฌ๋ฌ ๋น ์น์
) ํ ๋น ํฌ๊ธฐ๊ฐ
RtlpLargestLfhBlock๋ณด๋ค ์ปค์ง๊ฒ ํ๋ค; - ์ ์ฑ ์์ ์ ๋ฐ๋ณต ๋ก๋ํด ๋ฐฑ์๋์ ๋นํ์ ์ฑ์ด๋ค.
- ๋ฉํ๋ฐ์ดํฐ๋ฅผ ๋๋ ค(์ฌ๋ฌ ๋น ์น์
) ํ ๋น ํฌ๊ธฐ๊ฐ
- 2๋จ๊ณ ์ฌ๋ฐฐ์น ํ
์ด๋ธ์ ์ฌ์ฉํ๋ผ(๋จผ์
SectionArray๋ฅผ ์ฌํ๊นํ ํ๊ณ , ๋๋ฒ์งธ๋ ์ฐ๊ธฐ ์คํ๋ ์ด) ๊ทธ๋ฆฌ๊ณ ์ ์ ๋ ๋๋ง ์ค์ ์คํ๋ ํจ์ ํฌ์ธํฐ๋ค(allocator ์ฝ๋ฐฑ, ๊ฐ์ ํ ์ด๋ธ, ์ ๋๋ฉ์ด์ ๋์คํจ์ฒ ๋ฑ)์ ๋ฎ์ด์จ๋ผ.
์์์ ํ์ผ ์ฐ๊ธฐ ๊ถํ์ ์ป์ผ๋ฉด(์: ๋ฉํฐํ๋ ์ด์ด ์ธ์ด๋ธ ์ ์ก์ ๊ฒฝ๋ก ํ์์ ํตํด) ์กฐ์๋ .gr2๋ก RDA ์์นด์ด๋ธ๋ฅผ ์ฌํจํค์งํ๋ ๊ฒ์ ์๊ฒฉ ํด๋ผ์ด์ธํธ๊ฐ ์๋์ผ๋ก ์์ถ ํด์ ํ๋ ๊น๋ํ ์ ๋ฌ ๋ฒกํฐ๋ฅผ ์ ๊ณตํ๋ค.
์ฐธ๊ณ ์๋ฃ
- Synacktiv โ Exploiting Anno 1404
- W. Yason โ Windows 10 Segment Heap Internals (BlackHat USA 2016)
Tip
AWS ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training AWS Red Team Expert (ARTE)
GCP ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:HackTricks Training GCP Red Team Expert (GRTE)
Azure ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks ์ง์ํ๊ธฐ
- ๊ตฌ๋ ๊ณํ ํ์ธํ๊ธฐ!
- **๐ฌ ๋์ค์ฝ๋ ๊ทธ๋ฃน ๋๋ ํ ๋ ๊ทธ๋จ ๊ทธ๋ฃน์ ์ฐธ์ฌํ๊ฑฐ๋ ํธ์ํฐ ๐ฆ @hacktricks_live๋ฅผ ํ๋ก์ฐํ์ธ์.
- HackTricks ๋ฐ HackTricks Cloud ๊นํ๋ธ ๋ฆฌํฌ์งํ ๋ฆฌ์ PR์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.


