y_writemem.inc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #include "..\amx\windows\import_table"
  94. forward WriteMem(addr, value);
  95. #define asm(%0,%1,%2,%3) ((0x%0<<0)|(0x%1<<8)|(0x%2<<16)|(0x%3<<24))
  96. /*
  97. cell AMX_NATIVE_CALL
  98. n_WriteMem(AMX * amx, cell * params)
  99. {
  100. if (params[0] == 8)
  101. {
  102. DWORD
  103. oldp;
  104. VirtualProtect((cell *)params[1], 4, PAGE_EXECUTE_READWRITE, &oldp);
  105. *((cell *)params[1]) = params[2];
  106. }
  107. return 0;
  108. }
  109. BECOMES:
  110. align 16
  111. push ebp
  112. mov ebp, esp
  113. push esi
  114. mov esi, [ebp+12]
  115. cmp dword ptr [esi], 8
  116. jnz short loc_ret
  117. mov ecx, [esi+4]
  118. lea eax, [ebp+12]
  119. push eax
  120. push 40h
  121. push 4
  122. push ecx
  123. call ds:__imp__VirtualProtect@16 ; LOOK THIS UP FIRST.
  124. mov edx, [esi+4]
  125. mov eax, [esi+8]
  126. mov [edx], eax
  127. loc_ret:
  128. xor eax, eax
  129. pop esi
  130. pop ebp
  131. retn
  132. */
  133. static
  134. YSI_g_sWriteMem[] =
  135. {
  136. asm(CC,CC,CC,CC), asm(CC,CC,CC,CC), asm(CC,CC,CC,CC), asm(CC,CC,CC,CC),
  137. asm(55,8B,EC,56), asm(8B,75,0C,83), asm(3E,08,75,1A), asm(8B,4E,04,8D),
  138. //asm(45,0C,50,6A), asm(40,6A,04,51), asm(FF,15,8C,11), asm(4A,00,8B,56),
  139. asm(45,0C,50,6A), asm(40,6A,04,51), asm(FF,15,00,00), asm(00,00,8B,56),
  140. asm(04,8B,46,08), asm(89,02,33,C0), asm(5E,5D,C3,CC)
  141. },
  142. YSI_g_iWriteMemAddr = 0;
  143. stock WriteMem(addr, value)
  144. {
  145. static ptr = -1;
  146. // Push addr and value.
  147. #emit PUSH.S value
  148. #emit PUSH.S addr
  149. #emit PUSH.C 8
  150. if (ptr == -1)
  151. {
  152. // ptr = COD + CIP - DAT + <distance to nop #1>
  153. // Modified from code by Zeex_.
  154. #emit LCTRL 6 // CIP
  155. #emit LOAD.alt AMX_HEADER_COD
  156. #emit ADD
  157. #emit ADD.C 84
  158. #emit STOR.pri ptr
  159. // NOP #1 = SYSREQ.D
  160. #emit CONST.pri 135
  161. #emit SREF.pri ptr
  162. // ptr += 4
  163. #emit LOAD.pri ptr
  164. #emit ADD.C 4
  165. #emit STOR.pri ptr
  166. // NOP #2 = address
  167. #emit LOAD.pri YSI_g_iWriteMemAddr
  168. #emit SREF.pri ptr
  169. }
  170. // Reserve space for SYSREQ.D WriteMem.
  171. #emit NOP
  172. #emit NOP
  173. // Pop native arguments.
  174. #emit STACK 12
  175. }
  176. static WM_Shift(from, to, data[], len = sizeof (data))
  177. {
  178. if (FALSE)
  179. {
  180. WriteMem(0, 0);
  181. }
  182. while (from < len)
  183. {
  184. data[to++] = data[from++];
  185. }
  186. }
  187. hook OnScriptInit()
  188. {
  189. // 00 4A 61 98
  190. new
  191. addr = GetImportPointer("VirtualProtect");
  192. #emit LOAD.S.alt addr
  193. // Get the offset to the long call address.
  194. #emit CONST.pri YSI_g_sWriteMem
  195. #emit ADD.C 42
  196. #emit STOR.S.pri addr
  197. // Write the real address.
  198. #emit SREF.S.alt addr
  199. // Align the code to a 16-byte boundary.
  200. addr = AMX_GetGlobalAddress(YSI_g_sWriteMem);
  201. switch (addr & 15)
  202. {
  203. case 0:
  204. {
  205. WM_Shift(4, 0, YSI_g_sWriteMem);
  206. }
  207. case 4:
  208. {
  209. WM_Shift(4, 3, YSI_g_sWriteMem);
  210. addr += 12;
  211. }
  212. case 8:
  213. {
  214. WM_Shift(4, 2, YSI_g_sWriteMem);
  215. addr += 8;
  216. }
  217. case 12:
  218. {
  219. WM_Shift(4, 1, YSI_g_sWriteMem);
  220. addr += 4;
  221. }
  222. default:
  223. {
  224. P:E("Cannot relocate YSI_g_sWriteMem");
  225. }
  226. }
  227. YSI_g_iWriteMemAddr = addr;
  228. return 1;
  229. }