Advanced Shellcoding Techniques
Category: Tutorial
<< Buy This Book on Amazon >>
96 views since 2007-05-18, updated at 2007-05-27.
Description
***********************************************
* *
* Advanced Shellcoding Techniques - by Darawk *
* *
***********************************************
Introduction
This paper assumes a working knowledge of basic shellcoding techniques, and x86 assembly, I will not rehash these in this paper. I hope to teach you some of the lesser known shellcoding techniques that I have picked up, which will allow you to write smaller and better shellcodes. I do not claim to have invented any of these techniques, except for the one that uses the div instruction.
The multiplicity of mul
This technique was originally developed by Sorbo of darkircop.net. The mul instruction may, on the surface, seem mundane, and it's purpose obvious. However, when faced with the difficult challenge of shrinking your shellcode, it proves to be quite useful. First some background information on the mul instruction itself.
mul performs an unsigned multiply of two integers. It takes only one operand, the other is implicitly specified by the êx register. So, a common mul instruction might look something like this:
movl $0x0a,êx
mul $0x0a
This would multiply the value stored in êx by the operand of mul, which in this case would be 10*10. The result is then implicitly stored in EDX:EAX. The result is stored over a span of two registers because it has the potential to be considerably larger than the previous value, possibly exceeding the capacity of a single register(this is also how floating points are stored in some cases, as an interesting sidenote).
So, now comes the ever-important question. How can we use these attributes to our advantage when writing shellcode? Well, let's think for a second, the instruction takes only one operand, therefore, since it is a very common instruction, it will generate only two bytes in our final shellcode. It multiplies whatever is passed to it by the value stored in êx, and stores the value in both íx and êx, completely overwriting the contents of both registers, regardless of whether it is necessary to do so, in order to store the result of the multiplication. Let's put on our mathematician hats for a second, and consider this, what is the only possible result of a multiplication by 0? The answer, as you may have guessed, is 0. I think it's about time for some example code, so here it is:
xorl ìx,ìx
mul ìx
What is this shellcode doing? Well, it 0's out the ìx register using the xor instruction, so we now know that ìx is 0. Then it does a mul ìx, which as we just learned, multiplies it's operand by the value in êx, and then proceeds to store the result of this multiplication in EDX:EAX. So, regardless of êx's previous contents, êx must now be 0. However that's not all, íx is 0'd now too, because, even though no overflow occurs, it still overwrites the íx register with the sign bit(left-most bit) of êx. Using this technique we can zero out three registers in only three bytes, whereas by any other method(that I know of) it would have taken at least six.
The div instruction
Div is very similar to mul, in that it takes only one operand and implicitly divides the operand by the value in êx. Also like, mul it stores the result of the divide in êx. Again, we will require the mathematical side of our brains to figure out how we can take advantage of this instruction. But first, let's think about what is normally stored in the êx register. The êx register holds the return value of functions and/or syscalls. Most syscalls that are used in shellcoding will return -1(on failure) or a positive value of some kind, only rarely will they return 0(though it does occur). So, if we know that after a syscall is performed, êx will have a non-zero value, and that the instruction divl êx will divide êx by itself, and then store the result in êx, we can say that executing the divl êx instruction after a syscall will put the value 1 into êx. So...how is this applicable to shellcoding? Well, their is another important thing that êx is used for, and that is to pass the specific syscall that you would like to call to int $0x80. It just so happens that the syscall that corresponds to the value 1 is exit(). Now for an example:
xorl ëx,ëx
mul ëx
push íx
pushl $0x3268732f
pushl $0x6e69622f
mov %esp, ëx
push íx
push ëx
mov %esp,ìx
movb $0xb, %al #execve() syscall, doesn't return at all unless it fails, in which case it returns -1
int $0x80
divl êx # -1 / -1 = 1
int $0x80
Now, we have a 3 byte exit function, where as before it was 5 bytes. However, there is a catch, what if a syscall does return 0? Well in the odd situation in which that could happen, you could do many different things, like inc êx, dec êx, not êx anything that will make êx non-zero. Some people say that exit's are not important in shellcode, because your code gets executed regardless of whether or not it exits cleanly. They are right too, if you really need to save 3 bytes to fit your shellcode in somewhere, the exit() isn't worth keeping. However, when your code does finish, it will try to execute whatever was after your last instruction, which will most likely produce a SIG ILL(illegal instruction) which is a rather odd error, and will be logged by the system. So, an exit() simply adds an extra layer of stealth to your exploit, so that even if it fails or you can't wipe all the logs, at least this part of your presence will be clear.
Unlocking the power of leal
The leal instruction is an often neglected instruction in shellcode, even though it is quite useful. Consider this short piece of shellcode.
xorl ìx,ìx
leal 0x10(ìx),êx
This will load the value 17 into eax, and clear all of the extraneous bits of eax. This occurs because the leal instruction loads a variable of the type long into it's desitination operand. In it's normal usage, this would load the address of a variable into a register, thus creating a pointer of sorts. However, since ecx is 0'd and 0 17=17, we load the value 17 into eax instead of any kind of actual address. In a normal shellcode we would do something like this, to accomplish the same thing:
xorl êx,êx
movb $0x10,êx
I can hear you saying, but that shellcode is a byte shorter than the leal one, and you're quite right. However, in a real shellcode you may already have to 0 out a register like ecx(or any other register), so the xorl instruction in the leal shellcode isn't counted. Here's an example:
xorl êx,êx
xorl ëx,ëx
movb $0x17,%al
int $0x80
xorl ëx,ëx
leal 0x17(ëx),%al
int $0x80
Both of these shellcodes call setuid(0), but one does it in 7 bytes while the other does it in 8. Again, I hear you saying but that's only one byte it doesn't make that much of a difference, and you're right, here it doesn't make much of a difference(except for in shellcode-size pissing contests =p), but when applied to much larger shellcodes, which have many function calls and need to do things like this frequently, it can save quite a bit of space.
Conclusion
I hope you all learned something, and will go out and apply your knowledge to create smaller and better shellcodes. If you know who invented the leal technique, please tell me and I will credit him/her.
$$ Buy "Advanced Shellcoding Techniques" on Amazon $$
Search More...
Advanced Shellcoding TechniquesLinks
Search and Buy<< Search and Buy This Book on Amazon >>
No download links here
Please check the description for download links if any or do a search to find alternative books.Can't Download?
Please search mirrors if you can't find download links for "Advanced Shellcoding Techniques" in "Description" and someone else may update the links. Check the comments when back to find any updates.
Search Mirrors
Maybe some mirror pages will be helpful, search this book at top of this page or click here to find more info.
Related Books
Books related to "Advanced Shellcoding Techniques":
- Ebooks list page : 131
- On LISP: Advanced Techniques for
- Advanced 3D Photorealism Techniques
- Photoshop CS3 One-on-One: Advanced Techniques
- Advanced Hypnotic Techniques
- LabVIEW: Advanced Programming Techniques
- Advanced Techniques in Diagnostic Microbiology
- Advanced Fellatio Techniques and Secrets
- PowerPoint Advanced Presentation Techniques
- Advanced Data Mining Techniques
- Advanced Techniques in Soil Microbiology
- Advanced Hypnotherapy: Hypnodynamic Techniques
- Advanced Techniques in Diagnostic Microbiology
- LabVIEW.Advanced.Programming.Techniques
- Advanced Hypnotherapy: Hypnodynamic Techniques
- Advanced Data Mining Techniques
Comments
No comments for "Advanced Shellcoding Techniques".
Add Your Comments
- Download links and password may be in the description section, read description carefully!
- Do a search to find mirrors if no download links or dead links.



