SlideShare a Scribd company logo
www.SecurityXploded.com
Disclaimer
The Content, Demonstration, Source Code and Programs presented here
is "AS IS" without any warranty or conditions of any kind. Also the
views/ideas/knowledge expressed here are solely of the trainer’s only and
nothing to do with the company or the organization in which the trainer is
currently working.

However in no circumstances neither the trainer nor SecurityXploded is
responsible for any damage or loss caused due to use or misuse of the
information presented here.




                              www.SecurityXploded.com
Acknowledgement
 Special thanks to null & Garage4Hackers community for their extended
  support and cooperation.
 Thanks to all the Trainers who have devoted their precious time and
  countless hours to make it happen.




                                www.SecurityXploded.com
Reversing & Malware Analysis Training

This presentation is part of our Reverse Engineering & Malware
Analysis Training program. Currently it is delivered only during our local
meet for FREE of cost.




For complete details of this course, visit our Security Training page.


                               www.SecurityXploded.com
Who am I #1
Amit Malik (sometimes DouBle_Zer0,DZZ)
     Member SecurityXploded & Garage4Hackers
     Security Researcher
     RE, Exploit Analysis/Development, Malware Analysis
     Email: m.amit30@gmail.com




                            www.SecurityXploded.com
Who am I #2
Swapnil Pathak
      Member SecurityXploded
      Security Researcher
      RE, Malware Analysis, Network Security
      Email: swapnilpathak101@gmail.com




                             www.SecurityXploded.com
Presentation Outline
   Intro to x86-32

   Assembly Language

   Instructions

   Stack Operations

   Calling conventions

   Demo




                          www.SecurityXploded.com
x86-32
   32 bit instruction set architectures based on Intel 8086 CPU
   Address a linear address space up to 4GB
   8, 32 bit General Purpose Registers (GPR)
   6,16 bit Segment Registers
   EFLAGS and EIP register
   Control Registers (CR0-CR4) (16 bits)
   Memory Management Registers Descriptor Table Registers
    (GDTR, IDTR, LDTR)
   Debug Registers ( DR0-DR7)



                                 www.SecurityXploded.com
Registers Usage - RE
   Register

     Storage Locations.

     Much faster access compare to memory locations.

   EAX: Accumulator , mostly stores return values from functions (APIs)

   EBX: Base index (for use with arrays)

   ECX: Counter

   EDX: Data/general

   ESI: Source index for string operations.




                                 www.SecurityXploded.com
Registers Usage – RE Cont.
   EDI: Destination index for string operations.

   ESP: Stack pointer for top address of the stack.

   EBP: Stack base pointer for holding the address of the current stack frame.

   EIP: Instruction pointer. Holds the program counter, the next instruction
    address.

   Segment registers:

     Used to address particular segments of memory ( code, data, stack )

        !) CS: Code      !!) SS: Stack

        !!!) ES: Extra !V) DS: Data            V) FS, GS


                                   www.SecurityXploded.com
Registers – 32 bit (X86)




          www.SecurityXploded.com
(R/E)Flags Register
   Bit field of states
   Status Flags
     Carrry (CF) : set when an arithmetic carry/borrow has been generated
        out of the MSB.
       Zero (ZF) : set when an arithmetic operation result is zero and reset
        otherwise.
       Sign (SF) : set when an arithmetic operation set the MSB i.e. the result
        value was negative.
       Trap (TF ) : when set permits operation of processor in single-step.
        Mostly used by debuggers.
       Interrupt (IF) : determines whether the CPU should handle maskable
        hardware interrupts.
       Direction (DF) : determines the direction (left-to-right or right-to-left)
        of string processing.
       Overflow (OF) : indicates arithmetic overflow.

                                     www.SecurityXploded.com
Assembly Language
   Low level programming language

   Symbolic representation of machine codes, constants.

   Assembly language program consist of sequence of process instructions
    and meta statements

   Assembler translates them to executable instructions that are loaded into
    memory and executed.

   Basic Structure

    [label] : opcode operand1, operand2

    opcode – mnemonic that symbolize instructions

   Example.

     MOV AL, 61h => 10110000 01100001
                                www.SecurityXploded.com
