Author Topic: [MASM32] FindJmp snippet  (Read 2120 times)

0 Members and 1 Guest are viewing this topic.

Offline ca0s

  • VIP
  • Sir
  • *
  • Posts: 432
  • Cookies: 53
    • View Profile
    • ka0labs #
[MASM32] FindJmp snippet
« on: April 08, 2011, 10:46:27 pm »
It was a test I made time ago, it may be interesting for someone. It finds JMP ESP offsets in a DLL.

Code: [Select]
.386
.model flat, stdcall
option casemap :none

include \masm32\include\psapi.inc
include \masm32\include\masm32rt.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\masm32.lib
includelib \masm32\lib\psapi.lib

Main PROTO

MODULEINFO STRUCT
    lpBaseOfDll DWORD ?
    SizeOfImage DWORD ?
    EntryPoint  DWORD ?
MODULEINFO ENDS

.data?
    HND     HANDLE ?
    MODL    HMODULE  ?
    MINFO   MODULEINFO <?>

.data
    DLL     db  "msvcrt.dll", 0     ; Change DLL name
    Err1    db  "Error1", 13, 0
    EspT    db  "JMP ESP -> ", 0
    NL      db  13, 10, 0

.code
start:
    invoke Main
    invoke ExitProcess, 0

    Main PROC
        invoke GetCurrentProcess
        mov HND, eax

        invoke LoadLibrary, offset DLL
        mov MODL, eax
        cmp eax, 0
        je Fin

        invoke GetModuleInformation, HND, MODL, OFFSET MINFO, 12
        cmp eax, 0
        je Fin

        mov ebx, MINFO.lpBaseOfDll
        mov edx, MINFO.SizeOfImage

        xor ecx, ecx
        Next:
            inc ecx
            cmp edx, ecx
            je Fin
            cmp byte ptr[ebx+ecx], 0FFh        ; JMP ESP
            jne Next
            cmp byte ptr[ebx+ecx+1], 0E4h
            jne Next

        push ecx
        add ecx, ebx
        push ecx
        invoke StdOut, addr EspT    ;
        pop ecx
        invoke StdOut, uhex$(ecx)
        invoke StdOut, addr NL
        je Rest
    Rest:
        pop ecx
        jmp Next

    Fin:
        ret

    Main ENDP

end start