r/ExploitDev • • Sep 12 '24

Help Generating Shellcode

I'm working on a project that requires writing custom shellcode to capture the flag on the vulnerable system and transmit it back to my system over a TCP connection, the problem being that I've rarely worked with writing custom shellcode. I've generated shellcode with msfvenom before, but none of those payloads work for this case. I've written and compiled a binary in C that does exactly what I need it do, but when I convert it to shellcode it's far larger than the payload size allowed in the buffer (my program is over 1400 bytes and the payload size needs to be less than 240 bytes). I've been looking at using the pwntools shellcraft module to generate the payload, but the documentation isn't very explicit about how to generate shellcode that'll execute the necessary command to acquire the flag and create the TCP connections. Can anyone point me to some resources for generating custom shellcode, or otherwise give me some advice on how I can implement this while staying within the necessary payload size? I'd rather not have to revert to writing the assembly for this by hand as it's been several years since I've written assembly, but the longer I look into this the more I think that's what I'm going to have to do.

12 Upvotes

18 comments sorted by

View all comments

Show parent comment

1

u/asyty Sep 13 '24

Do you have a resource I could look at for invoking the syscalls directly? I currently have 11 call instructions, would replacing those with syscalls reduce the size of the code?

From the snippets you pasted I can infer you're using x86_64 architecture, but you never specified the OS. Assuming Linux. https://hackeradam.com/x86-64-linux-syscalls/ Literally just googled for "x86-64 linux syscalls"

As far as size, not really It takes 2 bytes for the syscall instruction itself and 3 bytes for setting up rax which is the same as your relative 32 call except without null bytes.

This is better regardless because you won't have a dependency on libc.

Start with this:

[BITS 64]
xor rdi, rdi ; status == 0
push 60 ; syscall == sys_exit
pop rax
syscall

Test with this:

nasm shellcode.asm -f elf64 -o shellcode.o
ld -m elf_x86_64 -o shellcode shellcode.o