.686
.387
.mmx
.k3d
.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\comdlg32.inc
;include \masm32\include\gdi32.inc
include \masm32\include\kernel32.inc
IncludeLib \masm32\lib\comdlg32.lib
IncludeLib \masm32\lib\user32.lib
IncludeLib \masm32\lib\kernel32.lib
;IncludeLib \masm32\lib\gdi32.lib



DlgProcForm PROTO :DWORD,:DWORD,:DWORD,:DWORD
.Data
DlgForm    DB  "Form",0
ofn   OPENFILENAME <>
Error		db "Error!",0
WrongSize	db "The Tables sizes do not match, please dump again!",0
ErrPatch	db "Error patching dump file! Unable to locate correct Jump opcodes!",0
PatchOK		db "The dump is fixed successfully! ",0
ByteMsg		db  " jumps instruction fixed and int3 removed!",0

JumpOpcodes	db 070h, 0, 0Fh, 080h		;JO
		db 076h, 0, 0Fh, 086h		;JBE
		db 07Bh, 0, 0Fh, 08Bh		;JNP
		db 07Eh, 0, 0Fh, 08Eh		;JLE	
		db 07Ch, 0, 0Fh, 08Ch		;JL
		db 0E3h, 0, 0, 0		;JECXZ
		db 076h, 0, 0Fh, 086h		;JBE
		db 072h, 0, 0Fh, 082h		;JB
		db 075h, 0, 0Fh, 085h		;JNZ
		db 07Eh, 0, 0Fh, 08Eh		;JLE
		db 07Ah, 0, 0Fh, 08Ah		;JP
		db 075h, 0, 0Fh, 085h		;JNZ
		db 0EBh, 0E9h, 0, 0		;JMP
		db 077h, 0, 0Fh, 087h		;JA
		db 074h, 0, 0Fh, 084h		;JZ
		db 072h, 0, 0Fh, 082h		;JB
		db 07Dh, 0, 0Fh, 08Dh		;JGE
		db 073h, 0, 0Fh, 083h		;JAE

.data?
hndInstForm    DD  ?
CmdLineForm    DD  ?

;      Do not change the order
hAddFileBrowse    		DD ?
hJumpTypeBrowse			dd ?
hInstructionLengthBrowse	DD ?
hJumpDistanceBrowse		DD ?
hAddressFileEdit		DD ?
hJumpTypeEdit			DD ?
hInstructionLengthEdit		DD ?
hJumpDistanceEdit		DD ?
hDumpFileEdit			DD ?
hDumpFileBrowse			DD ?
hPatchBtn			DD ?
hIDC_EXIT			DD ?
hIntLogEdit             DD ?
hIntLogBrowse           DD ?


hAddressFile			dd ?
hAddressMapping			dd ?
pAddressMapping	 		dd ?
hJumpTypeFile			dd ?
hJumpTypeMapping		dd ?
pJumpTypeMapping		dd ?
hInstructionLengthFile		dd ?
hInstructionLengthMapping	dd ?
pInstructionLengthMapping	dd ?
hJumpDistanceFile		dd ?
hJumpDistanceMapping		dd ?
pJumpDistanceMapping		dd ?
hDumpFile			dd ?
hDumpMapping			dd ?
pDumpMapping			dd ?
hIntLogFile             dd ?
hIntLogMapping                dd ?
pIntLogMapping                dd ?

AddressTableSize		dd ?
JumpTypeSize			dd ?
InstructionLengthSize		dd ?
JumpDistanceSize		dd ?
IntLogSize              dd ?

temp				dd ?
TableSize			dd ?
counter				dd ?
pNameBuffer			db 512 dup (?)
Buffer				db 512 dup (?)

.const
AddFileBrowse     		equ	1001
JumpTypeBrowse			equ	1004	
InstructionLengthBrowse		equ	1003
JumpDistanceBrowse		equ	1002
AddressFileEdit			equ	1005
JumpTypeEdit			equ     1006
InstructionLengthEdit		equ	1009
JumpDistanceEdit		equ     1008
DumpFileEdit			equ     1007
DumpFileBrowse			equ     1010
PatchBtn			equ     1011

IntLogEdit               equ          1019
IntLogBrowse             equ          1013    

IDC_EXIT			equ     1012


.code
start:
        Invoke GetModuleHandle, NULL
        mov hndInstForm, eax
        Invoke GetCommandLine
        mov CmdLineForm, eax
        Invoke DialogBoxParam, hndInstForm,addr DlgForm, 0, addr DlgProcForm,0
        Invoke ExitProcess, eax

