![]() |
|
[Tutorial] ~ Writing Custom ASCII Shellcode (Part I) - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Hacks, Exploits, and Various Discussions (https://hackforums.net/forumdisplay.php?fid=45) +--- Forum: Whitehat • Blue Team • Defensive Security (https://hackforums.net/forumdisplay.php?fid=448) +---- Forum: Pentesting and Forensics (https://hackforums.net/forumdisplay.php?fid=231) +---- Thread: [Tutorial] ~ Writing Custom ASCII Shellcode (Part I) (/showthread.php?tid=4241874) |
[Tutorial] ~ Writing Custom ASCII Shellcode (Part I) - Goodies - Jul 20, 2015 Hello everyone! This is going to be a very difficult tutorial so this may require that you have some knowledge in shellcode writing and basic assembly. This is the process I went through my very first time writing ASCII polymorphic code so please bear with me. If anything is incorrect or may be improved, please let me know. We are going to be creating a standard local Linux shellcode (you can find a tutorial I wrote here): https://hackforums.net/showthread.php?tid=3995986 First, we will construct a loader that will make room on the stack, copy our payload, and execute it all on the stack at runtime. This is the original shellcode: PHP Code Our goal is to create shellcode that does this exact thing but ONLY using instructions whose opcode is a printable ASCII character. Why ASCII Shellcode? There are a couple main reasons why ASCII shellcode can be extremely beneficial to an exploit (especially a remote exploit):
ASCII List Through a simple C file, I generated all of the ASCII characters and their values: Code 0x21 = 0x22 = ! 0x23 = " 0x24 = #Note: For some reason, I have been using 0x7e as the highest value. Don't worry, nothing needs to be changed, but just note that you can go up to 7F, not 7E. My apologies. C File Source: (Click to View) Basics of Finding ASCII Instructions/Values Note: Before I continue, you should know how I am getting the opcode of my commands. I put the command in a single ASM source file (1.asm) and run: nasm 1.asm -f bin hexdump -C 1 If you see any bytes with the value "66", ignore it. It tells the compiler to use 16-bit instructions rather than 32 bit. Chances are, it won't happen to you. Right off the bat, we see that many commands are not encoded in ASCII. For example, we need to clear the EAX register because, since we are overwriting a return address, there may have been something moved into EAX as a return value. If we look at the opcode for: PHP Code We need to think of ways to clear out the EAX register. One very common way is to use the AND operator with EAX, then AND it again with the exact opposite number. The thing is, we need to find two numbers who are both opposites and in the range 0x20 and 0x7E. So, let's go ahead and try this out. I wrote another C algorithm to go ahead and check this for me ;) This took longer than you might think... C FIle Source: PHP Code How to use it: If you just want to generate a list, change "#define SHOW 0" to "#define SHOW 1". This will generate output that will tell you which ASCII characters have opposites in the ASCII range. I'll use the first output: 21 (!) , 5e (^) What we would have to do now is replace "NUM_1_HERE" with 0x21212121 and "NUM_2_HERE" with 0x5e5e5e5e simply to ensure that they are, in fact, opposites. Be sure to change SHOW back to 0. Let's go ahead and compile our new ASM file to see if our code is within the ASCII boundaries: Code ;// ASM filehexdump -C 1 On the left, we see (after removing 0x66's): %!!!!%^^^^ Perfect! All of those are valid, printable ASCII characters that should not be blocked by any program. If they are, there are several more that you can try that are both alphanumeric (such as 38 (8) , 47 (G)). This works just as well. Makin' Room On The Stack This is going to be a huge pain in the ass if you've never worked with shellcode before. This code is polymorphic so our loader will decode and execute the memory on the stack. However, we MUST make an offset large enough for our loader. I'm going to use 1,000 bytes and the rest will be filled with NOPs (note that \x90 is not an ASCII value. We'll cross that bridge later). 1. How do we add space to the stack? That's simple! All we have to do is run: PHP Code Well, there are other things we can do. For example, we can move ESP into EAX and subtract the value from there, except the mov command is not within ASCII range. The only way that I see this is possible is by pushing ESP onto the stack and popping it into EAX. PHP Code Hooray! All of those work. Now, we need to subtract 1,000 bytes of space from EAX (ESP's value). However, this is going to be a problem because it doesn't have NULL bytes. Oh but wait, there's more! If any of you are familiar with TCP/IP stack attacks, there is an attack called NAT pinning. I'll spare you the details, but essentially, some browsers are blocking IRC ports due to cross protocol scripting attacks that the NAT pinning uses. They block port 6667. However, since a port is only 16 bits long, if we add 65566 (2^16) to 6667, we get 72233. Since it will only read the lowest 16 bits, the value is actually used as 6667. We are going to follow the same logic in reverse with our attack code. We are using 32 bit registers, right? 2^32 = 4294967296. If we subtract 1000 from that number, we can subtract THAT number from the register which will actually add more to it. Here's how it actually looks in binary: (Click to View) We need to subtract (2^32 - 1000) from EAX (which holds ESP's value). We compile it, but something is wrong: PHP Code PHP Code Note that, if we do any subtraction now, we must subtract at LEAST 0x20 from each byte or we will have either NULLs or bytes that are not in ASCII range. This won't be a problem. Now we need to subtract values to make sure they BOTH fit in ASCII range. (Reverse the order because of little endian) 0x81817d91 - 0x?? = 0x?? 0x81 - 0x30 = 51 (ASCII) 0x81 - 0x30 = 51 (ASCII) 0x7d - 0x30 = 4d (ASCII) 0x9a - 0x30 = 6a (ASCII) Therefore, our three numbers are now:
Our file so far: PHP Code We have the first part of our loader! Now we need to write the portion that actually spawns a shell. Let's go! Encoding Our Shellcode: We already have shellcode that we know works (thanks to my tutorial at the top of this thread) so, instead of recreating it in this tutorial, I'll just assume you read it (lol). I'll be testing our shellcode with this C file: PHP Code Our shellcode: Code \xeb\x16\x5b\x31\xc0\x88\x43\x07\x89\x5b\x08\x89\x43\x0c\xb0\x0b\x8d\x4b\x08\x8d\x53\x0c\xcd\x80\xe8\xe5\xff\xff\xff\x2f\x62\x69\x6e\x2f\x73\x68Since this is little endian, we must change this. After I changed it to little endian, I separated it into groups of 4 bytes like this: PHP Code All of these values need to be pushed onto the stack. If we do this, our shellcode will be able to execute! The only issues are:
* - Mastery - Jul 20, 2015 Great tutorial. You're a very skilled and smart pentester, one of the few around here. * - tendercareaustin - Jul 20, 2015 If only you can add exploits that can change previledges using local exploits.Some Shells are without "drwxrwxrwx" permissions.I wish I cn get some assistance on local exploits. * - Goodies - Jul 20, 2015 (Jul 20, 2015 10:46 AM)tendercareaustin Wrote: If only you can add exploits that can change previledges using local exploits.Some Shells are without "drwxrwxrwx" permissions.I wish I cn get some assistance on local exploits. That isn't really the point of this thread. You can use ROP gadgets to allocate rwx memory, copy shellocee, and execute from there. All of that should be able to be done in ASCII as well. * - Goodies - Jul 21, 2015 (Jul 20, 2015 10:46 AM)tendercareaustin Wrote: If only you can add exploits that can change previledges using local exploits.Some Shells are without "drwxrwxrwx" permissions.I wish I cn get some assistance on local exploits. I like your name, though. But yeah, look into ROP. * - PhantomElite - Jul 25, 2015 Good thread HQ user wish there was more of this stuff on HF. * - SIM-1866423 - Jul 27, 2015 You and ur tutorials always HQ. Congratulations 🤝 * - fLQZ - Aug 24, 2015 Nice man , thanks. Keep up doing! * - Langer343 - Nov 6, 2015 Damn son, this is really nice, keep up the great work! * - R^ptor - May 10, 2016 Fantastic tut always HQ!! * - roissy - Aug 2, 2016 thank you. amazing tutorials 🤝 * - drogba180 - Apr 1, 2017 Great tutorial. You're a very skilled and smart pentester * - Nomad.Null - Apr 1, 2017 Very nice tutorial, Goodies. You going for OSCE yet? |