Exemple de base 64 en asm


#1

Base64 pour Linux x32

format ELF executable 3
entry start
 
include 'include/linux/lin32a.inc'
include 'base64.inc'
 
interpreter '/lib/ld-linux.so.2'
needed 'libc.so.6'
 
import printf
 
;-------------------------------------------------------------------------
segment readable executable
;-------------------------------------------------------------------------
 
start:
 
	mov	edx,S_IRU
	mov	ecx,O_RD
	mov	ebx,fpath1
	mov	eax,_open
	int	0x80
	mov	[fd1],eax
 
	mov	edx,S_IRWU
	mov	ecx,O_CREAT or O_RDWR
	mov	ebx,fpath2
	mov	eax,_open
	int	0x80
	mov	[fd2],eax
 
	mov	ecx,statbuf
	mov	ebx,fpath1
	mov	eax,_stat
	int	0x80
 
	mov	edx,dword [statbuf+20]
	mov	ecx,readbuf
	mov	ebx,[fd1]
	mov	eax,_read
	int	0x80
 
	stdcall	cBase64,readbuf,writebuf,writebuflen
;	stdcall	dBase64,readbuf,writebuf,writebuflen
 
	mov	edx,[writebuflen]
	mov	ecx,writebuf
	mov	ebx,[fd2]
	mov	eax,_write
	int	0x80
 
	xor	ebx,ebx
	mov	eax,_exit
	int	0x80
 
;-------------------------------------------------------------------------
segment readable writeable
;-------------------------------------------------------------------------
 
fd1 dd 0
fd2 dd 0
 
fpath1 db 'test.txt',0
fpath2 db 'file64.txt',0
 
;fpath1 db 'file64.txt',0
;fpath2 db 'test.txt',0
 
statbuf rb 144
readbuf rb 1024
writebuf rb 1024
writebuflen dd 0
;-------------------------------------------------------------------------
segment readable executable
;-------------------------------------------------------------------------
 
proc cBase64 inbuf,outbuf,outbuflen
 
	mov	esi,[inbuf]
	mov	edi,[outbuf]
	mov	ecx,[outbuflen]
    @re:
	or	byte [esi],0
	jz	@end
	add	dword [ecx],4
	xor	eax,eax
 
	or	byte [esi+2],0
	jz	@f
	mov	al,byte [esi]
	inc	esi
	ror	eax,8
	mov	ax,word [esi]
	add	esi,2
	bswap	eax
	call	Encode
	mov	dword [edi],eax
	add	edi,4
	jmp	@re
     @@:
	or	byte [esi+1],0
	jz	@f
	mov	ax,word [esi]
	add	esi,2
	ror	ax,8
	ror	eax,8
	call	Encode
	rol	eax,8
	mov	al,0x3d
	ror	eax,8
	mov	dword [edi],eax
	add	edi,4
	jmp	@re
     @@:
	mov	al,byte [esi]
	inc	esi
	call	Encode
	rol	eax,16
	mov	ax,0x3d3d
	ror	eax,16
	mov	dword [edi],eax
	add	edi,4
	jmp	@re
   @end:
	ret
endp
 
 
proc Encode
 
	push	ecx
	xor	ecx,ecx
	mov	cl,4
     @@:
	xor	ebx,ebx
	mov	bl,al
	shr	bl,2
	rol	eax,6
	mov	dl,byte [base64+ebx]
	ror	edx,8
	dec	cl
	jnz	@b
 
	mov	eax,edx
	pop	ecx
	ret
endp
 
 
proc dBase64 inbuf,outbuf,outbuflen
 
	mov	esi,[inbuf]
	mov	edi,[outbuf]
	mov	ecx,[outbuflen]
    @re:
	or	byte [esi],0
	jz	@end
	or	byte [esi+1],0
	jz	@end
 
	cmp	byte [esi+3],0x3d
	jz	@f
	add	dword [ecx],3
	call	Decode
	mov	word [edi],ax
	add	edi,2
	ror	eax,16
	mov	byte [edi],al
	inc	edi
	jmp	@re
     @@:
	cmp	byte [esi+2],0x3d
	jz	@f
	mov	byte [esi+3],0
	add	dword [ecx],2
	call	Decode
	mov	word [edi],ax
	add	edi,2
	jmp	@re
     @@:
	mov	byte [esi+2],0
	add	dword [ecx],1
	call	Decode
	mov	byte [edi],al
	inc	edi
	jmp	@re
 
   @end:
	ret
endp
 
 
proc Decode
 
	push	ecx
	mov	cl,4
     @@:
	mov	al,byte [esi]
	inc	esi
	call	strlen
 
	shl	dl,2
	mov	al,dl
	shr	eax,2
	shl	eax,8
	dec	cl
	jnz	@b
 
	bswap	eax
	pop	ecx
	ret
endp
 
 
proc strlen
 
	push	edi
	push	ecx
	xor	ecx,ecx
	mov	edi,base64
     @@:
	inc	ecx
	inc	edi
	cmp	al,byte [edi]
	jnz	@b
 
	mov	dl,cl
	pop	ecx
	pop	edi
	ret
endp
 
 
base64 db 0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,\
	  0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,\
	  0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,\
	  0x59,0x5a,0x61,0x62,0x63,0x64,0x65,0x66,\
	  0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,\
	  0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,\
	  0x77,0x78,0x79,0x7a,0x30,0x31,0x32,0x33,\
	  0x34,0x35,0x36,0x37,0x38,0x39,0x2b,0x2f,0

A compiler avec Fasm


#2

Et ?
C’est quoi ? Quel intéret ça a ? Pourquoi tu mets ça là ?
Je suis juste curieux.


#3

Car c’est la section programmation.

Ca sert d’exemple à ceux qui s’intéresseraient à l’asm


#4

Je sais ce qu’est le codage en base64, je date moi même d’une époque ou les mails transitaient par UUCP, en batch, du minitel à 7bits de données un bit de stop, bien avant l’UTF.
Mais ça ne dit pas ce que fait ton code, par rapport à la commande base64 du paquet coreutils, ça peut aider à le lire de le savoir.


#5

Compile le et tu le sauras :wink: