首页
社区
课程
招聘
0816-cyclotron-任务完成-待整理
发表于: 2004-8-16 09:00 5641

0816-cyclotron-任务完成-待整理

2004-8-16 09:00
5641
LOADDLL.EXE

This is the full source code of loaddll.exe. It can be compiled using Borland's TASM32 with following commands:

tasm32 -mx -zi -m5 loaddll.asm,,loaddll.lst
tlink32 -v- -c -S:40000 -B:400000 -Tpe -aa -m loaddll,,,import32.lib
brc32 loaddll.rc -feloaddll.exe

Execution begins at START. loaddll gets command line, skips name of executable (must be taken into double quotes!), extracts path to DLL and passes it to LoadLibrary. On error, it places pointer to error message on fixed location and exits with code 0x1001. On success, it creates simple main window and pauses on Firstbp. This breakpoint is set by OllyDbg on startup.

All communication with OllyDbg is done through the 128-byte link area. This area must begin at address 0x420020 immediately after keyphrase. First several words contain addresses in loaddll.exe used by OllyDbg to set breakpoints and parameters, followed by address of function to call, contents of registers, number of arguments and arguments itself. Number of arguments is limited to 10. If argument is a pointer to memory, you can use 10 data buffers, 1 Kbyte each, named as Arg1, Arg2, ..., Arg10. These and some other names are exported and thus known to OllyDbg.

When loaddll passes main windows loop (WINLOOP), it constantly checks whether address of exported function in PROCADR is not 0. If this is the case, loaddll saves contents of ESP and EBP and pushes 16 zeros into stack. This is necessary to avoid crash if user specifies invalid number of arguments. Then it pushes arguments and sets registers. At address Prepatch there are 16 NOPs that you can use for small patches. If you need more space, you can jump to Patcharea 2 Kbytes long. Note that OllyDbg doesn't extract loaddll.exe from resources if file with this name already exists.

At CallDLL export is called. This command is followed by another 16 NOPs. Then routine saves modified registers and offset of ESP after call. If you supply invalid number of arguments to PASCAL-style function, OllyDbg will be able to report this error to you. Finally, loaddll restores ESP and EBP, zeroes PROCADR and breaks at INT3 at address Finished. When this point is reached, OllyDbg knows that execution is finished.

Treat LOADDLL.ASM as a freeware. I will not protest if you use this program as whole or in parts (without copyright) in your own programs. But do not dare to use the Green Bug (LOADDLL.RC) in projects not related to OllyDbg… That’s all, enjoy!

LOADDLL.ASM:

           P586                        ; 32-bit instructions used!
           MODEL FLAT,PASCAL
           IDEAL                       ; I really like it!
           LOCALS

           PUBLICDLL WndProc,Firstbp,Prepatch,CallDLL,Finished;
           PUBLICDLL Patcharea,Endpatch
           PUBLICDLL Arg1,Arg2,Arg3,Arg4,Arg5,Arg6,Arg7,Arg8,Arg9,Arg10

           SEGMENT _DATA1 PARA PUBLIC USE32 'DATA'

           ; Text below is a keyphrase used by OllyDbg to verify that LoadDll
           ; is correct. Loads at fixed address 00420000. Never change! Note
           ; how coward I am: you cannot replace copyright, otherwise this code
           ; will not work!
           DB "DLL Loader (C) 2004 Oleh Yuschuk"

           ; Link area. Never change the meaning or order of next 32 dwords!
ERRMSG     DD 0                        ; Pointer to error
HINST      DD 0                        ; Process instance
HWND       DD 0                        ; Handle of main window
DLLBASE    DD 0                        ; Base address of loaded DLL or NULL
           DD OFFSET Firstbp           ; Address of first breakpoint
           DD OFFSET Prepatch          ; Address of patch area before call
           DD OFFSET Arg1              ; Base of 10 arguments x 1024 bytes
           DD OFFSET Finished          ; Address of breakpoint after call
DUMMY      DD 4 DUP(0)                 ; Reserved for the future
PROCADR    DD 0                        ; Address of procedure, starts execution
REGEAX     DD 0                        ; Register arguments
REGECX     DD 0
REGEDX     DD 0
REGEBX     DD 0
REGESI     DD 0
REGEDI     DD 0
NARG       DD 0                        ; Number of arguments to push on stack
ARGLIST    DD 10 DUP(0)                ; DLL argument list
ESPDIFF    DD 0                        ; Difference in ESP caused by code
           DD 0                        ; Reserved for the future

WCLASS     = THIS DWORD                ; Hand-made WNDCLASS structure
           DD 0000002Bh                ; CS_HREDRAW|VREDRAW|DBLCLKS|OWNDC
           DD WndProc                  ; Window procedure
           DD 0                        ; Class extra bytes
           DD 0                        ; Window extra bytes
WCINST     DD 0                        ; Instance
WCICON     DD 0                        ; Icon
HCURS      DD 0                        ; Cursor
HBGND      DD 0                        ; Background brush
           DD 0                        ; No menu
           DD CLSNAME                  ; Class name

MSG        = THIS DWORD                ; Hand-made MSG structure
           DD 0                        ; Handle of window
MSGID      DD 0                        ; Message ID
           DD 0                        ; wParam
           DD 0                        ; lParam
           DD 0                        ; Timestamp
           DD 0                        ; X coordinate
           DD 0                        ; Y coordinate

