PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : [MASM][Snippet] Importliste erstellen



DizzY_D
10.05.2010, 16:54
Moin,

da SC mal wieder down ist und mir langweilig ist, poste ich diesen Snippet, der auch in PolyObscure verwendet wird, auch hier mal.
Es handelt sich um eine Prozedur, die das IMAGE_IMPORT_DIRECORY parst und eine Liste mit Modulnamen, FirstThunk und API-Namen/Ordinal erstellt.
Sowas findet z.B. in Packern wie UPX und so gut wie jedem Protector verwendung, um die APIs zur Laufzeit zu laden.
Das ist nützlich, wenn man z.B. bestimmte APIs oder sogar die ganze Import Directory aus der PE löschen will.

Vielleicht kanns jemand gebrauchen, vielleicht auch nicht. :)
Feedback wäre trotzdem schön.


buildITlist proc ITRVA:DWORD,ITS:DWORD,buffer:DWORD
LOCAL IToffset:DWORD
LOCAL listptr:DWORD
LOCAL liststart:DWORD
LOCAL listsize:DWORD
LOCAL modlen:DWORD
LOCAL apilen:DWORD
LOCAL ITbuff:DWORD
LOCAL FT:DWORD

invoke GlobalAlloc,GMEM_ZEROINIT,50000 ;Allocate memory for the list
mov listptr,eax
mov liststart,eax
invoke rvatoroffset,ITRVA,buffer
mov IToffset,eax
mov edi,eax
add edi,buffer
mov ITbuff,edi

assume edi:ptr IMAGE_IMPORT_DESCRIPTOR
.while !([edi].OriginalFirstThunk==0 && [edi].TimeDateStamp==0 && [edi].ForwarderChain==0 && [edi].Name1==0 && [edi].FirstThunk==0)
mov eax,[edi].Name1
invoke rvatoroffset,eax,buffer
add eax,buffer
push eax
invoke lstrlen,eax
inc eax
mov modlen,eax
pop eax
invoke lstrcpy,listptr,eax
mov eax,listptr
add eax,modlen
mov listptr,eax
add edi,sizeof IMAGE_IMPORT_DESCRIPTOR
.endw
inc listptr
mov edi, listptr
sub edi,liststart
mov APIListstart,edi
mov edi,ITbuff
assume edi:ptr IMAGE_IMPORT_DESCRIPTOR
.while !([edi].OriginalFirstThunk==0 && [edi].TimeDateStamp==0 && [edi].ForwarderChain==0 && [edi].Name1==0 && [edi].FirstThunk==0)
mov esi,[edi].FirstThunk
mov FT,esi
mov esi,[edi].OriginalFirstThunk
invoke rvatoroffset,esi,buffer
add eax,buffer
mov esi,eax
.while dword ptr [esi]!=0
test dword ptr [esi],IMAGE_ORDINAL_FLAG32
jnz ImportByOrdinal
invoke rvatoroffset,dword ptr[esi],buffer
add eax,buffer ;eax = Hint
add eax,2 ;eax = APIName
push eax
invoke lstrlen,eax
inc eax
mov apilen,eax
inc listptr ;skip ImportByOrdinal Flag
mov ecx,FT
mov edx,listptr
mov dword ptr[edx],ecx ;Write FirstThunk
add edx,4
add listptr,4
pop eax
invoke lstrcpy,edx,eax
mov eax,apilen
add listptr,eax
jmp nextgo
ImportByOrdinal:
mov eax,listptr
mov byte ptr[eax],1 ;Set ImportByOrdinal Flag
inc listptr
mov eax,listptr
mov ecx,FT
mov dword ptr[eax],ecx ;Write FirstThunk
add eax,4
mov ecx,dword ptr[esi]
mov dword ptr[eax],ecx ;Write Ordinal
add listptr,8
nextgo:
add FT,4
add esi,4
.endw
add edi,sizeof IMAGE_IMPORT_DESCRIPTOR
add listptr,4
.endw
mov eax,listptr
mov ecx,liststart
sub eax,ecx
mov listsize,eax
mov ecx,eax
mov eax,liststart
Ret
buildITlist EndP