Instructions
ADD dst, src
- Adds the values of src and dst and stores the result into dst.
- For example ADD EAX, 1

SUB dst, src
- Subtracts src value from dst and stores the result in dst.
- For example SUB EAX, 1

CMP dst, src
- Subtracts src value from dst but does store the result in dst
- Mostly used to set/reset decision making bits in EFLAGS register
  such as ZF
- For example CMP EAX, EBX

                                www.SecurityXploded.com
Instructions cont.
MOV dst, src
-  Moves data from src (left operand) to destination (right operand)
-  For example mov EDI, ESI
Note :
-  Both operands cannot be memory locations.
-  Both the operands must be of the same size

LEA dst, src
-  Stands for Load Effective Address.
-  Computes the effective address of src operand and stores it in dst operand.
-  For example LEA ECX,[EBX + 5]
Note:
-  Generally brackets denote value at memory locations.
-  In case of LEA it does simple arithmetic and stores it in dst
                                www.SecurityXploded.com
Instructions cont.
XOR dst, src
-   Performs a bitwise exclusive OR operation on the dst and src and
    stores the result in dst.
-   Each bit of the result is 1 if the corresponding bits of the operands
    are different, 0 if the corresponding bit are same


Note :
-   When used with same register clears the contents of the register
-   Optimized way to clear the register. Better than MOV EAX, 0



                                 www.SecurityXploded.com
Instructions cont.
REP
- Used with string operations
- Repeats a string instruction until ECX (counter register) value is equal to
  zero.
- For example REP MOVS byte ptr DS:[EDI], DS:[ESI]

LOOP
- Similar to loops in high level languages
- Used to execute sequence of instructions multiple times.
- For example
  MOV ECX, 10
  Test : INC EBX
        INC EAX
       LOOP Test
                                www.SecurityXploded.com
Instructions cont.
TEST dst, src
- Performs bitwise logical and between dst and src
- Updates the Zero flag bit of the EFLAGS register
- Mostly used to check if the return value of the function is not zero
- For example TEST EAX, EAX

INT 3h
-  Breakpoint instruction
-  Used by debuggers to stop execution of the program at particular
   instruction




                                www.SecurityXploded.com
Instructions cont.
CALL address
- Performs two functions
  - Push address of the next instruction on stack (return address)
  - Jump to the address specified by the instruction
- For example CALL dword ptr [EAX+4]

RET
- Transfers the control to the address previously pushed on the stack
  by CALL instruction
- Mostly denotes the end of the function




                               www.SecurityXploded.com
Instructions cont.
Jump instructions
-  Categorized as conditional and unconditional
-  Unconditional jump instructions
   - JMP (Far Jump) – E9 – (Cross segments)
   - JMP ( Short Jump ) – EB – (-127 to 128 bytes)
   - JMP ( Near Jump ) – E9 – (in a segment)
-  For example JMP EAX

-   Conditional jump instructions
    - Jumps according to bit flags set in the EFLAGS register
    - JC, JNC, JZ, JNZ, JS, JNS, JO, JNO
    - Unsigned comparisons JA, JAE, JB, JBE
    - Signed comparisons JG, JGE, JL, JLE
    - Usually followed by CMP instruction

                               www.SecurityXploded.com
Instructions cont.
PUSH operand
- Pushes operand on the stack
- Decrements the stack pointer register by operand size
- For example PUSH EAX

POP operand
- Stores the value pointed by the stack pointer in operand
- Increments the stack pointer register by operand size
- For example POP EAX

Note: POP/PUSH EIP is an invalid instruction

PUSHF, POPF


                               www.SecurityXploded.com
Calling Conventions
   Describes how the arguments are passed and values returned by functions.
   Steps performed when a function is called
     Arguments are passed to the called function
     Program execution is transferred to the address of the called function
     Called function starts with lines of code that prepare stack and registers for use within
       the function. Also known as function prologue.
       ○   For e.g.
                      push ebp
                      mov ebp, esp
                      or with enter instruction
     Called function ends with lines of code that restore stack and registers set initially. Also
       known as function epilogue.
       ○   For e.g.
                      mov esp, ebp
                      pop ebp
                      ret
                      or with leave instruction
     Passed arguments are removed from the stack, known as stack cleanup. Can be
       performed by both calling function or called function depending on the calling
       convention used.

                                              www.SecurityXploded.com
