Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.

Slides:



Advertisements
Similar presentations
Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Web:
Advertisements

C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
Target Processor Directives , When using.386, the program can only run on 386 and above processors.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
Accessing parameters from the stack and calling functions.
Recitation 2: Assembly & gdb Andrew Faulring Section A 16 September 2002.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Arithmetic Flags and Instructions
Sahar Mosleh California State University San MarcosPage 1 Nested Procedure calls and Flowcharts.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 22: Unconventional.
ICS312 Lecture13 String Instructions.
Assembly 09. Outline Strings in x86 esi, edi, ecx, eax stosb, stosw, stosd cld, std rep loop 1.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Chapter 8 String Operations. 8.1 Using String Instructions.
Debugging Survival Guide (x86-DSG) Divyanand Kutagulla
Computer Architecture and Assembly Language
Credits and Disclaimers
Presentation on Real Mode Memory Addressing
Subroutines and the Stack
Microprocessor and Assembly Language
Chapter 4 Data Movement Instructions
Introduction to Compilers Tim Teitelbaum
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
High-Level Language Interface
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Chapter 7 Subroutines Dr. A.P. Preethy
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Symbolic Instruction and Addressing
Introduction to Assembly Language
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Chapter 4: Instructions
Data Addressing Modes • MOV AX,BX; This instruction transfers the word contents of the source-register(BX) into the destination register(AX). • The source.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Symbolic Instruction and Addressing
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Subroutines and the Stack
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah –
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
EECE.3170 Microprocessor Systems Design I
Practical Session 4.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah –
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
T opic: S TRING I NSTRUCTION P RESENTED B Y: N OOR FATIMA M AHA AKRAM ASIF.
EECE.3170 Microprocessor Systems Design I
X86 Assembly Review.
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Subroutines and the Stack
Chapter 6 –Symbolic Instruction and Addressing
CSC 497/583 Advanced Topics in Computer Security
Credits and Disclaimers
ICS51 Introductory Computer Organization
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Presentation transcript:

Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail

All materials is licensed under a Creative Commons “Share Alike” license. http://creativecommons.org/licenses/by-sa/3.0/ Attribution condition: You must indicate that derivative work "Is derived from Xeno Kovah's ‘Intro x86-64’ class, available at http://OpenSecurityTraining.info/IntroX86-64.html" Attribution condition: You must indicate that derivative work "Is derived from Xeno Kovah's 'Intro x86-64’ class, available at http://OpenSecurityTraining.info/IntroX86-64.html”

Zero-initializing the array ArrayLocalVariable2.c Zero-initializing the array main: 0000000140001000 push rdi 0000000140001002 sub rsp,20h 0000000140001006 xor eax,eax 0000000140001008 mov word ptr [rsp+8],ax 000000014000100D lea rax,[rsp+0Ah] 0000000140001012 mov rdi,rax 0000000140001015 xor eax,eax 0000000140001017 mov ecx,0Ah 000000014000101C rep stos byte ptr [rdi] 000000014000101E mov dword ptr [rsp],100Dh 0000000140001025 mov eax,2 000000014000102A imul rax,rax,1 000000014000102E movzx ecx,word ptr [rsp] 0000000140001032 mov word ptr [rsp+rax+8],cx 0000000140001037 mov eax,2 000000014000103C imul rax,rax,1 0000000140001040 movzx eax,word ptr [rsp+rax+8] 0000000140001045 add rsp,20h 0000000140001049 pop rdi 000000014000104A ret //ArrayLocalVariable2.c: short main(){ int a; short b[6] = {0}; a = 0x100d; b[1] = (short)a; return b[1]; }

REP STOS - Repeat Store String 22 REP STOS - Repeat Store String STOS is one of number of instructions that can have the “rep” prefix added to it, which repeat a single instruction multiple times. All rep operations use *cx register as a “counter” to determine how many times to loop through the instruction. Each time it executes, it decrements *cx. Once *cx == 0, it continues to the next instruction. Either stores 1, 2, 4, or 8 bytes at a time Either fill 1 byte at [di] with al or fill 2/4/8 bytes at [*di] with *ax. Moves the *di register forward 1/2/4/8 bytes at a time, so that the repeated store operation is storing into consecutive locations. So there are 3 pieces which must happen before the actual rep stos occurs: set *di to the start destination, *ax/al to the value to store, and *cx to the number of times to store As with other instructions prefixes like “LOCK”, “REP” can only be used with certain instructions - as defined in the manual. Book p. 284

ArrayLocalVariable2.c takeaways If you’re manually coding asm, REP STOS is functionally a memset() Sometimes when you use memset() from C, the compiler may turn it into a REP STOS main: push rdi sub rsp,20h xor eax,eax mov word ptr [rsp+8],ax lea rax,[rsp+0Ah] mov rdi,rax mov ecx,0Ah rep stos byte ptr [rdi] mov dword ptr [rsp],100Dh mov eax,2 imul rax,rax,1 movzx ecx,word ptr [rsp] mov word ptr [rsp+rax+8],cx movzx eax,word ptr [rsp+rax+8] add rsp,20h pop rdi ret //ArrayLocalVariable2.c: short main(){ int a; short b[6] = {0}; a = 0x100d; b[1] = (short)a; return b[1]; }

ThereWillBe0xb100d.c int main(){ char buf[40]; buf[39] = 42; return 0xb100d; }

ThereWillBe0xb100d.c main: 0000000140001010 push rdi 0000000140001012 sub rsp,60h 0000000140001016 mov rdi,rsp 0000000140001019 mov ecx,18h 000000014000101E mov eax,0CCCCCCCCh 0000000140001023 rep stos dword ptr [rdi] 0000000140001025 mov eax,1 000000014000102A imul rax,rax,27h 000000014000102E mov byte ptr buf[rax],2Ah 0000000140001033 mov eax,0xb100d 0000000140001038 mov edi,eax 000000014000103A mov rcx,rsp 000000014000103D lea rdx,[__xi_z+1A0h (0140006910h)] 0000000140001044 call _RTC_CheckStackVars (01400010B0h) 0000000140001049 mov eax,edi 000000014000104B add rsp,60h 000000014000104F pop rdi 0000000140001050 ret

rep stos setup 0000000140001016 mov rdi,rsp Set rdi - the destination 0000000140001019 mov ecx,18h Set ecx - the count 000000014000101E mov eax,0CCCCCCCCh Set eax - the value 0000000140001023 rep stos dword ptr [rdi] Start the repeated store So what’s this going to do? Store 0x18 copies of the dword 0xCCCCCCCC starting at rsp And that just happens to be 0x60 bytes of 0xCC, the entire reserved stack space!

Q: Where does the rep stos come from in this example? A: Compiler-auto-generated code. From the stack frames runtime check option. This is enabled by default in the debug build. Disabling this option removes the compiler-generated code.

More straightforward without the runtime check main: 0000000140001010 sub rsp,38h 0000000140001014 mov eax,1 0000000140001019 imul rax,rax,27h 000000014000101D mov byte ptr [rsp+rax],2Ah 0000000140001021 mov eax,0B100Dh 0000000140001026 add rsp,38h 000000014000102A ret But still not entirely clear :)

Instructions we now know (29) NOP PUSH/POP CALL/RET MOV ADD/SUB IMUL MOVZX/MOVSX LEA JMP/Jcc (family) CMP/TEST AND/OR/XOR/NOT INC/DEC SHR/SHL/SAR/SAL DIV/IDIV REP STOS