SlideShare a Scribd company logo
BASIC
ASSEMBLY
FOR REVERSE ENGINEERING
ABOUT
ME
email : sven@unlogic.co.uk
web: https://unlogic.co.uk
twatter : @binaryheadache
freenode : unlogic
you can find github and the rest from there
ABOUT
THIS SESSION
▸x86 arch
▸Calling conventions
▸Basic ops
▸Identify some constructs
▸Cats
// If you have questions at any point, ask ‘em
ABOUT
WHY RE?
▸interoperability
▸figure out how stuff works
▸keygen/cracks
▸exploit development
▸propriety fileformats
ASSUME
MAKING AN ASS OUT OF U AND ME
‣ You know data types and sizes
‣ 0xDEADBEEF isn’t a deceased cow to you
‣ You understand endianness
‣ Intel syntax
‣ Have programmed before
THE BASICS
THE STACK
▸area of memory given to the program by the OS
▸LIFO data structure
▸Grows to lower memory addresses
▸Remember ESP
▸keeps track of prior called functions, holds local vars, and
used to pass args to functions
THE BASICS
THE HEAP & THE REST
▸Dynamic memory allocation
▸grows towards the stack
THE BASICS
REGISTERS
▸4 general purpose registers
▸6 segment registers
▸5 index and pointer registers
THE BASICS
REGISTERS
general purpose
EAX : return values
EBX : base register for memory access
ECX : loop counter
EDX : data register user for I/O
THE BASICS
REGISTERS
segment
CS : stores code segment
DS : stores data segment
ES, FS, GS : far addressing (video mem etc)
SS : Stack segment - usually same as ds
THE BASICS
REGISTERS
indexes and pointers
EDI : destination index register. Array ops
ESI : source index register. Array ops
EBP : base pointer
ESP : stack pointer
EIP : instruction pointer
THE BASICS
32/16/8 BIT REGISTERS
some registers can be accessed with 8 and 16bit instructions.
Most commonly used
THE BASICS
64 BIT
▸twice as good as 32bit
▸extended registers become really extended
rax, rip, rcx, rbp, etc
THE BASICS
FLAGS
Flags holds a number of one bit flags, but for now:
‣ ZF : zero flag
‣ SF : sign flag
CALLING
CONVEN
CALLING CONVENTIONS
CDECL
▸Arguments are passed on the stack in Right-to-Left order,
return values are passed in eax
▸The calling function cleans the stack
CALLING CONVENTIONS
STDCALL (AKA WINAPI)
▸Arguments are passed right-to-left, and return value passed
in eax
▸The called function cleans the stack
CALLING CONVENTIONS
FASTCALL
▸The first 2 or 3 32-bit (or smaller) arguments are passed in
registers, with the most commonly used registers being edx,
eax, and ecx
▸The calling function (usually) cleans the stack
CALLING CONVENTIONS
THISCALL (C++)
▸Only non-static member functions. Also no variadics
▸Pointer to the class object is passed in ecx, the arguments
are passed right-to-left on the stack and return value is
passed in eax
▸the called function cleans the stack
Basic ASM by @binaryheadache
ASM BASICS
OPERAND TYPES
▸immediates : 0x3f
▸registers : eax
▸memory : [0x80542a], [eax]
▸offset : [eax + 0x4]
▸sib : [eax * 4 + 0x53], [eax * 2 + ecx]
ASM BASICS
THE OPS YOU NEED TO KNOW (FOR
NOW)
▸mov
▸add, sub
▸cmp
▸test
▸jcc/jmp
▸push/pop
▸bitwise ops (and, xor, or)
ASM BASICS
MOV
▸mov eax, ecx
▸mov eax, [ecx]
▸mov [ecx], 0x44
▸mov edx, 0x34
▸mov edx, [0x6580fe]
▸mov [0x8045fe], eax
ASM BASICS
ADD
▸add eax, 1
▸add edx, eax
ASM BASICS
CMP
▸cmp eax, ecx
▸cmp eax, 0x45
ASM BASICS
TEST
▸test eax, ecx
▸test edx, 0x12
ASM BASICS
JCC
▸jz/jnz
▸ja/jae
▸jb/jbe/bjnb
…
ASM BASICS
PUSH & POP
▸push eax
▸pop ecx
▸push 0x32
ASM BASICS
BITWISE
▸and edx, ecx
▸and eax, 0x43
▸xor eax, eax
▸or edx, edx
▸not al
RECOGNISING
SOME
COMMON
COMMON CONSTRUCTS
FUNCTION PROLOGUE AND EPILOGUE
push ebp
mov ebp, esp
sub esp, N
.
.
.
mov esp, ebp
pop ebp
ret
COMMON CONSTRUCTS
ABOUT CALL & RET
▸have have an implicit op
▸call will push eip on the stack
▸ret will pop it
COMMON CONSTRUCTS
LOOPS
▸ecx is usually loop counter
▸conditional jumps based on loop counter
▸easier to spot in call graphs
int main() {
int x = 0;
int i = 0;
for (i = 20; i > 0; i--) {
x += i;
}
return 0;
}
COMMON CONSTRUCTS
LOOPS
0x00001f82 837df400 cmp dword [ebp - local_ch], 0
0x00001f86 0f8e17000000 jle 0x1fa3 ;[1]
0x00001f8c 8b45f4 mov eax, dword [ebp - local_ch]
0x00001f8f 0345f8 add eax, dword [ebp - local_8h]
0x00001f92 8945f8 mov dword [ebp - local_8h], eax
0x00001f95 8b45f4 mov eax, dword [ebp - local_ch]
0x00001f98 83c0ff add eax, -1
0x00001f9b 8945f4 mov dword [ebp - local_ch], eax
0x00001f9e e9dfffffff jmp 0x1f82 ;[2]
0x00001fa3 31c0 xor eax, eax
0x00001fa5 83c40c add esp, 0xc
0x00001fa8 5d pop ebp
0x00001fa9 c3 ret
COMMON CONSTRUCTS
LOOPS
SWITCH STATEMENTS
▸different ways to do it depending on compiler settings and
what the cases are
▸the interesting one to me is the look up table
COMMON CONSTRUCTS
SWITCH STATEMENTS
COMMON CONSTRUCTS
ff2485e89704. jmp dword [eax*4 + 0x80497e8]
0x080497e8 e08b 0408 008c 0408 168c 0408 288c 0408 ............(...
0x080497f8 408c 0408 528c 0408 648c 0408 768c 0408 @...R...d...v...
0x08049808 2564 00
meanwhile, at 0x80497e8
#include <stdio.h>
int main(int argc, char **argv) {
switch (argv[1][0]) {
case 'a':
printf("Selected an");
break;
case 'b':
printf("Selected bn");
break;
case 'c':
printf("Selected cn");
break;
default:
printf("poopn");
break;
}
return 0;
}
COMMON CONSTRUCTS
SWITCH STATEMENTS
Basic ASM by @binaryheadache
THE
THE BASICS
THE STACK
int add(int a, int b) {
int r;
r = a + b;
return r;
}
int main () {
int x = 19;
int y = 23;
int result = 0;
result = add(x, y);
return 0;
}
;— add
55 push ebp
89e5 mov ebp, esp
83ec08 sub esp, 8
8b450c mov eax, dword [ebp + arg_ch] ; [0xc:4]=2
8b4d08 mov ecx, dword [ebp + arg_8h] ; [0x8:4]=3
894dfc mov dword [ebp - local_4h], ecx
8945f8 mov dword [ebp - local_8h], eax
8b45fc mov eax, dword [ebp - local_4h]
0345f8 add eax, dword [ebp - local_8h]
83c408 add esp, 8
5d pop ebp
c3 ret
;— main
55 push ebp
89e5 mov ebp, esp
83ec18 sub esp, 0x18
c745fc000000. mov dword [ebp - local_4h], 0
c745f8130000. mov dword [ebp - local_8h], 0x13
c745f4170000. mov dword [ebp - local_ch], 0x17
c745f0000000. mov dword [ebp - local_10h], 0
8b45f8 mov eax, dword [ebp - local_8h]
8b4df4 mov ecx, dword [ebp - local_ch]
890424 mov dword [esp], eax
894c2404 mov dword [esp + local_4h_2], ecx
e8acffffff call sym._add
31c9 xor ecx, ecx
8945f0 mov dword [ebp - local_10h], eax
89c8 mov eax, ecx
83c418 add esp, 0x18
5d pop ebp
c3 ret
gcc -m32 -O0 -masm-intel -S main.c
THE STACK
IN ACTION
THE BASICS
THE STACK
EBP
0x000000
0xffffff
stack growth
EBP
ESP
push ebp
mov ebp, espEAX
EBX
ECX
EDX
THE BASICS
THE STACK 0x000000
0xffffff
stack growth
sub esp, 0x18
EAX
EBX
ECX
EDX
EBP
ESP
THE BASICS
THE STACK
0
0x13
0x17
0
0x000000
0xffffff
stack growth
mov dword [ebp - 0x4], 0
mov dword [ebp - 0x8], 0x13
mov dword [ebp - 0xc], 0x17
mov dword [ebp - 0x10], 0
-0x4
-0x8
-0xc
-0x10
EAX
EBX
ECX
EDX
EBP
ESP
THE BASICS
THE STACK
0
0x13
0x17
0
0x000000
0xffffff
stack growth
EAX
mov eax, dword [ebp - 0x8]
mov ecx, dword [ebp - 0xc]
0X13
EBX
ECX
0X17
EDX
-0x4
-0x8
-0xc
-0x10
EBP
ESP
THE BASICS
THE STACK
0
0x13
0x17
0
0x17
0x13
0x000000
0xffffff
stack growth
EAX
EBP
ESP
mov dword [esp], eax
mov dword [esp + 0x4], ecx
call sym._add
0X13
EBX
ECX
0X17
EDX
-0x4
-0x8
-0xc
-0x10
THE BASICS
THE STACK
0
0x13
0x17
0
0x17
0x13
[eip]
0x000000
0xffffff
stack growth
EAX
EBP
ESP
mov dword [esp], eax
mov dword [esp + 0x4], ecx
call sym._add
0X13
EBX
ECX
0X17
EDX
-0x4
-0x8
-0xc
-0x10
THE BASICS
THE STACK
0
0x13
0x17
0
0x17
0x13
[eip]
ebp
0x000000
0xffffff
stack growth
EAX
EBP
ESP
push ebp
mov ebp, esp
0X13
EBX
ECX
0X17
EDX
-0x4
-0x8
-0xc
-0x10
THE BASICS
THE STACK
[eip]
ebp
0x17
0x13
0x000000
0xffffff
stack growth
EAX
EBP
ESP
sub esp, 8
mov eax, dword [ebp + 0xc]
mov ecx, dword [ebp + 0x8]
mov dword [ebp - local_4h], ecx
mov dword [ebp - local_8h], eax
0X13
EBX
ECX
0X17
EDX
THE BASICS
THE STACK
[eip]
ebp
0x17
0x13
0x000000
0xffffff
stack growth
EAX
EBP
ESP
mov eax, dword [ebp - local_4h]
0X17
EBX
ECX
0X17
EDX
THE BASICS
THE STACK
[eip]
ebp
0x17
0x13
0x000000
0xffffff
stack growth
EAX
EBP
ESP
add eax, dword [ebp - local_8h]
add esp, 8
pop ebp
ret0X2A
EBX
ECX
0X17
EDX
THE BASICS
THE STACK
0
0x13
0x17
0x2a
0x17
0x13
[eip]
0x000000
0xffffff
stack growth
EAX
EBP
ESP
xor ecx, ecx
mov dword [ebp - local_10h], eax
mov eax, ecx
add esp, 0x18
pop ebp
ret
0X0
EBX
ECX
0X0
EDX
-0x4
-0x8
-0xc
-0x10
WE’RE

More Related Content

PDF
Klausz Melinda - Sáringer Viktória: Újgenerációs közösségi média oldalak és k...
PDF
eCommerce Business Plan & Strategy Tips
PPTX
Advertising Campaign Planning for FMCG Product (Mosquito Repellent) Launch PP...
PPTX
The Rise and Fall of Nokia By by Juan Alcacer, Tarun Khanna and Christine Sni...
PPTX
BoltWatch 2017
PPT
Walmart Value Chain by Ben Fieman
PPTX
Apple Case Study.pptx
PDF
Inauthentic complaint through amazon plan of action
Klausz Melinda - Sáringer Viktória: Újgenerációs közösségi média oldalak és k...
eCommerce Business Plan & Strategy Tips
Advertising Campaign Planning for FMCG Product (Mosquito Repellent) Launch PP...
The Rise and Fall of Nokia By by Juan Alcacer, Tarun Khanna and Christine Sni...
BoltWatch 2017
Walmart Value Chain by Ben Fieman
Apple Case Study.pptx
Inauthentic complaint through amazon plan of action

Similar to Basic ASM by @binaryheadache (20)

PPTX
Reversing malware analysis training part4 assembly programming basics
PPT
Assembly language
PPTX
C++ and Assembly: Debugging and Reverse Engineering
PDF
Stale pointers are the new black
PDF
Reversing & malware analysis training part 4 assembly programming basics
PDF
Reverse Engineering Dojo: Enhancing Assembly Reading Skills
PPTX
Intro to reverse engineering owasp
PDF
Creating a Fibonacci Generator in Assembly - by Willem van Ketwich
PPTX
PPTX
PPTX
Reversing & Malware Analysis Training Part 4 - Assembly Programming Basics
PPTX
Introduction to Assembly Language
DOCX
Instruction set of 8086 Microprocessor
PPT
8086 arch instns
PPT
Assem -lect-6
PDF
CNIT 127 Ch 1: Before you Begin
PPT
chapt_5+6AssemblyLanguagecompleteclear.ppt
PDF
CNIT 127 Ch Ch 1: Before you Begin
PPTX
Introduction to debugging linux applications
PPTX
Reversing malware analysis training part4 assembly programming basics
Assembly language
C++ and Assembly: Debugging and Reverse Engineering
Stale pointers are the new black
Reversing & malware analysis training part 4 assembly programming basics
Reverse Engineering Dojo: Enhancing Assembly Reading Skills
Intro to reverse engineering owasp
Creating a Fibonacci Generator in Assembly - by Willem van Ketwich
Reversing & Malware Analysis Training Part 4 - Assembly Programming Basics
Introduction to Assembly Language
Instruction set of 8086 Microprocessor
8086 arch instns
Assem -lect-6
CNIT 127 Ch 1: Before you Begin
chapt_5+6AssemblyLanguagecompleteclear.ppt
CNIT 127 Ch Ch 1: Before you Begin
Introduction to debugging linux applications
Ad

More from camsec (6)

PDF
Cleartext and PtH still alive
PDF
IPv6 for Pentesters
PDF
Custom Rules & Broken Tools (Password Cracking)
PDF
Reversing for beginners 2
PDF
Active Directory Delegation - By @rebootuser
PDF
Working with NIM - By Jordan Hrycaj
Cleartext and PtH still alive
IPv6 for Pentesters
Custom Rules & Broken Tools (Password Cracking)
Reversing for beginners 2
Active Directory Delegation - By @rebootuser
Working with NIM - By Jordan Hrycaj
Ad

Recently uploaded (20)

PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Spectroscopy.pptx food analysis technology
PPTX
1. Introduction to Computer Programming.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Empathic Computing: Creating Shared Understanding
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Tartificialntelligence_presentation.pptx
PDF
cuic standard and advanced reporting.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Mobile App Security Testing_ A Comprehensive Guide.pdf
Spectral efficient network and resource selection model in 5G networks
20250228 LYD VKU AI Blended-Learning.pptx
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Digital-Transformation-Roadmap-for-Companies.pptx
Spectroscopy.pptx food analysis technology
1. Introduction to Computer Programming.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
gpt5_lecture_notes_comprehensive_20250812015547.pdf
A comparative analysis of optical character recognition models for extracting...
Empathic Computing: Creating Shared Understanding
Encapsulation_ Review paper, used for researhc scholars
MYSQL Presentation for SQL database connectivity
Reach Out and Touch Someone: Haptics and Empathic Computing
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Tartificialntelligence_presentation.pptx
cuic standard and advanced reporting.pdf

Basic ASM by @binaryheadache

Editor's Notes

  • #8: bss: static uninit vars (static char* pies), filled with zeros data: init static vars text: binary image of process
  • #15: 22 flags in total
  • #26: Substracts source from destination and updates the flags but does not save result. Flags Affected: AdjustF, CarryF, OverflowF, ParityF, SignF, ZeroF
  • #27: bitwise and of operands flags SignF, ZeroF, ParityF are modified