Calling conventions cont.
   __cdecl (C calling convention)
       Arguments are passed from right to left and placed on the stack
       Stack cleanup is performed by the caller
       Return values are stored in EAX register
       Standard calling convention used by C compilers


   __stdcall (Standard calling convention)
       Arguments are passed from right to left and placed on the stack
       Stack cleanup is performed by the called function
       Return values are stored in EAX register
       Standard calling convention for Microsoft Win32 API

   __fastcall (Fast calling convention)
     Arguments passed are stored in registers for faster access


   Thiscall
     Arguments are passed from right to left and placed on the stack. this pointer placed in
        ECX
    -   Standard calling convention for calling member functions of C++ classes
                                       www.SecurityXploded.com
Stack operations
   Stack is a LIFO (Last In First Out) type data structure

   Stacks grows downward in memory, from higher memory address
    to lower memory address

   PUSH decrement the stack pointer i.e ESP

   POP Increment the stack pointer i.e ESP

   Each function has its own stack frame

   Function prologue setup the stack frame for each function

   Local variable of a function are stored into its stack frame




                                www.SecurityXploded.com
Stack #1




  www.SecurityXploded.com
Stack #2




  www.SecurityXploded.com
Stack #3
 Each function creates its own stack.
 Caller function stack: known as parent stack.
 Called function stack: known as child stack.
For e.g.
  main(){                                   ASM Pseudo:
                sum();                      _main:
         }                                  123:   push ebp
                                            124:   mov ebp,esp
                                            125:   sub esp,val
                                            126:   call _sum
                                            127:   mov esp,ebp
                                            128:   pop ebp
                                            129:   ret

                             www.SecurityXploded.com
Stack #4




  www.SecurityXploded.com
Stack #5




  www.SecurityXploded.com
Stack #6




  www.SecurityXploded.com
DEMO (Source Code)
   #include <stdio.h>
   /*
   Author: Amit Malik
   http://www.securityxploded.com - Compile in Dev C++
   */
   int mysum(int,int);
   int main()
   {
      int a,b,s;
      a = 5;
      b = 6;
      s = mysum(a,b);      // call mysum function
      printf("sum is: %d",s);
      getchar();
   }
   int mysum(int l, int m) // mysum function
   {
      int c;
      c = l + m;
      return c;
   }


                                             www.SecurityXploded.com
www.SecurityXploded.com
x86-64 Intro.
   64 bit instruction set architectures based on Intel 8086 CPU
   Address a linear address space up to 16TB
   16, 64 bit General Purpose Registers (GPR)
   6, 16 bit Segment Registers
   RFLAGS and RIP register
   Control Registers (CR0-CR4) and CR8 (16 bits)
   Memory Management Registers Descriptor Table Registers
    (GDTR, IDTR, LDTR) size expanded to 10 bytes
   Debug Registers ( DR0-DR7)



                                  www.SecurityXploded.com
Reference
   Complete Reference Guide for Reversing & Malware
    Analysis Training




                           www.SecurityXploded.com
Thank You !



www.SecurityXploded.com

More Related Content

What's hot (20)

PPTX
Reversing & Malware Analysis Training Part 11 - Exploit Development [Advanced]
securityxploded
 
PPTX
Reversing & malware analysis training part 1 lab setup guide
securityxploded
 
PDF
Introduction to ida python
geeksec80
 
PPTX
Primer on password security
securityxploded
 
PPTX
Advanced Malware Analysis Training Session 3 - Botnet Analysis Part 2
securityxploded
 
PPTX
Advanced Malware Analysis Training Session 6 - Malware Sandbox Analysis
securityxploded
 
PPTX
Advanced malware analysis training session5 reversing automation
Cysinfo Cyber Security Community
 
PDF
Captain Hook: Pirating AVs to Bypass Exploit Mitigations
enSilo
 
PPTX
Reversing malware analysis training part3 windows pefile formatbasics
Cysinfo Cyber Security Community
 
