SlideShare a Scribd company logo
Shellcoding, an introduction

                 Daniele Bellavista

University of Bologna, Scuola di Ingegneria ed Architettura
        Cesena Security Network and Applications


                December 24, 2012
Outline




      (CeSeNA)   December 24, 2012   2/1
Warm-Up

Materiale iniziale
    La teoria di questa presentazione vale per sistemi Unix, FreeBSD e
    Windows.
    Il materiale ` tuttavia preparato per sistemi Linux 64 bit con kernel
                 e
    >= 2.6 e processore > Pentium 4.
    Per chi non ha 64 bit sono disponibili anche gli esempi a 32 bit.
    Per chi non ha installato un OS Linux o non vuole sporcare il suo,
    sono disponibili due Virtual Machine:
          32 bit: sorry non ho fatto in tempo. . .
          64 bit: sorry non ho fatto in tempo. . .



        (CeSeNA)                                           December 24, 2012   3/1
What is shellcoding?
Exploiting software vulnerabilities

    Vulnerabilities like BOFs permit the insertion and the execution of a
    custom payload.
    The historical function of the payload is to spawn a shell (hence the
    name shell-code).
    If the exploited program runs as a privileged user, the payload can
    assume the control of the system.

Prerequisites
Basic notion of:
    Assembly language.
    Memory structure.
    System Calls.
        (CeSeNA)                                          December 24, 2012   4/1
System Memory
Memory management

   Memory management is a vast topic, so let’s discuss from an
   high-level viewpoint.
   Thanks to the Virtual Memory Management, each process sees a flat
   addressing space of 128 TiB (4 GiB in 32 bit processors).
   Users can create sections inside the memory with read, write and/or
   execute permissions.
   The basic operations for memory management are push and pop.
          push Insert a 64-bit value into the top of the stack (the
                bottom).
           pop Retrieve a 64-bit value from the top of the stack (the
                bottom).


      (CeSeNA)                                         December 24, 2012   5/1
Figure : Linux 64 bit memory management

(CeSeNA)                                             December 24, 2012   6/1
Program Code



Text segment and instruction pointer

    The code is saved in a RO and executable memory called Text
    segment.
    The current instruction is pointed by the Instruction Pointer.
    The instruction pointer is a value stored into the CPU.




       (CeSeNA)                                           December 24, 2012   7/1
Program Stack

   The stack is where function context,
   called frame, resides.
   The Base pointer points to the begin of
   the frame.
   The Stack pointer points to the first
   available memory location.

void    f () {
  int     xx = 1 2 ; i n t yy = 2 3 ;
  int     zz = 24;
  int     sum = xx + yy + z z ;
}




          (CeSeNA)                           December 24, 2012   8/1
Assembly Language


Low, low level programming

    Assembly is the final stage of the programming process.
    Each instruction is directly converted into an number.
    Writing the number on the CPU Command BUS cause the instruction
    to be executed.
    You can access to a set of 64 bit registers: rax, rbx, rcx, rdx, rsi, rdi,
    r8, r9, r10, r11, r12, r13, r14, r15
    As instruction pointer, base pointer and stack pointer the CPU uses
    respectively rip, rbp and rsp.




       (CeSeNA)                                              December 24, 2012   9/1
Assembly Language: function call

The call/ret instructions

 call address push the RIP into the stack.
         ret pop the RIP from the stack.


Creating a frame for the function

    The callee may need a stack for his own variables.
    RBP is saved (pushed) into the stack.
    The new RBP become the old RSP.
    Before doing ret, the opposite operations must be performed.



        (CeSeNA)                                         December 24, 2012   10 / 1
Assembly Language: function call and stack




      (CeSeNA)                               December 24, 2012   11 / 1
Assembly Language: function call and parameters
Where should I put the parameters?

    If you are coding in pure ASM, you can do what you want.
    Else, you have to follow the conventions (see
    http://en.wikipedia.org/wiki/X86_calling_conventions#
    List_of_x86_calling_conventions).


Linux calling convention, parameter order
First parameters:
     x86-64: rdi, rsi, rdx, rcx, r8, and r9. For system calls, r10 is used
             instead of rcx.
      IA-32: ecx, edx. For system calls ebx, ecx, edx, esi, edi, ebp.
Other parameters are pushed into the stack.

        (CeSeNA)                                            December 24, 2012   12 / 1
System calls



Invoking the OS

    User’s programs run in the exterior ring with PL 3, while kernel runs
    in the inner ring with PL 0: Kernel can access to the hardware (files,
    devices, . . . ), user not.
    Syscalls form the interface between user and kernel.
    E.g.: open(), read(), write(), chmod(), exec(), . . .




       (CeSeNA)                                             December 24, 2012   13 / 1
Try to use Syscalls
Using assembly

     In order to ease the programmer duty, syscalls are identified by a
     number.
     In modern x86-64 processor, a syscall is invoked using the operation
     syscall, putting the id in the register rax.
     In modern x86 processor, a syscall is invoked using the operation
     sysenter but a lot of operation must be performed before, so in the
     following exercise we will use the old-fashion-way int 80h, which rises
     a software interrupt.

 1   mov r d i , 0                    1   mov ebx , 0
 2   mov r a x , 60                   2   mov eax , 1
 3   syscall                          3   i n t 80 h


         (CeSeNA)                                          December 24, 2012   14 / 1
Linux Syscalls


    Linux
        Syscalls signatures: /. . . kernel-sources. . . /include/linux/syscall.h
        Syscalls numbers: /usr/include/asm/unistd 64.h


1   unistd 64.h
2    #d e f i n e N R e x i t 60
3   syscall.h
4     asmlinkage long s y s e x i t ( i n t error code ) ;




            (CeSeNA)                                              December 24, 2012   15 / 1
Writing shellcodes



The final preparation


First steps. . .

     Testing platform: http://dl.dropbox.com/u/16169203/data.zip
     Use nasm to compile your assembly code
     Feed the tester with the output.


Exercise 0
     Create a shellcode that does nothing!
     Test it!




         (CeSeNA)                                December 24, 2012   16 / 1
Writing shellcodes



Using a syscall




Exercise 1: The exit syscall

    Use the exit syscall to terminate the program
    Change your code setting the return code to 13h




        (CeSeNA)                                      December 24, 2012   17 / 1
Writing shellcodes



Data reference I



The reference problem

    When you write a program you can use global data because, at
    compile time, a static address is associated.
    But your shellcode is not compiled with the program it’s intended to
    run.
    You must use relative addressing, but before IA-64 it was not possible.




       (CeSeNA)                                          December 24, 2012   18 / 1
Writing shellcodes



    Data reference II

    Old IA-32 way

         You use a trick: jmp just before the data location, then do a call. Et
         voil`! On the top of the stack there is the data address.
             a


