• XSS.stack #1 – первый литературный журнал от юзеров форума

Writing a simple bootloader in NASM

basileusapoleiaoff

HDD-drive
Пользователь
Регистрация
02.08.2023
Сообщения
40
Реакции
21
Hello XSS community, today i want to share with you a simple method to write a bootloader
prerequisites:
  • A functional brain
  • Some low level experience with ASM
  • A desire to learn and understand
0x00: Install the tools

  1. Install linux in a virtual machine for me, I go with arch Linux: ArchWebsite and I use Virtualbox
  2. Install NASM https://nasm.us/ (in my case on arch linux -> sudo pacman -S nasm)
  3. install qemu to emulate our little bootloader https://www.qemu.org/
0x01: The code + Explanation

What is the BIOS ?
BIOS stand for BASIC INPUT/OUTPOUT SYSTEMP) in short term, it make the dev of OS easier
"provide runtime services for operating systems and programs and to perform hardware initialization during the booting process" -> https://en.wikipedia.org/wiki/BIOS



Код:
;This is ASM code

mov ax, 0x07c0
mov ds, ax
mov si, xssgreet
cld
loop:lodsb
     or al, al
     jz hang
     mov ah, 0x0E
     mov bh, 0
     int 0x10
     jmp loop
hang:
     jmp hang
   
xssgreet db 'Hello XSS', 13, 10, 0
    times 510-($-$$) db 0
    db 0x55
    db 0xAA

Explanation line by line:

Capture.PNG


line 1 -> The code in the boot sector of the disk is loaded by the BIOS at 0000:7c00, so we put 0x07c0 in the ax register
MOV copies the source operand to the destination operand without affecting the source.
line 2 -> After this we copy AX into DX
line 3 ->Wee put our little message into the SI register
line 4 -> CLD clears the direction flag = to the operation DF = 0
line 5 -> That's mean the program will repeatedly read bytes from consecutive memory addresses into the AL register.
After each iteration, it will jump back to the "loop" label to continue the process until the loop exits.
line 6 -> if it's zero it's the end of the string
line 7->If it's zero like line 6, it's "jump" to hang
function hang: like his name said, this jump infinitely
line 8->This write a char in TTY mode
line9 ->Going to text mode
line10->call INT 10h, the BIOS video service again https://en.wikipedia.org/wiki/BIOS_interrupt_call#Invoking_an_interrupt
line11->going to the loop, again

line 15->okay a little bit more complicated. First, we declare xssgreet as a data variable (db) stand for defined byte which contain the string Hello XSS. The "13" is like \r to move the cursor to the beginning of the line. And finally, the "10" is like the \n. And "0" it's a 0 just a null byte it indicate the end of the string.
line 16->This "times" is used for repetition. In this case, this ensure that the boot sector (512 bytes) is filled with "0" except for the bootloader and the data. This ensure that the bootloader start at the byte "0" of the boot sector.
line 17 & line 18-> This is the boot signature (0xAA55) that tell the bios that the sector is bootable and can be loaded in the memory.

0x02: Compiling and testing

Okay now you need to compile it with the following command
Код:
nasm boot.asm -f bin -o boot.bin
It should return nothing if they don't have any errors in the little program

After that you can simply run it with Qemu
Код:
qemu-system-i386 -fda boot.bin -nographic
This command emulate a floppy disk and if it's correct its should look like this:

Capture2.PNG



Conclusion:

I hope you appreciated this tutorial and learning new things.
 


Напишите ответ...
  • Вставить:
Прикрепить файлы
Верх