PPTX
Reversing malware analysis training part6 practical reversing
Cysinfo Cyber Security Community
 
PPTX
Advanced malwareanalysis training session2 botnet analysis part1
Cysinfo Cyber Security Community
 
PPTX
Advanced Malware Analysis Training Session 4 - Anti-Analysis Techniques
securityxploded
 
PPTX
Advanced Malware Analysis Training Session 1 - Detection and Removal of Malwares
securityxploded
 
PPTX
Advanced Malware Analysis Training Session 11 - (Part 2) Dissecting the Heart...
securityxploded
 
PPTX
Reversing & Malware Analysis Training Part 9 - Advanced Malware Analysis
securityxploded
 
PPTX
Advanced malware analysis training session 7 malware memory forensics
Cysinfo Cyber Security Community
 
PDF
Dynamic Binary Instrumentation
Cysinfo Cyber Security Community
 
PPTX
Tranning-2
Ali Hussain
 
PPTX
Reversing malware analysis training part11 exploit development advanced
Cysinfo Cyber Security Community
 
PPTX
Advanced Malware Analysis Training Session 7 - Malware Memory Forensics
securityxploded
 
Reversing & Malware Analysis Training Part 11 - Exploit Development [Advanced]
securityxploded
 
Reversing & malware analysis training part 1 lab setup guide
securityxploded
 
Introduction to ida python
geeksec80
 
Primer on password security
securityxploded
 
Advanced Malware Analysis Training Session 3 - Botnet Analysis Part 2
securityxploded
 
Advanced Malware Analysis Training Session 6 - Malware Sandbox Analysis
securityxploded
 
Advanced malware analysis training session5 reversing automation
Cysinfo Cyber Security Community
 
Captain Hook: Pirating AVs to Bypass Exploit Mitigations
enSilo
 
Reversing malware analysis training part3 windows pefile formatbasics
Cysinfo Cyber Security Community
 
Reversing malware analysis training part6 practical reversing
Cysinfo Cyber Security Community
 
Advanced malwareanalysis training session2 botnet analysis part1
Cysinfo Cyber Security Community
 
Advanced Malware Analysis Training Session 4 - Anti-Analysis Techniques
securityxploded
 
Advanced Malware Analysis Training Session 1 - Detection and Removal of Malwares
securityxploded
 
Advanced Malware Analysis Training Session 11 - (Part 2) Dissecting the Heart...
securityxploded
 
Reversing & Malware Analysis Training Part 9 - Advanced Malware Analysis
securityxploded
 
Advanced malware analysis training session 7 malware memory forensics
Cysinfo Cyber Security Community
 
Dynamic Binary Instrumentation
Cysinfo Cyber Security Community
 
Tranning-2
Ali Hussain
 
Reversing malware analysis training part11 exploit development advanced
Cysinfo Cyber Security Community
 
Advanced Malware Analysis Training Session 7 - Malware Memory Forensics
securityxploded
 

Similar to Reversing & Malware Analysis Training Part 4 - Assembly Programming Basics (20)

PDF
Reversing & malware analysis training part 4 assembly programming basics
Abdulrahman Bassam
 
PPTX
Introduction to debugging linux applications
commiebstrd
 
PPTX
Introduction to Assembly Language
ApekshaShinde6
 
PPTX
[ASM]Lab6
Nora Youssef
 
PPTX
Exploit Research and Development Megaprimer: DEP Bypassing with ROP Chains
Ajin Abraham
 
PDF
7986-lect 7.pdf
RiazAhmad521284
 
PPT
Malware Analysis - x86 Disassembly
Natraj G
 
PPTX
Advanced procedures in assembly language Full chapter ppt
Muhammad Sikandar Mustafa
 
PPT
Assembly language
Piyush Jain
 
PPTX
Coal (1)
talhashahid40
 
PPT
Advance ROP Attacks
n|u - The Open Security Community
 
PDF
Assembly language part I
Mohammed A. Imran
 
PDF
Assembly language part I
n|u - The Open Security Community
 
PDF
Erlang Message Passing Concurrency, For The Win
l xf
 
