home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Iczelion / w32_02.txt < prev    next >
Text File  |  2000-05-25  |  11KB  |  392 lines

  1.                                 -          
  2.                    -----=========-----
  3.            -------==========================================----------
  4. ----------=====_masta_'s tut on win32-ASM-coding part 2 revision 1=====-----------
  5.            -------==========================================----------
  6.  
  7. ( I called this revision 1 because it contains new sourcecode without errors
  8.   _masta_ found when going through it again - fungus )
  9.  
  10.                       --==INTRO==--
  11.  
  12. Hi,
  13. since part0 and part1 have been relatively successful, I am happy to
  14. present you part2 now.
  15. Actually I wanted to do something on GUI, but I was very busy lately
  16. so something without GUI-coding for now. I think it will be interesting
  17. anyway I hope.
  18. Starting from this tutorial I won't explain the easy things like 
  19. MessageBox anymore, because they have been fully explained in both of
  20. the first parts. I don't think it will cause you any problem once you
  21. did the ealier parts.
  22.  
  23.  
  24.  
  25.  
  26.                 --==WHAT IS NEEDED?==--
  27.  
  28.  
  29.     1. Texteditor
  30.     2. TASM 5.0 with libs, etc.
  31.     3. A Windows API reference (WIN32.HLP)
  32.     4. Starcraft (ONLY for testing purposes!;])
  33.     5. some Braincells left ;)
  34.     6. some basic ASM-knowledge (earlier lessons)
  35.         7. Numega Softice 3.xx (not really a must)
  36.  
  37.  
  38.  
  39.  
  40.  
  41.               --==WHAT IS IT ABOUT THIS TIME?==--
  42.  
  43. Is there any gamer who doesn't apreciate little aids sometimes ...
  44.  
  45. more lives
  46. more money
  47. more energy
  48. more gas
  49. more ...
  50.  
  51. What I am talking about is a trainer, very common in C64-/Amiga-/
  52. PC-DOS-times, unfortunately getting less lately, although there are
  53. some from time to time. But it is still not like in the 
  54. "good old times".
  55. So my target is Starcraft (Yes I know there are trainers for it!).
  56.  
  57. My reasons were: - the game is very popular
  58.          - I played it when I got the idea to this tut :)
  59.  
  60.  
  61.  
  62.  
  63.                    --==LET'S GO==--
  64.  
  65. Some thought before starting our session.
  66.  
  67. Definition of Trainer: - a little program, that changes parts of
  68.              memory used by a game to for example gain
  69.              more money, etc ...
  70.  
  71. Normally it isn't allowed in Windows to access memory addresses of
  72. another program. Luckily our dearest friend Billy implemented a
  73. couple of functions, which were meant to debug originally. We can
  74. use these for our purposes.
  75. These functions are OpenProcess, WriteProcessMemory and 
  76. ReadProcessMemory. With the help of these we can read (and write) 
  77. from (into) memory addresses of another program.
  78. Basically our program acts like a debugger, accessing other programs
  79. memory and changing it.
  80.  
  81.  
  82.  
  83.  
  84.                    --==STRUCTURE==--
  85.  
  86. 1. Intro (little introduction shown using a MessageBox)
  87.  
  88. 2. Get Process_ID of the program to be "trained"
  89.      (find main window of Starcraft; get process
  90.       with the help of the window) 
  91.  
  92. 3. OpenProcess
  93.  
  94. 4. Change values
  95.  
  96. 5. Close Handle to process, end (Cleanup)
  97.  
  98.  
  99.  
  100.  
  101.  
  102.                 --==IMPORTANT API-FUNCTIONS==--
  103.  
  104. The handle of the main window we can get with FindWindowA, where we gotta
  105. get the name of the windowclass ("SWarrClass") and the name of the window 
  106. ("Starcraft"). We can do this with the help of Softice (TASK->HWND).
  107.  
  108. With the windowhandle we can get the corresponding process, or rather PID
  109. by using GetWindowThreadProcessId.
  110.  
  111. Now we take a handle of the memory area of the process with the help of 
  112. the PID -> OpenProcess.
  113.  
  114. Everything is getting easier now. Like in "normal" fileoperations we can 
  115. write into the memory of a running program with the handle and the function 
  116. WriteProcessMemory.
  117.  
  118. Last but not least we call CloseHandle, to close our handle to the process,
  119. which is not really important in Win95, but who trusts software coming from
  120. Redmont ;-)?
  121.  
  122. And very last the known function ExitProcess.
  123.  
  124.  
  125.  
  126.  
  127.                  --==THE MEMORY ADDRESSES==--
  128.  
  129. We can easily get the adds of for example the minerals by using a debugger
  130. and searching for the hex-values of the decimal-values shown on the screen.
  131. In my version it it like the following:
  132.  
  133.             Minerals = 04EFE08h
  134.             Gas      = 04EFE38h
  135.  
  136.  
  137.  
  138.  
  139.                       --==THE SOURCE==--
  140.  
  141. This time not very long and as usual not very good structured, but should
  142. be easy to understand anyway ...
  143.  
  144.  
  145. ;This is a slightly edited source to my tutorial (Part 2)
  146. ;I did a mistake while searching the informations for the memory locations
  147. ;not taking care, that starcraft uses different locations ...
  148.  
  149. ;Only change is that the million-value is written 2 times
  150. ;and 8 bytes instead of 4
  151.  
  152.                                         ; Set some params for the assembler
  153. .386P
  154. Locals
  155. jumps
  156.  
  157.  
  158. .Model Flat ,StdCall
  159. PROCESS_VM_WRITE    equ 020h            ; Flags for the write-access
  160. PROCESS_VM_OPERATION    equ 008h        ; to the process
  161.  
  162.  
  163.  
  164. mb_ok      equ 0
  165. minerals_pos       equ 04efe08h
  166. gas_pos        equ 04efe38h
  167.  
  168.  
  169.  
  170. ; declaration of used API-functions
  171.  
  172.  
  173. extrn     MessageBoxA      : PROC       ; Show a Messagebox
  174. extrn     FindWindowA      : PROC       ; Find Window with the name
  175. extrn     GetWindowThreadProcessId :Proc; Find PID with the HWND
  176. extrn     OpenProcess      : PROC       ; Procedure to access the process
  177. extrn     WriteProcessMemory: PROC      ; Write into memory of the running
  178.                                         ; program
  179. extrn     CloseHandle      : PROC       ; Close the handle again
  180.                                         ; Cleanup, after use ;)
  181. extrn     ExitProcess      : PROC       ; Procedure to exit the program
  182.  
  183.  
  184.  
  185. ; here begins our Data
  186. .Data
  187.  
  188.  
  189. caption  db "_masta_'s essay on Win32-ASM-Coding, part 2",0 
  190.               ;Captionstring, 0-terminated
  191.  
  192.  
  193. text     db "Hi, here we are at part 2",13,10
  194.      db "This tut will describe you how to make",13,10
  195.      db "Win32-ASM Trainer",0
  196.  
  197.                                 ; Introtext , 0-terminated
  198.  
  199.  
  200. err_cap  db "ERROR",0           ; Caption for Errormessages
  201.  
  202.  
  203. notrun   db "Sorry, Starcraft is not running",0 ; Error if SC isn't running
  204.  
  205.  
  206. no_write db "Mmmhhhh, a problem, by writing",13,10
  207.      db "to Starcrafts memory",13,10,0 
  208.  
  209.  
  210. readycap db "Ready",0           ; Caption for "ready"
  211.  
  212.  
  213. readytxt db "Ok, now you have 1000000 Minerals and Gas",0 
  214.  
  215.                                 ; Text for "ready"
  216.  
  217.  
  218. million  dd 1000000             ; How much do you want??? ;]
  219.          dd 1000000
  220.  
  221. wnd_name db "Starcraft",0       ; Name of the Starcraft-window
  222. cls_name db "SWarClass",0       ; Class of the Starcraft-window
  223.  
  224.  
  225. pid_sc   dd ?                   ; Here we save the PID ...
  226.  
  227.  
  228. p_hand   dd ?                   ; and here the handle to the
  229.                                 ; process
  230.  
  231.  
  232. ; And here we start with our code
  233.  
  234. .Code
  235. Main:
  236.         push mb_ok
  237.         push offset caption
  238.         push offset text
  239.         push 0
  240.         call MessageBoxA        ;Startmessage
  241.  
  242.  
  243. is_SC_RUN:
  244.  
  245.  
  246.     push offset wnd_name
  247.     push offset cls_name
  248.     call FindWindowA    ; Find Window handle with Windowclass and
  249.                         ; -name
  250.  
  251.  
  252.     cmp  eax,0          ; if 0, window is not existing
  253.     jz   SC_isnt_run_end; --> Starcraft is not launched
  254.  
  255.  
  256.     push offset pid_sc            ; Where to save the PID ?
  257.     push eax                      ; PUSH Windowhandle 
  258.     call GetWindowThreadProcessId ; Determine PID with Windowhandle
  259.  
  260. open_the_process:
  261.     push pid_sc                                   ; PUSH PID
  262.     push 0                                        ; only used when 
  263.                                                   ; building new 
  264.                                                   ; processes
  265.     push PROCESS_VM_WRITE OR PROCESS_VM_OPERATION ; activate write-access
  266.  
  267.     call OpenProcess    ; Get handle of Starcraft
  268.     mov  p_hand,eax     ; Save handle to p_hand
  269.  
  270.  
  271. change_Minerals:
  272.  
  273.  
  274.     push 0                 ; Can be zero mostly
  275.     push 8                 ; Write 8 Bytes (2 Dwords)
  276.     push offset million    ; How much ? (1 Million)
  277.     push minerals_pos      ; 1st Memoryaddress
  278.     push p_hand            ; Handle to the process
  279.     call WriteProcessMemory; write minerals  
  280.     cmp  eax,0
  281.     jz   error_on_write    ; If any error while writing (eax=0) -> end
  282.  
  283.  
  284. change_gas:                ; the same again for gas, but this time
  285.                            ; the memory address of the gas is PUSHed
  286.     push 0
  287.     push 8
  288.     push offset million
  289.     push gas_pos
  290.     push p_hand
  291.     call WriteProcessMemory
  292.     cmp  eax,0
  293.     jz   error_on_write
  294.  
  295.  
  296.  
  297. Trainer_ready:
  298.  
  299.  
  300.         push mb_ok              
  301.         push offset readycap    
  302.         push offset readytxt    
  303.         push 0                  
  304.         call MessageBoxA    ; Everything OK
  305.  
  306.  
  307.  
  308. close_the_PID_Handle:
  309.     push p_hand
  310.     Call CloseHandle    ; CloseHandle
  311.     jmp  end_           ; Go to End
  312.  
  313.  
  314.  
  315. error_on_write:
  316.  
  317.  
  318.         push mb_ok              
  319.         push offset err_cap     
  320.         push offset no_write    
  321.         push 0               
  322.         call MessageBoxA          ; Mmmhhh, Error while writing
  323.         jmp  close_the_PID_Handle ; Close handle before quit
  324.  
  325.  
  326. SC_isnt_run_end:
  327.  
  328.  
  329.         push mb_ok              
  330.         push offset err_cap     
  331.         push offset notrun      
  332.         push 0               
  333.         call MessageBoxA        ; nothing there to train =(
  334.     
  335. end_:
  336.  
  337.  
  338.         CALL    ExitProcess     ; Exit program
  339. End Main                        ; End of Code Determination 
  340.                                   of Jump-point (Main)
  341.  
  342. ;--------------------------==END OF SOURCE==----------------------------
  343.  
  344.  
  345. ;--------------------------------START---------------------------make.bat
  346. @echo off
  347. echo assembling your trainer
  348. tasm32 /mx /m3 /z /q w95asm_2
  349. tlink32 -x /Tpe /aa /c w95asm_2,w95asm_2,, import32.lib
  350. del *.obj
  351. del *.map
  352. ;---------------------------------END----------------------------make.bat
  353.  
  354.  
  355.  
  356.                       --==FINAL WORDS==--
  357.  
  358. OK, as I told you before this was a little tutorial, but I think very
  359. interesting anyway. I guess there is not much to optimize (sorry fungus), 
  360. maybe the routine for writing into memory (use of a procedure).
  361. I hope my mailbox (masta_t@usa.net) is flodded soon (CRITICS ARE WELCOME)
  362. and you are all here next time. I promise the next one will be about GUI,
  363. because many people told me to do so.
  364. BTW, I am trying to build an IRC channel (EFNET) on this (#win32asm) and
  365. finally there is a project-page 'HTTP://win32asm.cjb.net'!
  366. If anyone is interested, any contribution to this subject is very welcome,
  367. we are waiting for it ... 
  368. I really hope there are enough people, who spend their time on this 
  369. subject and who are willing to give their knowledge to others, too.
  370.  
  371.  
  372.  
  373.                       --==GREETINX==--
  374.  
  375. VucoeT (Translator and Designer), scut (You are GREAT, why not code in Win32?),
  376. |caligo| (bad news about you :(), fravia (best on the web), +Aescalapius
  377. (i hope to break Brainbreaker), not4you (wir Ossis muessen zusammenhalten ;)),
  378. fungus (something to optimze), CyberBobjr (for translating to frensh), DASavant,
  379. mornings, i_magnus, Quest, Silvio, TheDoctor, everyone on #LAC and 
  380. #cracking4newbies and to every cracker around the world.
  381.  
  382.  
  383.  
  384.  
  385.                       --==WISE WORDS==--
  386.  
  387.   ------===========================================================-------
  388. -----=====A hardcoded serial is as common as a 25-year-old virgin=====------
  389.   ------===========================================================-------
  390.                           -----=========-----
  391.                    -
  392.