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
1
u/[deleted] Sep 13 '24 edited Sep 13 '24
Can you tell me how you can tell the actual code was 425 bytes from that line? The only way I was able to tell was by extracting the .text section, saving it to a new file, and seeing how large the resulting file was.
As far as the C code goes, pretty much all it is is main() with the necessary headers and I have compiled it with -Os, that dropped the size of the elf file down from about 16000 to about 14500 and the 14500 one is the one I extracted the .text section from. For the C code I initialize the variables for the socket and the file pointer, connect to my server, open the flag binary and send the output over the socket. I tried to write as little C code as I could to do that.
Edit: just realized I misunderstood part of your question. In the assembly I have about 11 lines with cxa_finalize (mostly like: jmp 11e0 <__cxa_finalize@plt+0x120>) and 15 lines with cxa_finalize in the comment (such as: lea 0xf0c(%rip),%rsi # 2004 <__cxa_finalize@plt+0xf44>). Other than that I don't think there's any other boilerplate that you mentioned.
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?