1   jmp message
2   run :
3     pop ebx ; now ebx c o n t a i n s t h e s t r i n g r e f e r e n c e
4     ; . . . shellcode
5   message :
6     c a l l run
7     db ’ CeSeNA ’ , 0 x0a , 0




             (CeSeNA)                                               December 24, 2012   19 / 1
Writing shellcodes



    Data reference III


    New IA-64 way

         IA-64 introduces the RIP relative addressing.


1   l e a r d i , [ r e l message ] ; now ebx c o n t a i n s
2                                   ; the s t r i n g r e f e r e n c e
3   ; . . . shellcode
4

5   message db ’ CeSeNA ’ , 0 x0a , 0




             (CeSeNA)                                                December 24, 2012   20 / 1
Writing shellcodes



    Data reference IV


    Generic Way

         You can pop the string in hex format over the stack.
         The stack pointer is then the string reference.


1   push 0 x 0 0 0 a 4 1 4 e ; 0 x00 , 0 x0a , ’AN ’
2   push 0 x65536543 ; ’ eSeC ’
3   mov ebx , e s p ; now ebx c o n t a i n s t h e s t r i n g r e f e r e n c e
4   ; ...




             (CeSeNA)                                                December 24, 2012   21 / 1
Writing shellcodes



Data reference V



Exercise 2: print on stdout

    Get the exercise skeleton.
    Understand the code
    Write your own message




       (CeSeNA)                                December 24, 2012   22 / 1
Writing shellcodes



    Execute a program

