Ch 01 ²Ä¤@­Ó Win32 ²Õ¦X»y¨¥µ{¦¡

¦b³o¤@³¹ùرN¦V¦U¦ì¤¶²Ð ( µù¤@ ) ¦b Win32 §@·~¨t²Î¤¤¡A³Ì²³æªº²Õ¦X»y¨¥µ{¦¡¡A¦P®É½m²ß¦p¦ó¥H MASM32 v7.0 ²ÕĶ³sµ²¡A¥H¤Î§Q¥Î Soft-ICE ¸ü¤Jµ{¦¡°£¿ù ( ©ÎªÌ¥Î OllyDebug °£¿ù ) ³o¤T¶µ¤u§@¡C


Win32 §@·~¨t²Î¤¤³Ì²³æªºµ{¦¡

¨ÌºD¨Ò¡Aµ{¦¡±Ð¾Çªº²Ä¤@­Óµ{¦¡¡A¶È¦b¿Ã¹õ¤WÅã¥Ü¤@­Ó¦r¦ê¡A¤p¤ì°¸¤]¤£·Q¨Ò¥~¡A©Ò¥H²Ä¤@­Ó¥Î²Õ¦X»y¨¥©Ò¼¶¼gªº Win32 µ{¦¡¤]¬O¦p¦¹¡C¥¦¶È¶ÈÅã¥Ü¤@­Óµøµ¡¡A¼ÐÃD¼g¡y³Ì²³æªºµ{¦¡¡z¡Aµøµ¡¤º¤å¼g¡y³o¬O¦b Win32 §@·~¨t²Î¡A¥Î²Õ¦X»y¨¥¼gªºµ{¦¡¡C¡z¡A¨Ã¥B¦³¤@­Ó¡y½T©w¡z«ö¶s¡A·í¨Ï¥ÎªÌ«ö¤U³o­Ó«ö¶s¥i¥Hµ²§ôµ{¦¡¡C¥¦¨S¦³²o¯A¤Ó¦h Win32 ½ÆÂøªº¨t²Î¡A¬Æ¦Ü³s³Ì°ò¥»ªº°T®§¶Ç»¼¤]³£¨S¦³¡A³Ì¾A¦X§@¬° Win32 ²Õ¦X»y¨¥¤Jªùªºµ{¦¡¡C³o­Óµ{¦¡¡A¤p¤ì°¸¦W¤§¬° message.asm¡A¨ä°õ¦æµ²ªG¦p¤U¡G

©ó Win32 §@·~¨t²ÎùØ¡A³Ì²³æªºµ{¦¡

¤W­±¬O¦b Win 98 SE ªº°õ¦æµe­±¡A¦Ü©ó XP/Vista ¤]³£Ãþ¦ü¡AŪªÌ¥i¥H¸Õ¸Õ¡Cmessage.asm ªº­ì©l½X¦p¤U¡G

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
        OPTION  CASEMAP:NONE
        .386
        .MODEL  FLAT,STDCALL
INCLUDE         WINDOWS.INC
INCLUDE         KERNEL32.INC
INCLUDE         USER32.INC
INCLUDELIB      KERNEL32.LIB
INCLUDELIB      USER32.LIB
 
;***************************************************************************************************
.DATA
szTitle         DB      "³Ì²³æªºµ{¦¡",0
szMessage       DB      "³o¬O¦b Win32 §@·~¨t²Î¡A¥Î²Õ¦X»y¨¥¼gªºµ{¦¡¡C",0
;***************************************************************************************************
.CODE
start:          INVOKE  MessageBox,NULL,ADDR szMessage,ADDR szTitle,MB_OK
                INVOKE  ExitProcess,NULL
;***************************************************************************************************
END             start

¬Ý°_¨Ó¥u¦³ 19 ¦æ¡A¦ü¥G«Ü²³æ¡A¨º¬O¦]¬°¦³³\¦h©w¸q³£¤wÅn¬A¦b¥]§tÀÉ ( *.INC ÀÉ ) ¤¤¤F¡C©³¤U¤p¤ì°¸±N¤ÀªR³o¨Çµ{¦¡½X¡C

²Ä¤@¦æ¬O§i¶D²ÕĶ¾¹¡AML.EXE¡A¨Ï¥Î§Ú­Ì©Ò©w¸qªºÅܼƦWºÙ¡B¼Ð°O¦WºÙ¡BWin32 API ¦WºÙµ¥µ¥¬O°Ï¤À¤j¤p¼gªº¡A¤]´N¬O»¡ message »P MESSAGE ¥Nªí¨â­Ó¤£¦Pªº¦r¦ê¦W¡C°²¦p±z¤£¦b­ì©lµ{¦¡¤¤¼g³o¤@¦æ¡A¦b²ÕĶ®É¤U /Cp °Ñ¼Æ¤]¬O¤@¼Ëªº¡C¦]¬°©Ò¦³ªº Win32 µ{¦¡³£·|©I¥s Win32 API¡A¦Ó Win 32 API ¤j¤p¼g¤£¦Pªº¦r¥Àµø¬°¤£¦PªºÅܼơA©Ò¥H¥²¶·¦³³o¤@¦æ¡A³o¤@ÂI©M DOS ¤¤¼¶¼gµ{¦¡¤£¤À¤j¤p¼g¬O¤£¤@¼Ëªº¡Aªì¾ÇªÌ±o¤p¤ß¡C

²Ä¤G¦æ¬O«Å§i¥i¥H¨Ï¥Î 80386 «ü¥O¶°¡A¦]¬°§Ú­Ìªºµ{¦¡±N¦b Win32 ¨t²Î¤W°õ¦æ¡A¦]¬° Win32 ¨t²Î¥²¶·¦b 80386 ¥H¤Wªº CPU ¤~¯à°õ¦æ¡A¦Ó¥B­n¥Î 32 ¦ì¤¸ªº©w§}¤è¦¡ ( §Y flat mode )¡A©Ò¥H¡y.386¡z³o¤@¦æ¬O¥²­nªº¡C

²Ä¤T¦æ¬O«ü©w°O¾ÐÅé¼Ò¦¡¥H¤Î©I¥s¨ó©w ( calling convention )¡C¥ý»¡°O¾ÐÅé¼Ò¦¡¡C¦b DOS ®É¥N¡A°O¾ÐÅé¬O¥H 16 ¦ì¤¸©w§}¤è¦¡©w§}¡A¤À¬°°Ï¬q»P°¾²¾¦ì§}¡A¨C­Ó°Ï¬q¤£±o¶W¹L 64 KB¡A¦ý¦b Win32 §@·~¨t²ÎùØ¡A°O¾ÐÅé¬O¥H 32 ¦ì¤¸¤è¦¡©w§}¡A³Ì¦h¥i¹F 4GB¡A¦p¦¹¤jªº°O¾ÐÅ餣¦A»Ý­n¡A¤]¨S¦³¥²­n¤À¦¨°Ï¬q»P°¾²¾¦ì§}ªº©w§}¤è¦¡¡A±z¥i¥H§â°O¾ÐÅé¦ì§}¥þ³£¬Ý¦¨¬O¤@­Ó«Ü¤jªº¡y°Ï¬q¡z¡A¥Ñ 00 ¨ì 4GB ¦¨ª½½u±Æ¦C¡AºÙ¤§¬°¡yflat¡z¼Ò¦¡ ( ¥­©Z¼Ò¦¡ ) ( µù¤G )¡C¦b Win32 §@·~¨t²Î¤¤¡A±z¤]¥u¯à¥Î³oºØ¼Ò¦¡¡C¦b Win32 ¨t²Îªº FLAT ¼Ò¦¡¤§¤U¡A·íµ{¦¡³Q¸ü¤J°õ¦æ®É¡A§@·~¨t²Î·|¤À°t¤@­Ó 4GB ªº°O¾ÐÅéªÅ¶¡µ¹µ{¦¡¨Ï¥Î¡A§Ú­Ìªºµ{¦¡¥u¯à¦b³o 4GB ªºªÅ¶¡°õ¦æ¡A¤]¨S¦³¿ìªk¬Ý¨ì¨ä¥Lµ{¦¡ªº°O¾ÐÅ餺®e¡A°O¾ÐÅ骺¤À°t¡BºÞ²z¥þ³¡³£¥Ñ§@·~¨t²Î½Õ«×«ü´§¡A©Ò¥H·í¬Y­Óµ{¦¡·í±¼¡A¨Ã¤£¼vÅT¨ä¥Lµ{¦¡¡C§Ú­Ì¦A¤]¨S¦³¿ìªk¡A¤]¨S¦³¥²­n¥hÃö¤ß¨ä¥Lµ{¦¡ªº°O¾ÐÅ餺®e¡A¥u­n±Mª`©ó¦Û¤vµ{¦¡ªº 4GB ¤º§Y¥i¡C

©I¥s¨ó©w¬O«ü¥Dµ{¦¡©I¥s°Æµ{¦¡®É¡A­n¦p¦ó§â°Ñ¼Æ¶Çµ¹°Æµ{¦¡¡A¥Dµ{¦¡»P°Æµ{¦¡¥²¶·¤@­P¡A§_«h©Ò±oµ²ªG«K¨S¦³·N¸q¡C¦b DOS ùØ©I¥s¤¤Â_¬O¨Ï¥Î¼È¦s¾¹¶Ç»¼°Ñ¼Æ¨ÃµL©Ò¿×©I¥s¨ó©w¡A¦ý¬O¦b Win32 ùØ©I¥s Win32 API¡A«o¬O¨Ï¥Î°ïÅ|¶Ç»¼°Ñ¼Æ¡A¦Ó¦b¤¤¡B°ª¶¥»y¨¥ùØ©I¥s°Æµ{¦¡±`¥H¤@¦æ§¹¦¨¡A¨Ò¦p C/C¡Ï¡Ï ¤¤©I¥s MessageBox »yªk¦p¤U¡G

MessageBox ( NULL,szMessage,szTitle,MB_OK )

©I¥s¨ó©w¬O«ü­n¨M©w¦b³Ì¥kÃäÁÙ¬O³Ì¥ªÃ䪺°Ñ¼Æ¥ý±À¤J°ïÅ|¡B¬O¥Ñ¥Dµ{¦¡ÁÙ¬O°Æµ{¦¡²M°£°ïÅ|¡B¬O§_¤¹³\¤£©T©w­Ó¼Æªº°Ñ¼Æµ¥µ¥¡C©³¤U´XºØ¬O±`¨£ªº©I¥s¨ó©w¡G

©I¥s¨ó©w STDCALLCBASIC
³Ì¥ý±À¤J°ïÅ|ªº°Ñ¼Æ¥k ¥k¥ª
¥Ñ½Ö­t³d²M°£°ïÅ|°Æµ{¦¡ ¥Dµ{¦¡°Æµ{¦¡
¬O§_¤¹³\¤£©w­Ó¼Æªº°Ñ¼Æ¬O ¬O§_

¨ä¥LÁÙ¦³¤@¨Ç©I¥s¨ó©w¡A¦p PASCAL¡BFORTRAN µ¥µ¥¡A¤£¹L¦b Win32 ùإΤ£µÛ¡A©Ò¥H¨S¦³¤¶²Ð¡C¦b Win32 ²Õ¦X»y¨¥¤¤¦]¬°¤j¶q©I¥s Win32 API µ{¦¡¡A¦Ó Win32 API ¬O¥Î STDCALL ¤è¦¡¶Ç»¼°Ñ¼Æ¡A©Ò¥H§Ú­Ì¤]±o«Å§i¥H STDCALL ¤è¦¡¶Ç»¼°Ñ¼Æ¡A¨Ã¥B¤]¥u¯à¥H STDCALL ¤è¦¡©I¥s Win32 API¡C¦p¤Wªí©Ò¥Ü¡A¥H STDCALL ©I¥s¨ó©w²ÕĶ®É¡A¬O§i¶D²ÕĶ¾¹³Ì¥kÃ䪺°Ñ¼Æ³Ì¥ý±À¤J°ïÅ|¡AµM«á¬O¥kÃä²Ä¤G­Ó°Ñ¼Æ±À¤J°ïÅ|¡K¡K²Ä¤@­Ó°Ñ¼Æ¬O³Ì«á±À¤J°ïÅ|¡C

³o«e¤T¦æ¡A±z¥i¥H¬Ý¦¨¬O¥²¶·¥B¤£¤Ó·|§ó°Êªºµ{¦¡½X¡C

²Ä¥|¦æ¨ì²Ä¤K¦æ¬O§âµ{¦¡©Ò©I¥s Win32 API ©Ò»Ý­nªºÅܼƩw¸q¤Îµ{¦¡®w§t¬A¶i¨Ó¡C¦b DOS ¤¤¡ADOS ´£¨Ñ¤F³\¦h¤¤Â_ªA°Èµ{¦¡¨Ñ¨Ï¥ÎªÌ©I¥s¡F¬Û¦P¦a¡A¦b Win 32 ¨t²Î¤¤¡AWindows ¤]´£¨Ñ¤F³\¦h±j¤jªº°Æµ{¦¡¡A¤]´N¬O Win32 API¡A¨ÑÀ³¥Îµ{¦¡©I¥s¡C»P DOS ¤£¦Pªº¬O DOS ¤¤Â_ªA°Èµ{¦¡©ñ¦b°O¾ÐÅ餤¡A¦Ó Win32 API ¬O¥]§t¦b°ÊºAµ{¦¡®w¤º¡A³o¨Ç°ÊºAµ{¦¡®w¬O©ñ¦b C:\WINDOWS\SYSTEM ¤l¥Ø¿ýùتº DDL ÀÉ ( ³Ì­«­nªº¤T­Ó¬O KERNEL32.DLL¡BGDI32.DLL ©M USER32.DLL ) ¡A·íÀ³¥Îµ{¦¡©I¥s®É¡A¤~¥h³o­Ó¤l¥Ø¿ý§ä¨ì¸ÓÀÉ´O¤J°õ¦æ¡A¤£¹³ DOS ªº¤¤Â_ªA°Èµ{¦¡¥u»Ý­n¥Î CPU «ü¥O INT ´N¥i¥H¤F¡C

°ÊºAµ{¦¡®wªº¨ç¼Æ©w¸q³£©ñ¦b¬Û¹ïÀ³¦P¥DÀɦWªº¥]§tÀɤº¡A·N«ä¬O KERNEL32.DLL ªº¨ç¼Æ©w¸q©ñ¦b KERNEL32.INC ¥]§tÀɤº¡AUSER32.DLL ªº¨ç¼Æ©w¸q©ñ¦b KUSER32.INC ¥]§tÀɤº¡Cµ{¦¡ªº²Ä 4 ¨ì²Ä 8 ¦æ´N¬O«ü©w©Ò¥Î¨ìªº¥]§tÀÉ»Pµ{¦¡®w¡C¦Ü©ó­n¥[¤J¨º¤@­Ó¥]§tÀɩε{¦¡®w¥²¶·µøµ{¦¡©Ò¥Î¨ìªº Win32 API ¦Ó©w¡A¥i¥H¬d API ¤â¥U¡C¤£¹L¤@¯ë¦³­Ó³W«h¡A¦pªG±z©I¥sªº API ¬O¦b KERNEL32.DLL Àɮפº¡A¨º´N´N¥²¶·¥]§t KERNEL32.INC¡A¥H¤Îµ{¦¡®w KERNEL32.LIB¡Cµ{¦¡®wªº¥]§tÀɨèS¦³¥]§tµ²ºcÅé¡B±`¼Æµ¥©w¸q¡A³o¨Ç©w¸q¬O¥]§t¦b WINDOWS.INC Àɮפº¡A©Ò¥H­ì©lµ{¦¡¤]­n¥]§t WINDOWS.INC ÀÉ¡C

²Ä¤Q¦æ¬Oµù¸Ñ¡A²Õ¦X»y¨¥µù¸Ñ§¡¥H¡y;¡z¶}©l¡A²ÕĶ¾¹·|©¿²¤¡y;¡z¥H«áªº¤å¦r¡C¦³®Éµù¸Ñ¤Óªø¡A¥²¶·¨Ï¥Î¦n´X¦æ¤å¦r®É¡A¥i¥H¥Î COMMENT ¨Ó·í¦¨µù¸Ñ¡C

²Ä 11 ¦æ¨ì²Ä 13 ¦æ¬O©w¸q¸ê®Æ¡y¬q¡z¡A§Ú§â³o­Ó¡y¬q¡z¥H¤Þ¸¹¬A°_¨Ó¬O¦]¬°¥¦©M¦b DOS ¼Ò¦¡¤Uªº°Ï¬q·N¸q¤£¦P¡A¦b DOS ¤¤¤@­Ó¬q¥u¦³ 64KB ªº¤j¤p¡A¦Ó¦b Win32 ¤¤¤@­Ó°Ï¬q´N¦³ 4GB ªºªø«×¡A¦Ó³o 4GB ªº¤j¤p¦A¤À¦¨¸ê®Æ¬q»Pµ{¦¡½X¬q¡C¦b MASM 6.0 ¥H«áªºª©¥»¥i¥H¥Î²³æªº¤èªk¥h©w¸q°Ï¬q¡A¥¦¬Ù²¤¤F°Ï¬q¦W¡A¦Ó¥u§i¶D²ÕĶ¾¹³o¬O°ïÅ|°Ï¬q¡B¸ê®Æ°Ï¬q©Î¬Oµ{¦¡½X°Ï¬q¡A¥Ñ²ÕĶ¾¹¤º©w°Ï¬q¦WºÙ¡C

²³æ©w¸q°Ï¬qªº¤èªk´N¬O¦b¤@¦æªº³Ì«e­±¼g¡y.¡zªí¥Ü³o¬O¤@­Ó°Ï¬q¶}©l¡A°Ï¬qªºµ²§ô¨ÃµL°²«ü¥O¡A¦ý¬O¤@­Ó°Ï¬qªº¶}©l´N¬O«e¤@°Ï¬qªºµ²§ô¡A¦]¦¹¤£·|·d²V¡C±`¥Îªº°Ï¬q¦³¤U­±¥|ºØ¡G

.DATA    ¡÷ ¸ê®Æ°Ï¬q
.CONST   ¡÷ ¸ê®Æ°Ï¬q¡A¦ý¬O¥u¯àÀx¦s±`¼Æ¡A¤]´N¬O¦¹°Ï¬q¤ºªº¸ê®Æ¤£¯à§ó§ï
.DATA?   ¡÷ ÅܼƨS¦³ªì©l­Èªº¸ê®Æ°Ï¬q
.CODE    ¡÷ µ{¦¡½X°Ï¬q

¡y.data¡z¬O¸ê®Æ°Ï¬q¥B¦¹°Ï¬q¤ºªºÅܼơB¦r¦ê³£¬O¤w¸g¦³ªì©l­Èªº¡C¡y.data?¡z©Ò¶}©lªº°Ï¬q¬O¥¼¦³ªì©l­Èªº¸ê®Æ°Ï¬q¡C¡y.code¡z¶}©lªº°Ï¬q´N¬Oµ{¦¡½X°Ï¬q¡C¦¹³B¤p¤ì°¸©w¸q¤F¨â­Ó¦r¦ê¡A³o¨â­Ó¦r¦ê³£¦³¤º®e¤F¡A©Ò¥H©ñ¦b¡y.data¡z°Ï¬q¤º¡C³o¨Ç°Ï¬q¬O²ÕĶ¾¹¤ºªº«O¯d¦r¡A¨Ã¤£¬OÅܼơA¦]¦¹¤j¤p¼g¤£¨ü¡yOPTION  CASEMAP:NONE¡zªº¼vÅT¡C¦¹¥~¡A¹³°²«ü¥O ( ¨Ò¦p INVOKE¡Baddr¡Boffset µ¥µ¥ )¡B80x86 «ü¥O¶° ( ¨Ò¦p mov¡Bpush¡Bdiv µ¥µ¥ )¡K³£¤£¨ü¡yOPTION  CASEMAP:NONE¡zªº¼vÅT¡A¥ç§Y¤j¤p¼g¨S¦³°Ï§O¡C

