Y_Less

y_writemem.inc

Feb 4th, 2012
746
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 5.11 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.         Zeex_: Changed to use SYSREQ.D call not SYSREQ.C.
  55.         Zeex_: Modified "VirtualProtect" pointer to SA:MP server one.
  56.         Added "VirtualProtect" calls to the code.
  57.         First version.
  58. Functions:
  59.     Public:
  60.         -
  61.     Core:
  62.         -
  63.     Stock:
  64.         -
  65.     Static:
  66.         -
  67.     Inline:
  68.         -
  69.     API:
  70.         WriteMem - Write data to an address.
  71. Callbacks:
  72.     -
  73. Definitions:
  74.     asm - Convert a stream of assembly to a cell.
  75. Enums:
  76.     -
  77. Macros:
  78.     -
  79. Tags:
  80.     -
  81. Variables:
  82.     Global:
  83.         -
  84.     Static:
  85.         YSI_g_sWriteMem - The assembly.
  86. Commands:
  87.     -
  88. Compile options:
  89.     -
  90. Operators:
  91.     -
  92. \*----------------------------------------------------------------------------*/
  93.  
  94. #include <a_samp>
  95. #include <YSI\y_amx>
  96. #include <YSI\y_hooks>
  97. #include <YSI\y_utils>
  98.  
  99. forward WriteMem(addr, value);
  100.  
  101. #define asm(%0,%1,%2,%3) ((0x%0<<0)|(0x%1<<8)|(0x%2<<16)|(0x%3<<24))
  102.  
  103. /*
  104. cell AMX_NATIVE_CALL
  105.     n_WriteMem(AMX * amx, cell * params)
  106. {
  107.     if (params[0] == 8)
  108.     {
  109.         DWORD
  110.             oldp;
  111.         VirtualProtect((cell *)params[1], 4, PAGE_EXECUTE_READWRITE, &oldp);
  112.         *((cell *)params[1]) = params[2];
  113.     }
  114.     return 0;
  115. }
  116.  
  117. BECOMES:
  118.  
  119.     align   16
  120.     push    ebp
  121.     mov     ebp, esp
  122.     push    esi
  123.     mov     esi, [ebp+12]
  124.     cmp     dword ptr [esi], 8
  125.     jnz     short loc_ret
  126.     mov     ecx, [esi+4]
  127.     lea     eax, [ebp+12]
  128.     push    eax
  129.     push    40h
  130.     push    4
  131.     push    ecx
  132.     call    ds:__imp__VirtualProtect@16
  133.     mov     edx, [esi+4]
  134.     mov     eax, [esi+8]
  135.     mov     [edx], eax
  136. loc_ret:
  137.     xor     eax, eax
  138.     pop     esi
  139.     pop     ebp
  140.     retn
  141. */
  142.  
  143. static
  144.     YSI_g_sWriteMem[] =
  145.     {
  146.         asm(CC,CC,CC,CC), asm(CC,CC,CC,CC), asm(CC,CC,CC,CC), asm(CC,CC,CC,CC),
  147.         asm(55,8B,EC,56), asm(8B,75,0C,83), asm(3E,08,75,1A), asm(8B,4E,04,8D),
  148.         asm(45,0C,50,6A), asm(40,6A,04,51), asm(FF,15,8C,11), asm(4A,00,8B,56),
  149.         asm(04,8B,46,08), asm(89,02,33,C0), asm(5E,5D,C3,CC)
  150.     };
  151.  
  152. static
  153.     YSI_g_iWriteMemAddr = 0;
  154.  
  155. stock
  156.     WriteMem(addr, value)
  157. {
  158.     static ptr = -1;
  159.    
  160.     // Push addr and value.
  161.     #emit PUSH.S value
  162.     #emit PUSH.S addr
  163.     #emit PUSH.C 8
  164.    
  165.     if (ptr == -1)
  166.     {
  167.         // ptr = COD + CIP - DAT + <distance to nop #1>
  168.         // Modified from code by Zeex_.
  169.         #emit LCTRL      6  // CIP
  170.         #emit LOAD.alt   AMX_HEADER_COD
  171.         #emit ADD
  172.         #emit ADD.C      84
  173.         #emit STOR.S.pri ptr
  174.        
  175.         // NOP #1 = SYSREQ.D
  176.         #emit CONST.pri  135
  177.         #emit SREF.S.pri ptr
  178.        
  179.         // ptr += 4
  180.         #emit LOAD.S.pri ptr
  181.         #emit ADD.C      4
  182.         #emit STOR.S.pri ptr
  183.        
  184.         // NOP #2 = address
  185.         #emit LOAD.pri   YSI_g_iWriteMemAddr
  186.         #emit SREF.S.pri ptr
  187.     }
  188.     // Reserve space for SYSREQ.D WriteMem.
  189.     #emit NOP
  190.     #emit NOP
  191.    
  192.     // Pop native arguments.
  193.     #emit STACK 12
  194. }
  195.  
  196. static
  197.     WM_Shift(from, to, data[], len = sizeof (data))
  198. {
  199.     if (FALSE)
  200.     {
  201.         WriteMem(0, 0);
  202.     }
  203.     while (from < len)
  204.     {
  205.         data[to++] = data[from++];
  206.     }
  207. }
  208.  
  209. hook OnScriptInit()
  210. {
  211.     new
  212.         addr = AMX_GetGlobalAddress(YSI_g_sWriteMem);
  213.     // Align the code to a 16-byte boundary.
  214.     switch (addr & 15)
  215.     {
  216.         case 0:
  217.         {
  218.             WM_Shift(4, 0, YSI_g_sWriteMem);
  219.         }
  220.         case 4:
  221.         {
  222.             WM_Shift(4, 3, YSI_g_sWriteMem);
  223.             addr += 12;
  224.         }
  225.         case 8:
  226.         {
  227.             WM_Shift(4, 2, YSI_g_sWriteMem);
  228.             addr += 8;
  229.         }
  230.         case 12:
  231.         {
  232.             WM_Shift(4, 1, YSI_g_sWriteMem);
  233.             addr += 4;
  234.         }
  235.         default:
  236.         {
  237.             P:E("Cannot relocate YSI_g_sWriteMem");
  238.         }
  239.     }
  240.     YSI_g_iWriteMemAddr = addr;
  241.     return 1;
  242. }
Advertisement
Add Comment
Please, Sign In to add comment