Writing my first shellcode
0day.work
0day.work
http://www.pentesteracademy.com/course?id=3 http://www.pentesteracademy.com/course?id=7
There are many free videos in this collection.
(*(void(*)()) shellcode)();
and suddenly this looks way more interesting. Is it really possible to literally just execute random bytes stored in a string like that? I mean, sure, you'd have to guarantee that it's running on the right platform with the right type of assembly, but still. This is fascinating! Plus, I don't even see execve actually pushed anywhere! Is that because of int 0x80? Wow this stuff is neat! I'm whelmed.Aside, is there any using HN syntax to write that code inline (i.e. not in its own paragraph) without the asterisks insisting that I mean italics?
(*(void(*)()) shellcode)();
> and suddenly this looks way more interesting. Is it really possible to literally just execute random bytes stored in a string like that?Well it's just a sequence of bytes somewhere in-memory, no? If the C compiler allows the cast, then it'd compile fine, and the CPU wouldn't know the difference; it'd just execute whatever instructions the sequence of bytes listed.
Because DEP aka W^X will mark the section of memory containing the string as non-executable, so it will segfault when the instruction pointer hits that address. You can disable that security feature though with a compiler option.
DEP is a kernel-level security feature no? Since the kernel is what sets up the .text and .data sections.
As for OpenBSD and Linux without grsec/pax, one can bypass NX (whether the CPU has the NX-bit or not) by marking the region with the shellcode as executable, eg:
mprotect(shellcode & -pagesize, len, PROT_EXEC);
((void()()) shellcode)();
in an exploit this could be accomplished by ROPing
1: http://marc.info/?l=openbsd-misc&m=105056000801065
2: https://pax.grsecurity.net/docs/mprotect.txt
3: https://outflux.net/blog/archives/2009/05/14/nx-emulation-in...
DEP is just how Microsoft calls executable space protection. That's not industry standard terminology for it.
execve does nothing more than swap out which bytes are loaded into the address space of a program (okay, it does some other book-keeping too, but from 100 feet...). It is not necessary for continuing execution outside of what the program has set up at compile-time. In fact, you can dynamically generate code on the fly, as JIT compilers do.
It's a series of challenges by tptacek that task you with exploiting the firmware for a digital smart lock. It starts out by assuming no knowledge, with the first level literally just requiring that you read a memory dump, but by the last level you'll be reverse engineering custom heap implementations, injecting shellcode into ASLR'd binaries, and bypassing memory protections.
It really is the best introduction to this sort of material that I've ever come across.
This 'shell code' was the 'payload' of other code that exploited e.g. a buffer overflow; the goal of such an exploit was always to trick some program into executing that payload, i.e. the shell code. So there were/are essentially two parts to writing an exploit - first getting the software to actually execute random code, secondly (if you actually want to use the exploit) write shell code that does something useful to an attacker, like spawn a shell or (as in the OP) drop firewall rules.
As a less objective side note, writing 'shell code' was always considered (in the circles I hung around in) as being less honorable and prestigious. The 'exploit writers' (exploiting the primary bug) were the samurais, the shell code guys the ninjas, as it were. Writing shell code was considered a bit dirty grunt work, and only script kiddies needed it anyway, because after the PoC for the actual exploit was done, the 'interesting' part was over. (I never 100% agreed with this, for some exploits you need some serious trickery to make your PoC actually do something useful).
This kind of thing works on most non-Linux embedded devices. The OS on these is commonly VxWorks or ThreadX.
It works on Linux if you use a 486, Pentium, or 32-bit PowerPC. (and weirder stuff) It works on Linux if you use a kernel that is 10 years old. It works on Linux if you add a *.s file to your build. (an assembly file; empty is fine)
It works on Windows if the program wasn't built with some compiler option I forget. The security is opt-in, so most developers don't bother.