PDF
Exploitation Crash Course
UTD Computer Security Group
 
PDF
Assembly_80x86- Assembly languages programming and 80x861.pdf
ahmedmohammed246810a
 
PDF
Specialized Compiler for Hash Cracking
Positive Hack Days
 
PDF
AllBits presentation - Lower Level SW Security
AllBits BVBA (freelancer)
 
PPTX
Procedure.lecture number pptx slide form
itxdevilmehar
 
Reversing & malware analysis training part 4 assembly programming basics
Abdulrahman Bassam
 
Introduction to debugging linux applications
commiebstrd
 
Introduction to Assembly Language
ApekshaShinde6
 
[ASM]Lab6
Nora Youssef
 
Exploit Research and Development Megaprimer: DEP Bypassing with ROP Chains
Ajin Abraham
 
7986-lect 7.pdf
RiazAhmad521284
 
Malware Analysis - x86 Disassembly
Natraj G
 
Advanced procedures in assembly language Full chapter ppt
Muhammad Sikandar Mustafa
 
Assembly language
Piyush Jain
 
Coal (1)
talhashahid40
 
Assembly language part I
Mohammed A. Imran
 
Assembly language part I
n|u - The Open Security Community
 
Erlang Message Passing Concurrency, For The Win
l xf
 
Exploitation Crash Course
UTD Computer Security Group
 
Assembly_80x86- Assembly languages programming and 80x861.pdf
ahmedmohammed246810a
 
Specialized Compiler for Hash Cracking
Positive Hack Days
 
AllBits presentation - Lower Level SW Security
AllBits BVBA (freelancer)
 
Procedure.lecture number pptx slide form
itxdevilmehar
 
Ad

More from securityxploded (20)

PPTX
Fingerprinting healthcare institutions
securityxploded
 
PDF
Hollow Process Injection - Reversing and Investigating Malware Evasive Tactics
securityxploded
 
PDF
Buffer Overflow Attacks
securityxploded
 
PPTX
Malicious Client Detection Using Machine Learning
securityxploded
 
PDF
Understanding CryptoLocker (Ransomware) with a Case Study
securityxploded
 
PDF
Linux Malware Analysis using Limon Sandbox
securityxploded
 
PPT
Introduction to SMPC
securityxploded
 
PPTX
Breaking into hospitals
securityxploded
 
PPTX
Bluetooth [in]security
securityxploded
 
PPTX
Basic malware analysis
securityxploded
 
PPTX
Automating Malware Analysis
securityxploded
 
PPTX
Reverse Engineering Malware
securityxploded
 
PPTX
DLL Preloading Attack
securityxploded
 
PPTX
Partial Homomorphic Encryption
securityxploded
 
PPTX
Hunting Rootkit From the Dark Corners Of Memory
securityxploded
 
PPTX
Return Address – The Silver Bullet
securityxploded
 
PPTX
Defeating public exploit protections (EMET v5.2 and more)
securityxploded
 
PPTX
Hunting Ghost RAT Using Memory Forensics
securityxploded
 
PPTX
Malicious Url Detection Using Machine Learning
securityxploded
 
PPTX
Anatomy of Exploit Kits
securityxploded
 
Fingerprinting healthcare institutions
securityxploded
 
Hollow Process Injection - Reversing and Investigating Malware Evasive Tactics
securityxploded
 
Buffer Overflow Attacks
securityxploded
 
Malicious Client Detection Using Machine Learning
securityxploded
 
Understanding CryptoLocker (Ransomware) with a Case Study
securityxploded
 
Linux Malware Analysis using Limon Sandbox
securityxploded
 
Introduction to SMPC
securityxploded
 
Breaking into hospitals
securityxploded
 
Bluetooth [in]security
securityxploded
 
Basic malware analysis
securityxploded
 
Automating Malware Analysis
securityxploded
 
Reverse Engineering Malware
securityxploded
 
DLL Preloading Attack
securityxploded
 
Partial Homomorphic Encryption
securityxploded
 
Hunting Rootkit From the Dark Corners Of Memory
securityxploded
 
Return Address – The Silver Bullet
securityxploded
 
Defeating public exploit protections (EMET v5.2 and more)
securityxploded
 