;-----------------------------------------------------------------------------
DlgProcForm proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM

          .If uMsg==WM_INITDIALOG
                Invoke GetDlgItem, hWnd,AddFileBrowse
                mov hAddFileBrowse,eax
                Invoke GetDlgItem, hWnd,JumpTypeBrowse
                mov hJumpTypeBrowse,eax
                Invoke GetDlgItem, hWnd,IntLogBrowse
                mov hIntLogBrowse,eax
                Invoke GetDlgItem, hWnd,InstructionLengthBrowse
                mov hInstructionLengthBrowse,eax
                Invoke GetDlgItem, hWnd,JumpDistanceBrowse
                mov hJumpDistanceBrowse,eax
                Invoke GetDlgItem, hWnd,AddressFileEdit
                mov hAddressFileEdit,eax
                Invoke GetDlgItem, hWnd,IntLogEdit
                mov hIntLogEdit,eax
                Invoke GetDlgItem, hWnd,JumpTypeEdit
                mov hJumpTypeEdit,eax
                Invoke GetDlgItem, hWnd,InstructionLengthEdit
                mov hInstructionLengthEdit,eax
                Invoke GetDlgItem, hWnd,JumpDistanceEdit
                mov hJumpDistanceEdit,eax
                Invoke GetDlgItem, hWnd,DumpFileEdit
                mov hDumpFileEdit,eax
                Invoke GetDlgItem, hWnd,DumpFileBrowse
                mov hDumpFileBrowse,eax
                Invoke GetDlgItem, hWnd,PatchBtn
                mov hPatchBtn,eax
                Invoke GetDlgItem, hWnd,IDC_EXIT
                mov hIDC_EXIT,eax
        
  	.ElseIf uMsg==WM_CLOSE
        	invoke SendMessage,hWnd,WM_COMMAND,IDC_EXIT,0
        	
  	.ElseiF uMsg==WM_COMMAND
  		mov eax,wParam
		.IF lParam==0
			.IF ax==IDC_EXIT
				invoke EndDialog, hWnd,NULL
			.ENDIF
  		.Else 
			mov edx,wParam
			shr edx,16
			.If dx == BN_CLICKED
				.IF ax==AddFileBrowse
					  	mov ofn.lStructSize,SIZEOF ofn
						mov  ofn.lpstrFile, OFFSET pNameBuffer
						mov  ofn.nMaxFile,512
						mov  ofn.Flags, OFN_FILEMUSTEXIST or \
        						OFN_PATHMUSTEXIST or OFN_LONGNAMES or\
        						OFN_EXPLORER or OFN_HIDEREADONLY
						invoke GetOpenFileName, ADDR ofn				; Nothing much here, just Open File Dialog
						.if eax==TRUE	
							invoke SetDlgItemText, hWnd, AddressFileEdit, ofn.lpstrFile						
						.endif
			
				.elseif ax==JumpTypeBrowse
					  	mov ofn.lStructSize,SIZEOF ofn
						mov  ofn.lpstrFile, OFFSET pNameBuffer
						mov  ofn.nMaxFile,512
						mov  ofn.Flags, OFN_FILEMUSTEXIST or \
        						OFN_PATHMUSTEXIST or OFN_LONGNAMES or\
        						OFN_EXPLORER or OFN_HIDEREADONLY
						invoke GetOpenFileName, ADDR ofn				; Nothing much here, just Open File Dialog
						.if eax==TRUE	
							invoke SetDlgItemText, hWnd, JumpTypeEdit, ofn.lpstrFile						
						.endif

				.elseif ax==InstructionLengthBrowse
					  	mov ofn.lStructSize,SIZEOF ofn
						mov  ofn.lpstrFile, OFFSET pNameBuffer
						mov  ofn.nMaxFile,512
						mov  ofn.Flags, OFN_FILEMUSTEXIST or \
        						OFN_PATHMUSTEXIST or OFN_LONGNAMES or\
        						OFN_EXPLORER or OFN_HIDEREADONLY
						invoke GetOpenFileName, ADDR ofn				; Nothing much here, just Open File Dialog
						.if eax==TRUE	
							invoke SetDlgItemText, hWnd, InstructionLengthEdit, ofn.lpstrFile						
						.endif

				.elseif ax==JumpDistanceBrowse
					  	mov ofn.lStructSize,SIZEOF ofn
						mov  ofn.lpstrFile, OFFSET pNameBuffer
						mov  ofn.nMaxFile,512
						mov  ofn.Flags, OFN_FILEMUSTEXIST or \
        						OFN_PATHMUSTEXIST or OFN_LONGNAMES or\
        						OFN_EXPLORER or OFN_HIDEREADONLY
						invoke GetOpenFileName, ADDR ofn				; Nothing much here, just Open File Dialog
						.if eax==TRUE	
							invoke SetDlgItemText, hWnd, JumpDistanceEdit, ofn.lpstrFile						
						.endif

				.elseif ax==DumpFileBrowse
					  	mov ofn.lStructSize,SIZEOF ofn
						mov  ofn.lpstrFile, OFFSET pNameBuffer
						mov  ofn.nMaxFile,512
						mov  ofn.Flags, OFN_FILEMUSTEXIST or \
        						OFN_PATHMUSTEXIST or OFN_LONGNAMES or\
        						OFN_EXPLORER or OFN_HIDEREADONLY
						invoke GetOpenFileName, ADDR ofn				; Nothing much here, just Open File Dialog
						.if eax==TRUE	
							invoke SetDlgItemText, hWnd, DumpFileEdit, ofn.lpstrFile						
						.endif

				.elseif ax==IntLogBrowse
					  	mov ofn.lStructSize,SIZEOF ofn
						mov  ofn.lpstrFile, OFFSET pNameBuffer
						mov  ofn.nMaxFile,512
						mov  ofn.Flags, OFN_FILEMUSTEXIST or \
        						OFN_PATHMUSTEXIST or OFN_LONGNAMES or\
        						OFN_EXPLORER or OFN_HIDEREADONLY
						invoke GetOpenFileName, ADDR ofn				; Nothing much here, just Open File Dialog
						.if eax==TRUE	
							invoke SetDlgItemText, hWnd, IntLogEdit, ofn.lpstrFile						
						.endif

				.elseif ax==PatchBtn
						push ebx
						push ecx
						invoke GetDlgItemText, hWnd, AddressFileEdit, addr pNameBuffer, 0512h
						invoke CreateFile, addr pNameBuffer, GENERIC_READ,NULL, \ ;open file name
                					NULL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,NULL
						mov hAddressFile, eax
						invoke GetFileSize, eax, addr temp
						mov AddressTableSize, eax
						
						invoke GetDlgItemText, hWnd, JumpTypeEdit, addr pNameBuffer, 0512h
						invoke CreateFile, addr pNameBuffer, GENERIC_READ,NULL, \ ;open file name
                					NULL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,NULL
						mov hJumpTypeFile, eax
						invoke GetFileSize, eax, addr temp
						mov JumpTypeSize, eax

						invoke GetDlgItemText, hWnd, InstructionLengthEdit, addr pNameBuffer, 0512h
						invoke CreateFile, addr pNameBuffer, GENERIC_READ,NULL, \ ;open file name
                					NULL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,NULL
						mov hInstructionLengthFile, eax
						invoke GetFileSize, eax, addr temp
						mov InstructionLengthSize, eax

						invoke GetDlgItemText, hWnd, JumpDistanceEdit, addr pNameBuffer, 0512h
						invoke CreateFile, addr pNameBuffer, GENERIC_READ,NULL, \ ;open file name
	               					NULL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,NULL
						mov hJumpDistanceFile, eax
						invoke GetFileSize, eax, addr temp
						mov JumpDistanceSize, eax

						invoke GetDlgItemText, hWnd, IntLogEdit, addr pNameBuffer, 0512h
						invoke CreateFile, addr pNameBuffer, GENERIC_READ,NULL, \ ;open file name
                					NULL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,NULL
						mov hIntLogFile, eax
						invoke GetFileSize, eax, addr temp
						mov IntLogSize, eax
						
						mov eax, AddressTableSize
						mov ebx, JumpTypeSize
						mov TableSize, ebx
						mov ecx, ebx
						shl ecx, 2
						.If (JumpDistanceSize==eax)
						.if (ebx==InstructionLengthSize)
						.if (eax==ecx)

						        invoke  CreateFileMapping, hAddressFile, NULL, PAGE_READONLY \ ;create filemapping
					        	       ,0,0,NULL
							mov     hAddressMapping, eax      
        						invoke  MapViewOfFile,eax,FILE_MAP_READ,0,0,0   ;view and store result
        						mov     pAddressMapping, eax        			; Open the damn file

						        invoke  CreateFileMapping, hJumpTypeFile, NULL, PAGE_READONLY \ ;create filemapping
					        	       ,0,0,NULL
							mov     hJumpTypeMapping, eax      
	       						invoke  MapViewOfFile,eax,FILE_MAP_READ,0,0,0   ;view and store result
        						mov     pJumpTypeMapping, eax        			; Open the damn file

						        invoke  CreateFileMapping, hInstructionLengthFile, NULL, PAGE_READONLY \ ;create filemapping
					        	       ,0,0,NULL
							mov     hInstructionLengthMapping, eax      
	     						invoke  MapViewOfFile,eax,FILE_MAP_READ,0,0,0   ;view and store result
        						mov     pInstructionLengthMapping, eax        			; Open the damn file

						        invoke  CreateFileMapping, hJumpDistanceFile, NULL, PAGE_READONLY \ ;create filemapping
					        	       ,0,0,NULL
							mov     hJumpDistanceMapping, eax      
        						invoke  MapViewOfFile,eax,FILE_MAP_READ,0,0,0   ;view and store result
        						mov     pJumpDistanceMapping, eax        			; Open the damn file

						        invoke  CreateFileMapping, hIntLogFile, NULL, PAGE_READONLY \ ;create filemapping
					        	       ,0,0,NULL
							mov     hIntLogMapping, eax      
        						invoke  MapViewOfFile,eax,FILE_MAP_READ,0,0,0   ;view and store result
        						mov     pIntLogMapping, eax        			; Open the damn file

							invoke GetDlgItemText, hWnd, DumpFileEdit, addr pNameBuffer, 0512h
							invoke lstrcpy, addr Buffer, addr pNameBuffer
							lea edi, Buffer
							xor eax, eax
							repne scasb
							mov dword ptr [edi-1], "kab."
							mov byte ptr [edi+3], 0
							invoke CopyFile, addr pNameBuffer, addr Buffer, TRUE
							
							invoke CreateFile, addr pNameBuffer, GENERIC_READ + GENERIC_WRITE ,NULL, \ ;open file name
	        	       					NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL
							mov hDumpFile, eax

						        invoke  CreateFileMapping, hDumpFile, NULL, PAGE_READWRITE \ ;create filemapping
					        	       ,0,0,NULL
							mov     hDumpMapping, eax      
        						invoke  MapViewOfFile,eax,FILE_MAP_ALL_ACCESS,0,0,0   ;view and store result
        						mov     pDumpMapping, eax        			; Open the damn file
							

							mov ecx, 0
                                          
							mov counter, 0					; ECX ACTS AS A COUNTER
							.While ecx < TableSize
								mov esi, pAddressMapping
								lea esi, [esi+ecx*4]
								mov ebx, dword ptr [esi]

                                                xor edx, edx
                                                mov edi, pIntLogMapping
                                                .While edx< IntLogSize
                                                    .if dword ptr [edx+edi]==ebx
                                                        jmp Found
                                                    .else 
                                                        add edx, 04h
                                                    .endif
                                                .endw        
                                      Found:    .if edx<IntLogSize
      								sub ebx, 0400001h
      								add ebx, pDumpMapping
                                                         
      								.if byte ptr [ebx] == 0CCh
      
      									mov edi, pJumpDistanceMapping
      									lea edi, [edi+ecx*4]									
      									mov esi, pJumpTypeMapping
      									add esi, ecx
      									xor edx, edx
      									mov dl, byte ptr [esi]
      									cmp edx, 011h
      									ja @end_loop
      									mov esi, pInstructionLengthMapping
      									add esi, ecx
      									xor eax, eax
      									mov al, [esi]
      									dec eax
      									je ShortJump
      									dec eax
      									dec eax
      									dec eax
      									jne NearJump
      									
      									mov al, byte ptr JumpOpcodes[edx*4+1]
      									test al, al
      									je @end_loop
      									mov byte ptr [ebx], al
      									mov eax, dword ptr [edi]
      									sub eax, 4
      									mov dword ptr [ebx+1], eax
      									jmp Done
      								
      								ShortJump:
      									mov al, byte ptr JumpOpcodes[edx*4]
      									test al, al
      									je @end_loop
      									mov byte ptr [ebx], al
      									xor eax, eax
      									mov al, byte ptr [edi]
      									dec eax
      									mov byte ptr [ebx+1], al
      									jmp Done
      							
      								NearJump:
      									mov ax, word ptr JumpOpcodes[edx*4+2]
      									test al, al
      									je @end_loop
      									mov word ptr [ebx], ax
      									mov eax, dword ptr [edi]
      									sub eax, 5
      									mov dword ptr [ebx+2], eax
      								
      								Done: 
      									inc counter
      
      								.endif
								.endif
								inc ecx
							.endw
						@end_loop:
                                           mov ecx, TableSize	
							.if ecx < TableSize
								Invoke MessageBox, NULL, addr ErrPatch, addr Error, MB_OK 
							.else
								mov eax, counter
								lea edi, pNameBuffer
								call IToDecf
								inc edi
								inc edi
								mov byte ptr [edi+8], 0
								mov al, 030h
								repe scasb
								lea edx, [edi-1]
								lea esi, PatchOK
								invoke lstrlen, addr PatchOK
								lea edi, Buffer
								mov ecx, eax
								rep movsb
								.if byte ptr [edx] == 0
									mov al, 030h
									stosb
								.else
									mov esi, edx
									invoke lstrlen, esi
									mov ecx, eax
									rep movsb
								.endif
								lea esi, ByteMsg
								invoke lstrlen, esi
								mov ecx, eax
								rep movsb
								mov byte ptr [edi], 0
								Invoke MessageBox, NULL, addr Buffer, addr Error, MB_OK 
							.endif
							
					       		invoke UnmapViewOfFile, pDumpMapping
							invoke CloseHandle, hDumpMapping
							invoke CloseHandle, hDumpFile

					       		invoke UnmapViewOfFile, pJumpDistanceMapping
							invoke CloseHandle, hJumpDistanceMapping
							invoke CloseHandle, hJumpDistanceFile


					       		invoke UnmapViewOfFile, pInstructionLengthMapping
							invoke CloseHandle, hInstructionLengthMapping
							invoke CloseHandle, hInstructionLengthFile


					       		invoke UnmapViewOfFile, pJumpTypeMapping
							invoke CloseHandle, hJumpTypeMapping
							invoke CloseHandle, hJumpTypeFile


					       		invoke UnmapViewOfFile, pAddressMapping
							invoke CloseHandle, hAddressMapping
							invoke CloseHandle, hAddressFile

						.else
							Invoke MessageBox, NULL, addr WrongSize, addr Error, MB_OK 
						.endif	
						
						.else
							Invoke MessageBox, NULL, addr WrongSize, addr Error, MB_OK 
						.endif
						
						.else
							Invoke MessageBox, NULL, addr WrongSize, addr Error, MB_OK 
						.endif	
						
						pop ecx
						pop ebx
								
				.Elseif ax==IDC_EXIT
					invoke SendMessage,hWnd,WM_COMMAND,IDC_EXIT,0
				.endif	
							
			.Endif
  		.endif
  	.Else	
	        mov eax, FALSE
        	ret
	.EndIf
   
        mov eax, TRUE
        ret
