iceplus
May 19th, 2004, 20:40
00000:
00001: ;================================================================================
00002: ; SiceLdrBreak Patch V0.1beta
00003: ;
00004: ;================================================================================
00005:
00006: format PE GUI 4.0
00007: entry start
00008:
00009: include '%fasminc%\win32a.inc'
00010: include 'pe.inc'
00011: ;================================================================================
00012: ; DATA
00013: ;================================================================================
00014: section '.data' data readable writeable
00015: hIcon dd 0
00016: hInstance dd 0
00017: flags dd ?
00018: pNewFileName rb 40h
00019:
00020: ofn OPENFILENAME
00021:
00022: hFile dd ?
00023: FileSize dd ?
00024: hMapFile dd ?
00025: pMemory dd ?
00026: pFileFilter db "WDM Files",0,"*.sys",0,0
00027:
00028: pFileName rb 40h
00029:
00030: message db 'Patch error !', 0
00031: message2 db 'Good! Patch succeed !',0dh,0ah,0dh, 0ah,\
00032: 'Please restart your computer!',0
00033: caption db 'SiceLdrBreak Patch', 0
00034:
00035: codebin db 66h,81h,0E1h,00h,18h,66h,81h,0F9h,0,18h
00036: codesize dd $-codebin
00037:
00038: nop1 db 75h,5bh
00039: size_nop1 dd $-nop1
00040: nop2 db 75h, 4fh
00041: size_nop2 dd $-nop2
00042: nop3 db 72h,3bh
00043: size_nop3 dd $-nop3
00044:
00045: ;================================================================================
00046: ; CODE
00047: ;================================================================================
00048: section '.code' code readable executable
00049:
00050: start:
00051:
00052: invoke GetModuleHandle,0
00053: mov dword [hInstance], eax
00054:
00055: invoke DialogBoxParam,eax,37,HWND_DESKTOP,DialogProc,0
00056: or eax,eax
00057: jz exit
00058: exit:
00059: invoke ExitProcess,0
00060:
00061: proc DialogProc,hWnd,msg,wparam,lparam
00062: push ebx esi edi
00063: cmp [msg],WM_INITDIALOG
00064: je WmInitdialog
00065: cmp [msg],WM_COMMAND
00066: je WmCommand
00067: cmp [msg],WM_CLOSE
00068: je WmClose
00069: xor eax,eax
00070: jmp finish
00071: WmInitdialog:
00072: ; set icon
00073: invoke LoadIcon,[hInstance],IDI_FIRST
00074: mov [hIcon],eax
00075: invoke SendMessage,[hWnd],WM_SETICON,1,eax
00076:
00077: mov [ofn.lStructSize],sizeof.OPENFILENAME
00078: mov eax,[hWnd]
00079: mov [ofn.hwndOwner],eax
00080: mov eax,[hInstance]
00081: mov [ofn.hInstance],eax
00082: mov [ofn.lpstrFilter], pFileFilter
00083: mov [ofn.lpstrTitle], caption
00084:
00085: mov [ofn.lpstrFile], pFileName
00086: mov [ofn.nMaxFile],1000h
00087:
00088: jmp processed
00089:
00090: WmCommand:
00091: cmp [wparam],BN_CLICKED shl 16 + ID_File
00092: je getfilename
00093: cmp [wparam],BN_CLICKED shl 16 + ID_Patch
00094: jne processed
00095: ;---------------------------------------------------------------------------------------------
00096: invoke GetDlgItemText,[hWnd],ID_FileName,pFileName,40h
00097: mov [flags],MB_OK
00098:
00099: invoke IsDlgButtonChecked,[hWnd],ID_BackUp
00100: cmp eax,BST_CHECKED
00101: jne not_backup
00102: invoke CopyFile,pFileName,pNewFileName,FALSE
00103:
00104: not_backup:
00105:
00106: invoke CreateFile, pFileName,GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ or FILE_SHARE_WRITE,\
00107: 0,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,0
00108: cmp eax,-1
00109: je failed
00110: mov [hFile], eax
00111: invoke GetFileSize, eax, NULL
00112: mov [FileSize], eax
00113: invoke CreateFileMapping, [hFile],NULL,PAGE_READWRITE,0,0,NULL
00114: mov [hMapFile], eax
00115: invoke MapViewOfFile, [hMapFile],FILE_MAP_ALL_ACCESS,0,0,0
00116: mov [pMemory], eax
00117: ;;;;;;;;;;;;;;;;;;;;Repair NTice;;;;;;;;;;;;;;;;;;;;;;;;;;
00118:
00119: stdcall GetNticeCodeSize, eax
00120: stdcall BinSearch, esi, ecx, codebin, dword [codesize]
00121: cmp eax, -1
00122: jz failed
00123: add esi, eax
00124: sub esi, 8h
00125: stdcall BinSearch, esi, 30h, nop1, dword [size_nop1]
00126: cmp eax, -1
00127: jz failed
00128: add esi, eax
00129: mov word [esi], 9090h
00130: stdcall BinSearch, esi, 30h, nop2, dword [size_nop2]
00131: cmp eax, -1
00132: jz failed
00133: add esi, eax
00134: mov word [esi], 9090h
00135: stdcall BinSearch, esi, 30h, nop3, dword [size_nop3]
00136: add esi, eax
00137: mov word [esi], 9090h
00138: stdcall RepairCheckNum, [pMemory]
00139: invoke MessageBox, [hWnd],message2,caption,MB_OK
00140:
00141: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
00142: invoke UnmapViewOfFile,[pMemory]
00143: invoke CloseHandle,[hMapFile]
00144: invoke CloseHandle,[hFile]
00145: jmp processed
00146: ;---------------------------------------------------------------------------------------------
00147: getfilename:
00148: mov [ofn.Flags],OFN_EXPLORER+OFN_FILEMUSTEXIST+OFN_HIDEREADONLY
00149: mov [ofn.lpstrTitle],NULL
00150: invoke GetOpenFileName, ofn
00151: or eax,eax
00152: jz failed
00153: invoke SetDlgItemText,[hWnd],ID_FileName,pFileName
00154: invoke lstrcpy,pNewFileName,pFileName
00155: invoke lstrlen,pNewFileName
00156: mov esi, pNewFileName
00157: add esi, eax
00158: mov dword [esi],'.bak'
00159: jmp processed
00160: ;----------------------------------------------------------------------------------------------
00161: load_out_of_memory:
00162: invoke CloseHandle,ebx
00163: failed:
00164: invoke MessageBox,[hWnd],message,caption,MB_OK
00165: jmp processed
00166: ;----------------------------------------------------------------------------------------------
00167: WmClose:
00168: invoke EndDialog,[hWnd],0
00169:
00170:
00171: processed:
00172: mov eax,1
00173: finish:
00174: pop edi esi ebx
00175: return
00176: endp
00177:
00178: ;--------------------------------------------------------------------------------
00179: proc GetNticeCodeSize, pMem
00180:
00181: mov esi, [pMem]
00182: mov eax, dword [esi+IMAGE_DOS_HEADER.e_lfanew]
00183: add eax, esi
00184:
00185: mov esi, dword [eax+IMAGE_NT_HEADERS.OptionalHeader+IMAGE_OPTIONAL_HEADER32.BaseOfCode]
00186: mov ecx, dword [eax+IMAGE_NT_HEADERS.OptionalHeader+IMAGE_OPTIONAL_HEADER32.BaseOfData]
00187: sub ecx, esi
00188: add esi, [pMem]
00189: return
00190: endp
00191:
00192: proc RepairCheckNum, pMem
00193:
00194: mov esi, [pMem]
00195: mov edi,[esi+IMAGE_DOS_HEADER.e_lfanew]
00196: add edi,esi
00197: mov dword [edi+IMAGE_NT_HEADERS.OptionalHeader+IMAGE_OPTIONAL_HEADER32.CheckSum],0
00198:
00199: mov ecx,[FileSize]
00200: inc ecx
00201: shr ecx,1
00202: xor eax,eax
00203: clc
00204:
00205: cal_checksum:
00206: adc ax,word [esi]
00207: inc esi
00208: inc esi
00209: loop cal_checksum
00210: mov ebx,[FileSize]
00211: add eax,ebx
00212: mov dword [edi+IMAGE_NT_HEADERS.OptionalHeader+IMAGE_OPTIONAL_HEADER32.CheckSum], eax
00213:
00214: return
00215: endp
00216:
00217: ;===========================================================
00218: proc BinSearch,lpSource,sLen,lpPattern,pLen
00219:
00220: push ebx
00221: push esi
00222: push edi
00223: ; ----------------
00224: ; setup loop code
00225: ; ----------------
00226:
00227: mov esi, [lpSource]
00228: mov edi, [lpPattern]
00229: mov al, [edi] ; get 1st char in pattern
00230:
00231: mov ecx, [sLen]
00232: add esi, ecx ; add source length
00233: neg ecx ; invert sign
00234: dec [pLen]
00235:
00236: jmp Scan_Loop
00237:
00238: ; ---------------------------
00239:
00240: Pre_Match:
00241: lea ebx, [esi+ecx] ; put current scan address in EBX
00242: mov edx, [pLen] ; put pattern length into EDX
00243:
00244: Test_Match:
00245: mov ah, [ebx+edx-1] ; load last byte of pattern length in main string
00246: cmp ah, [edi+edx-1] ; compare it with last byte in pattern
00247: jne Pre_Scan ; exit loop on mismatch
00248:
00249: sub edx, 1
00250: jnz Test_Match
00251: jmp Match
00252:
00253: Pre_Scan:
00254: add ecx, 1 ; start on next byte
00255:
00256: Scan_Loop:
00257: cmp al, [esi+ecx] ; scan for 1st byte of pattern
00258: je Pre_Match ; test if it matches
00259: add ecx, 1
00260: js Scan_Loop ; exit on sign inversion
00261:
00262: ;-----------------------------
00263:
00264: No_Match: ; fall through here on no match
00265: mov eax, -1
00266: jmp isOut
00267:
00268: Match:
00269: add ecx, [sLen]
00270: mov eax, ecx
00271:
00272: isOut:
00273: pop edi
00274: pop esi
00275: pop ebx
00276:
00277: return
00278: endp
00279:
00280: ;================================================================================
00281: ; IAT
00282: ;================================================================================
00283: section '.idata' import data readable writeable
00284:
00285: library kernel,'KERNEL32.DLL',\
00286: user,'USER32.DLL',\
00287: comdlg32,'comdlg32.dll'
00288:
00289: import kernel,\
00290: GetModuleHandle,'GetModuleHandleA',\
00291: lstrlen,'lstrlenA',\
00292: lstrcpy,'lstrcpyA',\
00293: CopyFile,'CopyFileA',\
00294: CreateFile,'CreateFileA',\
00295: GetFileSize,'GetFileSize',\
00296: CreateFileMapping,'CreateFileMappingA',\
00297: MapViewOfFile,'MapViewOfFile',\
00298: UnmapViewOfFile,'UnmapViewOfFile',\
00299: CloseHandle,'CloseHandle',\
00300: ExitProcess,'ExitProcess'
00301:
00302: import user,\
00303: DialogBoxParam,'DialogBoxParamA',\
00304: LoadIcon,'LoadIconA',\
00305: SendMessage,'SendMessageA',\
00306: CheckRadioButton,'CheckRadioButton',\
00307: GetDlgItemText,'GetDlgItemTextA',\
00308: SetDlgItemText,'SetDlgItemTextA',\
00309: IsDlgButtonChecked,'IsDlgButtonChecked',\
00310: MessageBox,'MessageBoxA',\
00311: EndDialog,'EndDialog'
00312:
00313: import comdlg32,\
00314: GetOpenFileName,'GetOpenFileNameA'
00315:
00316: ;================================================================================
00317: ; RSRC
00318: ;================================================================================
00319: section '.rsrc' resource data readable
00320:
00321: ID_Patch = 101
00322: ID_File = 102
00323: ID_FileName = 201
00324: ID_BackUp = 401
00325: IDI_MAIN = 501
00326: IDI_FIRST = 502
00327: IDB_LOGO = 601
00328:
00329: directory RT_DIALOG,dialogs,\
00330: RT_ICON,icons,\
00331: RT_GROUP_ICON,grpicons,\
00332: RT_BITMAP,image
00333:
00334: resource icons,\
00335: IDI_MAIN,LANG_NEUTRAL,icon_data
00336: resource grpicons,\
00337: IDI_FIRST,LANG_NEUTRAL,main_icon
00338: resource dialogs,\
00339: 37,LANG_ENGLISH+SUBLANG_DEFAULT,demonstration
00340: resource image,\
00341: IDB_LOGO,LANG_ENGLISH,LogoBMP
00342: icon main_icon,icon_data,'main.ico'
00343:
00344: bitmap LogoBMP,'logo.bmp'
00345:
00346: dialog demonstration,'NTiceLdrPatch for DS v3.x',200,200,168,80,WS_CAPTION+WS_POPUP+WS_SYSMENU+DS_MODALFRAME
00347: dialogitem 'STATIC','NTice:',-1,5,40,70,8,WS_VISIBLE
00348: dialogitem 'EDIT','',ID_FileName,30,40,120,10,WS_VISIBLE+BS_FLAT+WS_TABSTOP,WS_EX_STATICEDGE
00349: dialogitem 'STATIC','IcePlus[2004]',-1,5,60,70,8,WS_VISIBLE+WS_DISABLED
00350: dialogitem 'BUTTON','&BackUp',ID_BackUp,75,60,40,10,WS_VISIBLE+WS_TABSTOP+BS_AUTOCHECKBOX+BS_FLAT
00351: dialogitem 'BUTTON','&Patch',ID_Patch,125,59,40,12,WS_VISIBLE+WS_TABSTOP,WS_EX_STATICEDGE
00352: dialogitem 'BUTTON','...',ID_File,153,40,11,11,WS_VISIBLE+WS_TABSTOP+BS_PUSHBUTTON,WS_EX_STATICEDGE
00353: dialogitem 'STATIC',601,-1,0,0,250,50,WS_VISIBLE+SS_BITMAP,WS_EX_STATICEDGE
00354: enddialog
00355: section '.reloc' fixups data readable discardable
00356:
00357:
Kayaker
May 20th, 2004, 00:30
Thank you Iceplus

