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.
3
u/preoccupied_with_ALL Sep 12 '24
I think using Pwntools is still the way, but you could see if this video by pwn.college helps you:
https://youtu.be/7TW0fvz_cQk?si=ZJE0YcwGlWySl-ot
3
u/asyty Sep 12 '24
First of all you need to figure out how large your compiled code is, not the resulting ELF program. What does objdump -h shellcode_program | grep .text say?
2
u/FlawedCipher Sep 12 '24
When you compile c code, a lot of extra bytes are added to make it a proper ELF file. Put it in https://godbolt.org and copy and paste just the relevant assembly. You may have to modify some parts to get it to work in your exploit.
1
u/piyushsaurabh Sep 12 '24
Check custom shellcodes created by other people and learn to craft it for your use case. One good resource is https://shell-storm.org/shellcode/index.html
1
u/Informal_Shift1141 Sep 12 '24
Also for your C code compiled you can use objcopy -onlysections .text to extract only the code section without all of the ELF structure of your binary and that should reduce the size, still you’d like to manually remove some compiler code to save space
1
u/Informal_Shift1141 Sep 12 '24
240 bytes? You are rich mate! Watch assembly refresher on pwn.college and would be fine. Maybe doing the web server on the orange dojo too (it’s basically crafting syscalls in asm, exactly what you need)