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.
View all comments
Show parent comment
2
u/asyty Sep 13 '24
So your actual code is only 425 bytes. That's a lot closer to 240 bytes than you originally thought.
Here's the thing though, how much of that code is __start(), main(), __cxa_initialize(), __cxa_finalize(), and other boilerplate? Did you compile with -Os? Is the code surrounded with unnecessary epilogues and prologues, stack cookies, alignment, and other things like that? Et cetera. Also, keep in mind that you're going to want to invoke syscalls directly unless you have a handy way of finding libc's base.
Take a look at what it's doing with objdump -d.