y_writemem.inc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /**--------------------------------------------------------------------------**\
  2. ===========================
  3. Y Sever Includes - Writemem
  4. ===========================
  5. Description:
  6. Write to any absolute address in the SA:MP server in pure PAWN with embedded
  7. assembly (i.e. a new native). Calls "VirtualProtect" to make writes safe.
  8. Legal:
  9. Version: MPL 1.1
  10. The contents of this file are subject to the Mozilla Public License Version
  11. 1.1 (the "License"); you may not use this file except in compliance with
  12. the License. You may obtain a copy of the License at
  13. http://www.mozilla.org/MPL/
  14. Software distributed under the License is distributed on an "AS IS" basis,
  15. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  16. for the specific language governing rights and limitations under the
  17. License.
  18. The Original Code is the YSI ini include.
  19. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  20. Portions created by the Initial Developer are Copyright (C) 2011
  21. the Initial Developer. All Rights Reserved.
  22. Contributors:
  23. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  24. Thanks:
  25. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  26. ZeeX - Very productive conversations.
  27. koolk - IsPlayerinAreaEx code.
  28. TheAlpha - Danish translation.
  29. breadfish - German translation.
  30. Fireburn - Dutch translation.
  31. yom - French translation.
  32. 50p - Polish translation.
  33. Zamaroht - Spanish translation.
  34. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  35. for me to strive to better.
  36. Pixels^ - Running XScripters where the idea was born.
  37. Matite - Pestering me to release it and using it.
  38. Very special thanks to:
  39. Thiadmer - PAWN, whose limits continue to amaze me!
  40. Kye/Kalcor - SA:MP.
  41. SA:MP Team past, present and future - SA:MP.
  42. Version:
  43. 1.0
  44. Changelog:
  45. 01/02/12:
  46. Zeex_: Changed to use SYSREQ.D call not SYSREQ.C.
  47. Zeex_: Modified "VirtualProtect" pointer to SA:MP server one.
  48. Added "VirtualProtect" calls to the code.
  49. First version.
  50. Functions:
  51. Public:
  52. -
  53. Core:
  54. -
  55. Stock:
  56. -
  57. Static:
  58. -
  59. Inline:
  60. -
  61. API:
  62. WriteMem - Write data to an address.
  63. Callbacks:
  64. -
  65. Definitions:
  66. asm - Convert a stream of assembly to a cell.
  67. Enums:
  68. -
  69. Macros:
  70. -
  71. Tags:
  72. -
  73. Variables:
  74. Global:
  75. -
  76. Static:
  77. YSI_g_sWriteMem - The assembly.
  78. Commands:
  79. -
  80. Compile options:
  81. -
  82. Operators:
  83. -
  84. \**--------------------------------------------------------------------------**/
  85. #if defined _INC_y_writemem
  86. #endinput
  87. #endif
  88. #define _INC_y_writemem
  89. #include <a_samp>
  90. #include "..\YSI_Storage\y_amx"
  91. #include "..\YSI_Coding\y_hooks"
  92. #include "..\YSI_Core\y_utils"
  93. #tryinclude "..\amx\windows\import_table"
  94. #tryinclude <amx_assembly\windows\import_table>
  95. #tryinclude "..\amx_assembly\windows\import_table"
  96. #tryinclude "..\..\amx_assembly\windows\import_table"
  97. forward WriteMem(addr, value);
  98. #define asm(%0,%1,%2,%3) ((0x%0<<0)|(0x%1<<8)|(0x%2<<16)|(0x%3<<24))
  99. /*
  100. cell AMX_NATIVE_CALL
  101. n_WriteMem(AMX * amx, cell * params)
  102. {
  103. if (params[0] == 8)
  104. {
  105. DWORD
  106. oldp;
  107. VirtualProtect((cell *)params[1], 4, PAGE_EXECUTE_READWRITE, &oldp);
  108. *((cell *)params[1]) = params[2];
  109. }
  110. return 0;
  111. }
  112. BECOMES:
  113. align 16
  114. push ebp
  115. mov ebp, esp
  116. push esi
  117. mov esi, [ebp+12]
  118. cmp dword ptr [esi], 8
  119. jnz short loc_ret
  120. mov ecx, [esi+4]
  121. lea eax, [ebp+12]
  122. push eax
  123. push 40h
  124. push 4
  125. push ecx
  126. call ds:__imp__VirtualProtect@16 ; LOOK THIS UP FIRST.
  127. mov edx, [esi+4]
  128. mov eax, [esi+8]
  129. mov [edx], eax
  130. loc_ret:
  131. xor eax, eax
  132. pop esi
  133. pop ebp
  134. retn
  135. */
  136. static
  137. YSI_g_sWriteMem[] =
  138. {
  139. asm(CC,CC,CC,CC), asm(CC,CC,CC,CC), asm(CC,CC,CC,CC), asm(CC,CC,CC,CC),
  140. asm(55,8B,EC,56), asm(8B,75,0C,83), asm(3E,08,75,1A), asm(8B,4E,04,8D),
  141. //asm(45,0C,50,6A), asm(40,6A,04,51), asm(FF,15,8C,11), asm(4A,00,8B,56),
  142. asm(45,0C,50,6A), asm(40,6A,04,51), asm(FF,15,00,00), asm(00,00,8B,56),
  143. asm(04,8B,46,08), asm(89,02,33,C0), asm(5E,5D,C3,CC)
  144. },
  145. YSI_g_iWriteMemAddr = 0;
  146. stock WriteMem(addr, value)
  147. {
  148. static ptr = -1;
  149. // Push addr and value.
  150. #emit PUSH.S value
  151. #emit PUSH.S addr
  152. #emit PUSH.C 8
  153. if (ptr == -1)
  154. {
  155. // ptr = COD + CIP - DAT + <distance to nop #1>
  156. // Modified from code by Zeex_.
  157. #emit LCTRL 6 // CIP
  158. #emit LOAD.alt AMX_HEADER_COD
  159. #emit ADD
  160. #emit ADD.C 84
  161. #emit STOR.pri ptr
  162. // NOP #1 = SYSREQ.D
  163. #emit CONST.pri 135
  164. #emit SREF.pri ptr
  165. // ptr += 4
  166. #emit LOAD.pri ptr
  167. #emit ADD.C 4
  168. #emit STOR.pri ptr
  169. // NOP #2 = address
  170. #emit LOAD.pri YSI_g_iWriteMemAddr
  171. #emit SREF.pri ptr
  172. }
  173. // Reserve space for SYSREQ.D WriteMem.
  174. #emit NOP
  175. #emit NOP
  176. // Pop native arguments.
  177. #emit STACK 12
  178. }
  179. static WM_Shift(from, to, data[], len = sizeof (data))
  180. {
  181. if (FALSE)
  182. {
  183. WriteMem(0, 0);
  184. }
  185. while (from < len)
  186. {
  187. data[to++] = data[from++];
  188. }
  189. }
  190. hook OnScriptInit()
  191. {
  192. // 00 4A 61 98
  193. new
  194. addr = GetImportPointer("VirtualProtect");
  195. #emit LOAD.S.alt addr
  196. // Get the offset to the long call address.
  197. #emit CONST.pri YSI_g_sWriteMem
  198. #emit ADD.C 42
  199. #emit STOR.S.pri addr
  200. // Write the real address.
  201. #emit SREF.S.alt addr
  202. // Align the code to a 16-byte boundary.
  203. addr = AMX_GetGlobalAddress(YSI_g_sWriteMem);
  204. switch (addr & 15)
  205. {
  206. case 0:
  207. {
  208. WM_Shift(4, 0, YSI_g_sWriteMem);
  209. }
  210. case 4:
  211. {
  212. WM_Shift(4, 3, YSI_g_sWriteMem);
  213. addr += 12;
  214. }
  215. case 8:
  216. {
  217. WM_Shift(4, 2, YSI_g_sWriteMem);
  218. addr += 8;
  219. }
  220. case 12:
  221. {
  222. WM_Shift(4, 1, YSI_g_sWriteMem);
  223. addr += 4;
  224. }
  225. default:
  226. {
  227. P:E("Cannot relocate YSI_g_sWriteMem");
  228. }
  229. }
  230. YSI_g_iWriteMemAddr = addr;
  231. return 1;
  232. }