Hunting Ghost RAT Using Memory Forensics
securityxploded
 
Malicious Url Detection Using Machine Learning
securityxploded
 
Anatomy of Exploit Kits
securityxploded
 
Ad

Recently uploaded (20)

PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Digital Circuits, important subject in CS
contactparinay1
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 

Reversing & Malware Analysis Training Part 4 - Assembly Programming Basics

  • 2. Disclaimer The Content, Demonstration, Source Code and Programs presented here is "AS IS" without any warranty or conditions of any kind. Also the views/ideas/knowledge expressed here are solely of the trainer’s only and nothing to do with the company or the organization in which the trainer is currently working. However in no circumstances neither the trainer nor SecurityXploded is responsible for any damage or loss caused due to use or misuse of the information presented here. www.SecurityXploded.com
  • 3. Acknowledgement  Special thanks to null & Garage4Hackers community for their extended support and cooperation.  Thanks to all the Trainers who have devoted their precious time and countless hours to make it happen. www.SecurityXploded.com
  • 4. Reversing & Malware Analysis Training This presentation is part of our Reverse Engineering & Malware Analysis Training program. Currently it is delivered only during our local meet for FREE of cost. For complete details of this course, visit our Security Training page. www.SecurityXploded.com
  • 5. Who am I #1 Amit Malik (sometimes DouBle_Zer0,DZZ)  Member SecurityXploded & Garage4Hackers  Security Researcher  RE, Exploit Analysis/Development, Malware Analysis  Email: [email protected] www.SecurityXploded.com
  • 6. Who am I #2 Swapnil Pathak  Member SecurityXploded  Security Researcher  RE, Malware Analysis, Network Security  Email: [email protected] www.SecurityXploded.com
  • 7. Presentation Outline  Intro to x86-32  Assembly Language  Instructions  Stack Operations  Calling conventions  Demo www.SecurityXploded.com
  • 8. x86-32  32 bit instruction set architectures based on Intel 8086 CPU  Address a linear address space up to 4GB  8, 32 bit General Purpose Registers (GPR)  6,16 bit Segment Registers  EFLAGS and EIP register  Control Registers (CR0-CR4) (16 bits)  Memory Management Registers Descriptor Table Registers (GDTR, IDTR, LDTR)  Debug Registers ( DR0-DR7) www.SecurityXploded.com
  • 9. Registers Usage - RE  Register  Storage Locations.  Much faster access compare to memory locations.  EAX: Accumulator , mostly stores return values from functions (APIs)  EBX: Base index (for use with arrays)  ECX: Counter  EDX: Data/general  ESI: Source index for string operations. www.SecurityXploded.com
  • 10. Registers Usage – RE Cont.  EDI: Destination index for string operations.  ESP: Stack pointer for top address of the stack.  EBP: Stack base pointer for holding the address of the current stack frame.  EIP: Instruction pointer. Holds the program counter, the next instruction address.  Segment registers:  Used to address particular segments of memory ( code, data, stack ) !) CS: Code !!) SS: Stack !!!) ES: Extra !V) DS: Data V) FS, GS www.SecurityXploded.com
  • 11. Registers – 32 bit (X86) www.SecurityXploded.com
  • 12. (R/E)Flags Register  Bit field of states  Status Flags  Carrry (CF) : set when an arithmetic carry/borrow has been generated out of the MSB.  Zero (ZF) : set when an arithmetic operation result is zero and reset otherwise.  Sign (SF) : set when an arithmetic operation set the MSB i.e. the result value was negative.  Trap (TF ) : when set permits operation of processor in single-step. Mostly used by debuggers.  Interrupt (IF) : determines whether the CPU should handle maskable hardware interrupts.  Direction (DF) : determines the direction (left-to-right or right-to-left) of string processing.  Overflow (OF) : indicates arithmetic overflow. www.SecurityXploded.com
  • 13. Assembly Language  Low level programming language  Symbolic representation of machine codes, constants.  Assembly language program consist of sequence of process instructions and meta statements  Assembler translates them to executable instructions that are loaded into memory and executed.  Basic Structure [label] : opcode operand1, operand2 opcode – mnemonic that symbolize instructions  Example.  MOV AL, 61h => 10110000 01100001 www.SecurityXploded.com
  • 14. Instructions ADD dst, src - Adds the values of src and dst and stores the result into dst. - For example ADD EAX, 1 SUB dst, src - Subtracts src value from dst and stores the result in dst. - For example SUB EAX, 1 CMP dst, src - Subtracts src value from dst but does store the result in dst - Mostly used to set/reset decision making bits in EFLAGS register such as ZF - For example CMP EAX, EBX www.SecurityXploded.com
  • 15. Instructions cont. MOV dst, src - Moves data from src (left operand) to destination (right operand) - For example mov EDI, ESI Note : - Both operands cannot be memory locations. - Both the operands must be of the same size LEA dst, src - Stands for Load Effective Address. - Computes the effective address of src operand and stores it in dst operand. - For example LEA ECX,[EBX + 5] Note: - Generally brackets denote value at memory locations. - In case of LEA it does simple arithmetic and stores it in dst www.SecurityXploded.com
  • 16. Instructions cont. XOR dst, src - Performs a bitwise exclusive OR operation on the dst and src and stores the result in dst. - Each bit of the result is 1 if the corresponding bits of the operands are different, 0 if the corresponding bit are same Note : - When used with same register clears the contents of the register - Optimized way to clear the register. Better than MOV EAX, 0 www.SecurityXploded.com
  • 17. Instructions cont. REP - Used with string operations - Repeats a string instruction until ECX (counter register) value is equal to zero. - For example REP MOVS byte ptr DS:[EDI], DS:[ESI] LOOP - Similar to loops in high level languages - Used to execute sequence of instructions multiple times. - For example MOV ECX, 10 Test : INC EBX INC EAX LOOP Test www.SecurityXploded.com
  • 18. Instructions cont. TEST dst, src - Performs bitwise logical and between dst and src - Updates the Zero flag bit of the EFLAGS register - Mostly used to check if the return value of the function is not zero - For example TEST EAX, EAX INT 3h - Breakpoint instruction - Used by debuggers to stop execution of the program at particular instruction www.SecurityXploded.com
  • 19. Instructions cont. CALL address - Performs two functions - Push address of the next instruction on stack (return address) - Jump to the address specified by the instruction - For example CALL dword ptr [EAX+4] RET - Transfers the control to the address previously pushed on the stack by CALL instruction - Mostly denotes the end of the function www.SecurityXploded.com
  • 20. Instructions cont. Jump instructions - Categorized as conditional and unconditional - Unconditional jump instructions - JMP (Far Jump) – E9 – (Cross segments) - JMP ( Short Jump ) – EB – (-127 to 128 bytes) - JMP ( Near Jump ) – E9 – (in a segment) - For example JMP EAX - Conditional jump instructions - Jumps according to bit flags set in the EFLAGS register - JC, JNC, JZ, JNZ, JS, JNS, JO, JNO - Unsigned comparisons JA, JAE, JB, JBE - Signed comparisons JG, JGE, JL, JLE - Usually followed by CMP instruction www.SecurityXploded.com
  • 21. Instructions cont. PUSH operand - Pushes operand on the stack - Decrements the stack pointer register by operand size - For example PUSH EAX POP operand - Stores the value pointed by the stack pointer in operand - Increments the stack pointer register by operand size - For example POP EAX Note: POP/PUSH EIP is an invalid instruction PUSHF, POPF www.SecurityXploded.com
  • 22. Calling Conventions  Describes how the arguments are passed and values returned by functions.  Steps performed when a function is called  Arguments are passed to the called function  Program execution is transferred to the address of the called function  Called function starts with lines of code that prepare stack and registers for use within the function. Also known as function prologue. ○ For e.g. push ebp mov ebp, esp or with enter instruction  Called function ends with lines of code that restore stack and registers set initially. Also known as function epilogue. ○ For e.g. mov esp, ebp pop ebp ret or with leave instruction  Passed arguments are removed from the stack, known as stack cleanup. Can be performed by both calling function or called function depending on the calling convention used. www.SecurityXploded.com
  • 23. Calling conventions cont.  __cdecl (C calling convention)  Arguments are passed from right to left and placed on the stack  Stack cleanup is performed by the caller  Return values are stored in EAX register  Standard calling convention used by C compilers  __stdcall (Standard calling convention)  Arguments are passed from right to left and placed on the stack  Stack cleanup is performed by the called function  Return values are stored in EAX register  Standard calling convention for Microsoft Win32 API  __fastcall (Fast calling convention)  Arguments passed are stored in registers for faster access  Thiscall  Arguments are passed from right to left and placed on the stack. this pointer placed in ECX - Standard calling convention for calling member functions of C++ classes www.SecurityXploded.com
  • 24. Stack operations  Stack is a LIFO (Last In First Out) type data structure  Stacks grows downward in memory, from higher memory address to lower memory address  PUSH decrement the stack pointer i.e ESP  POP Increment the stack pointer i.e ESP  Each function has its own stack frame  Function prologue setup the stack frame for each function  Local variable of a function are stored into its stack frame www.SecurityXploded.com
  • 25. Stack #1 www.SecurityXploded.com
  • 26. Stack #2 www.SecurityXploded.com
  • 27. Stack #3  Each function creates its own stack.  Caller function stack: known as parent stack.  Called function stack: known as child stack. For e.g. main(){ ASM Pseudo: sum(); _main: } 123: push ebp 124: mov ebp,esp 125: sub esp,val 126: call _sum 127: mov esp,ebp 128: pop ebp 129: ret www.SecurityXploded.com
  • 28. Stack #4 www.SecurityXploded.com
  • 29. Stack #5 www.SecurityXploded.com
  • 30. Stack #6 www.SecurityXploded.com
  • 31. DEMO (Source Code)  #include <stdio.h>  /*  Author: Amit Malik  http://www.securityxploded.com - Compile in Dev C++  */  int mysum(int,int);  int main()  {  int a,b,s;  a = 5;  b = 6;  s = mysum(a,b); // call mysum function  printf("sum is: %d",s);  getchar();  }  int mysum(int l, int m) // mysum function  {  int c;  c = l + m;  return c;  } www.SecurityXploded.com
  • 33. x86-64 Intro.  64 bit instruction set architectures based on Intel 8086 CPU  Address a linear address space up to 16TB  16, 64 bit General Purpose Registers (GPR)  6, 16 bit Segment Registers  RFLAGS and RIP register  Control Registers (CR0-CR4) and CR8 (16 bits)  Memory Management Registers Descriptor Table Registers (GDTR, IDTR, LDTR) size expanded to 10 bytes  Debug Registers ( DR0-DR7) www.SecurityXploded.com
  • 34. Reference  Complete Reference Guide for Reversing & Malware Analysis Training www.SecurityXploded.com

