[MASM] AmpliarSeccion

Iniciado por Arkangel, Junio 17, 2013, 08:54:30 AM

Tema anterior - Siguiente tema

0 Miembros y 1 Visitante están viendo este tema.

Junio 17, 2013, 08:54:30 AM Ultima modificación: Febrero 08, 2014, 05:29:11 PM por Expermicid
Enconte este code posteado en GedZac, pero como ya no hay actividad. Lo reposteo aqui. No funciona con exes de 64bits
Código: asm

AmpliarSeccion proc PFilePE:dword,PSizeFile,minSize:dword
LOCAL NumSections
LOCAL NumSectionsRestante
LOCAL PArrySections
LOCAL FileAlignment
LOCAL PFileCopia
LOCAL SeccionEjecu
LOCAL NewSize


mov ebx,PFilePE
cmp word ptr[ebx],5A4DH
jne NoExe
mov edx,[ebx+3Ch]
lea edx,[edx+ebx]
cmp dword ptr[edx],00004550H;Compruevo El PE signature
jne NoExe
mov ecx,[edx+3Ch]
mov FileAlignment,ecx

Sizing: ;Calculando el tamaño que se ampliara
push edx

xor edx,edx
mov eax,minSize
div ecx
test edx,edx
jz resto
inc eax
resto:

mul ecx
mov minSize,eax

pop edx
movzx eax,word ptr[edx+6]
mov NumSections,eax
mov ecx,eax

;Pido memoria para almacenar los datos de cada seccion
shl eax,4;Murtiplico por 16
push eax
push 0
call new
mov PArrySections,eax

lea edx,[edx+0f8h];Tabla de Secciones
mov ebx,eax;PArrySections

bucle:
dec ecx

mov [ebx],edx;PName
mov eax,[edx+14h];PointerToRawData
mov [ebx+4],eax
mov eax,[edx+10h];SizeOfRawData
mov [ebx+8],eax
mov eax,[edx+24h];Characteristics
mov [ebx+0Ch],eax

lea edx,[edx+28h]
add ebx,10h
test ecx,ecx
jnz bucle


push 10h;SizeEstruc
push 4;CampoOrdenar
push NumSections;Size
push PArrySections;PArrySections
Call ordenar_struc


mov ecx,NumSections
shl ecx,4
mov ebx,PArrySections

NoEjecutable:
test ecx,ecx
jz NoExe
sub ecx,10h
test dword ptr[ecx+ebx+0Ch],20000000h;Busca la seccion que tiene la marca de ejecutable
jne NoEjecutable
mov SeccionEjecu,ebx

mov eax,[NumSections]
shl eax,4
sub eax,ecx
mov NumSectionsRestante,ecx


mov eax, PSizeFile
mov eax,dword ptr[eax]
add eax,minSize

mov NewSize,eax
push eax
push 0
call new
mov PFileCopia,eax

mov ebx,SeccionEjecu
mov edx,[ebx+4];Base de la seccion
add edx,[ebx+8];Tamaño de la seccion en el archivo

push edx
push PFilePE
push eax
call CopyMem ;Copio la parte del archivo que no cambiara su posicion


mov eax,edx
mov ecx,PSizeFile
mov ecx,[ecx]
sub ecx,edx;calculo lo que queda por copiar


add eax,minSize
add eax,PFileCopia
add edx,PFilePE

push ecx
push edx
push eax
call CopyMem


mov ecx,NumSectionsRestante
mov eax,PFilePE
mov edx,SeccionEjecu
;//Puntero a una entrada de la tabla de secciones
mov ebx,[edx]
sub ebx, eax
add ebx,PFileCopia

mov esi,minSize
add [ebx+10h],esi
add edx,10h

Reubicando:
;Ajusto los punteros de PointerToRawData a la nueva posicion de las secciones
sub ecx,10h
mov ebx,[edx+ecx]
sub ebx, eax
add ebx,PFileCopia
mov esi,minSize
add [ebx+14h],esi

test ecx,ecx
jnz Reubicando



mov ebx,PSizeFile
mov eax,NewSize
mov [ebx],eax
mov eax,PFileCopia


ret
NoExe:
xor eax,eax
ret

AmpliarSeccion endp