Hack. Eat. Sleep. Repeat!!!
### X86 Assembly intro
In binary,each number is a bit,if we combine 8 bits,it is a byte.A byte can be divided into 4 top bits and 4 lower bits.4 bits is a nibble,Since 4 bits gives you the possible range from 0 - 15 a base 16 number system is easier to work with. Keep in mind when we say base 16 we start with 0 and therefore 0 - 15 is 16 different numbers.This exciting number system is called hexadecimal. The reason why we use this number system is that in x86 Assembly it is much easier to express binary number representations in hexadecimal than it is in any other numbering system.
>>> 2*10**0 + 4*10**1
42
base 16
.Converting “2a” based on the number charts, 10 is a
in hexadecimal while 2
is 2
in hexadecimal.Number has to be converted first in decimal.>>> 10*16**0 + 2*16**1
42
>>> 5*16**0+15*16**1
245
>>>
F1CD
to decimal-:>>> 13*16**0 + 12*16**1 + 1*16**2 + 15*16**3
61901
>>>
Electronic computers are simply made out of transistor switches. Transistors are microscopic crystals of silicon that use electrical properties of silicon to act as switches. Modern computers have what are referred to as field-effect transistors.
8 bits
.Two bytes are a called a word
and two words are called a double word
which 4 bytes(32 bit).Quad word is 4 words
and equal to 64 bits
.A byte is 8 bits and is 2^8 power which is 256. The number of binary numbers 8 bits in size is one of 256 values starting at 0 and going to 255.4 bits is a nibble.nibble
.If we look at 40
,it is 8 bits long which is a byte.If we look at d040, we have two bytes or a word in length. Finally, ffffd040 is a double word or 4 bytes in length which is 32-bits long.he 0x at the beginning of the address just designates that is is a hexadecimal value.Flags - Indicate events when execution occurs.
EIP
or the instruction pointer tht contains the next instruction to be fetched from the memory and then executed.We can immediately see that if we controlled flow of EIP, we can alter the program to do things it was NOT intended to do. This is a popular technique upon which malware operates.#include <stdio.h>
#include <stdlib.h>
void unreachableFunction(void) {
printf("flag{hidden_function_12345678}\n");
}
int main(void) {
printf("Hello World!\n")
return 0;
}
UnreachableFunction
because it cannot be called by the program itself
❯ gcc -m32 -o eipexample eipexample.c
In file included from eipexample.c:1:
/usr/include/stdio.h:28:10: fatal error: bits/libc-header-start.h: No such file or directory
28 | #include <bits/libc-header-start.h>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
sudo apt-get install gcc-multilib
source /home/<user</pwndbg/gdbinit.py
in the ~/.gdbinit
file.gdb ./program
, set disasseumble flavor to intel architecture.set disassembly-flavor intel
Set a breakpoint at function main
with b main
Then, run with r
disas
to disassemble the code with gdb
r
, we will see where it is pointing to.x/1xw $eip
and x/1xb $eip
.disas
and the highlighted part is the memory address.set $eip=<addr>
to hijack the flow of the program.$eip
is $rip
in 64-bit programs.c
to continue-:System flags
CF: Carry Flag
PF: Parity Flag
AF: Adjust Flag
ZF: Zero Flag
SF: Sign Flags
OF: Overflow Flag
Sign flag-:The sign flag is set to the most significant bit of the result which is the sign bit and indicates whether the result is positive or negative.
TF: Trap Flag
IF: Interrupt Enable Flag
IOPL: I/O Privilege Level Flag
NT: Nested Task Flag
RF: Resume Flag
VM: Virtual-8086 Mode Flag
AC: Alignment Check Flag
VIF: Virtual Interrupt Flag
VIP: Virtual Interrupt Pending Flag
ID: Identification Flag
#include <stdio.h>
#include <stdlib.h>
void unreachableFunction(void) {
printf("flag{hidden_function_12345678}\n");
}
int main(void) {
printf("Hello World!\n");
return 0;
}
activation record
.A stack frame exist whenever a function executes and it is yet to complete.For example, inside of the body of the int main(void) there is a call to int addMe(int a, int b) which takes two arguments a and b. There needs to be assembly language code in int main(void) to push the arguments for int addMe(int a, int b) onto the stack.malloc()
or calloc()
which are built-in C functions.Once you have allocated memory on the heap, you are responsible for freeing it by using free() to de-allocate that memory once you don’t need it any more and if it is not done it can lead to a memory leak.That is, memory on the heap will still be set aside and won’t be available to other processes that need it.int main(void) {
return 0;
}
objdump
to find the function within itobjdump -d -M intel test | grep main.: -A11