1   unistd 64 . h
2    #d e f i n e      N R e x e c v e 59
3   syscall .h
4     i n t k e r n e l e x e c v e ( const char ∗ filename ,
5                                     const char ∗ const argv [ ] ,
6                                     c o n s t c h a r ∗ c o n s t envp [ ] ) ;



    Exercise 3: A real shellcode, exec a shell!

      4 HaXoRs
          4 g33k
         4 n00bz


              (CeSeNA)                                                     December 24, 2012   23 / 1
Writing shellcodes



Reverse shell I
One shellcode to rule them all
Step to execute:
  1   Open a socket to the attacker server.
  2   Duplicate the socket file descriptor into 0 and 1 (and optionally 2).
  3   Exec a shell.

Step1: Create a socket

      The standard procedure involves socket creation and connection.
      socket() and connect() syscalls.
      The sockaddr in structure requires port in network byte order
      (htons()) and ip address in numeric form (inet pton()).
      The usual sockaddr in size is 16.

         (CeSeNA)                                          December 24, 2012   24 / 1
Writing shellcodes



    Reverse shell II
1   l o n g s y s s o c k e t ( i n t domain ,
2                               i n t type ,
3                                     int protocol ) ;
4   l o n g s y s c o n n e c t ( i n t fd ,
5                                 s t r u c t sockaddr   user ∗,
6                                 int addrlen ) ;



1   struct sockaddr in {
2   short            s i n f a m i l y ; // AF INET
3   unsigned short   s i n p o r t ; // n e t w o r k o r d e r ( h t o n s ( ) )
4   struct in addr   s i n a d d r ; // As 32 b i t
5   char             sin zero [8];
6   };



             (CeSeNA)                                               December 24, 2012   25 / 1
Writing shellcodes



    Reverse shell III


    Step2: Duplicate the file descriptor

        The return value of socket() is the file descriptor.
        The syscall dup2() copy the file descriptor and close the destination.
        dup2(fd, 0); dup2(fd, 1), dup2(fd, 2);


1   long sys dup2 ( unsigned i n t oldfd ,
2                   u n s i g n e d i n t newfd )




            (CeSeNA)                                          December 24, 2012   26 / 1
Writing shellcodes



Reverse shell IV

Step3: Exec a shell
Already seen ;)

Exercise 4: Reverse shelling
Remember to open a server listening into a known address! E.g.: nc -l -p
1234, preparing reverse shell for 127.0.0.1:1234
  4 HaXoRs
     4 g33k
    4 n00bz




        (CeSeNA)                                        December 24, 2012   27 / 1
Writing shellcodes



Shellcode optimization


Why optimization?
    When writing a shellcode, you must consider various factors. The
    most important are:
      1    How the vulnerable program receives the payload.
      2    How long the payload can be.
    The previous exercise solutions, for instance, are not suitable in most
    cases.
    The next slides will discuss about the NULL byte presence.
    About the payload length, it’s all about the exploiter expertise.




          (CeSeNA)                                            December 24, 2012   28 / 1
Writing shellcodes



String payload
zero-bytes problem

    If the payload is red as a string, C interprets a zero byte as string
    terminator, cutting the shellcode.
    Zero-bytes presence is caused by data and addresses:
         mov rax, 11h is equivalent to mov rax, 0000000000000011h.
         lea rax, [rel message] is equivalent to lea rax, [rip + 0000. . . xxh].
         execve for instance, requires a null terminated string and some null
         parameters.
    Solutions are quite straightforward:
         Use xor operation to zero a register.
         Use when possible smaller part of registers (e.g.: rax → eax → ax →
         [ah,al])
         Use add operation: immediate operators are not expanded.
         Place not-null marker in strings and substitute them inside the code.
         When using relative addressing, place the message above: offset will be
         negative [?].

        (CeSeNA)                                                   December 24, 2012   29 / 1
Writing shellcodes



Zero-byte removal example
; S e t r a x = 60 h                            ; S e t t o 0 a mem a r e a
xor rax , rax                                   n u l l db ’ x x x x ’
mov a l , 60                                    x o r rbx , r b x
                                                mov [ r e l n u l l ] , ebx


; S e t r d i = 12 h
xor rdi , r d i                                 ; terminate s t r i n g with 0
add r d i , 12 h                                message db ’ CeSeNA ’ , ’ x ’
                                                x o r rbx , r b x
                                                l e a r d i , [ r e l message ]
                                                mov [ r d i +7] , b l
