- -----=========----- -------==========================================---------- ---------=== _masta_的 Win95 汇编教程 Part 1 ===---------- -------==========================================----------
--==INTR0==--
-[Hi!]-
Part 0 发表以后我收到了一些人的邮件,他们告诉我说让汇编这门古老的艺术在 Windows 下复兴是一个好主意。我们都知道 DOS 快玩完了(这并不是每个人都高兴的), 但是不幸的是我们无法改变现状。
--==我们需要什么?==--
1. 文本编辑器 2. TASM 5.0, 带着完整的库文件, 等等。 3. Win32 API 参考手册 (Win32.HLP)
假设你已经具有汇编的基础知识了,尽管本篇的好些东东还是很容易掌握的。
--==本篇的目的==--
我们要做一个通用的补丁(generic patcher),也叫做"Search-and-Destroy-Patcher"。 由于很多人可能不知道这个"通用的补丁"是干什么用的,我就简单的解释一下。 它是一个软件的补丁,但并不是针对软件的某一个版本设计的,如果各版的软件都 差不多的话,它在以后的版本中也能用。它是通过搜索特定的字节序列来完成的,而不是 修改指定的地方,这样做可能更通用一些。 由于编程者(在各个版本的软件中)很可能不改变程序保护的方法,尽管代码的偏移 已经不一样了,可是字节序列还是一样的。这是一个小技巧 =).
--==LET'S GO!==--
OK, 首先考虑一下我们的主程序的结构,然后再考虑该用哪些函数, 然后再写出程序。
1. 介绍 - 小介绍, 用一个 MessageBox 显示 2. 打开文件 - 得到文件的句柄(Handle) 如果文件不存在 -> MessageBox 3. 得到文件大小 4. 分配内存 - 分配和文件大小相同的内存空间 出错了 -> MessageBox 5. 读文件 - 把整个文件读入内存 6. 搜索字节序列 - 寻找特定的字节序列. 如果出错 -> MessageBox 7. 设置文件指针 到要修改的地方 8. 写文件 - Patch of the file. Success -> MessageBox with new bytes 9. 关闭文件 - 成功! 释放内存空间 退出
--==API 函数==--
- 通过对话框显示所有的信息, i.e. 'MessageBoxA'.
- 用 'CreateFileA'函数打开文件,虽然它比 'OpenFile'复杂那么一点点,但是 使用更灵活那么一点点
- 用 'CloseHandle' 关闭文件
- 通过 'GetFileSize'得到文件的大小
- 用 'GlobalAlloc' 分配内存; 用 'GlobalFree' 释放
- 用 'ReadFile' 读文件, 用 'WriteFile' 写文件
- 可以用 'SetFilePointer' 来设置文件指针
- 用 'ExitProcess' 退出程序
--==搜索字节==--
这是我们的程序的心脏,先在目标文件中找到特定的字节序列,然后修改它。 OK,先把文件(分配的内存)的大小放入 ECX, 在后面的 REPNZ 命令中要用到它。 在把字节序列的第一个字节放入 AL, 将ESI设置为原始字节序列的偏移。 'REPNZ SCASB'命令会将 AL 和 EDI 所指向内存的值相比较(EDI 会自动加1)。 'REPNZ' 命令会重复执行 'SCASB'直到 ECX=0 或者 AL=[EDI]( ZF=1 ). 如果比较的值相等了,就在ECX中放入要PATCH 的长度;因为'SCASB'会多 向前计算一个字节,所以再将 EDI 减1。 再执行 'REPZ CMPSB'重复执行'COMSB'(比较 [ESI]和[EDI]中的值),直到 ECX=0 或者所比较的两个不同才结束。
--==补丁==--
下面就是'补丁'程序了。 偏移的计算: 将 ECX 加 1,然后再从文件大小中减去这个数。
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (文件大小) - (到文件尾的长度) = 偏移 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
将这个值放进 STACK 里,调用 'SetFilePointer'设置文件指针。 然后将 实际写入的个数的buffer(译者注: 函数执行完毕后将实际 写入的字节数返回于此),要写入的长度,新的字节序列和文件HANDLE 入栈, 调用'WriteFile'。
--==THE SOURCE==--
可能有点复杂, 但是我想应该很好理解 =) ...
;-------------------------------===START===--------------------------- ; set a couple of options for the assembler .386P Locals jumps
.Model Flat ,StdCall mb_ok equ 0 hWnd equ 0 FILE_ATTRIBUTE_NORMAL equ 080h OPEN_EXISTING equ 3 GENERIC_READ equ 80000000h GENERIC_WRITE equ 40000000h
; --==声明所用到的API==--
extrn ExitProcess : PROC ;结束程序 extrn MessageBoxA : PROC ;显示消息框 extrn CreateFileA : PROC ; " ... 打开文件 extrn ReadFile : PROC ;读文件 extrn WriteFile : PROC ;写文件 extrn CloseHandle : PROC ;关闭文件 extrn GetFileSize : PROC ;得到文件大小 extrn GlobalAlloc : PROC ;分配内存 extrn GlobalFree : PROC ;释放内存 extrn SetFilePointer : PROC ;设置文件指针
; --==数据存放区==--
.Data
caption db "_masta_'s essay on Win32-ASM-Coding, part 1",0 ;标题, 0 作为结束符
text db "Hi, nice to CU again",13,10 db "This tut will describe you how to make",13,10 db "Win32-ASM Search and Destroy patchers",0 ;introtext, 0-terminated
err_cap db "ERROR",0 ;caption for errormessage
openerr db "Error on opening File",0 ;errortext opening file memerr db "Error on allocating memory",0 ;errortext alloc. memory byterr db "File is here, but i can't find the original bytes",0 ;error while bytesearch
readycap db "Ready",0 ;caption for 'done'
readytxt db "Ok, file is patched",0 ;text for 'done'
file db "make.old",0 ;what file we want to patch? org_val db "Xmas'97" ;original values new_val db "_masta_" ;new values len equ $-new_val ;how many values (length) ;org_val and new_val must be equal
fhandle dd ? ;variable for the filehandle fsize dd ? ;variable for the filesize memptr dd ? ;pointer to allocated memory bread dd ? ;number of read bytes bwrite dd ? ;number of written bytes
;--==and here we start with our code==--
.Code Main: push mb_ok ;PUSH value for 'uType' push offset caption ;PUSH pointer to caption push offset text ;PUSH pointer to Text push hWnd ;PUSH Masterhandle call MessageBoxA ;CALL MessageBoxA
push 0 ;for Win95 always 0 push FILE_ATTRIBUTE_NORMAL ;standard Fileattributes push OPEN_EXISTING ;open existing file push 0 ;no Security-attributes push 0 ;disable Share-Mode push GENERIC_READ + GENERIC_WRITE ;read- and writeaccess push offset file ;offset of the filename Call CreateFileA ;open file mov fhandle,eax ;save filehandle cmp eax,0FFFFFFFFh ;if eax=FFFFFFFF then error jnz file_is_here
push mb_ok push offset err_cap push offset openerr push hWnd call MessageBoxA ; showerrormessage jmp end_ ; jump to end
file_is_here: ;file is there, so go on
push 0 ;can be 0, if the filesize is less then 4,3 GB :) push fhandle ;PUSH filehandle Call GetFileSize ;get the filesize mov fsize,eax ;save the filesize
push fsize ;PUSH filesize=size of the buffer push 0 ;0=GMEM_FIXED -> fixed memory-area Call GlobalAlloc ;allocate as much as memory as filesize mov memptr,eax ;save pointer to memory-area
cmp eax,0 ;if eax=0, then there were errors jnz mem_ok
push mb_ok push offset err_cap push offset memerr push hWnd call MessageBoxA jmp end_kill_handle ;end program, close file b4
mem_ok: ;memory is allocated -> next step
push 0 ;set to 0 in most cases push offset bread ;pointer to number of read bytes push fsize ;read how many bytes?, fsize=whole file push memptr ;save where? ->allocated memory push fhandle ;filehandle Call ReadFile ;read file!
read_ok:
mov edi,memptr ;set EDI to memory-area mov ecx,fsize ;set ECX (for repnz) to filesize mov esi,offset org_val ;set ESI to the string to find mov al, byte ptr [esi] ;load AL with the first byte
loop_: repnz scasb ;repeat until ECX=0 or AL equals ;the value of the byte [EDI], EDI is ;incremented by 1 every run cmp ecx,0 ;If ECX=0, nothing is found jz not_found
here_is_something: ;found matching byte
push ecx ;save register push edi push esi dec edi ;EDI-1, cos REPNZ SCASB is one step too far mov ecx,len ;ECX=length of the patch repz cmpsb ;repeat until the values in the memory of ;[EDI] and [ESI] are different, ;or ecx=0 cmp ecx,0 ;If ecx=0, then the org_val is in memory jz patch ;->jump to patcher
not_that: ;it is not yet here
pop esi ;POP ESI pop edi pop ecx jmp loop_ ;search next byte
patch: ;start of the patcher pop esi ;POP registers pop edi pop ecx dec edi ;EDI-1 inc ecx ;ECX+1 mov eax,fsize sub eax,ecx ;compute Offset push 0 ;offset from the beginning of the file push 0 ;is 0, if file < 4,3GB push eax ;offset push fhandle ;filehandle call SetFilePointer ;set FilePointer
push 0 ;normally 0 push offset bwrite ;how many bytes where written? push len ;length of the bytes to write push offset new_val ;offset to new values push fhandle ;filehandle Call WriteFile ;write block to file
push mb_ok push offset readycap push offset readytxt push hwnd call MessageBoxA ;OK, patch is done!
jmp end_kill_all ;END! Cleanup!
not_found:
push mb_ok push offset err_cap push offset byterr push hWnd call MessageBoxA ;the bytes where not in the file
end_kill_all:
push memptr ;pointer to Memoryarea call GlobalFree ;enable (free) memory
end_kill_handle:
push fhandle ;PUSH filehandle call CloseHandle ;CloseHandle
end_:
CALL ExitProcess ;Quit program End Main ;end of code, JUMP-spot (main)
;-----------------------==END OF SOURCE==----------------------------
;-------------------------------START---------------------------make.bat
tasm32 /mx /m3 /z /q tut tlink32 -x /Tpe /aa /c tut,tut,, import32.lib
;--------------------------------END----------------------------make.bat
--==A LITTLE NOTICE==--
Until now I didn't see a reason to use include-files And well, the INC-files coming with TASM are not very complete, BUT if there is anybody out there possessing complete *.incs then don't hesitate to send'em to me!
--==END==--
OK, 我想这次总算做了一点有用的东西,而不像上一次仅仅是一个 MessageBox , 这次的产品是一个 Cracker 常用的工具。(译者注:Cracker 可不是常说的网络黑客啊, Cracker的爱好是破解软件的保护,给软件做补丁,称他们为解密高手更为合适。) 当然,这段代码你可以自由使用,也许你还能够做一些优化,尤其在 "搜索"那一部分(很蘑菇),但是出于学习的目的,我想这样写还是不错的。
一个小挑战:
--> You could search for the first 4 bytes from the start
OK, I hope that my mailbox ([email protected]) will explode soon (CRITICS ARE WELCOME) and I will see ya all next time ... =) By the way I am trying to establish an IRC-channel about these facts ...
--> #win32asm
I just hope there are enough people interested in this stuff and also in giving there knowledge to others.
--==WISE WORDS==-- -------====================-- --====================-------- ------======everybody was a lamer, before they become ELITE======------- -------====================-- --====================-------- -----==========----- -

|