Guest User

y_writemem.inc

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