home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Hutch / DOWNLOAD / Example2.exe / SMC / SMC.ASM < prev    next >
Encoding:
Assembly Source File  |  1999-05-28  |  3.4 KB  |  123 lines

  1. ; #########################################################################
  2. ;
  3. ;   SMC.ASM is a test piece for reading and writing to the code section
  4. ;   of a PE file. In this example, there are two procedures, one which
  5. ;   is called, the second that is read and written at the address
  6. ;   of the first. The proc is called twice, before & after the code has
  7. ;   been modified.
  8. ;
  9. ;   This is made possible by using the link option "/section:.text,RWE"
  10. ;   which sets the code section as read/write/execute. This file should
  11. ;   be built with the supplied "build.bat"
  12. ;
  13. ;   Iczelion assisted in the development of this example.
  14. ;
  15. ; #########################################################################
  16.  
  17.       .386
  18.       .model flat, stdcall
  19.       option casemap :none   ; case sensitive
  20.  
  21. ; #########################################################################
  22.  
  23.       include \masm32\include\windows.inc
  24.       include \masm32\include\user32.inc
  25.       include \masm32\include\kernel32.inc
  26.  
  27.       includelib \masm32\lib\user32.lib
  28.       includelib \masm32\lib\kernel32.lib
  29.  
  30.       CalledProc PROTO
  31.       ReplaceMentProc  PROTO
  32.  
  33. ; #########################################################################
  34.  
  35.     .data
  36.         lnth1       dd 0
  37.         szDlgTitle  db "SMC example",0
  38.         Phony       db "This is 1st call of proc",0
  39.         Replc       db "This is 2nd call of proc",0
  40.         ttl1        db "Original Code",0
  41.         ttl2        db "Replacement Code",0
  42.  
  43. ; #########################################################################
  44.     
  45.     .code
  46.  
  47. start:
  48.  
  49.     invoke CalledProc   ; call proc as written
  50.     
  51.     lea eax, rpEnd      ; end label of 2nd proc
  52.     lea edx, rpStart    ; start label of 2nd proc
  53.     sub eax, edx        ; get offset differences
  54.     mov lnth1, eax      ; save the length
  55.  
  56.     lea esi, rpStart    ; load address of 2nd proc
  57.     lea edi, ppStart    ; load address of 1st proc
  58.     mov ecx, lnth1      ; put length of 2nd proc in ecx
  59.     rep movsb           ; write code from 2nd proc to address of 1st
  60.  
  61.     invoke CalledProc   ; call it again after it has been modified
  62.  
  63.     invoke ExitProcess,0
  64.  
  65. ; #########################################################################
  66.  
  67. CalledProc proc
  68.  
  69.     ppStart::   ; labels with [ :: ] are visable GLOBALLY.
  70.  
  71.     invoke MessageBox,0,ADDR Phony,ADDR ttl1,MB_OK
  72.  
  73.     ppEnd::
  74.  
  75.   ; ------------------------------------------------------
  76.   ; The "nop's" are padding to ensure that there is enough
  77.   ; room for the code that is written between the 2 labels
  78.   ; ------------------------------------------------------
  79.  
  80.     nop
  81.     nop
  82.     nop
  83.     nop
  84.     nop
  85.     nop
  86.     nop
  87.     nop
  88.  
  89.     ret
  90.  
  91. CalledProc endp
  92.  
  93. ; #########################################################################
  94.  
  95. ReplaceMentProc proc
  96.  
  97.     ; -----------------------------------
  98.     ; this proc is never called but the
  99.     ; code between the two labels is read
  100.     ; and then written to the address of
  101.     ; the first proc
  102.     ; -----------------------------------
  103.  
  104.     rpStart::
  105.     
  106.     push MB_OK or MB_ICONEXCLAMATION
  107.     lea eax, ttl2
  108.     push eax
  109.     lea eax, Replc
  110.     push eax
  111.     push 0
  112.     lea eax, MessageBox
  113.     call eax
  114.  
  115.     rpEnd::
  116.  
  117.     ret
  118.  
  119. ReplaceMentProc endp
  120.  
  121. ; #########################################################################
  122.  
  123. end start