Editor's Notes

  • #29: Here main is preparing its stack. EBP (base pointer) register is used to track the variables. So we need to push parent function ebp on the stack so that when function return to parent function it can start normally.
  • #30: The instruction mov ebp,esp setting the stack for main(or setting the ebp for main to track variables). And sub esp, val is creating the space for local variables of main function. Although in e.g. we have no local variable but this is for illustrative purpose. NOTE: As we can see in the above pic that stack grows downward (from higher memory address to lower memory address) means something [ebp+val] point to parameters for function (can be pass by value or pass by reference) and everything [ebp-val] point to the local variable for that function.*val- can be any value in hex..like 3,4,a,b etc. etc.Note: ret (startup) address is the address of next instruction in startup function. Explanation of startup function is beyond the scope of this presentation. But main function return 0 or 1 to startup function. Means process successfully completed or error.
  • #31: Now main is calling sum. The first task is to push the address of next instruction means address 127 on stack. So that when function sum return it returns to 127 so that program execution continue in a normal way. Please note that no (another) instruction is required to push ret(127) on stack because call sum will do it for us.. Similar to like main, sum will creates its stack with similar instructions and also destroy its stack with similar instructions (127,128,129).Note: The instructions to create stack and destroy stack.. may vary with compiler to complier and are the issues of compiler optimization..for eg.Some compilers user push reg. instead of sub esp,val for integers.