B-Sides Jax 2014 - Intro To Linux x86 Assembly

From JaxHax
Jump to: navigation, search

Overview

Slides from an Introduction to x86 Linux Assembly Presentation Given at B-Sides Jax on 2014/11/15

Slides

Introduction to x86 Assembly on Linux (pdf - 584 KB)

buildnasm.sh

#!/bin/bash
################################################
# Script: assemble_nasm.sh
# Author: Travis Phillips
# Date: 08/02/2014
# Purpose: A simple script to compile and link
#          NASM .asm files that use syscalls.
################################################
 
####################################
# Just setting some formatting
# codes into easy to use Shell vars
####################################
RED="\033[31;1m"
GREEN="\033[31;1m"
YELLOW="\033[33;1m"
NONE="\033[0m"
 
####################################
# Print a quick header
####################################
echo -e "\n$YELLOW --==[ Compile and Link NASM Script v1.0]==--$NONE"
echo -e "  $YELLOW         Coded By: Travis Phillips          $NONE"
 
####################################
# Check if the user provided a nasm
# source code file. if not, print
# usage and exit
####################################
if [ $# -ne 1 ]; then
	echo -e "\n   Usage: $0 <nasm_source_file>\n"
	exit
fi
 
####################################
#
####################################
file $1 | grep "ASCII text" > /dev/null
if [ $? -ne 0 ]; then
	echo -e "\n $RED[*] File doesn't appear to be text, perhaps it's not a source code file?\n$NONE"
	exit
fi
 
FILEPREFIX=$(echo $1 | cut -d \. -f 1)
 
echo -en "\n [*] Compiling $1 with NASM..."
nasm -f elf $1
if [ $? -ne 0 ]; then
	echo -e " $RED[*] ERROR: NASM Encountered an Error...Exiting!$NONE"
	exit
else
	echo "Done!"
fi
 
echo -n " [*] Linking the object file with ld..."
ld -m elf_i386 $FILEPREFIX.o -o $FILEPREFIX
if [ $? -ne 0 ]; then
	echo -e " $RED[*] ERROR: ld Encountered an Error...Exiting!$NONE"
else
	echo -e "Done!"
fi
 
echo -ne " [*] Removing Object file $FILEPREFIX.o..."
rm $FILEPREFIX.o
echo "Done!"
 
echo -e " \033[32;1m[*] Done Son!\n$NONE"


helloworld.asm

global _start     ; global is used to export the _start label
 
section .text
_start:
   mov eax, 4     ; Syscall number for Write()
 
   mov ebx, 1     ; File Descriptor to write to
                  ; In this case: STDOUT is 1
 
   mov ecx, msg   ; String to write. A pointer to
                  ; the variable 'msg'
 
   mov edx, 14    ; The length of string to print
                  ; which is 14 characters
 
   int 0x80       ; Poke the kernel and tell it to run the
                  ; write() call we set up
 
   mov al, 1      ; Syscall for Exit()
   mov ebx, 0     ; The Exit Code we want to provide.
   int 0x80       ; Poke kernel. This will end the program.
 
section .bbs
   msg: db "Hello, World!",0xa


helloinc.c

#include <stdio.h>
 
int main(){
	printf("Hello, World!\n");
}


forkbomb.asm

global _start
 
section .text
_start:
	xor eax, eax
	mov eax, 2
	int 0x80
	jmp short _start


helloworld_null-less.asm

global _start     ; global is used to export the _start label
 
section .text
_start:
   mov al, 4      ; Syscall number for Write()
 
   mov bl, 1      ; File Descriptor to write to
                  ; In this case: STDOUT is 1
 
   mov ecx, msg   ; String to write. A pointer to
                  ; the variable 'msg'
 
   mov dl, 14     ; The length of string to print
                  ; which is 14 characters
 
   int 0x80       ; Poke the kernel and tell it to run the
                  ; write() call we set up
 
   mov al, 1      ; Syscall for Exit()
   dec ebx        ; The Exit Code we want to provide.
   int 0x80       ; Poke kernel. This will end the program.
 
section .bbs
   msg: db "Hello, World!",0xa


forkbomb_null-less.asm

global _start
 
section .text
_start:
	xor eax, eax
	mov eax, 2
	int 0x80
	jmp short _start