¦b Win32 ²Õ¦X»y¨¥ùØ¡A©Ò¦³¦³Ãö¦ì§}ªº¼È¦s¾¹¡A¨Ò¦p ESI¡BEDI¡BEBP µ¥³£¬O 32 ¦ì¤¸ªø¡A¦Ó¥B 232=4GB¡A©Ò¥H³o¨Ç¼È¦s¾¹³£¯à©w§}¨ì 4GB ªº¤j¤p¡A¦]¦¹¤£»Ý­n¦A¹³ DOS ®É¥N¡A¥h§â¦ì§}ªí¥Ü¦¨°Ï¬q»P°¾²¾ªº¤è¦¡¤F¡C¨º»ò°Ï¬q¼È¦s¾¹¬O¤£¬O¯uªº¨S¥Î¤F©O¡H·íµM¤£¬O¡A«OÅ@¼Ò¦¡ùتº°Ï¬q¼È¦s¾¹¥t¦³¨ä·N¸q¡A¨Ã«D¤T¨¥¨â»y¥i¥H¤¶²Ð±o§¹ªº¡A¦pªG¶Ã§ï³o¨Ç¼È¦s¾¹ªº¸Ü¡A«Ü®e©ö´N³y¦¨·í¾÷¡AWindows ·|¦Û°Ê¦bµ{¦¡¸ü¤J°õ¦æ®É¡A´N·|§â³o¨Ç­È³]©w¦n¡A¨Ï¥ÎªÌªºµ{¦¡¤£¯à¥h­×§ï¥¦¡C©Ò¥H¦b Win32 ²Õ¦X»y¨¥ùØ¡A§Ú­Ì¥i¥H¤£¥²¥h¦b·N°Ï¬q¼È¦s¾¹ªº¼Æ­È¡A·íµM°²¦p±z·Q²`¤J¬ã¨sªº¸Ü¡A´N¥t·í§O½×¤F¡C

²Ä 15 ¦æ¬Oµ{¦¡½X°Ï¬q¶}©l¡C¦A¤U¤@¦æ¡A¦³¤@­Ó¼Ð°O¡Astart:¡A©M¦b DOS ®É­Ô¤@¼Ë¡A¦bµ{¦¡ªº³Ì«á¤@¦æ¦³¤@­Ó END °²«ü¥O¡Aªí¥Ü¾ã­Ó­ì©l¾×¨ì¦¹µ²§ô¡AEND «á­±©Ò±µªº¼Ð°Oªí¥Ü³o­Óµ{¦¡ªº¶i¤JÂI¡A´«¥y¸Ü»¡¡A³o­Óµ{¦¡¬O±q²Ä 16 ¦æ¶}©l°õ¦æªº¡C

INVOKE °²«ü¥O

²Ä¤@­Ó«ü¥O¬O INVOKE¡A³o¬O¤@­Ó°²«ü¥O¡A¥¦©Ò¥Nªíªº·N¸q¬O©I¥s°Æµ{¦¡¡A¥ý¬Ý¬Ý¥¦ªº»yªk¡G

INVOKE  °Æµ{¦¡¦W, °Ñ¼Æ1, °Ñ¼Æ2, °Ñ¼Æ3, ¡K¡K

INVOKE «á­±±µ¤W­n©I¥sªº°Æµ{¦¡¦WºÙ¡A¦Ó°Æµ{¦¡©Ò»Ý­nªº°Ñ¼Æ«h¬O±µ¦b°Æµ{¦¡¤§«á¡A°Æµ{¦¡»P°Ñ¼Æ¤§¶¡¥H¡y,¡z¤À¹j¡A°Ñ¼Æ»P°Ñ¼Æ¤§¶¡¤]¥Î¡y,¡z¤À¹j¡C°²¦p°Ñ¼Æ¤Ó¦h¦Ó¤@¦æ®e¯Ç¤£¤U®É¡A¥i¥H¥Î¡y\¡zªí¥Ü¤U¤@¦æ¬O±µµÛ³o¤@¦æ¤§«á¡C³oùئ³­Ó°ÝÃD²£¥Í¤F¡A¥H«e¦b DOS ©I¥s¤¤Â_®É¡A¬O§Q¥Î¼È¦s¾¹¶Ç»¼°Ñ¼Æ¡A¨Ò¦p

mov     dx,offset string ;¡÷DX¬°¶Ç»¼ªº°Ñ¼Æ
mov     ah,9
int     21h

¦Ó INVOKE ©Ò»Ýªº°Ñ¼Æ¦s¦b¨º¸Ì©O¡H­ì¨Ó±µ¦b°Æµ{¦¡¦WºÙ«á­±ªº°Ñ¼Æ1¡B°Ñ¼Æ2¡K¡K³£·|³Q²ÕĶ¦¨ push «ü¥O¡AµM«á¥þ³¡³Q±À¤J°ïÅ|¤º¡A¦Ó°Æµ{¦¡«h¨ì°ïÅ|¥h§ä¨ì©Ò»Ý­nªº°Ñ¼Æ¡C³o¨Ç°Ê§@¥þ³¡¥Ñ²ÕĶ¾¹¦Û°Ê§@¦n¡Aµ{¦¡³]­p®v©Ò»Ý­n°µªº´N¬O¬d Win32 API °Ñ¦Ò¤â¥U¤¤¡A³o­Ó API ¥Î¨ì¨º¨Ç°Ñ¼Æ¡A¥H¤Î°Ñ¼Æ©Ò¥Nªíªº·N¸q©M¶¶§Ç¡C(µù¤T)

ML.EXE ²ÕĶ®É¡A©Ò±À¤J°ïÅ|ªº¶¶§Ç¬O¨Ì¾Úµ{¦¡²Ä 3 ¦æ¡Amodel¡Aªº©w¸q¡A¦pªG¬O STDCALL ªº¸Ü¡A¬O³Ì¥kÃ䪺ÅܼƳ̥ý³Q±À¤J°ïÅ|¡A³Ì¥ªÃ䪺ÅܼƳ̫á³Q±À¤J°ïÅ|¡C©Ò¥H²Ä 16 ¦æ¨ä¹ê¬O©³¤U¤­¦æµ{¦¡ªºÂ²¼g¡C

push    MB_OK
push    addr szTitle    ;¨ä¹ê°Ñ¼Æ¦³ addr °²«ü¥O®É¡A¨Ã«D½¦¨ push addr szTitle ©Î
push    addr szMessage  ;push addr szTitle ³o¼Ë²³æ¡A½Ð°Ñ¦Òµù¤­
push    NULL
call    MessageBox

MessgaeBox ( µù¥|) ³o­Ó API ·|¦Û°Ê¨ì°ïÅ|¸Ì¥h§ä¥X©Ò»Ý°Ñ¼Æ¡C

¦¹³B INVOKE ©Ò©I¥sªº°Æµ{¦¡¡AMessageBox¡A¦b¾ã­Ó­ì©lµ{¦¡¤¤¨Ã¥¼©w¸q¡A¨º¥¦¨ì©³¦b¨ºùØ©O¡H­ì¨Ó¦b USER32.INC ùؤw¸g¦³©w¸q¤F¡A±z¦pªG¶}±Ò USE32.INC¡A¥i¥H§ä¨ì¡G

MessageBoxA PROTO   :DWORD,:DWORD,:DWORD,:DWORD
MessageBox  equ     <MessageBoxA>

³o¨â¦æ¡A´N¬O©w¸q MessageBox¡A¸Ô²Ó±¡§Î½Ð¬Ýµù¤T»Pµù¤C¡C³o¤]´N¬O¦b Win32 ¼¶¼g²Õ¦X»y¨¥¥²¶·§â¥]§tÀÉÅn¬A¶i¨Óªº­ì¦]¡C·í²ÕĶ¾¹²ÕĶ®É¡A¥¦¥i¥Hª¾¹D³o¬O¡y¥~³¡¡z°Æµ{¦¡¡A¦Ó¦b¥ØªºÀɤ¤°O¿ý°_¨Ó¡Aµ¥³sµ²¾¹§â¥ØªºÀÉ»Pµ{¦¡®w³sµ²®É¡A¦]¬°µ{¦¡®w¦s¦³ *.DLL ªº¸ê®Æ¡A©Ò¥H§â³o¨Ç¸ê®Æ©M¥ØªºÀɤñ¹ï´Nª¾¹D©I¥sªº API ¦s©ó¦ó³B¡AÀ³¸Ó¦b°õ¦æ®É«ç¼Ë³s±µ¡C

MessageBox API

MessageBox ÅU¦W«ä¸q¡A¬O¥Î¨Ó§â¦r¦ê¦L¦bµøµ¡ªº API¡A¦³Ãö¥¦°Ñ¼Æªº·N¸q¡A¥i¥H¬d Win32 API ¤â¥U¦p¤U¡G

int MessageBox(
    HWND     hWnd,          // handle of owner window
    LPCTSTR  lpText,        // address of text in message box
    LPCTSTR  lpCaption,     // address of title of message box  
    UINT     uType          // style of message box
   );

¦b¤p¬A©·¤ºªºªí¥Ü MessageBox API ©Ò»Ýªº¥|­Ó°Ñ¼Æ¡A¨ä°Ñ¼Æ¦WºÙ¬O hWnd¡BlpText¡BlpCaption¡BuType¡A¦Ó³o¥|­Ó°Ñ¼Æªº¸ê®Æ«¬ºA¬O HWND¡BLPCTSTR¡BUINT¡A©Ò¿×¸ê®Æ«¬ºA´N¬O¹³ DB¡BDW¡BDD ³o¼Ë©w¸qÅܼƪºªø«×¡A¥¦­Ì©M DB¡BDW¡BDD ¤£¦Pªº¬O¥¦­Ì©w¸q¦b WINDOWS.INC ùئӲÕĶ¾¹¤£»{±o¡A³o¤]´N¬O­n§â WINDOWS.INC ¥]§t¶i¨Óªº­ì¦]¡CHWND¡BLPCTSTR¡BUINT ªºªø«×§¡¬°Âù¦r²Õ ( 4 ­Ó¦ì¤¸²Õ¡A¨ä¹ê©Ò¦³ªº API °Ñ¼Æªºªø«×³£¬OÂù¦r²Õ )¡A¤§©Ò¥H©w¸q³o´X­Ó·sªº¸ê®Æ«¬ºA¬O¬°¤F¥iŪ©Ê ( ¦ý¦³®É§Úı±o¼W¥[¤F½ÆÂø©Ê )¡A¨Ò¦p¬Ý¨£ HWND ´Nª¾¹D³o¬Oµøµ¡¥N½X¡A¬Ý¨ì LPCTSTR¡A´Nª¾¹D³o¬O«ü¦V¦r¦ê©Ò¦b¦ì§}ªº«ü¼Ð¡C©³¤U¬O³o¥|­Ó°Ñ¼Æ©Òªí¥Üªº·N¸q¡G

  1. hWnd¡G³o¬O¤÷µ{¦¡ªºµøµ¡¥N½X¡C°²¦p¬O NULL ªº¸Ü¡Aªí¥Ü MessageBox ©Ò«Ø¥ßªºµøµ¡µL¤÷µøµ¡¡CNULL ¬O¦b WINDOWS.INC ùØ©w¸qªº¤@­Ó±`¼Æ¡A´N¬O¹s¡C

  2. lpText¡Gªí¥Ü­nÅã¥Ü¦bµøµ¡¤ºªº¦r¦ê°_©l¦ì§}¡A¸Ó¦r¦ê¥²¶·¥H 0 ¬°µ²§À¡C

  3. lpCaption¡Gªí¥Ü­nÅã¥Ü©óµøµ¡¼ÐÃDªº¦r¦ê°_©l¦ì§}¡A¸Ó¦r¦ê¥²¶·¥H 0 ¬°µ²§À¡C

  4. uType¡Gªí¥ÜÅã¥Ü©óµøµ¡ªº«ö¶s§Î¦¡¡A¥i¥H¬d Win32 API ±oª¾¡AMB_OK ´N¬O¥uÅã¥Ü¤@­Ó¡y½T©w¡z«ö¶s¡A¥¦¤]¬O©w¸q¦b WINDOWS.INC ùØ¡A¨ä¼Æ­È¬°¹s¡C©³¤U¦C¥X±`¥Îªº uType¡G
    uType ¼Æ­È·N¸q
    ©³¤Uªº uType ·|Åã¥Ü¤£¦Pªº«ö¶s
    MB_OK0h ¥uÅã¥Ü¡y½T©w¡z«ö¶s
    MB_OKCANCEL1h Åã¥Ü¡y½T©w¡z»P¡y¨ú®ø¡z¨â­Ó«ö¶s
    MB_ABORTRETRYIGNORE2h Åã¥Ü¡y²×¤î¡z¡B¡y­«¸Õ¡z¡B¡y²¤¹L¡z¤T­Ó«ö¶s
    MB_YESNOCANCEL3h Åã¥Ü¡y¬O¡z¡B¡y§_¡z¡B¡y¨ú®ø¡z¤T­Ó«ö¶s
    MB_YESNO4h Åã¥Ü¡y¬O¡z¡B¡y§_¡z¨â­Ó«ö¶s
    MB_RETRYCANCEL5h Åã¥Ü¡y­«¸Õ¡z¡B¡y¨ú®ø¡z¨â­Ó«ö¶s
    MB_CANCELTRYCONTINUE6h Åã¥Ü¡y¨ú®ø¡z¡B¡y­«¸Õ¡z¡B¡yÄ~Äò¡z¤T­Ó«ö¶s
    MB_HELP4000h Åã¥Ü¡y½T©w¡z¡B¡y»¡©ú¡z¨â­Ó«ö¶s¡A¦pªG¨Ï¥ÎªÌ«ö¤U¡y»¡©ú¡z«ö¶s¡A¨t²Î·|µo¥X WM_HELP °T®§µ¹¤÷µ{¦¡¡C¦³Ãö°T®§¡A°Ñ¦Ò²Ä¤G³¹¡C
    ©³¤Uªº uType ·|¦bµøµ¡ªº¥ªÃäÅ㤣¦Pªº¹Ï¥Ü
    MB_ICONSTOP10h ·|Åã¥Ü¹Ï¥Ü
    MB_ICONERROR10h ¦P MB_ICONSTOP
    MB_ICONHAND10h ¦P MB_ICONSTOP
    MB_ICONQUESTION20h ·|Åã¥Ü¹Ï¥Ü¡A¦ý·L³n«Øij¾¨¶q¤£­n¨Ï¥Î
    MB_ICONEXCLAMATION30h ·|Åã¥Ü¹Ï¥Ü
    MB_ICONWARNING30h ¦P MB_ICONEXCLAMATION
    MB_ICONINFORMATION40h ·|Åã¥Ü¹Ï¥Ü
    MB_ICONASTERISK40h ¦P MB_ICONINFORMATION
    ©³¤Uªº uType ¨M©w­þ¤@­Ó¬O¤º©wªº«ö¶s
    MB_DEFBUTTON10h ²Ä¤@­Ó«ö¶s¬°¤º©w«ö¶s¡A¤º©w«ö¶sÃäÃä·|¥Hµê½u®Ø®Ø³ò¦í¡A·í¨Ï¥ÎªÌ«ö¤UÁä½L¤Wªº¡yEnter¡zÁä¡A´N¬Û·í¨Ï¥ÎªÌ¥H·Æ¹«ÂI¿ï¤º©w«ö¶s¤@¼Ë¡A¨ã¦³¬Û¦Pªº®ÄªG
    MB_DEFBUTTON2100h ²Ä¤G­Ó«ö¶s¬°¤º©w«ö¶s
    MB_DEFBUTTON3200h ²Ä¤T­Ó«ö¶s¬°¤º©w«ö¶s
    MB_DEFBUTTON4300h ²Ä¥|­Ó«ö¶s¬°¤º©w«ö¶s
    ©³¤Uªº uType ¨M©w MessageBox µøµ¡¥X²{«á¡A¨Ï¥ÎªÌ¯à§_Ä~Äò¤u§@
    MB_APPLMODAL0h ¦pªG MessageBox ¬O¬Y­Óµ{¦¡ªº¤lµøµ¡¡A¨º»ò¨Ï¥ÎªÌ¤@©w­n«ö¤U MessageBox ©Ò²£¥Íªºµøµ¡¤¤ªº¥ô¤@«ö¶s¡A¤~¯à¤Á´«¨ì¤÷µøµ¡¡F¦ý¥i¥H¤Á´«¨ì¨ä¥Lµøµ¡Ä~Äò¤u§@¡C¦pªG¨S¦³«ü©w MB_SYSTEMMODAL¡A¤]¨S¦³«ü©w MB_TASKMOOAL¡A«h MB_APPLMODAL ¬°¹w³]­È
    MB_SYSTEMMODAL1000h ¦¹ºX¼Ð·|¨Ï¹ï¸Ü²°¥X²{¦b³Ì«e­±¡A§Y¨Ï¥H·Æ¹«ÂI¿ï¨ä¥Lµøµ¡¡A¤]¤´¦b³Ì«e­±¡C³q±`¥Î¨Ó³qª¾«ÜÄY­«ªº¿ù»~¡C
    MB_TASKMODAL2000h
    ¨ä¥L
    MB_SETFOREGROUND10000h ¨t²Î©I¥s SetForegroundWindow¡A¨Ï MessageBox ²£¥Íªºµøµ¡¦b³Ì«e­±¡C
    MB_DEFAULT_DESKTOP_ONLY20000h
    MB_TOPMOST40000h ²£¥Íªºµøµ¡¨ã¦³ WS_EX_TOPMOST ©µ¦ù­·®æ
    MB_RIGHT80000h ¼ÐÃD¡Bµøµ¡¤º¤å¦r¾a¥k¹ï»ô
    MB_RTLREADING100000h ¼ÐÃD¡Bµøµ¡¤º¤å¦r¥Ñ¥k¦Ü¥ª±Æ¦C¡A¥Î¦bªü©Ô§B¤å©Î§Æ§B¨Ó¤å
    MB_SERVICE_NOTIFICATION200000h

  5. ¶Ç¦^­È¡G¦pªG©I¥s¹Lµ{¥X¿ù¡AMessageBox ·|§â 0 ¦s©ó EAX¡A¦Aªð¦^¤÷µ{¦¡ ( ¤÷µ{¦¡´N¬O©I¥s MessageBox ªºµ{¦¡ )¡F¦pªG¦¨¥\¡AMessageBox §â¶Ç¦^­È¦s©ó EAX ¼È¦s¾¹ùØ¡Aªí¥Ü¨Ï¥ÎªÌ«ö¤U¤F¤°»ò«ö¶s¡A¦p¤Uªí¡G
    ²Å¸¹ ¼Æ­È·N¸q
    IDOK1h «ö¤U¡y½T©w¡z«ö¶s
    IDCANCEL2h «ö¤U¡y¨ú®ø¡z«ö¶s¡C¦pªGµøµ¡¤¤¦³¡y¨ú®ø¡z«ö¶s¡A«ö¤UÁä½Lªº Esc Áä¡A¤]¯àÃö³¬µøµ¡¡A¨Ãªð¦^ IDCANCEL¡F¦pªG¨S¦³¡y¨ú®ø¡z«ö¶s¡AEsc Áä´N¨S¦³§@¥Î¡C
    IDABORT3h «ö¤U¡y²×¤î¡z«ö¶s
    IDRETRY4h «ö¤U¡y­«¸Õ¡z«ö¶s
    IDIGNORE5h «ö¤U¡y©¿²¤¡z«ö¶s
    IDYES6h «ö¤U¡y¬O¡z«ö¶s
    IDNO7h «ö¤U¡y§_¡z«ö¶s
    IDCONTINUE0Bh «ö¤U¡yÄ~Äò¡z«ö¶s

Win32 API ªº¶Ç¦^­È§¡¦s©ñ¦b EAX ¼È¦s¾¹ùØ¡C

ADDR °²«ü¥O

¬°¤F­n¨ú±o¨â­Ó¦r¦êªº°_©l¦ì§}¡A¦b¦r¦ê«e¥[¤W addr °²«ü¥O¡Aªí¥Ü¨ú±o¦ì§}¤§·N¡Caddr ©M¥H«eªº offset «Ü¬Û¦ü¡A®t§O¦b addr ¤£¯à¦V«e¤Þ¥Î¡A·N«ä¬O±z¥²¶·¥ý©w¸qÅܼƤ~¯à¦bµ{¦¡«á­±¨ú±o¸ÓÅܼƦì§}¡A¤£¯à¦bµ{¦¡«á­±©w¸qÅܼƦӦb©w¸q«e¨Ï¥Î addr¡A¦Ó offset «h¥i¥H¡Caddr ¤£¯à§âÅܼƦì§}¶Çµ¹¨ä¥LÅܼƩμȦs¾¹¡A¨Ò¦p

mov     si,addr string

³o¼Ë¼g¬O¤£¦Xªkªº¡A¦ý¬O offset «o¥i¥H¡Caddr ¤@¯ë³£¬O°t¦X INVOKE °²«ü¥O¥Îªº¡C(µù¤­)

¤F¸Ñ¤W­z¤§«á¡AINVOKE °²«ü¥O´N¤£Ãø¤F¸Ñ¤F¡AINVOKE ´N¦n¹³©I¥s¤@­Ó°Æµ{¦¡ (API)¡A¦Ó³o­Ó°Æµ{¦¡©Ò»Ý­nªº°Ñ¼Æ¡A´N±µ¦b°Æµ{¦¡ªº«á­±¡A³o¼Ë¥Î INVOKE ©I¥s¤ñ¥Î call ©I¥s¦Ü¤Ö¦³¤@ÂI¦n³B¡A¨º´N¬O INVOKE ·|À°§Ú­ÌÀˬd±À¤J°ïÅ|ùتº°Ñ¼Æ­Ó¼Æ¬O§_¥¿½T¡A°²¦p°Ñ¼Æ¼Æ¥Ø©Î«¬ºA¤£¥¿½Tªº¸Ü¡A¦b²ÕĶ¶¥¬q´N·|²£¥Í¿ù»~°T®§¡C¦Ó¨Ï¥Î call ©I¥s«h¤£·|²£¥Í¿ù»~¡A¦ý¬O¦b°õ¦æ®É¡A«Ü®e©ö¤Þ°_·í¾÷¡C

ExitProcess API

µ{¦¡²Ä 13 ¦æ¤]¬O INVOKE¡A©Ò©I¥sªº¬O ExitProcess¡A³o¤]¬O¤@­Ó Win32 API¡AÅU¦W«ä¸q¡A¨ä¥\¯à¬Oµ²§ôµ{¦¡ªº¡A¬d Win32 API ¤â¥U¡G

VOID ExitProcess(
    UINT    uExitCode      // exit code for all threads  
   );

±oª¾°Ñ¼Æ¥u¦³¤@­Ó¡A§âªð¦^½X¶Çµ¹§@·~¨t²Î¡C


²ÕĶ»P³sµ²

§â¤W­z­ì©lµ{¦¡¥Î¤å®Ñ³B²z³nÅé¦s¦¨ MESSAGE.ASM Àɮ׫á¡A«ö¤U¡y¶}©l¡z¡÷¡yµ{¦¡¶°¡z¡÷¡yMS-DOS ¼Ò¦¡¡zªº´£¥Ü²Å¸¹¤U¿é¤J¡G

H:\HomePage\SOURCE>ml message.asm [Enter]
Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997.  All rights reserved.

 Assembling: message.asm
Microsoft (R) Incremental Linker Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

/SUBSYSTEM:WINDOWS
"message.obj"
"/OUT:message.exe"

H:\MASM32ASM>

´N¯à²ÕĶ»P³sµ²¤F¡C°²¦p¨S¦³¿ìªk¥¿½T±o¨ì MESSAGE.EXE ªº¸Ü¡AÀˬd¤@¤U±zªºÀô¹Ò¬O§_¥¿½T¡A¦b DOS ´£¥Ü¸¹¿é¤J

H:\HomePage\SOURCE>set [Enter]
TMP=C:\WINDOWS\TEMP
TEMP=C:\WINDOWS\TEMP
PROMPT=$p$g
winbootdir=C:\WINDOWS
COMSPEC=C:\WINDOWS\COMMAND.COM
windir=C:\WINDOWS
INCLUDE=C:\masm32\INCLUDE
LIB=C:\masm32\LIB
PATH=C:\MASM32\BIN;C:\WINDOWS;C:\WINDOWS\COMMAND
ML=/coff /link /SUBSYSTEM:WINDOWS

°²¦p¨S¦³¥X²{Ãþ¦ü¥Õ¦â¤å¦rªº¸Ü¡A¨º¥i¯à¥²¶·±z¦Û¤v³]©wÀô¹Ò¤F¡A¤èªk¤]«Ü²³æ¡Aª½±µ¦b DOS ´£¥Ü¸¹¿é¤J

H:\HomePage\SOURCE>path C:\masm32\BIN;%path% [Enter]

H:\HomePage\SOURCE>set include=C:\masm32\INCLUDE [Enter]

H:\HomePage\SOURCE>set lib=C:\masm32\LIB [Enter]

H:\HomePage\SOURCE>set ml=/coff /link /SUBSYSTEM:WINDOWS [Enter]

¥|¦æ«ü¥O¡A¦¹³B¬O°²³]±z¦w¸Ëªº MASM32 ¦b¡yC:\masm32¡z¡C¦A°õ¦æ ML.EXE ¤@¦¸¸Õ¸Õ¡A°²¦p¤@¤Á¥¿±`¡A±z¥i¥H±o¨ì¤@­Ó MESSAGE.EXE ÀɮסA¥¦¬O¤@­Ó¤£§é¤£¦©ªº Win32 ¥i°õ¦æÀÉ¡A¦p­n°õ¦æ¥¦¡A´N¦b DOS ´£¥Ü²Å¸¹¤U¿é¤J message §Y¥i¬Ý¨ì¼u¥X¤@­Óµøµ¡¡A¤W­±ªº°T®§´N¬O§Ú­Ì©Ò¼gªºµ{¦¡¤º®e¡I


¥Î Soft-ICE Æ[¹î

¥H«e¥Î DEBUG ©Î SYMDEB ®É¡A­n¸ü¤J°£¿ùªºµ{¦¡ª½±µ±µ¦b DEBUG ©Î SYMDEB ¤§«á¡A¦ý¬O Soft-ICE µLªk³o¼Ë¸ü¤J¡A¨º­n¦p¦óÆ[¹î MESSAGE.EXE °õ¦æ±¡§Î©O¡H½Ð·Ó¤U­±¤èªk¸Õ¸Õ¡G

¿ï¨ú¥Ñ¡y¶}©l¡z¡÷¡yµ{¦¡¶°¡z¡÷¡yNuMega SoftICE¡z¡÷¡ySymbol Loader¡z¡A°õ¦æ Symbol Loader¡AµM«á¦b¨ä¿ï³æ¤º¿ï¾Ü¡yFile¡z¡÷¡yOpen Modules...¡z¶}±Ò¿ï¾ÜÀɮתº¹ï¸Ü²°¡A¦p¤U¹Ï¡G

¥Î Symbol Loader ¸ü¤J°õ¦æÀÉ°£¿ù
ª`·N¨ì³o­Ó¹ï¸Ü²°¤U­±ªº¡yÀÉ®×Ãþ«¬¡z¬O¥i°õ¦æÀÉ (*.EXE) ¤Î°ÊºA³sµ²µ{¦¡®w (*.DLL)¡CµM«á¤Á´«¨ì±z­n¸ü¤J°£¿ùªº¥i°õ¦æÀÉ©Ò¦b¤l¥Ø¿ý¿ï¾Ü¸ÓÀÉ¡C«ö¡y¶}±ÒÂÂÀÉ¡z«á·|¬Ý¨ì¦b Symbol Loader µøµ¡¤ºÅã¥Ü

=========================
H:\HomePage\SOURCE\MESSAGE.exe - loaded successfully

µM«á¥Ñ¿ï³æ¤º¡A¿ï¾Ü¡yModule¡z¡÷¡yLoad¡z¡ASoft-ICE ·|¥X²{¨S¦³²Å¸¹ÀÉ ( µù¤» ) ªº°T®§¡A°Ý±z¬O§_Ä~Äò¸ü¤J¡A¦p¤U¹Ï¡G

¤Á´«¨ì°£¿ùµe­±
«ö¤U¡y¬O¡z´N¥i¥H¤Á´«¨ì°£¿ùµe­±¡C¶i¤J°£¿ùµe­±µLªk¥Î¨ä¥L³nÅé¡A³s·Æ¹«³£µLªk¨Ï¥Î¡A¦]¦¹µLªk§ì¤U¹Ï¤ùµ¹¦U¦ì¬Ý¡C

Soft¡ÐICE «ü¥O¡G°lÂÜ¡Ðt ©Î F8

°£¿ùµe­±¬O¤@¶ô¶Â©³¥Õ¦rªºªø¤è§Î°Ï°ì¡A¨ä¤¤ÁÙ³Q¤À³Î¦¨¦n´X¶ô¡A³Ì¤U­±¨º¤@¶ôÀ³¸Ó¬O¡y©R¥Oµøµ¡¡z(command window)¡A¥i¥Hµ¹¨Ï¥ÎªÌ¿é¤J©R¥O¡C¦b©R¥Oµøµ¡¤W­±¬O¡yµ{¦¡½Xµøµ¡¡z(code window)¡A²{¦bÁÙ¨S¦³Åã¥Ü­n°£¿ùµ{¦¡ªº¤º®e¡C¦n¡A§Ú­Ì«ö¤U F8 Áä ( F8 Á䪺¥\¥Î¡A±z¤]¥i¥H¦b©R¥Oµøµ¡¿é¤J¡yt¡z«ü¥O¦A«ö Enter Áä¡Aµ²ªG¬O¬Û¦Pªº¡C³o­Ó t ªº·N«ä´N¬O³æ¨B°lÂÜ¡A©M¦b DEBUG/SYMDEB ¬Û¦P )¡A±z·|µo²{µ{¦¡½Xµøµ¡ªº¤º®e§ïÅܤF¡AÅܦ¨ MESSAGE.EXE ªº¤º®e¡A¨Ã¥B°ª«G«×¥ú´Î²¾¨ì¦ì§} 00401002 ³B¡G

017F:00401000  6A00              PUSH    00
017F:00401002  6800304000        PUSH    00403000
017F:00401007  680D304000        PUSH    0040300D
017F:0040100C  6A00              PUSH    00
017F:0040100E  E80D000000        CALL    00401020
017F:00401013  6A00              PUSH    00
017F:00401015  E800000000        CALL    0040101A
017F:0040101A  FF2500204000      JMP     [00402000]
017F:00401020  FF2508204000      JMP     [00402008]

À˵ø³o¤@¬qµ{¦¡½X¡A¥Ñ 017F:00401000 ¨ì 0040100C ³sÄò¥|­Ó push «ü¥O¡A´N¬O§â MessageBox ©Ò»Ýªº°Ñ¼Æ±À¨ì°ïÅ|¤¤¡AµM«á MessageBox ¦A¥Ñ°ïÅ|¤¤¥h¨ú¥X¡C¦ì§} 0040100E ªº call ´N¬O call MessageBox¡A³o¥|­Ó push «ü¥O»P call «ü¥O¥i¥H¬Ý§@¬O INVOKE °²«ü¥O¹B§@ªºµ²ªG¡C00401013 ¨ì 00401015 ³o¨â¦æ«ü¥O©M¤W­±¤@¼Ë¡A¬O§â ExitProcess ©Ò»Ý¤§°Ñ¼Æ±À¤J°ïÅ|¡A¥H¤Î©I¥s ExitProcess API¡C

¨C·í±z¨C¿é¤J¤@­Ó F8 Áä®É¡A°ª«G«×ªº¥ú´Î°õ¦æ¸Ó¦æ«ü¥O¨Ã²¾°Ê¨ì¤U¤@­Ó±N­n°õ¦æªº«ü¥O³B¡A°£¤F¶i¤J Soft¡ÐICE ªº²Ä¤@­Ó F8¡C

Soft¡ÐICE «ü¥O¡G°Æµ{¦¡°lÂÜ¡Ðp ©Î F10

·í±z°lÂܨì 017F:0040100E ³B¡A³o¬O Win32 API MessageBox¡A¥i¥H«ö F10 ©Î¿é¤J¡yp¡ÏEnter Áä¡z ªí¥Ü¤£°lÂܰƵ{¦¡¤º³¡¡A§â°Æµ{¦¡·í¦¨¤@­Ó«ü¥O°õ¦æ¡A´N¹³ SYMDEB ùتº¡yp¡z«ü¥O¤@¼Ë¡A¦ý¬O°²¦p±z­n°lÂÜ MessageBox ¤ºªº¸ê®Æ¥i¥H«ö F8 ÁäÄ~Äò°lÂÜ¡C

¤p¤ì°¸¬°¤F§ó¸Ô²Ó¤F¸Ñ INVOKE °²«ü¥O¡A«ö¤U F8 °lÂÜ MessageBox ùتºµ{¦¡½X¡A·í±z«ö¤U F8 Áä®É¡Aµo²{¥ú´Î¸õ¨ì¦ì§} 00401020 ³B¡A±N°õ¦æ JMP [00402008] ³o¤@¦æ«ü¥O¡A·í±z¦A«ö¤U F8 Áä¡Aµ{¦¡·|¸õ¨ì¡A¾ã­Óµe­±·|Åܦ¨¤U¹Ï¡G

017F:BFF541BA  55                PUSH    EBP
017F:BFF541BB  8BEC              MOV     EBP,ESP
017F:BFF541BD  6A00              PUSH    00
017F:BFF541BF  FF7514            PUSH    DWORD PTR [EBP+14]
017F:BFF541C2  FF7510            PUSH    DWORD PTR [EBP+10]
017F:BFF541C5  FF750C            PUSH    DWORD PTR [EBP+0C]
017F:BFF541C8  FF7508            PUSH    DWORD PTR [EBP+08]
017F:BFF541CB  E84CECFFFF        CALL    BFF52E1C
017F:BFF541D0  5D                POP     EBP
017F:BFF541D1  C21000            RET     0010

µo²{ MessgaeBox ­º¥ý§â EBP ¼È¦s¾¹¦s¤J°ïÅ|¡AµM«á§â EBP «ü¦V°ïÅ|³»ºÝ¡A¥H EBP ¥h°ïÅ|§ä¥X°Ñ¼Æ­È¨Ó¡C¦ý¬O MessageBox °Æµ{¦¡ ( À³¸Ó»¡ API ¤Ó½ÆÂø¡A¤£©öÆ[¹î°ïÅ|ª¬ºA¡Aµù¤C¦³¥t¤@­Ó²³æªº¨Ò¤l¡C )

¤F¸Ñ¤F INVOKE °²«ü¥O«á¡A§Ú·Q¥H«á¤j·§¤£»Ý­n¶O¨Æ¥h°lÂÜ Win API ªA°Èµ{¦¡¡A¬Ý¨ì¬O Win API ª½±µ«ö¤U F10 Áä°õ¦æ§¹²¦´N¥i¥H¤F¡C¨ä¹ê Soft-ICE ¥i¥H§â API ¦WºÙÅã¥Ü¥X¨Ó¡A¤èªk¬O­×§ï winice.dat Àɮ׫á¥b¬q¡A§â©Ò¦³ªº¡y;EXP=¡K¡K¡z«e­±ªº¡y;¡z¥h±¼¡A­«·s¶}¾÷¡A¦A¥H¬Û¦P¤èªk¸ü¤J MESSAGE.EXE ´N·|¬Ý¨ì¤U­±ªºµ{¦¡½X¡G

017F:00401000  6A00              PUSH    00
017F:00401002  6800304000        PUSH    00403000
017F:00401007  680D304000        PUSH    0040300D
017F:0040100C  6A00              PUSH    00
017F:0040100E  E80D000000        CALL    USER32!MessageBoxA
017F:00401013  6A00              PUSH    00
017F:00401015  E800000000        CALL    KERNEL32!ExitProcess

³o¼Ë¹ï©ó°£¿ù©Î°lÂܳ£«Ü¤è«K¡A¤£¬O¶Ü¡H

¦pªG±z·Qªð¦^ Win 9x §@·~¨t²Î¡A¥i¥H«ö Ctrl-D¡ASoft-ICE ·|§â±±¨îÅv¥æÁÙµ¹Win 9x §@·~¨t²Î¡A¦ý¬O Soft-ICE ¤´µMÁÙ¦b°O¾ÐÅ餺¡A±z¥i¥HÀH®É«ö¤U Ctrl-D ¥s¥X Soft-ICE¡C¦Ü©ó Soft-ICE §ó¸Ô²Óªº¥Îªk½Ð°Ñ¦Ò¹q¸£¤Wªº»¡©ú¡C


¥Î OllyDebug Æ[¹î

·íµ{¦¡³]­p®v·Q¥Î OllyDebug °£¿ù MESSAGE.EXE ®É¡A¥ý°õ¦æ OLLYDBG.EXE µ{¦¡¡AµM«á¦b OllyDebug µøµ¡ªº¿ï³æùØ¡A¿ï¾Ü¡yFile¡z¡÷¡yOpen¡z¡AµM«á¦b¼u¥Xªº¹ï¸Ü²°¤¤¡A¤Á´«¨ì MESSAGE.EXE ©Ò¦bªº¤l¥Ø¿ý¡A¦A¿ï¾Ü MESSAGE.EXE¡A´N¥X²{¹³¤U­±ªºµe­±¡G

OllyDebug°£¿ùµe­±
OllyDebug ³Ì±`¥Î¨ìªº´N¬O¤W­±ªº CPU µøµ¡¡A¾ã­Óµøµ¡¤À¦¨¥i¥Î·Æ¹«½Õ¾ã¤j¤pªº¤­­Ó°Ï°ì¡A¤À§O¬O¤Ï²ÕĶ°Ï ( Disassembler )¡B°T®§°Ï ( Information )¡B°O¾ÐÅéÅã¥Ü°Ï ( Dump )¡B¼È¦s¾¹°Ï ( Registers ) ©M°ïÅ|°Ï ( Stack )¡C§Ú­Ì¥D­n¤u§@¬O¦b¤Ï²ÕĶ°Ï¡A¦¹°Ï¤S¦A²Ó¤À¦¨µ{¦¡½X¦ì§}¡B¾÷±ñ½X¡B§U°O¾Ð½X¡Bµù¸Ñ¥|°Ï¡C¦b¾÷±ñ½X¦³¤@¥ª¤¤¬A¸¹¡A¦¹¥ª¤¤¬A¸¹©Ò¬A°_¨Óªºµ{¦¡½Xªí¥Ü¦b¦P¤@°Æµ{¦¡ùØ¡Fµù¸Ñ°Ï¤]¦³¨â­Ó¥ª¤¤¬A¸¹¡A¥¦­Ìªí¥Ü©I¥s Win32 API ©Ò»Ý¤§°Ñ¼Æ¡C¦b¼È¦s¾¹°ÏùØ¡A±z¥i¥H«ö¤U·Æ¹«¥kÁä¡A¿ï¾ÜÅã¥Ü FPU¡BMMX¡B3DNow! ©Î°£¿ù¼È¦s¾¹¡C¨ä¾lªº¥\¯àÁÙ¦³«Ü¦h¡A¤p¤ì°¸¤£¤@¤@¤¶²Ð¡A½Ð¤j®a¸Õ¸Õ¡A¤p¤ì°¸¶È¤¶²Ð´X­Ó±`¥Îªº¥\¯à¡C

­×§ï¼È¦s¾¹¼Æ­È

±z¥i¥H§â·Æ¹«´å¼Ð²¾¨ì·Q­×§ïªº¼È¦s¾¹¤W¡A¹ï³o­Ó¼È¦s¾¹§Ö«ö¨â¦¸·Æ¹«¥ªÁä¡C

³]©w©Î²¾°£¤¤Â_ÂI¡GF2 Áä

±z¥i¥H²¾°Ê·Æ¹«´å¼Ð©Î«öÁä½Lªº¦V¤W¡B¦V¤UÁä²¾°Ê¦ì©ó¤Ï²ÕĶ°Ïªº¦Ç¦â¥ú´Î ( ¤W¹Ïªº¦Ç¦â¥ú´Î¦b¦ì§} 00401000 ³B ) ¨ì±z·Q³]©wªº¤¤Â_ÂI¦ì§}¡AµM«á«öÁä½Lªº F2 Áä¤@¦¸¡A¦b¤Ï²ÕĶ°Ïµ{¦¡½X¦ì§}³B·|Åܦ¨¬õ©³¶Â¦r¡Aªí¥Ü¦¹³B¤w¸g³]©w¦n¤¤Â_ÂI¡C

­Y­n²¾°£¤¤Â_ÂI¡A«h¨Ï¦Ç¦â¥ú´Î²¾¨ì­n²¾°£ªº¤¤Â_ÂI³B¡A¦A«ö¤@¦¸ F2 Áä§Y¥i¡C

°lÂÜ¡GF7 ©Î F8

F7 ©M F8 ³£¬O°lÂܵ{¦¡¡A¤£¹L F7 ¹J¨ì°Æµ{¦¡©Î LOOP¡BREP MOVS µ¥«ü¥O®É¤´·|¤@¨B¤@¨B¥h°lÂÜ¡F¦Ó F8 ¹J¨ì°Æµ{¦¡©Î Win32 API «hª½±µ§â¾ã­Ó°Æµ{¦¡©Î Win32 API °õ¦æ§¹²¦¡A¹J¨ì LOOP¡BREP WOVS µ¥«ü¥O¤]¬Oª½±µ°õ¦æ§¹²¦¡C

°õ¦æµ{¦¡¡GF9

«ö¤U F9¡A¥i¨Ïµ{¦¡°õ¦æ¨ìµ²§ô¡A©Î¨ì¤¤Â_ÂI¬°¤î¡C

Ãö³¬µ{¦¡¡GCtrl¡ÐF2 ©Î Alt¡ÐF2

Ãö³¬µ{¦¡·|µ²§ô­ì°£¿ù¤¤ªºµ{¦¡¡A­Y«ö¤U Ctrl¡ÐF2¡AOllyDebug ·|¦ÛµwºÐ­«·s¸ü¤J­èµ²§ôªºµ{¦¡¡F­Y«ö¤U Alt¡ÐF2¡AOllyDebug ¤£·|¦A¸ü¤Jµ{¦¡¡A©ó¬O¤u§@°ÏÅܦ¨ªÅ¥Õ¡C¨Æ¹ê¤W¡A¦b OllyDebug ¤@¶}©l°õ¦æ«á¡A©|¥¼¸ü¤J­n°£¿ùªºµ{¦¡«e¡A­Y«ö¤U Ctrl¡ÐF2¡AOllyDebug ·|¦Û°Ê¸ü¤J¤W¦¸°£¿ùªºµ{¦¡¡C

¦b OllyDebug ¥Î­ì©l½X°£¿ù

OllyDebug ¤ä´©­ì©l½X°£¿ù¡A¬O¤@­Ó¦b RING 3 «Ü¤£¿ùªº°£¿ù¾¹¡C­n¦b OllyDebug Æ[¹î¨ì­ì©l½X¡A¥²¶·§â­ì©l½Xªº²Å¸¹¸ê®Æ¼g¤J EXE ÀÉùØ¡A¦b²ÕĶ®É¥²¶·¤U¹F¡y/Zi¡z°Ñ¼Æ¡G

E:\HomePage\SOURCE>ml /Zi message.asm [Enter]
Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997.  All rights reserved.

 Assembling: message.asm
Microsoft (R) Incremental Linker Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

/SUBSYSTEM:WINDOWS
"message.obj" /DEBUG
"/OUT:message.exe"

E:\HomePage\SOURCE>

µM«á¦A¥H¥¿±`¤è¦¡°õ¦æ OllyDebug¡A¸ü¤J message.exe §Y¥i¬Ý¨ì­ì©l½X¤F¡A´N¹³¤U­±ªº¹Ï¤ù¡G

OllyDebug¦b­ì©l½X¶¥¬qªº°£¿ùµe­±
¦b¤W¹Ï¤¤¡AMessageBox API ªº¨â­Ó°Ñ¼Æ¡A¼ÐÃD¤Î¦r¦ê¡A³£Åܦ¨¤F­ì©l½X¤¤ªºÅܼƦWºÙ¤F¡A¤£¦A¬O¥H¦ì§}ªí¥Ü¡A³o¼Ë¹ï©ó¸û¤jµ{¦¡ªº°£¿ù«Ü¦³À°§U¡C


µù¤@¡G¨ä¹ê¤p¤ì°¸¦Û¤v¤]¥¿¾Ç²ß Win32 ²Õ¦X»y¨¥¡A©Ò¥H°²¦p¦³¤£¥¿½Tªº¦a¤è¡AÁٽФj®a¨Ó«H§iª¾¡C

µù¤G¡G¦pªG±z´¿¾Ç¹L C/C++¡A¦Ó¥B´¿¼g¹L DOS µ{¦¡¡A¥i¯à´¿¬Ý¹L TINY¡BSMALL¡BCOMPACK¡BMEDIUM¡BLARGE¡BHUGE µ¥¼Ò¦¡¡C¦b¤p¤ì°¸©Ò¼gªº DOS µ{¦¡¤¤±`±`§âµ{¦¡½X¬q¡B¸ê®Æ¬q¡B°ïÅ|¬q¶°¤¤©ó 64KB ¤º¡AµM«á»s§@¦¨ *.COM ÀÉ¡A¹³³oºØ¼Ò¦¡ºÙ¤§¬° TINY ¼Ò¦¡(·L¤p¼Ò¦¡)¡CSMALL ¼Ò¦¡¬O¸ê®Æ¬q©Mµ{¦¡½X¬q¤À§O¦b¤£¦Pªº°Ï¬q¡CCOMPACK ¼Ò¦¡«h¬O¥u¦³¤@­Óµ{¦¡½X°Ï¬q¡A¦Ó¸ê®Æ¬q¥i¥H¦³¦n´X­Ó¡CMEDIUM ¼Ò¦¡«h¬O¥u¦³¤@­Ó¸ê®Æ¬q¡A¦Óµ{¦¡½X°Ï¬q¥i¥H¦³¦n´X­Ó¡CLARGE ¼Ò¦¡«h¬Oµ{¦¡½X°Ï¬q©M¸ê®Æ¬q³£¥i¥H¦³¦n´X­Ó¡CHUGE ¼Ò¦¡«h¬Oµ{¦¡½X°Ï¬q©M¸ê®Æ¬q³£¥i¥H¦³¦n´X­Ó¡A¨Ã¥B¦³¥²­nªº¸Ü¡A¨ä¬Y­ÓÅܼƪø«×¥i¥H¶W¹L 64KB¡C

µù¤T¡G¼¶¼g Win32 ¨t²Îªºµ{¦¡¡A¦pªG­n©I¥s°Æµ{¦¡¡A¤£½×¬O¦¹°Æµ{¦¡¬O¦b­ì©lÀɤ¤©Î¬O¥~³¡°Æµ{¦¡¡A­ì©lµ{¦¡¥²¶·¥ý«Å§i°Æµ{¦¡¡A³oÂI©M¦b DOS ªº±¡§Î¤£¦P ( ¦pªG°Æµ{¦¡¦b call/INVOKE ©I¥s¤§«e¡A´N¤£¶·¥ý«Å§i )¡C­ì¦]¬O¦b Win32 ¨t²Îùتºµ{¦¡·|¥Î¨ì Win32 API¡A¦Ó¨ä©Ò»Ý°Ñ¼Æ¬O©ñ¦b°ïÅ|ùØ¡A©Ò¥H¥²¶·ª¾¹D°Æµ{¦¡»Ý­n´X­Ó°Ñ¼Æ¡AÅý²ÕĶ¾¹¦n¦b°ïÅ|¤¤ªÅ¥X¤@¨ÇªÅ¶¡¡A®e¯Ç³o¨Ç°Ñ¼Æ¡C«Å§i°Æµ{¦¡»Ý­nªº°Ñ¼Æ¡AºÙ¬°¡y«Å§i¨ç¼Æ­ì«¬¡z¡C³oÂI©M C/C++ «Ü¹³¡A¦b C/C++ ùØ¡A¨Ï¥Î°Æµ{¦¡ ( C/C++ ºÙ¬°¨ç¦¡©Î¨ç¼Æ ) ®É¡A¤]³£­n¨Æ¥ý«Å§i¡C

PROTO °²«ü¥O

¦b Win32 ²Õ¦X»y¨¥¤¤«Å§i°Æµ{¦¡­ì«¬ªº¤èªk¬O¥Î PROTO ¡A¤@¯ë PROTO ³£¬O©ñ¦b­ì©lµ{¦¡ªº³Ì«e­±¡A¦Ü¤Ö¦b©I¥s°Æµ{¦¡¤§«e¡CPROTO ¬O¤@­Ó MASM ªº°²«ü¥O¡A¨ä»yªk¦p¤U¡F

°Æµ{¦¡¦WºÙ  PROTO  [¦ì²¾] [µ{¦¡»y¨¥] [[°Ñ¼Æ¤@]:¸ê®Æ«¬ºA,] [[°Ñ¼Æ¤G]:¸ê®Æ«¬ºA,] ¡K¡K

¤W¦¡¤¤ªº¦ì²¾¥i¥H¥Î NEAR¡BFAR¡K¡Kµ¥¡Aªí¥Ü°Æµ{¦¡¶ZÂ÷©I¥sªÌ¦h»·¡A¬O§_¬O¦b¦P¤@­Ó°Ï¬qµ¥µ¥¡A¦ý¬O¦b Win32 ùØ¡A°O¾ÐÅé¼Ò¦¡³£¬O FLAT ¼Ò¦¡¡A©Ò¥H¦b Win32 ²Õ¦X»y¨¥ùØ¡A³o­Ó¿ï¶µ¬OµL¥Îªº¡Cµ{¦¡»y¨¥¥i¥Î C¡BPASCAL¡BSTDCALL¡A¨ä·N¸q©M .MODEL ¬Û¦P¡A°²¦p³o­Ó¿ï¶µ¬Ù²¤ªº¸Ü¡A´N±Ä¨ú .MODEL ©Ò©w¸qªº¤è¦¡¡A¦b Win32 ²Õ¦X»y¨¥¨Ó»¡¡AÀ³¸Ó­n¨Ï¥Î STDCALL¡A¦Ó¥B¦b .MODEL ©w¸q§Y¥i¡C°Æµ{¦¡©Ò»Ýªº°Ñ¼Æ¦C¦b³Ì«á­±¡A¹ï Win32 ²Õ¦X»y¨¥¨Ó»¡¡A°Ñ¼Æªº¸ê®Æ«¬ºA³£¬OÂù¦r²Õ¡A³o¬O¦]¬°­n±À¤J°ïÅ|¡A°ïÅ|¤¤ªº¨C¤@µ§¸ê®Æ³£¬O 32 ¦ì¤¸ªø¡A¦Ó°Ñ¼Æ¦WºÙ³£¥i¥H¬Ù²¤¡A¦]¬°²ÕĶ¾¹¥u»Ýª¾¹D±À¤J°ïÅ|ªº°Ñ¼Æ­Ó¼Æ¡A¦Ü©ó°Ñ¼Æ¦WºÙ¬O¤£­«­nªº¡A¦pªG¤£¬O¬°¤F¥iŪ©Êªº¸Ü¬O¥i¥H¬Ù²¤ªº¡C

¦b message.asm ­ì©lµ{¦¡ùبèS¦³«Å§i MessageBox ³o­Ó Win32 API¡A¦ý¬O¦b USER32.INC ùئ³¤@¦æ´N¬O©w¸q MessageBoxA ªº¨ç¼Æ­ì«¬¡F

MessageBoxA PROTO :DWORD,:DWORD,:DWORD,:DWORD

©Ò¥HÁöµM­ì©lµ{¦¡¨Ã¨S¦³«Å§i MessageBox¡A¦ý¬O¥u­n§â USER32.INC ¥]§t¶i¨Ó´N¥i¥H¤F¡C¦Ü©ó MessageBox ©M MessageBoxA ªºÃö«Y½Ð°Ñ¦Òµù¥|¡C

PROC ©M ENDP °²«ü¥O

·íµM°£¤F©I¥s Win32 API ¤§¥~¡A¤]¥i¥H©I¥s¦Û¤vªº°Æµ{¦¡¡A°²¦p­n©I¥s¦Û¤vªº°Æµ{¦¡¡A¨º»ò°£¤F¤@¶}©l´N­n¥Î PROTO «Å§i°Æµ{¦¡¤§¥~¡AÁÙ­n¦Û¤v¥Î PROC/ENDP ¨Ó¼¶¼g°Æµ{¦¡¡A§_«h¦b API ¨S¦³¦Ó±z¤S¤£¦Û¤v¼¶¼g¡A³o¼ËµLªk»s¦¨ *.EXE ÀÉ¡C¦b MASM 6.x ¤§«á¡A PROC ¤w¸g°µ¤F¤@¨Ç­×¥¿¡A¥¦ªº»yªk¦p¤U¡F

°Æµ{¦¡¦W  PROC  [¦ì²¾][µ{¦¡»y¨¥][¨Ï¥ÎÅv­­][USES ¼È¦s¾¹][°Ñ¼Æ¤@:¸ê®Æ«¬ºA,]¡K¡K
°Æµ{¦¡¦W  ENDP

°Æµ{¦¡¦W¥i¥H¥ô·N¨ú¡A¥u­n²Å¦X©R¦W³W«h¨Ã¥B PROC ©M ENDP ¤@­P§Y¥i¡C¦ì²¾¡Bµ{¦¡»y¨¥¡B°Ñ¼Æ©M PROTO ·N¸q¬Û¦P¡A¦Ó¥B¦b PROTO «Å§i®É©M¦b PROC °²«ü¥O«ü©wªº¿ï¶µ¤]À³¸Ó­n¤@­P¡C¨Ï¥ÎÅv­­¥i¥H¥Î PUBLIC¡BPRIVATE¡BEXPORT ¤TºØ¡APUBLIC ¬Oªí¥Ü©Ò¦³ªº¼Ò²Õ³£¥i¥H¨Ï¥Î¡A°²¦p¦b PROC ¨S¦³«ü©ú®É¡A²ÕĶ¾¹·|±Ä¥Î³oºØ¤è¦¡¡CPRIVATE ¬Oªí¥Ü¥u¦³¥»¼Ò²Õ¯à°÷¨Ï¥Î¡CEXPORT ¬O«ü¦pªG­n½s¼g *.DLL ÀÉ®ÉÅý¦¹°Æµ{¦¡¯à°÷Â^¨ú¥X¨Ó¡CUSES ¤§«á±µ¤Wªº¼È¦s¾¹¬Oªí¥Ü²ÕĶ¾¹·|¦b°Æµ{¦¡Ä¶¶}©l¦Û°Ê¦w´¡ push «ü¥O¨Ï¼È¦s¾¹±À¤J°ïÅ|¡A¦b ENDP «ü¥O«e¥Î pop ¨ú¦^¼È¦s¾¹¡A³o¬OÅé¶Kµ{¦¡³]­p®vªº¤@ºØ³]­p¡A¦ý¦³®É¤£¦p¦Û¤v¥Î pushad ©M popad ³o¨â­Ó 80386 «ü¥O¦Û¤v«O¦s¼È¦s¾¹¡CENDP °²«ü¥O¬Oªí¥Ü°Æµ{¦¡µ²§ô¡C

¦b¼¶¼g Win32 ²Õ¦X»y¨¥®É¡A¦pªG­n©I¥s°Æµ{¦¡¡AÀ³¸Ó­n¦bµ{¦¡¤@¶}©l¨Ï¥Î PROTO «Å§i¨ç¦¡­ì«¬¡A¨Ã¥B§â¸Ó°Æµ{¦¡ªº©Ò¥Î¨ìªº°Ñ¼Æ¦b PROTO «á­±¡A¨Ã¥B¥Î PROC ªí¬O°Æµ{¦¡¶}©l¡A¦P®É¤]§â©Ò¨Ï¥Î¨ìªº°Ñ¼Æ¦C¦b PROC «á­±¡AµM«á¦b©I¥s®É¾¨¶q¥Î INVOKE ¦Ó¤£­n¥Î call¡C³o¼Ëªº¸Ü¡A¦pªG©I¥s®É°Ñ¼Æªº¼Æ¥Ø©M«Å§i®É¤£¤@¼Ë®É¡A²ÕĶ¾¹·|²£¥Í¿ù»~¡A¦Ü©ó±À¤J¦h¤Ö­Ó°Ñ¼Æ¨ì°ïÅ|¥H¤Îµ²§ô°Æµ{¦¡®É±Ë±ó¦h¤Öªº°ïÅ|¸ê®Æ¡A³£¥Ñ²ÕĶ¾¹¦Û°Ê­pºâ¡A¤£¥Îµ{¦¡³]­pªÌ¾Þ¤ß¡C°²¦p±z·Q¥Î call¡A·íµM¤]¥i¥H¡A¦ý¬O¥²¶·¦Û¦æª`·N°ïÅ|¬O§_¿ù»~¡A§_«h«Ü®e©ö·í¾÷¡Cµù¤C¦³¤@­Ó¨Ò¤l»¡©ú PROTO¡BPROC¡BINVOKE ªºÃö«Y¡A¥H¤Î°ïÅ|¾Þ§@¡C

µù¥|¡G°²¦p±z¥h¬d Win32 API ¤â¥U¡A¥i¯à·|¬d¨ì MessageBoxA ©Î MessageBoxW¡A¦ý¬O´N§ä¤£¨ì MessageBox¡A¨ä¹ê MessgaeBoxA ¬O ANSI ª©¥»ªº¡AMessageBoxW ¬O UNICODE ª©¥»ªº¡A¦b Win 9x ¨t²Î¤¤¥u¤ä´© ANSI ª©¥»¡ANT/XP ¤ä´© ANSI ¤Î UNICODE¡A©Ò¥H¦b user32.inc ùØ°½°½§â MessgaeBox ©w¸q¦¨ MessgaeBoxA¡C±z¥i¥H¥Î¥ô¦ó¤å®Ñ³B²zµ{¦¡¶}±Ò user32.inc ÀɮסA¥i¥H§ä¨ì³o¤@¦æ¡F

MessageBox equ <MessageBoxA>

µù¤­¡G¦³Ãö addr »P offset ªº¤£¦P¡A¨ä¹ê¬O©M°Ï°ìÅܼƻP¥þ°ìÅܼƦ³Ãö¡A½Ð°Ñ¦Ò²Ä¤T³¹¦³Ãö addr »P offset ªº»¡©ú¡C

µù¤»¡G°²¦p±z¦b²ÕĶ®É¡A¿é¤J

ML /Zi /Cp /coff MESSAGE.ASM

¨º´N·|»s°µ¥X§t¦³²Å¸¹ªº MESSAGE.EXE¡AÁöµM³o¼Ëªº°µªk·|¨Ï±o MESSAGE.EXE ÀÉ®×Åܤj¤@¨Ç¡A¦ý¬O·í Soft-ICE ¸ü¤J®É¤£·|¥X²{³o­Ó¿ù»~°T®§¡A¦Ó¥B¥i¥H¬Ý¨£­ì©l½X¡A¨Ã¥B¥i¥H¥Î­ì©l½X¨Ó°£¿ù¡A·íµ{¦¡¸û¬°½ÆÂø®É¤ñ¸û¤è«K¡C¨ä¥Lªº ML °Ñ¼Æ¥i¥H¥Î

ML /help | more

¨ÓÆ[¹î¡C

µù¤C¡G¦b message.asm ùØ©I¥s¤F MessageBox API¡A¦ý¬O§Ú­Ì¤£©öÆ[¹î¨ì¨ä°ïÅ|¹B§@±¡§Î¡C¬°¤F¸Ô²ÓÆ[¹îÆ[¹î°ïÅ|ªº¹B§@¥H¤Î PROC¡BINVOKE µ¥°²«ü¥O·t¦aùØ©Ò°µªº¨Æ¡A¤p¤ì°¸¥t¥~¼g¤F¤@­Ó²³æªºµ{¦¡¨Ó»¡©ú¡C³o­Óµ{¦¡·|©I¥s¤@­Ó addition °Æµ{¦¡¡Aaddition °Æµ{¦¡ªº¥\¥Î¬O¥Î¨Ó¨D¤T­Ó°Ñ¼Æªº©M«á¦s©ó EAX ¼È¦s¾¹¡A¦Aªð¦^¥Dµ{¦¡¡A­ì©lµ{¦¡¦p¤U¡G

.386
.MODEL      FLAT,STDCALL
OPTION      CASEMAP:NONE 
INCLUDE     WINDOWS.INC
INCLUDE     KERNEL32.INC
INCLUDELIB  KERNEL32.LIB

.DATA
a1       DWORD   20h
a2       DWORD   100h
a3       DWORD   40000h

.DATA?
sum      DWORD   ?

.CODE
addition PROC    para1:DWORD,para2:DWORD,para3:DWORD
         mov     eax,para1
         add     eax,para2
         add     eax,para3
         ret
addition ENDP

start:   INVOKE  addition, a1, a2, a3
         mov     sum,eax
         INVOKE  ExitProcess,0
         END     start

²ÕĶ¡B³sµ²¦n«á¡A¥Î Soft-ICE ¸ü¤J¡A¦A«ö¤U¤@¦¸ F8 Áä¡AÆ[¹îµ{¦¡½X¦p¤U¡G

0187:00401000 55             PUSH   EBP
0187:00401001 8BEC           MOV    EBP,ESP
0187:00401003 8B4508         MOV    EAX,[EBP+08]
0187:00401006 03450C         ADD    EAX,[EBP+0C]
0187:00401009 034510         ADD    EAX,[EBP+10]
0187:0040100C C9             LEAVE
0187:0040100D C20C00         RET    0C
0187:00401010 FF3508304000   PUSH   DWPRD PTR [00403008]
0187:00401016 FF3504304000   PUSH   DWPRD PTR [00403004]
0187:0040101C FF3500304000   PUSH   DWPRD PTR [00403000]
0187:00401022 E809FFFFFF     CALL   00401000
0187:00401027 A30C304000     MOV    [0040300C],EAX
0187:0040102C 6A00           PUSH   00
0187:0040102E E801000000     CALL   KERNEL32!ExitProcess

¤W­zµ{¦¡½X¤¤¾ï¦âªº³¡¥÷¬O addition °Æµ{¦¡¡AÂŦ⪺³¡¥÷¬O INVOKE °²«ü¥O³Q²ÕĶ«áªºµ²ªG¡C¥i¥H«Ü©úÅã¬Ý¥X¨Ó¡A²ÕĶ¾¹®Ú¾Ú INVOKE addition «á­±ªº°Ñ¼Æ¦C¡A²ÕĶ¦¨¤T­Ó push «ü¥O ( ª`·N³Ì¥kÃ䪺°Ñ¼Æ¥ý³Q±À¤J°ïÅ| )¡C¦b addition °Æµ{¦¡¤¤¡A²ÕĶ¾¹·|¦Û°Ê¥[¤W¤T¦æ¨Ã­×§ï RET «ü¥O¡F

PUSH   EBP
MOV    EBP,ESP
¡K¡K¡K¡K
LEAVE
RET    0C

«e¨â¦æ¬O¬°¤F¡A·í»Ý­n°Ñ¼Æ®É´N§Q¥Î EBP ¼È¦s¾¹¦Û°ïÅ|¤¤¨ú¥X¡CEBP ©M BP ¼È¦s¾¹³£¬O¥i¥H§Q¥Î¼È¦s¾¹©w§}¨ú¥X°ïÅ|¬q ( SS ) ªº¼Æ­È¡C³oÂI©M¥H«e¦b DOS ²Õ¦X»y¨¥¤¤¥Î BX¡BSI ©w§}¨ú¥X¸ê®Æ¬q ( DS ) ªº¼Æ­È¡A©M¥Î DI ©w§}¨ú¥XÃB¥~¬q ( ES ) ªº¼Æ­È¬OÃþ¦üªº¡C³Ì«á¨â¦æ¬O¬°¤F¦Û°Æµ{¦¡ªð¦^¥Dµ{¦¡®É¡A¯à©ß±ó°Ñ¼Æ¸ê®Æ¥H¤Î«ì´_¥¿½Tªº EBP¡C©³¤U§Ú­Ì¥ý¨Ó¬Ý¬Ý Soft-ICE ªº¼È¦s¾¹µøµ¡¡G

EAX=00401010   EBX=00000000   ECX=8196BF28   EDX=8196BF68   ESI=8196BF08
EDI=00000000   EBP=0063FF78   ESP=0063FE38   EIP=00401016
CS=0187   DS=018F   SS=018F   ES=018F   FS=43CF   GS=0000

¦A¦b Soft-ICE ³Ì¤U­±ªºµøµ¡¿é¤J¡yd ds:402FF0¡z¡AÆ[¹î DATA °Ï¬qªº¸ê®Æ¡G

0187:00402FF0  00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00
0187:00403000  20 00 00 00 00 01 00 00-00 00 04 00 00 00 00 00

¾ï¦âªº¬O a1 ÅܼơA¦ì§}¦b 018F:00403000¡A²HÂŦ⪺¬O a2 ÅܼơA¦ì§}¦b018F:00403004¡A²H¶À¦âªº¬O a3 ÅܼơA¦ì§}¦b018F:00403008¡A¦b¦ì§}018F:0040300C ªº¬O sum ÅܼơF³o¨Ç¦ì§}¥i¥H¦b¤W­±¥H Soft¡ÐICE ¤Ï²ÕĶ®É¡APUSH ¤T­ÓÅܼƪº¦ì§}¬Ý¥X¨Ó¡C³o®É­Ô Soft-ICE ¥¿­n°õ¦æ

0187:00401016 FF3504304000   PUSH   DWPRD PTR [00403004]

³o¤@¦æ¡A¦ý©|¥¼°õ¦æ¡C³o¤@¦æ¨Ã¤£¬Oµ{¦¡ªº²Ä¤@¦æ¡A³o¬O¦]¬°¤@¶}©l§Ú­Ì´¿¸g«ö¤U¤@¦¸ F8 Áä¡A©Ò¥H¨ä¹êµ{¦¡¤w¸g°õ¦æ¹L push a3 ¤F¡A´«¥y¸Ü»¡¡Aa3 ¤w³Q±À¤J°ïÅ|¤F¡A±z¥i¥H¨£¨ì°ïÅ|ªº±¡§Î¬O¤U¹Ïªº¹Ï¤@¡A40000h ¤w¸g¦b°ïÅ|ùؤF¡C¦Ó³Ì¥ªÃ䪺¥Õ¦â¼Æ¦r¡A0063FE24 ¨ì 0063FE3C¡A¬O°ïÅ|¦ì§}¡A¶À¦âªº¤T¨¤§Î½b¸¹»P ESP ¬O²{¦b ESP ©Ò«üªº¦ì§}¡A¦Ó³Ì¤W­±ªº¤@¦æ¥Î¥Õ¦âªí¥Üªº«ü¥O¬O±N­n°õ¦æªº«ü¥O¡C©³¤U¤p¤ì°¸¥H¹Ï§Î»¡©ú°ïÅ|ÅܤƱ¡§Î¡F

»¡©ú°ïÅ|¹B§@±¡§Î

¦A³sÄò«ö¨â¦¸ F8 «á¡A°ïÅ|±¡§ÎÅܦ¨¹Ï¤T¡A³o®É­Ô¤w¸g§â addition ©Ò»Ýªº¤T­Ó°Ñ¼Æ±À¤J°ïÅ|ùؤF¡C¦A«ö¤@¦¸ F8 Áä¡A©I¥s¦ì©ó 00401000 ªº addition °Æµ{¦¡¡A¦P®É§â¥Dµ{¦¡¤U¤@­Ó±N°õ¦æ«ü¥Oªº¦ì§}¡A00401027¡A±À¤J°ïÅ|Àx¦s¡A¦p¹Ï¥|¡C¦A«ö¤U¤@¦¸ F8 Áä¡A°õ¦æ°Æµ{¦¡ªº²Ä¤@­Ó«ü¥O¡A§â­ì¨Óªº EBP ±À¤J°ïÅ|Àx¦s°_¨Ó¡A¦p¹Ï¤­¡C¦A«ö¤@¦¸ F8 Áä¡A¨Ï EBP «ü¦V°ïÅ|³»¡A¤@ª½¨ì°Æµ{¦¡µ²§ô«e¡AEBP ¤§­È³£¬O 63FE28¡A¦p¹Ï¤»¡C

»¡©ú°ïÅ|¹B§@±¡§Î
¹Ï¤»¤W¤èªº«ü¥O¸ò±µ¤U¨Óªº¨â­Ó«ü¥O¡A¤@¦@¤T­Ó«ü¥O³£»P°ïÅ|µLÃö¡A«Ü§Öªº³sÄò«ö¤T¦¸ F8¡Aµ²ªG¦p¹Ï¤E¡A³o®É±N°õ¦æ LEAVE «ü¥O¡A³o­Ó«ü¥O¬O§â ESP ¤§­È³]¬° EBP¡AµM«á¦A¨Ï ESP ©Ò«üªº°ïÅ|³»¼Æ­È¼u¥X¦s©ó EBP¡C«ö¤U F8¡A´N°õ¦æ¤W­z¹Lµ{¡A§Y EBP «ì´_©I¥s«e­ì­È¡A¥B ESP ¥[¥|¡A«ü¦V 63FE2C¡A¦p¹Ï¤Q¡C¦A¨Ó¬O°õ¦æ RET 0C «ü¥O¡A³o­Ó«ü¥O¬O±q°ïÅ|¨ú¥Xªð¦^¦ì§}¡AµM«á¦A±Ë¥h 0CH ­Ó¦ì¤¸²Õªº¸ê®Æ¡A©Ò¥H¦A«ö¤U¤@¦¸ F8 Áä«á¡AESP «K«ü¦V 63FE3C¡A³o­Ó¦ì§}¬O¥¼°õ¦æ INVOKE ¤§«e ESP ©Ò«üªº¦ì§}¡C©³¤UÁÙ¦³¤@­Ó INVOKE «ü¥O¡A¦ý¬O¾Þ§@¤èªkÃþ¦ü¡A©Ò¥H¤p¤ì°¸¤£¥´ºâ¦A»¡©ú¤F¡C

¥Ñ¤W­±ªº¨Ò¤l¥i¥H¬Ý¨ì¡A¥Dµ{¦¡¥ý§â°Ñ¼Æ¥Ñ¥k¦Ü¥ª±À¤J°ïÅ|¡A±µµÛ°õ¦æ call «ü¥O®É§âªð¦^¦ì§}±À¤J°ïÅ|¡A¨Ã¥H EBP ·í§@«ü¼Ð¦s¨ú°Ñ¼Æ¡F¦pªG°Æµ{¦¡»Ý­n¥Î¨ì°Ï°ìÅܼƮɡA¤]·|¦b³o¶ô°ïÅ|¤¤«Ø¥ß°Ï°ìÅܼơC³o¶ô°ïÅ|¥i»¡¬O°Æµ{¦¡»P¥Dµ{¦¡·¾³qªº¾ô¼Ù¡A¤]¬O°Æµ{¦¡¦s¨ú¸ê®Æ©Ò¦b¡A³o¶ô°ïÅ|ºÙ¬°¡y°ïÅ|®Ø¡z( stack frame )¡C¤W¹Ï¤¤ªº 63FE38 ¨ì 63FE28 §Y¬°°ïÅ|®Ø¡C¦b Win32 ùØ­±¡A°Æµ{¦¡ªº²Ä¤@­Ó°Ñ¼Æ¥H [EBP¡Ï08H] ªí¥Ü¡B²Ä¤G­Ó°Ñ¼Æ¥H [EBP¡Ï0CH] ªí¥Ü¡B²Ä¤T­Ó°Ñ¼Æ¥H [EBP¡Ï10H] ªí¥Ü¡K¡K¡F¦Óªð¦^¦ì§}«hÀx¦s¦b [EBP¡Ï04H] ¤§³B¡C

LEAVE «ü¥O

LEAVE «ü¥O¬O 80X86 CPU «ü¥O¶°ªº¤@­Ó«ü¥O¡A¥¦¥ý¨Ï ESP ¼È¦s¾¹³]©w¬° EBP ¤§­È¡AµM«á¦A¼u¥X¤@­Ó°ïÅ|¼Æ­È¦s©ó EBP¡C±`¥Î©óªð¦^¥Dµ{¦¡®É§â ESP ¤§­È³]©w¬°¥¿½T­È¡C¦b°Æµ{¦¡¤¤¡AESP ¤§­È¤£¤@©w¬O«ü¦V±z©Ò¹w´Áªº¦ì§}¡A¦]¬°±`¦³¤¤Â_µo¥Í¡A·|¨Ï ESP ­È¤£½T©w¡A¦ý¬O¦pªG¦b°Æµ{¦¡¤@¶}©l³]©w EBP ­È¡A¨º EBP ¤£·|§ïÅÜ¡C©Ò¥H Win32 ¨t²Î±`§Q¥Î LEAVE «ü¥O¦bªð¦^¥Dµ{¦¡®É§â ESP ¤§­È³]©w¬°¥¿½T­È¡C¦b Win32 ¨t²Î¤¤¡A¤@­Ó°ïÅ|¼Æ­Èªø«×¬°¤@­ÓÂù¦r²Õ ( 32 ­Ó¦ì¤¸ )¡C

¦b 16 ¦ì¤¸ªº DOS ¨t²Î¤¤¡A¤]¥i¥H¨Ï¥Î LEAVE «ü¥O¡A³o®É¨Ï SP ¼È¦s¾¹¤§­È³]¬° BP ¤§­È¡AµM«á¦A¼u¥X¤@­Ó°ïÅ|¼Æ­È¦s©ó BP¡C©Ò¤£¦Pªº¬O¡A¦b 16 ¦ì¤¸ªº¨t²ÎùØ¡A°ïÅ|¤@­Ó¼Æ­Èªø«×¬°¤@­Ó¦r²Õ ( 16 ­Ó¦ì¤¸ )¡C

RET n «ü¥O

RET «ü¥O¤]¬O¤@­Ó 80X86 CPU «ü¥O¶°ªº¤@­Ó«ü¥O¡A°²¦p n ¬°¹s¥i¥H¶È¼g¡yRET¡z¡A¨º´N¬O¨ú¥X¤@­Ó°ïÅ|ªº¸ê®Æ¡A¦s©ó IP ©Î EIP¡Aµø Win32 ©Î DOS ¨t²Î¦Ó©w¡C°²¦p¦³ n ­È¡A¨º»ò¥ý¨ú¥X¤@­Ó°ïÅ|ªº¸ê®Æ¡A¦s©ó IP ©Î EIP¡AµM«á¦A¨Ï SP ©Î ESP ¤§­È¥[¤W n¡A©ß±ó°ïÅ|¤W n ­Ó¦ì¤¸²Õªº¸ê®Æ¡A³o¨Ç¸ê®Æ¤]´N¬O©I¥s°Æµ{¦¡®É¦s©ó°ïÅ|ªº°Ñ¼Æ¡C¤@¯ë¦Ó¨¥¡An ­È³q±`¥Ñ²ÕĶ¾¹³]©w¡A¼gµ{¦¡®É¡A¥i¥H©¿²¤¦Ó¥u¼g¡yRET¡z§Y¥i¡C


¨ì²Ä¹s³¹¡A ¦^¨ì­º­¶¡A ¨ì²Ä¤G³¹