PSTRUCT    = THIS DWORD                ; Hand-made PAINTSTRUCT structure
           DD 0                        ; HDC
           DD 0                        ; fErase
           DD 0                        ; rcPaint.left
           DD 0                        ; rcPaint.top
           DD 0                        ; rcPaint.right
           DD 0                        ; rcPaint.bottom
           DD 0                        ; fRestore
           DD 0                        ; fIncUpdate
           DB 32 DUP(0)                ; rgbReserved

ORIGESP    DD 0                        ; Original ESP before call
ORIGEBP    DD 0                        ; Original EBP before call
EXPESP     DD 0                        ; Expected ESP after call (C)
WNDNAME    DB "OllyDbg DLL Loader",0
CLSNAME    DB "LoadDLLClass",0
ICONAME    DB "MAINICON",0             ; Green smashed bug - igitt!
E_NONAM    DB "Missing DLL name",0     ; Error notifications to OllyDbg
E_NODLL    DB "Unable to load DLL",0
E_NPARM    DB "Too many parameters",0

           ALIGN 16
Arg1       DB 1024 DUP (?)             ; Area for 10 memory arguments, 1 k each
Arg2       DB 1024 DUP (?)
Arg3       DB 1024 DUP (?)
Arg4       DB 1024 DUP (?)
Arg5       DB 1024 DUP (?)
Arg6       DB 1024 DUP (?)
Arg7       DB 1024 DUP (?)
Arg8       DB 1024 DUP (?)
Arg9       DB 1024 DUP (?)
Arg10      DB 1024 DUP (?)

           ENDS _DATA1

           SEGMENT _TEXT1 PARA PUBLIC USE32 'CODE'

           EXTRN GetModuleHandleA:     PROC
           EXTRN GetCommandLineA:      PROC
           EXTRN LoadIconA:            PROC
           EXTRN LoadCursorA:          PROC
           EXTRN GetStockObject:       PROC
           EXTRN RegisterClassA:       PROC
           EXTRN CreateWindowExA:      PROC
           EXTRN DestroyWindow:        PROC
           EXTRN PostQuitMessage:      PROC
           EXTRN ShowWindow:           PROC
           EXTRN Sleep:                PROC
           EXTRN BeginPaint:           PROC
           EXTRN EndPaint:             PROC
           EXTRN DefWindowProcA:       PROC
           EXTRN LoadLibraryA:         PROC
           EXTRN PeekMessageA:         PROC
           EXTRN TranslateMessage:     PROC
           EXTRN DispatchMessageA:     PROC
           EXTRN ExitProcess:          PROC

           ; Window procedure of main LoadDLL window.
           PROC WndProc
           ARG LP:DWORD,WP:DWORD,MS:DWORD,HW:DWORD
           PUSH EDX
           PUSH EDI
           PUSH ESI
           MOV EAX,[MS]
           CMP EAX,0001h               ; WM_CREATE
           JE RET0
           CMP EAX,0002h               ; WM_DESTROY
           JNE @@080
             PUSH 0
             CALL PostQuitMessage
             JMP RET0
@@080:     CMP EAX,000Fh               ; WM_PAINT
           JNE @@100
             PUSH OFFSET PSTRUCT
             PUSH [HW]
             CALL BeginPaint
             PUSH OFFSET PSTRUCT
             PUSH [HW]
             CALL EndPaint
             JMP RET0
@@100:     CMP EAX,0010h               ; WM_CLOSE
           JNE @@200
             PUSH [HW]
             CALL DestroyWindow
             JMP RET0
@@200:     ; None of listed above, pass to DefWindowProc().
           PUSH [LP]
           PUSH [WP]
           PUSH [MS]
           PUSH [HW]
           CALL DefWindowProcA
           JMP RETA
RET0:      XOR EAX,EAX
           JMP SHORT RETA
RET1:      MOV EAX,1
RETA:      POP ESI
           POP EDI
           POP EDX
           RET
           ENDP WndProc

START:     MOV EBP,ESP                 ; Here execution begins
           PUSH 0
           CALL GetModuleHandleA
           MOV [DWORD DS:WCINST],EAX
           MOV [DWORD DS:HINST],EAX
           CALL GetCommandLineA        ; Path to LOADDLL is taken into quotes
           MOV ESI,EAX
           INC ESI                     ; Skip first quote
@@10:        MOV AL,[BYTE DS:ESI]      ; Skip path to LOADDLL.EXE
             INC ESI
             OR AL,AL
             JNE @@12
               MOV [DWORD DS:ERRMSG],OFFSET E_NONAM
               JMP ERROR
@@12:        CMP AL,'"'
             JNE @@10
@@20:        MOV AL,[BYTE DS:ESI]      ; Skip spaces
             CMP AL,' '
             JNE @@30
             INC ESI
             JMP SHORT @@20
@@30:      PUSH ESI
           CALL LoadLibraryA           ; Load DLL
           OR EAX,EAX
           JNE @@32
             MOV [DWORD DS:ERRMSG],OFFSET E_NODLL
             JMP ERROR