; Negative refe rence
message db ’ CeSeNA ’ , ’ x ’
l e a r d i , [ r e l message ]



         (CeSeNA)                                                  December 24, 2012   30 / 1
Writing shellcodes



References




      (CeSeNA)                        December 24, 2012   31 / 1

More Related Content

PPTX
Проведение криминалистической экспертизы и анализа руткит-программ на примере...
PDF
Reconstructing Gapz: Position-Independent Code Analysis Problem
PDF
Building a Virtualized Continuum with Intel(r) Clear Containers
PDF
Mem forensic
PDF
Inside Docker for Fedora20/RHEL7
PDF
Solaris DTrace, An Introduction
PDF
Parallella: Embedded HPC For Everybody
PDF
john-devkit: 100 типов хешей спустя / john-devkit: 100 Hash Types Later
Проведение криминалистической экспертизы и анализа руткит-программ на примере...
Reconstructing Gapz: Position-Independent Code Analysis Problem
Building a Virtualized Continuum with Intel(r) Clear Containers
Mem forensic
Inside Docker for Fedora20/RHEL7
Solaris DTrace, An Introduction
Parallella: Embedded HPC For Everybody
john-devkit: 100 типов хешей спустя / john-devkit: 100 Hash Types Later

What's hot (20)

PDF
Tickling CGI Problems (Tcl Web Server Scripting Vulnerability Research)
PDF
Semtex.c [CVE-2013-2094] - A Linux Privelege Escalation
PDF
IAP09 CUDA@MIT 6.963 - Guest Lecture: Out-of-Core Programming with NVIDIA's C...
PDF
Advanced Evasion Techniques by Win32/Gapz
PDF
IAP09 CUDA@MIT 6.963 - Lecture 01: GPU Computing using CUDA (David Luebke, NV...
PDF
A22 Introduction to DTrace by Kyle Hailey
PPTX
Eloi Sanfelix - Hardware security: Side Channel Attacks [RootedCON 2011]
PDF
introduction to linux kernel tcp/ip ptocotol stack
PPTX
grsecurity and PaX
PPT
Trusted Launch of Generic Virtual Machine Images in Public IaaS Environments
PDF
Analysis of Cryptographic Algorithms
PDF
IAP09 CUDA@MIT 6.963 - Lecture 04: CUDA Advanced #1 (Nicolas Pinto, MIT)
PDF
Uncloaking IP Addresses on IRC
PDF
Tokyo OpenStack Summit 2015: Unraveling Docker Security
PDF
Использование KASan для автономного гипервизора
PDF
Kernel Recipes 2015: Anatomy of an atomic KMS driver
PDF
44CON London - Attacking VxWorks: from Stone Age to Interstellar
PDF
Linux Container Technology inside Docker with RHEL7
PDF
Francisco Jesús Gómez + Carlos Juan Diaz - Cloud Malware Distribution: DNS wi...
PDF
Wrangling with the Ghost: An Inside Story of Mitigating Speculative Execution...
Tickling CGI Problems (Tcl Web Server Scripting Vulnerability Research)
Semtex.c [CVE-2013-2094] - A Linux Privelege Escalation
IAP09 CUDA@MIT 6.963 - Guest Lecture: Out-of-Core Programming with NVIDIA's C...
Advanced Evasion Techniques by Win32/Gapz
IAP09 CUDA@MIT 6.963 - Lecture 01: GPU Computing using CUDA (David Luebke, NV...
A22 Introduction to DTrace by Kyle Hailey
Eloi Sanfelix - Hardware security: Side Channel Attacks [RootedCON 2011]
introduction to linux kernel tcp/ip ptocotol stack
grsecurity and PaX
Trusted Launch of Generic Virtual Machine Images in Public IaaS Environments
Analysis of Cryptographic Algorithms
IAP09 CUDA@MIT 6.963 - Lecture 04: CUDA Advanced #1 (Nicolas Pinto, MIT)
Uncloaking IP Addresses on IRC
Tokyo OpenStack Summit 2015: Unraveling Docker Security
Использование KASan для автономного гипервизора
Kernel Recipes 2015: Anatomy of an atomic KMS driver
44CON London - Attacking VxWorks: from Stone Age to Interstellar
Linux Container Technology inside Docker with RHEL7
Francisco Jesús Gómez + Carlos Juan Diaz - Cloud Malware Distribution: DNS wi...
Wrangling with the Ghost: An Inside Story of Mitigating Speculative Execution...
Ad

Viewers also liked (20)

PDF
Game Engine
PDF
Light talk @ coscup 2011 : Incremental Global Prelink for Android
PDF
How to find_vulnerability_in_software
PPTX
Algorithms
PDF
Function Call Stack
PDF
Design and Implementation of GCC Register Allocation
PDF
References Are 'Nice' Pointers
PPTX
Buffer overflow explained
PDF
Rethinking the debugger
PPT
Exception handling poirting in gcc
PPTX
Programmazione Genetica per l'Inferenza di Reti di Kauffman
PPTX
Program activation records
PPTX
Buffer overflow
PPTX
PPTX
Buffer Overflow Demo by Saurabh Sharma
PDF
Ceh v5 module 20 buffer overflow
PDF
ARM procedure calling conventions and recursion
PDF
COSCUP 2014 : open source compiler 戰國時代的軍備競賽
PDF
Android C Library: Bionic 成長計畫
PDF
System Hacking Tutorial #3 - Buffer Overflow - Egg Hunting
Game Engine
Light talk @ coscup 2011 : Incremental Global Prelink for Android
How to find_vulnerability_in_software
Algorithms
Function Call Stack
Design and Implementation of GCC Register Allocation
References Are 'Nice' Pointers
Buffer overflow explained
Rethinking the debugger
Exception handling poirting in gcc
Programmazione Genetica per l'Inferenza di Reti di Kauffman
Program activation records
Buffer overflow
Buffer Overflow Demo by Saurabh Sharma
Ceh v5 module 20 buffer overflow
ARM procedure calling conventions and recursion
COSCUP 2014 : open source compiler 戰國時代的軍備競賽
Android C Library: Bionic 成長計畫
System Hacking Tutorial #3 - Buffer Overflow - Egg Hunting
Ad

Similar to Shellcoding, an Introduction (20)

PDF
Smashing The Stack
PPTX
NASM Introduction.pptx
PDF
Shellcoding in linux
PDF
The walking 0xDEAD
PDF
X86 assembly nasm syntax
PDF
Shellcode Disassembling - Reverse Engineering
PDF
Direct Code Execution - LinuxCon Japan 2014
PPTX
Buffer overflow – Smashing The Stack
PPTX
Advanced Windows Debugging
PPTX
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...
PDF
Software Security
PDF
Let your Mach-O fly, Black Hat DC 2009
PDF
Buffer Overflow - Smashing the Stack
PPTX
CA-Lec4-RISCV-Instructions-1aaaaaaaaaa.pptx
PPTX
Linux Initialization Process (1)
PPT
Swug July 2010 - windows debugging by sainath
PDF
SDAccel Design Contest: Xilinx SDAccel
PDF
Expanding the control over the operating system from the database
PDF
Linux Shellcode disassembling
PDF
www.indonezia.net Hacking Windows Registry
Smashing The Stack
NASM Introduction.pptx
Shellcoding in linux
The walking 0xDEAD
X86 assembly nasm syntax
Shellcode Disassembling - Reverse Engineering
Direct Code Execution - LinuxCon Japan 2014
Buffer overflow – Smashing The Stack
Advanced Windows Debugging
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...
Software Security
Let your Mach-O fly, Black Hat DC 2009
Buffer Overflow - Smashing the Stack
CA-Lec4-RISCV-Instructions-1aaaaaaaaaa.pptx
Linux Initialization Process (1)
Swug July 2010 - windows debugging by sainath
SDAccel Design Contest: Xilinx SDAccel
Expanding the control over the operating system from the database
Linux Shellcode disassembling
www.indonezia.net Hacking Windows Registry

Recently uploaded (20)

PDF
cuic standard and advanced reporting.pdf
PPTX
Big Data Technologies - Introduction.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Spectroscopy.pptx food analysis technology
PPTX
A Presentation on Artificial Intelligence
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Machine learning based COVID-19 study performance prediction
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPTX
Cloud computing and distributed systems.
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Empathic Computing: Creating Shared Understanding
cuic standard and advanced reporting.pdf
Big Data Technologies - Introduction.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Review of recent advances in non-invasive hemoglobin estimation
Encapsulation_ Review paper, used for researhc scholars
Spectroscopy.pptx food analysis technology
A Presentation on Artificial Intelligence
Programs and apps: productivity, graphics, security and other tools
Per capita expenditure prediction using model stacking based on satellite ima...
Machine learning based COVID-19 study performance prediction
Dropbox Q2 2025 Financial Results & Investor Presentation
Mobile App Security Testing_ A Comprehensive Guide.pdf
Spectral efficient network and resource selection model in 5G networks
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Network Security Unit 5.pdf for BCA BBA.
Assigned Numbers - 2025 - Bluetooth® Document
Cloud computing and distributed systems.
The AUB Centre for AI in Media Proposal.docx
Empathic Computing: Creating Shared Understanding

Shellcoding, an Introduction

  • 1. Shellcoding, an introduction Daniele Bellavista University of Bologna, Scuola di Ingegneria ed Architettura Cesena Security Network and Applications December 24, 2012
  • 2. Outline (CeSeNA) December 24, 2012 2/1
  • 3. Warm-Up Materiale iniziale La teoria di questa presentazione vale per sistemi Unix, FreeBSD e Windows. Il materiale ` tuttavia preparato per sistemi Linux 64 bit con kernel e >= 2.6 e processore > Pentium 4. Per chi non ha 64 bit sono disponibili anche gli esempi a 32 bit. Per chi non ha installato un OS Linux o non vuole sporcare il suo, sono disponibili due Virtual Machine: 32 bit: sorry non ho fatto in tempo. . . 64 bit: sorry non ho fatto in tempo. . . (CeSeNA) December 24, 2012 3/1
  • 4. What is shellcoding? Exploiting software vulnerabilities Vulnerabilities like BOFs permit the insertion and the execution of a custom payload. The historical function of the payload is to spawn a shell (hence the name shell-code). If the exploited program runs as a privileged user, the payload can assume the control of the system. Prerequisites Basic notion of: Assembly language. Memory structure. System Calls. (CeSeNA) December 24, 2012 4/1
  • 5. System Memory Memory management Memory management is a vast topic, so let’s discuss from an high-level viewpoint. Thanks to the Virtual Memory Management, each process sees a flat addressing space of 128 TiB (4 GiB in 32 bit processors). Users can create sections inside the memory with read, write and/or execute permissions. The basic operations for memory management are push and pop. push Insert a 64-bit value into the top of the stack (the bottom). pop Retrieve a 64-bit value from the top of the stack (the bottom). (CeSeNA) December 24, 2012 5/1
  • 6. Figure : Linux 64 bit memory management (CeSeNA) December 24, 2012 6/1
  • 7. Program Code Text segment and instruction pointer The code is saved in a RO and executable memory called Text segment. The current instruction is pointed by the Instruction Pointer. The instruction pointer is a value stored into the CPU. (CeSeNA) December 24, 2012 7/1
  • 8. Program Stack The stack is where function context, called frame, resides. The Base pointer points to the begin of the frame. The Stack pointer points to the first available memory location. void f () { int xx = 1 2 ; i n t yy = 2 3 ; int zz = 24; int sum = xx + yy + z z ; } (CeSeNA) December 24, 2012 8/1
  • 9. Assembly Language Low, low level programming Assembly is the final stage of the programming process. Each instruction is directly converted into an number. Writing the number on the CPU Command BUS cause the instruction to be executed. You can access to a set of 64 bit registers: rax, rbx, rcx, rdx, rsi, rdi, r8, r9, r10, r11, r12, r13, r14, r15 As instruction pointer, base pointer and stack pointer the CPU uses respectively rip, rbp and rsp. (CeSeNA) December 24, 2012 9/1
  • 10. Assembly Language: function call The call/ret instructions call address push the RIP into the stack. ret pop the RIP from the stack. Creating a frame for the function The callee may need a stack for his own variables. RBP is saved (pushed) into the stack. The new RBP become the old RSP. Before doing ret, the opposite operations must be performed. (CeSeNA) December 24, 2012 10 / 1
  • 11. Assembly Language: function call and stack (CeSeNA) December 24, 2012 11 / 1
  • 12. Assembly Language: function call and parameters Where should I put the parameters? If you are coding in pure ASM, you can do what you want. Else, you have to follow the conventions (see http://en.wikipedia.org/wiki/X86_calling_conventions# List_of_x86_calling_conventions). Linux calling convention, parameter order First parameters: x86-64: rdi, rsi, rdx, rcx, r8, and r9. For system calls, r10 is used instead of rcx. IA-32: ecx, edx. For system calls ebx, ecx, edx, esi, edi, ebp. Other parameters are pushed into the stack. (CeSeNA) December 24, 2012 12 / 1
  • 13. System calls Invoking the OS User’s programs run in the exterior ring with PL 3, while kernel runs in the inner ring with PL 0: Kernel can access to the hardware (files, devices, . . . ), user not. Syscalls form the interface between user and kernel. E.g.: open(), read(), write(), chmod(), exec(), . . . (CeSeNA) December 24, 2012 13 / 1
  • 14. Try to use Syscalls Using assembly In order to ease the programmer duty, syscalls are identified by a number. In modern x86-64 processor, a syscall is invoked using the operation syscall, putting the id in the register rax. In modern x86 processor, a syscall is invoked using the operation sysenter but a lot of operation must be performed before, so in the following exercise we will use the old-fashion-way int 80h, which rises a software interrupt. 1 mov r d i , 0 1 mov ebx , 0 2 mov r a x , 60 2 mov eax , 1 3 syscall 3 i n t 80 h (CeSeNA) December 24, 2012 14 / 1
  • 15. Linux Syscalls Linux Syscalls signatures: /. . . kernel-sources. . . /include/linux/syscall.h Syscalls numbers: /usr/include/asm/unistd 64.h 1 unistd 64.h 2 #d e f i n e N R e x i t 60 3 syscall.h 4 asmlinkage long s y s e x i t ( i n t error code ) ; (CeSeNA) December 24, 2012 15 / 1
  • 16. Writing shellcodes The final preparation First steps. . . Testing platform: http://dl.dropbox.com/u/16169203/data.zip Use nasm to compile your assembly code Feed the tester with the output. Exercise 0 Create a shellcode that does nothing! Test it! (CeSeNA) December 24, 2012 16 / 1
  • 17. Writing shellcodes Using a syscall Exercise 1: The exit syscall Use the exit syscall to terminate the program Change your code setting the return code to 13h (CeSeNA) December 24, 2012 17 / 1
  • 18. Writing shellcodes Data reference I The reference problem When you write a program you can use global data because, at compile time, a static address is associated. But your shellcode is not compiled with the program it’s intended to run. You must use relative addressing, but before IA-64 it was not possible. (CeSeNA) December 24, 2012 18 / 1
  • 19. Writing shellcodes Data reference II Old IA-32 way You use a trick: jmp just before the data location, then do a call. Et voil`! On the top of the stack there is the data address. a 1 jmp message 2 run : 3 pop ebx ; now ebx c o n t a i n s t h e s t r i n g r e f e r e n c e 4 ; . . . shellcode 5 message : 6 c a l l run 7 db ’ CeSeNA ’ , 0 x0a , 0 (CeSeNA) December 24, 2012 19 / 1
  • 20. Writing shellcodes Data reference III New IA-64 way IA-64 introduces the RIP relative addressing. 1 l e a r d i , [ r e l message ] ; now ebx c o n t a i n s 2 ; the s t r i n g r e f e r e n c e 3 ; . . . shellcode 4 5 message db ’ CeSeNA ’ , 0 x0a , 0 (CeSeNA) December 24, 2012 20 / 1
  • 21. Writing shellcodes Data reference IV Generic Way You can pop the string in hex format over the stack. The stack pointer is then the string reference. 1 push 0 x 0 0 0 a 4 1 4 e ; 0 x00 , 0 x0a , ’AN ’ 2 push 0 x65536543 ; ’ eSeC ’ 3 mov ebx , e s p ; now ebx c o n t a i n s t h e s t r i n g r e f e r e n c e 4 ; ... (CeSeNA) December 24, 2012 21 / 1
  • 22. Writing shellcodes Data reference V Exercise 2: print on stdout Get the exercise skeleton. Understand the code Write your own message (CeSeNA) December 24, 2012 22 / 1
  • 23. Writing shellcodes Execute a program 1 unistd 64 . h 2 #d e f i n e N R e x e c v e 59 3 syscall .h 4 i n t k e r n e l e x e c v e ( const char ∗ filename , 5 const char ∗ const argv [ ] , 6 c o n s t c h a r ∗ c o n s t envp [ ] ) ; Exercise 3: A real shellcode, exec a shell! 4 HaXoRs 4 g33k 4 n00bz (CeSeNA) December 24, 2012 23 / 1
  • 24. Writing shellcodes Reverse shell I One shellcode to rule them all Step to execute: 1 Open a socket to the attacker server. 2 Duplicate the socket file descriptor into 0 and 1 (and optionally 2). 3 Exec a shell. Step1: Create a socket The standard procedure involves socket creation and connection. socket() and connect() syscalls. The sockaddr in structure requires port in network byte order (htons()) and ip address in numeric form (inet pton()). The usual sockaddr in size is 16. (CeSeNA) December 24, 2012 24 / 1
  • 25. Writing shellcodes Reverse shell II 1 l o n g s y s s o c k e t ( i n t domain , 2 i n t type , 3 int protocol ) ; 4 l o n g s y s c o n n e c t ( i n t fd , 5 s t r u c t sockaddr user ∗, 6 int addrlen ) ; 1 struct sockaddr in { 2 short s i n f a m i l y ; // AF INET 3 unsigned short s i n p o r t ; // n e t w o r k o r d e r ( h t o n s ( ) ) 4 struct in addr s i n a d d r ; // As 32 b i t 5 char sin zero [8]; 6 }; (CeSeNA) December 24, 2012 25 / 1
  • 26. Writing shellcodes Reverse shell III Step2: Duplicate the file descriptor The return value of socket() is the file descriptor. The syscall dup2() copy the file descriptor and close the destination. dup2(fd, 0); dup2(fd, 1), dup2(fd, 2); 1 long sys dup2 ( unsigned i n t oldfd , 2 u n s i g n e d i n t newfd ) (CeSeNA) December 24, 2012 26 / 1
  • 27. Writing shellcodes Reverse shell IV Step3: Exec a shell Already seen ;) Exercise 4: Reverse shelling Remember to open a server listening into a known address! E.g.: nc -l -p 1234, preparing reverse shell for 127.0.0.1:1234 4 HaXoRs 4 g33k 4 n00bz (CeSeNA) December 24, 2012 27 / 1
  • 28. Writing shellcodes Shellcode optimization Why optimization? When writing a shellcode, you must consider various factors. The most important are: 1 How the vulnerable program receives the payload. 2 How long the payload can be. The previous exercise solutions, for instance, are not suitable in most cases. The next slides will discuss about the NULL byte presence. About the payload length, it’s all about the exploiter expertise. (CeSeNA) December 24, 2012 28 / 1
  • 29. Writing shellcodes String payload zero-bytes problem If the payload is red as a string, C interprets a zero byte as string terminator, cutting the shellcode. Zero-bytes presence is caused by data and addresses: mov rax, 11h is equivalent to mov rax, 0000000000000011h. lea rax, [rel message] is equivalent to lea rax, [rip + 0000. . . xxh]. execve for instance, requires a null terminated string and some null parameters. Solutions are quite straightforward: Use xor operation to zero a register. Use when possible smaller part of registers (e.g.: rax → eax → ax → [ah,al]) Use add operation: immediate operators are not expanded. Place not-null marker in strings and substitute them inside the code. When using relative addressing, place the message above: offset will be negative [?]. (CeSeNA) December 24, 2012 29 / 1
  • 30. Writing shellcodes Zero-byte removal example ; S e t r a x = 60 h ; S e t t o 0 a mem a r e a xor rax , rax n u l l db ’ x x x x ’ mov a l , 60 x o r rbx , r b x mov [ r e l n u l l ] , ebx ; S e t r d i = 12 h xor rdi , r d i ; terminate s t r i n g with 0 add r d i , 12 h message db ’ CeSeNA ’ , ’ x ’ x o r rbx , r b x l e a r d i , [ r e l message ] mov [ r d i +7] , b l ; Negative refe rence message db ’ CeSeNA ’ , ’ x ’ l e a r d i , [ r e l message ] (CeSeNA) December 24, 2012 30 / 1
  • 31. Writing shellcodes References (CeSeNA) December 24, 2012 31 / 1