I feel better running patch code if its purpose isn't hidden, plus it makes the post more interesting
The key seems to be the LAR (Load Access Rights) instruction, if it fails so does the rest, hence the extra jump patches.
Description:
----------------------------------
Loads the access rights from the segment descriptor specified by the second operand (source operand) into the first operand (destination operand) and sets the ZF flag in the EFLAGS register. The source operand (which can be a register or a memory location) contains the segment selector for the segment descriptor being accessed. The destination operand is a general-purpose register.
The processor performs access checks as part of the loading process. Once loaded in the destination register, software can perform additional checks on the access rights information.
When the operand size is 32 bits, the access rights for a segment descriptor include the type and DPL fields and the S, P, AVL, D/B, and G flags, all of which are located in the second doubleword (bytes 4 through 7) of the segment descriptor. The doubleword is masked by 00FXFF00H before it is loaded into the destination operand. When the operand size is 16 bits, the access rights include the type and DPL fields. Here, the two lower-order bytes of the doubleword are masked by FF00H before being loaded into the destination operand.
This instruction performs the following checks before it loads the access rights in the destination register:
Checks that the segment selector is not null. Checks that the segment selector points to a descriptor that is within the limits of the GDT or LDT being accessed Checks that the descriptor type is valid for this instruction. All code and data segment descriptors are valid for (can be accessed with) the LAR instruction. The valid system segment and gate descriptor types are given in the following table. If the segment is not a conforming code segment, it checks that the specified segment descriptor is visible at the CPL (that is, if the CPL and the RPL of the segment selector are less than or equal to the DPL of the segment selector).
If the segment descriptor cannot be accessed or is an invalid type for the instruction, the ZF flag is cleared and no access rights are loaded in the destination operand.
----------------------------------
In this case, the Softice code can be traced live and you can follow what's happening. There *are* certain areas of ntice code where a standard BPX will break, and this appears to be one of them. The code can be found with a standard byte search and you can set a BP on the proc start, for example:
:driver ntice
Start - BEF25000 Size - 175880
:s BEF25000 L 175880
8D 7B 0E FC E8
will find the code
lea edi, [ebx+0Eh] ; pointer to "NOTEPAD"
cld
Start Loader32.exe with notepad and it should break.
What I found is that the single argument (arg_0) of the function is a pointer to this data:
Code:
01 00 1B 00 00 10 00 01 CA 65 00 00 00 00 4E 4F .........e....NO
54 45 50 41 44 TEPAD
With the valid segment selector 001B (user mode programs in Win2K), this function *should* succeed and loader32 will break at Winmain.
mov ebx, [ebp+arg_0]
...
movzx eax, word ptr [ebx+2] ; 001B
lar ecx, eax ; ecx was 0, now 00CFFB00
jnz exit
and cx, 1800h ; ecx = 00CF1800
cmp cx, 1800h
...
Why this might be failing for Iceplus in XP I couldn't say, if the segment selector is passed correctly then there may be some other issue with access rights going on.
Btw, the call
.text:000310BD call sub_30431
is a call to the function pMapSelectorToLinAddr (The Owl's definition)
Regards,
Kayaker
Powered by vBulletin® Version 4.2.2 Copyright © 2018 vBulletin Solutions, Inc. All rights reserved.