@@32:      MOV [DWORD DS:DLLBASE],EAX
           PUSH OFFSET ICONAME
           PUSH [DWORD DS:HINST]
           CALL LoadIconA
           MOV [DWORD DS:WCICON],EAX
           PUSH 7F88h                  ; IDC_NO
           PUSH 0                      ; External resource
           CALL LoadCursorA
           MOV [DWORD DS:HCURS],EAX
           PUSH 0                      ; WHITE_BRUSH
           CALL GetStockObject
           MOV [DWORD DS:HBGND],EAX
           PUSH OFFSET WCLASS
           CALL RegisterClassA
           PUSH 0                      ; Parameters: none
           PUSH [DWORD DS:HINST]       ; Instance
           PUSH 0                      ; Menu: none
           PUSH 0                      ; Parent window: none
           PUSH 100                    ; Width
           PUSH 200                    ; Height
           PUSH 80000000h              ; CW_USEDEFAULT
           PUSH 80000000h              ; CW_USEDEFAULT
           PUSH 10CF0000h              ; WS_OVERLAPPEDWINDOW|WS_VISIBLE
           PUSH OFFSET WNDNAME         ; Window name
           PUSH OFFSET CLSNAME         ; Class name
           PUSH 0                      ; Extended style: none
           CALL CreateWindowExA
           MOV [DWORD DS:HWND],EAX     ; Save handle
           PUSH 9                      ; SW_RESTORE
           PUSH EAX
           CALL ShowWindow
Firstbp:   NOP                         ; First breakpoint is set here
WINLOOP:     CMP [DWORD DS:PROCADR],0  ; Request to call some function?
             JE NOCALL
               MOV [DWORD DS:ORIGESP],ESP
               MOV [DWORD DS:ORIGEBP],ESP
               PUSH 0                  ; Security buffer (16 doublewords)
               PUSH 0
               PUSH 0
               PUSH 0
               PUSH 0
               PUSH 0
               PUSH 0
               PUSH 0
               PUSH 0
               PUSH 0
               PUSH 0
               PUSH 0
               PUSH 0
               PUSH 0
               PUSH 0
               PUSH 0
               MOV ECX,[DWORD DS:NARG]
               JECXZ @@44
               CMP ECX,10
               JBE @@40
                 MOV [DWORD DS:ERRMSG],OFFSET E_NPARM
                 JMP ERROR
@@40:          MOV EAX,OFFSET ARGLIST
@@42:            PUSH [DWORD EAX]      ; Push requested number of arguments
                 ADD EAX,4
                 LOOP @@42
@@44:          MOV [DWORD DS:EXPESP],ESP ; Expected ESP after return (C)
               MOV EAX,[DWORD DS:REGEAX] ; Preset registers
               MOV ECX,[DWORD DS:REGECX]
               MOV EDX,[DWORD DS:REGEDX]
               MOV EBX,[DWORD DS:REGEBX]
               MOV ESI,[DWORD DS:REGESI]
               MOV EDI,[DWORD DS:REGEDI]
Prepatch:      NOP                     ; Patch area before call
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
CallDLL:       CALL [DWORD DS:PROCADR] ; Call DLL function
               NOP                     ; Patch area after call
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               MOV [DWORD DS:REGEAX],EAX ; Get modified registers
               MOV [DWORD DS:REGECX],ECX
               MOV [DWORD DS:REGEDX],EDX
               MOV [DWORD DS:REGEBX],EBX
               MOV [DWORD DS:REGESI],ESI
               MOV [DWORD DS:REGEDI],EDI
               MOV EAX,ESP
               SUB EAX,[DWORD DS:EXPESP]
               MOV [DWORD DS:ESPDIFF],EAX
               MOV EBP,[DWORD DS:ORIGEBP]
               MOV ESP,[DWORD DS:ORIGESP]
               MOV [DWORD DS:PROCADR],0 ; Confirm execution
               NOP
Finished:      INT 3                   ; Pause after execution
               NOP
NOCALL:      PUSH 0
             CALL Sleep                ; Be fair to other applications
             PUSH 1                    ; PM_REMOVE
             PUSH 0                    ; Process all messages
             PUSH 0
             PUSH 0                    ; Any window
             PUSH OFFSET MSG
             CALL PeekMessageA
             OR EAX,EAX
             JZ WINLOOP
             PUSH OFFSET MSG
             CALL TranslateMessage
             PUSH OFFSET MSG
             CALL DispatchMessageA
             MOV EAX,[DWORD DS:MSGID]
             CMP EAX,12h               ; WM_QUIT
             JNE WINLOOP
           PUSH 0
           CALL ExitProcess            ; Hasta la vista!
ERROR:     PUSH 00001001h              ; Special return code, means error
           CALL ExitProcess            ; Error detected
           ALIGN 4
Patcharea: DB 2047 DUP(90h)            ; Big patch area (2 K of NOPs)
Endpatch:  NOP
           ENDS _TEXT1

           END START

LOADDLL.RC:

MAINICON ICON                          // Green bug
{
'00 00 01 00 02 00 20 20 10 00 00 00 00 00 E8 02'
'00 00 26 00 00 00 10 10 10 00 00 00 00 00 28 01'
'00 00 0E 03 00 00 28 00 00 00 20 00 00 00 40 00'
'00 00 01 00 04 00 00 00 00 00 80 02 00 00 00 00'
'00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00'
'00 00 00 00 80 00 00 80 00 00 00 80 80 00 80 00'
'00 00 80 00 80 00 80 80 00 00 80 80 80 00 C0 C0'
'C0 00 00 00 FF 00 00 FF 00 00 00 FF FF 00 FF 00'
'00 00 FF 00 FF 00 FF FF 00 00 FF FF FF 00 00 00'
'00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00'
'00 00 00 00 02 A0 00 00 00 00 A2 00 00 00 00 00'
'00 00 00 00 0A 20 00 00 00 0A 2A 2A 00 00 00 00'
'00 00 00 00 A2 A2 00 00 00 A2 A2 A2 00 00 00 00'
'00 00 00 00 2A 2A 00 00 0A 2A 2A 2A 00 00 00 00'
'00 00 00 00 A2 A2 00 00 A2 A2 A2 A0 00 00 00 00'
'00 00 00 00 2A 2A 00 0A 2A 2A 2A 20 00 00 02 A2'
'00 00 00 00 02 A2 A0 A2 A2 A2 A2 00 00 00 0A 2A'
'2A 2A 00 00 0A 2A 2A 2A 2A 2A 20 00 00 00 02 A2'
'A2 A2 A0 00 02 A2 A2 A2 A2 A2 00 00 00 00 0A 2A'
'2A 2A 2A 2A 2A 2A 2A 2A 2A 00 00 00 00 00 00 A2'
'A2 A2 A2 A2 A2 A2 A2 A2 00 00 00 00 00 00 00 00'
'2A 2A 2A 2A 2A 2A 2A 2A 00 00 00 00 00 00 00 00'
'00 02 A2 A2 A2 A2 A2 A2 00 00 00 00 00 00 00 00'
'00 00 0A 2A 2A 2A 2A 2A 2A 00 00 00 00 00 00 00'
'00 00 02 A2 A2 A2 A2 A2 A2 A2 A2 00 00 00 00 00'
'00 00 0A 2A 2A 2A 2A 2A 2A 2A 2A 20 00 00 00 00'
'00 00 02 A2 A2 A2 A2 A2 A2 A2 A2 A2 A2 00 00 00'
'00 00 0A 2A 2A 2A 2A 2A 00 00 2A 2A 2A 20 00 00'
'00 00 A2 A2 A0 A2 A2 A0 00 00 02 A2 A2 A0 00 00'
'00 00 2A 2A 00 0A 2A 20 00 00 00 2A 2A 20 00 00'
'00 00 00 00 00 00 A2 A0 00 00 00 00 00 00 00 00'
'00 00 00 00 00 00 2A 20 00 00 00 00 00 00 00 00'
'00 00 00 00 00 00 A2 A0 00 00 00 00 00 00 00 00'
'00 2A 20 00 00 00 2A 2A 00 00 00 00 00 00 00 00'
'A2 A2 A0 00 00 00 A2 A2 00 00 00 00 00 00 00 00'
'2A 2A 20 00 00 00 2A 2A 00 00 00 00 00 00 00 00'
'A2 A2 A0 00 00 00 02 A2 00 00 00 00 00 00 00 0A'
'2A 2A 20 00 00 00 0A 2A 00 00 00 00 00 00 00 02'
'A2 A2 00 00 00 00 00 00 00 00 00 00 00 00 00 0A'
'2A 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00'
'00 00 00 00 00 00 00 00 00 00 00 00 00 00 FF F9'
'FF 3F FF F0 FE 0F FF F0 FC 07 FF E0 78 07 FF E0'
'70 07 FF E0 60 0F 8F E0 40 0F 00 F0 00 1F 00 70'
'00 3F 00 10 00 7F 00 00 00 FF 80 00 03 FF C0 00'
'0F FF E0 00 03 FF FC 00 00 3F FF 00 00 1F FF 00'
'00 03 FF 00 00 01 FF 00 00 00 FE 00 0F 00 FE 04'
'0F 80 FF 0E 0F C1 FF FE 0F FF FC 7E 0F FF F0 3E'
'07 FF E0 3E 07 FF E0 3E 07 FF C0 3F 07 FF C0 3F'
'07 FF C0 7F 0F FF C0 FF FF FF E3 FF FF FF 28 00'
'00 00 10 00 00 00 20 00 00 00 01 00 04 00 00 00'
'00 00 C0 00 00 00 00 00 00 00 00 00 00 00 00 00'
'00 00 00 00 00 00 00 00 00 00 00 00 80 00 00 80'
'00 00 00 80 80 00 80 00 00 00 80 00 80 00 80 80'
'00 00 80 80 80 00 C0 C0 C0 00 00 00 FF 00 00 FF'
'00 00 00 FF FF 00 FF 00 00 00 FF 00 FF 00 FF FF'
'00 00 FF FF FF 00 00 00 00 00 00 00 00 00 00 00'
'00 AA 00 0A AA 00 00 00 00 AA 00 AA AA 00 00 00'
'00 AA 0A AA A0 00 0A AA 00 0A AA AA 00 00 0A AA'
'AA AA AA 00 00 00 00 0A AA AA AA 00 00 00 00 00'
'0A AA AA A0 00 00 00 00 0A AA AA AA AA 00 00 00'
'AA AA AA 00 AA A0 00 00 00 00 A0 00 00 00 00 00'
'00 00 A0 00 00 00 00 0A 00 00 AA 00 00 00 00 AA'
'00 00 AA 00 00 00 00 AA 00 00 00 00 00 00 00 00'
'00 00 00 00 00 00 FC E3 00 00 F8 41 00 00 F8 01'
'00 00 88 03 00 00 00 07 00 00 00 0F 00 00 80 1F'
'00 00 E0 03 00 00 F0 01 00 00 E0 00 00 00 F0 31'
'00 00 EE 3F 00 00 86 1F 00 00 86 1F 00 00 87 3F'
'00 00 8F FF 00 00'

[培训]科锐逆向工程师培训第53期2025年7月8日开班!

收藏
免费 1
支持
分享
最新回复 (1)
雪    币: 392
活跃值: (909)
能力值: ( LV9,RANK:690 )
在线值:
发帖
回帖
粉丝
2
LOADDLL.EXE

This is the full source code of loaddll.exe. It can be compiled using Borland's TASM32 with following commands:

这是loaddll.exe的完整源代码,可以在Borland的TASM32中以下列命令编译。

tasm32 -mx -zi -m5 loaddll.asm,,loaddll.lst
tlink32 -v- -c -S:40000 -B:400000 -Tpe -aa -m loaddll,,,import32.lib
brc32 loaddll.rc -feloaddll.exe

Execution begins at START. loaddll gets command line, skips name of executable (must be taken into double quotes!), extracts path to DLL and passes it to LoadLibrary. On error, it places pointer to error message on fixed location and exits with code 0x1001. On success, it creates simple main window and pauses on Firstbp. This breakpoint is set by OllyDbg on startup.

程序从START标号处开始执行,loaddll接受命令行参数,跳过可执行文件名(必须以双引号引用),解析DLL路径并传递给LoadLibrary。如果发生错误,它将在固定位置设置指向错误信息的指针,并以退出码0x1001退出。如果加载成功,它将建立一个简单的主窗口并停在第一个断点上。该断点在启动时由OllyDbg设置。

All communication with OllyDbg is done through the 128-byte link area. This area must begin at address 0x420020 immediately after keyphrase. First several words contain addresses in loaddll.exe used by OllyDbg to set breakpoints and parameters, followed by address of function to call, contents of registers, number of arguments and arguments itself. Number of arguments is limited to 10. If argument is a pointer to memory, you can use 10 data buffers, 1 Kbyte each, named as Arg1, Arg2, ..., Arg10. These and some other names are exported and thus known to OllyDbg。

所有与OllyDbg的通信行为都要借助128字节的关联区域。这个区域必须在标志性短语【keyphrase】之后从0x420020地址处立即开始。区域的头几个字包含了OllyDbg在loaddll.exe中用来设置断点和参数的地址,接着是待调用函数的地址【address of function to call】,寄存器的内容,参数个数以及参数本身。其中参数不得超过10个。如果将参数用作指向内存区域的指针,那么您可以使用10个大小为1K字节的数据缓冲区,名字分别为Arg1, Arg2, ..., Arg10。这些参数名以及其他的一些名称将被输出并通知OllyDbg。

When loaddll passes main windows loop (WINLOOP), it constantly checks whether address of exported function in PROCADR is not 0. If this is the case, loaddll saves contents of ESP and EBP and pushes 16 zeros into stack. This is necessary to avoid crash if user specifies invalid number of arguments. Then it pushes arguments and sets registers. At address Prepatch there are 16 NOPs that you can use for small patches. If you need more space, you can jump to Patcharea 2 Kbytes long. Note that OllyDbg doesn't extract loaddll.exe from resources if file with this name already exists.

每当loaddll通过了主窗口循环(WINLOOP),它都会检测PROCADR输出的函数地址是否为0。如果是的话,loaddll就保存ESP和EBP的内容并将16个0压入堆栈。这种必要的检测可以在用户指定了无效的参数个数时用来避免崩溃。然后它会把参数压入堆栈并设定寄存器。在Prepatch处有16个NOP指令,您可以将它们用作小型补丁区。如果需要更多的空间,您可以跳到Patcharea处,那里有2K字节的空间。需要注意的是,如果以loaddll.exe为名的文件已经存在,则OllyDbg不会再把loaddll.exe从资源中释放出来。

At CallDLL export is called. This command is followed by another 16 NOPs. Then routine saves modified registers and offset of ESP after call. If you supply invalid number of arguments to PASCAL-style function, OllyDbg will be able to report this error to you. Finally, loaddll restores ESP and EBP, zeroes PROCADR and breaks at INT3 at address Finished. When this point is reached, OllyDbg knows that execution is finished.

输出函数在CallDll处被调用。这个命令后面也紧跟了16个NOP。程序将保存修改过的寄存器以及调用之后ESP的偏移。如果您向遵循PASCAL约定的函数提供了无效的参数个数,OllyDbg将会向您报告这一错误。最后,loaddll恢复ESP和EBP的值,清空PROCADR并在结束地址处的INT3指令上中断下来。到达这里后,OllyDbg就得知执行已经结束。

Treat LOADDLL.ASM as a freeware. I will not protest if you use this program as whole or in parts (without copyright) in your own programs. But do not dare to use the Green Bug (LOADDLL.RC) in projects not related to OllyDbg… That’s all, enjoy!

请将LOADDLL.ASM当作自由软件。我不反对您在自己的程序中不带版权地使用这一的程序部分或全部,但请勿斗胆在与OllyDbg无关的项目中使用GreenBug图标……我的说明完了,好好享用吧!

LOADDLL.ASM:

           P586                        ; 32-bit instructions used!
                                           使用32位指令
           MODEL FLAT,PASCAL
           IDEAL                       ; I really like it!
                                           我真的很喜欢它!
           LOCALS

           PUBLICDLL WndProc,Firstbp,Prepatch,CallDLL,Finished;
           PUBLICDLL Patcharea,Endpatch
           PUBLICDLL Arg1,Arg2,Arg3,Arg4,Arg5,Arg6,Arg7,Arg8,Arg9,Arg10

           SEGMENT _DATA1 PARA PUBLIC USE32 'DATA'

           ; Text below is a keyphrase used by OllyDbg to verify that LoadDll
           ; is correct. Loads at fixed address 00420000. Never change! Note
           ; how coward I am: you cannot replace copyright, otherwise this code
           ; will not work!
           
           下面的文本是OllyDbg用来确认LoadDll文件正常的标志性短语【keyphrase】,
           它总是被加载到固定地址00420000处。我是怎样一个胆小鬼啊:您不准替换版权,
           否则这段代码就无法工作!
           
           DB "DLL Loader (C) 2004 Oleh Yuschuk"

           ; Link area. Never change the meaning or order of next 32 dwords!
ERRMSG     DD 0                        ; Pointer to error
                                        指向错误信息的指针
HINST      DD 0                        ; Process instance
                                        进程实例句柄
HWND       DD 0                        ; Handle of main window
                                        主窗口句柄
DLLBASE    DD 0                        ; Base address of loaded DLL or NULL
                                        被加载DLL的基址或者就是零
           DD OFFSET Firstbp           ; Address of first breakpoint
                                           第一个断点的地址
           DD OFFSET Prepatch          ; Address of patch area before call
                                           调用之前的补丁区地址
           DD OFFSET Arg1              ; Base of 10 arguments x 1024 bytes
                                           10个参数x1024个字节的基址
           DD OFFSET Finished          ; Address of breakpoint after call
                                           调用后断点地址
DUMMY      DD 4 DUP(0)                 ; Reserved for the future
                                        保留未作使用
PROCADR    DD 0                        ; Address of procedure, starts execution
                                        过程地址,从这里开始执行
REGEAX     DD 0                        ; Register arguments
                                        寄存器参数
REGECX     DD 0
REGEDX     DD 0
REGEBX     DD 0
REGESI     DD 0
REGEDI     DD 0
NARG       DD 0                        ; Number of arguments to push on stack
                                        压栈参数的个数
ARGLIST    DD 10 DUP(0)                ; DLL argument list
                                        DLL参数表
ESPDIFF    DD 0                        ; Difference in ESP caused by code
                                        代码致ESP偏移
           DD 0                        ; Reserved for the future
                                           保留未作使用

WCLASS     = THIS DWORD                ; Hand-made WNDCLASS structure
                                        手工构造的WNDCLASS结构
           DD 0000002Bh                ; CS_HREDRAW|VREDRAW|DBLCLKS|OWNDC
           DD WndProc                  ; Window procedure
                                           窗口过程
           DD 0                        ; Class extra bytes
                                           类附加字节
           DD 0                        ; Window extra bytes
                                           窗口附加字节
WCINST     DD 0                        ; Instance
                                        实例句柄
WCICON     DD 0                        ; Icon
                                        图标
HCURS      DD 0                        ; Cursor
                                        光标
HBGND      DD 0                        ; Background brush
                                        背景刷
           DD 0                        ; No menu
                                           无菜单
           DD CLSNAME                  ; Class name
                                           类名

MSG        = THIS DWORD                ; Hand-made MSG structure
                                        手工构造的MSG结构
           DD 0                        ; Handle of window
                                           窗口句柄
MSGID      DD 0                        ; Message ID
                                        消息ID
           DD 0                        ; wParam
                                           字参数
           DD 0                        ; lParam
                                           长字参数
           DD 0                        ; Timestamp
                                           时间戳
           DD 0                        ; X coordinate
                                           X 坐标
           DD 0                        ; Y coordinate
                                           Y 坐标

PSTRUCT    = THIS DWORD                ; Hand-made PAINTSTRUCT structure
                                        手工构造的PAINTSTRUCT结构
           DD 0                        ; HDC
                                           设备描述符句柄
           DD 0                        ; fErase
                                           擦除标志
           DD 0                        ; rcPaint.left
           DD 0                        ; rcPaint.top
           DD 0                        ; rcPaint.right
           DD 0                        ; rcPaint.bottom
           DD 0                        ; fRestore
           DD 0                        ; fIncUpdate
           DB 32 DUP(0)                ; rgbReserved

ORIGESP    DD 0                        ; Original ESP before call
                                        调用前初始ESP
ORIGEBP    DD 0                        ; Original EBP before call
                                        调用前初始EBP
EXPESP     DD 0                        ; Expected ESP after call (C)
                                        调用后预期ESP
WNDNAME    DB "OllyDbg DLL Loader",0
CLSNAME    DB "LoadDLLClass",0
ICONAME    DB "MAINICON",0             ; Green smashed bug - igitt!
E_NONAM    DB "Missing DLL name",0     ; Error notifications to OllyDbg
                                        发错误通知给OllyDbg
E_NODLL    DB "Unable to load DLL",0
E_NPARM    DB "Too many parameters",0

           ALIGN 16
Arg1       DB 1024 DUP (?)             ; Area for 10 memory arguments, 1 k each
                                        10个内存指针参数的目标区域,每个1k字节
Arg2       DB 1024 DUP (?)
Arg3       DB 1024 DUP (?)
Arg4       DB 1024 DUP (?)
Arg5       DB 1024 DUP (?)
Arg6       DB 1024 DUP (?)
Arg7       DB 1024 DUP (?)
Arg8       DB 1024 DUP (?)
Arg9       DB 1024 DUP (?)
Arg10      DB 1024 DUP (?)

           ENDS _DATA1

           SEGMENT _TEXT1 PARA PUBLIC USE32 'CODE'

           EXTRN GetModuleHandleA:     PROC
           EXTRN GetCommandLineA:      PROC
           EXTRN LoadIconA:            PROC
           EXTRN LoadCursorA:          PROC
           EXTRN GetStockObject:       PROC
           EXTRN RegisterClassA:       PROC
           EXTRN CreateWindowExA:      PROC
           EXTRN DestroyWindow:        PROC
           EXTRN PostQuitMessage:      PROC
           EXTRN ShowWindow:           PROC
           EXTRN Sleep:                PROC
           EXTRN BeginPaint:           PROC
           EXTRN EndPaint:             PROC
           EXTRN DefWindowProcA:       PROC
           EXTRN LoadLibraryA:         PROC
           EXTRN PeekMessageA:         PROC
           EXTRN TranslateMessage:     PROC
           EXTRN DispatchMessageA:     PROC
           EXTRN ExitProcess:          PROC

           ; Window procedure of main LoadDLL window.
           LoadDLL主窗口的窗口过程
           PROC WndProc
           ARG LP:DWORD,WP:DWORD,MS:DWORD,HW:DWORD
           PUSH EDX
           PUSH EDI
           PUSH ESI
           MOV EAX,[MS]
           CMP EAX,0001h               ; WM_CREATE
           JE RET0
           CMP EAX,0002h               ; WM_DESTROY
           JNE @@080
             PUSH 0
             CALL PostQuitMessage
             JMP RET0
@@080:     CMP EAX,000Fh               ; WM_PAINT
           JNE @@100
             PUSH OFFSET PSTRUCT
             PUSH [HW]
             CALL BeginPaint
             PUSH OFFSET PSTRUCT
             PUSH [HW]
             CALL EndPaint
             JMP RET0
@@100:     CMP EAX,0010h               ; WM_CLOSE
           JNE @@200
             PUSH [HW]
             CALL DestroyWindow
             JMP RET0
@@200:     ; None of listed above, pass to DefWindowProc().
                以上情况都不满足,则传递给DefWindowProc()
           PUSH [LP]
           PUSH [WP]
           PUSH [MS]
           PUSH [HW]
           CALL DefWindowProcA
           JMP RETA
RET0:      XOR EAX,EAX
           JMP SHORT RETA
RET1:      MOV EAX,1
RETA:      POP ESI
           POP EDI
           POP EDX
           RET
           ENDP WndProc

START:     MOV EBP,ESP                 ; Here execution begins
                                        从这里开始执行
           PUSH 0
           CALL GetModuleHandleA
           MOV [DWORD DS:WCINST],EAX
           MOV [DWORD DS:HINST],EAX
           CALL GetCommandLineA        ; Path to LOADDLL is taken into quotes
                                           LOADDLL路径被引入引号
           MOV ESI,EAX
           INC ESI                     ; Skip first quote
                                           跳过第一个引号
@@10:        MOV AL,[BYTE DS:ESI]      ; Skip path to LOADDLL.EXE
                                        跳过LOADDLL.EXE路径
             INC ESI
             OR AL,AL
             JNE @@12
               MOV [DWORD DS:ERRMSG],OFFSET E_NONAM
               JMP ERROR
@@12:        CMP AL,'"'
             JNE @@10
@@20:        MOV AL,[BYTE DS:ESI]      ; Skip spaces
                                        跳过空格
             CMP AL,' '
             JNE @@30
             INC ESI
             JMP SHORT @@20
@@30:      PUSH ESI
           CALL LoadLibraryA           ; Load DLL
                                           加载DLL
           OR EAX,EAX
           JNE @@32
             MOV [DWORD DS:ERRMSG],OFFSET E_NODLL
             JMP ERROR
@@32:      MOV [DWORD DS:DLLBASE],EAX
           PUSH OFFSET ICONAME
           PUSH [DWORD DS:HINST]
           CALL LoadIconA
           MOV [DWORD DS:WCICON],EAX
           PUSH 7F88h                  ; IDC_NO
           PUSH 0                      ; External resource
                                           外部资源
           CALL LoadCursorA
           MOV [DWORD DS:HCURS],EAX
           PUSH 0                      ; WHITE_BRUSH
           CALL GetStockObject
           MOV [DWORD DS:HBGND],EAX
           PUSH OFFSET WCLASS
           CALL RegisterClassA
           PUSH 0                      ; Parameters: none
                                           无参数
           PUSH [DWORD DS:HINST]       ; Instance
           PUSH 0                      ; Menu: none
                                           无菜单
           PUSH 0                      ; Parent window: none
                                           无父窗口
           PUSH 100                    ; Width
           PUSH 200                    ; Height
           PUSH 80000000h              ; CW_USEDEFAULT
           PUSH 80000000h              ; CW_USEDEFAULT
           PUSH 10CF0000h              ; WS_OVERLAPPEDWINDOW|WS_VISIBLE
           PUSH OFFSET WNDNAME         ; Window name
           PUSH OFFSET CLSNAME         ; Class name
           PUSH 0                      ; Extended style: none
                                           无扩展风格
           CALL CreateWindowExA
           MOV [DWORD DS:HWND],EAX     ; Save handle
                                           保存句柄
           PUSH 9                      ; SW_RESTORE
           PUSH EAX
           CALL ShowWindow
Firstbp:   NOP                         ; First breakpoint is set here
                                        第一个断点设在这里
WINLOOP:     CMP [DWORD DS:PROCADR],0  ; Request to call some function?
                                        请求调用某函数?
             JE NOCALL
               MOV [DWORD DS:ORIGESP],ESP
               MOV [DWORD DS:ORIGEBP],ESP
               PUSH 0                  ; Security buffer (16 doublewords)
                                               安全缓冲区(16个双字)
               PUSH 0
               PUSH 0
               PUSH 0
               PUSH 0
               PUSH 0
               PUSH 0
               PUSH 0
               PUSH 0
               PUSH 0
               PUSH 0
               PUSH 0
               PUSH 0
               PUSH 0
               PUSH 0
               PUSH 0
               MOV ECX,[DWORD DS:NARG]
               JECXZ @@44
               CMP ECX,10
               JBE @@40
                 MOV [DWORD DS:ERRMSG],OFFSET E_NPARM
                 JMP ERROR
@@40:          MOV EAX,OFFSET ARGLIST
@@42:            PUSH [DWORD EAX]      ; Push requested number of arguments
                                        压入请求的参数个数
                 ADD EAX,4
                 LOOP @@42
@@44:          MOV [DWORD DS:EXPESP],ESP ; Expected ESP after return (C)
                                        返回后的预期ESP
               MOV EAX,[DWORD DS:REGEAX] ; Preset registers
                                               预设寄存器
               MOV ECX,[DWORD DS:REGECX]
               MOV EDX,[DWORD DS:REGEDX]
               MOV EBX,[DWORD DS:REGEBX]
               MOV ESI,[DWORD DS:REGESI]
               MOV EDI,[DWORD DS:REGEDI]
Prepatch:      NOP                     ; Patch area before call
                                        调用前的补丁区
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
CallDLL:       CALL [DWORD DS:PROCADR] ; Call DLL function
                                        调用DLL函数
               NOP                     ; Patch area after call
                                               调用后的补丁区
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               NOP
               MOV [DWORD DS:REGEAX],EAX ; Get modified registers
                                                       取得修改过的寄存器值
               MOV [DWORD DS:REGECX],ECX
               MOV [DWORD DS:REGEDX],EDX
               MOV [DWORD DS:REGEBX],EBX
               MOV [DWORD DS:REGESI],ESI
               MOV [DWORD DS:REGEDI],EDI
               MOV EAX,ESP
               SUB EAX,[DWORD DS:EXPESP]
               MOV [DWORD DS:ESPDIFF],EAX
               MOV EBP,[DWORD DS:ORIGEBP]
               MOV ESP,[DWORD DS:ORIGESP]
               MOV [DWORD DS:PROCADR],0 ; Confirm execution
                                               确认执行
               NOP
Finished:      INT 3                   ; Pause after execution
                                        执行后暂停
               NOP
NOCALL:      PUSH 0
             CALL Sleep                ; Be fair to other applications
                                             【补偿其他程序的消息处理】
             PUSH 1                    ; PM_REMOVE
             PUSH 0                    ; Process all messages
                                             处理所有消息
             PUSH 0
             PUSH 0                    ; Any window
                                             来自所有窗口
             PUSH OFFSET MSG
             CALL PeekMessageA
             OR EAX,EAX
             JZ WINLOOP
             PUSH OFFSET MSG
             CALL TranslateMessage
             PUSH OFFSET MSG
             CALL DispatchMessageA
             MOV EAX,[DWORD DS:MSGID]
             CMP EAX,12h               ; WM_QUIT
             JNE WINLOOP
           PUSH 0
           CALL ExitProcess            ; Hasta la vista!
                                           【什么意思?成功退出?】
ERROR:     PUSH 00001001h              ; Special return code, means error
                                        特定的返回值,表示有错误发生
           CALL ExitProcess            ; Error detected
                                           检测到错误
           ALIGN 4
Patcharea: DB 2047 DUP(90h)            ; Big patch area (2 K of NOPs)
                                        大型补丁区(2K字节的NOP指令)
Endpatch:  NOP
           ENDS _TEXT1

           END START

LOADDLL.RC:
2004-8-16 19:44
0
游客
登录 | 注册 方可回帖
返回