DlgProcForm endp

;  #########################################################################
;ULTRA FAST DWORD TO DECIMAL CONVERTER.I have removed some instruction because I need only 8 char.
IToDecf:
	push	0			; get some additional space on stack (we need 10 bytes min.)
	push	0			; '' upper dword is 0
	push	eax			; '' lower dword -> 32 bit integer to convert
	fild	qword ptr[esp]		; load 64 bit integer value on TOS
	fbstp	real10 ptr[esp] 	; store value as 80bit BCD value
	pop	ecx
	pop	eax
	mov	edx, ecx
;	mov	ebx, eax
	shr	ecx, 4
;	shr	eax, 4
;	and	ebx, 0000000Fh		; mask nibbles
	and	edx, 0F0F0F0Fh		; ...
	and	ecx, 0F0F0F0Fh
	add	edx, 30303030h		; build ascii numbers
	add	ecx, 30303030h		; ...
;	add	eax, 30h
;	add	ebx, 30h
	;mov	[edi], al		; store in buffer 
	;mov	[edi+1], bl		; ...
	mov	[edi+8], cl
	mov	[edi+9], dl
	mov	[edi+6], ch
	mov	[edi+7], dh
	bswap	ecx			; endian conversion
	bswap	edx			; dto.
	mov	[edi+2], cl
	mov	[edi+3], dl
	mov	[edi+4], ch
	mov	[edi+5], dh
	pop	eax		      ; correct stack
	RET


end start
