ctfs_tech_pie - determined6730/public GitHub Wiki
PIE(Position Independent Executable)
- pie๊ฐ ๊ฑธ๋ฆฐ๊ฒ๊ณผ ์๊ฑธ๋ฆฐ๊ฒ์ ์ฐจ์ด
- aslr๊ณผ์ ์ฐ๊ด๊ด๊ณ ์ค๋ช ์์คํ ์ aslr์ด ๊บผ์ ธ์์ ๊ฒฝ์ฐ ? ์ผ์์ ๊ฒฝ์ฐ ์ฐจ์ด?
PIE
์์น ๋
๋ฆฝ ์คํ ํ์ผ๋ก ์คํ๋ ๋๋ง๋ค ๋งคํ๋๋ ์ฃผ์๊ฐ ์ด๋๋ ์๊ด์์ด ์คํ๋๋ ํ์ผ
๋งตํ๋๋ ์ฃผ์์ ์๊ด์์ด ์คํ์ด ๊ฐ๋ฅํ๋จ ๋ป์ symbol๋ค์ ์ฃผ์๊ฐ์ด ์ ๋๊ฐ์ด ์๋๋ผ ์๋๊ฐ(offset)์ด๋ ๊ฒ
PIE๊ฐ ๊ฑธ๋ฆฐ ํ์ผ๊ณผ ์๊ฑธ๋ฆฐ ํ์ผ์ ๋ณด๋ฉด ์ข๋ ํ์ฐํ ์ฐจ์ด๋ฅผ ์ ์ ์์
##TODOLIST It turns out to be quite simple to create a PIE: a PIE is simply an executable shared library. To make a shared library executable you just need to give it a PT_INTERP segment and appropriate startup code. The startup code can be the same as the usual executable startup code, though of course it must be compiled to be position independent.
PIE build option
# PIE ์ ์ฉ ( ubuntu 18.04 , gcc 7.4 ์์ default๋ก PIE๊ฐ ์ ์ฉ๋์ด ์์. )
gcc -fPIE -o pie pie.c
gcc -o pie pie.c
# PIE ํด์
gcc -no-pie -o no_pie pie.c
PIE vs no_PIE
file ๋ช
๋ น ๋๋ checksec์ ํตํด์ ์ฐจ์ด๋ฅผ ํ์ธ ํ ์ ์์ผ๋ฉฐ, disassemble์ ํตํด์๋ ํ์ธ ๊ฐ๋ฅ

symbol์ด ๋งตํ๋ ์ฃผ์๋ฅผ ๋ณด๊ฒ ๋๋ฉด PIE๊ฐ ์ ์ฉ๋ ๊ฒฝ์ฐ offset ๊ฐ์ผ๋ก ๋งตํ๋์ด ์์ผ๋ฉฐ, PIE๊ฐ ์ ์ฉ ์๋์์ ๋์๋ ์ ๋๊ฐ์ผ๋ก ๋งตํ๋์ด ์์.gdb๋ฅผ ํตํด์ ์ค์ ์คํ๋ ๋ ์ฃผ์๊ฐ์ ํ์ธํด ๋ณด๋ฉด PIE๊ฐ ์ ์ฉ๋์ง ์์ ์คํํ์ผ์ disassembleํด์ ๋ณด์ธ ์ฃผ์๊ฐ์ด ๊ทธ๋๋ก ์ฌ์ฉ๋๋ฉฐ PIE ๊ฐ ์ ์ฉ๋ ํ์ผ์ ์คํ๋ ๋ ๊ฒฐ์ ๋ image base address์ ํด๋น symbol์ ์ฃผ์๊ฐ(offset)์ด ๋ํด์ ธ ์ฌ์ฉ๋๋ค.
references
- PIC ๊ด๋ จ artical : https://eli.thegreenplace.net/2011/11/03/position-independent-code-pic-in-shared-libraries
- PIC ๊ด๋ จ x64 article : https://eli.thegreenplace.net/2011/11/03/position-independent-code-pic-in-shared-libraries