Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.80 KB | None | 0 0
  1. public object ExecuteCommand(short opcode, params object[] args)
  2.         {
  3.             /*
  4.              * SCM Memory Layout:
  5.              * <o> <t> <v>
  6.              *
  7.              * Where:
  8.              *  <o> - Opcode(short)
  9.              *  <t> - Type(byte)
  10.              *  <v> - Value(depends on <t>)
  11.              *  
  12.              * While using regular vars is very simple, usage of variables is a bit complicated.
  13.              * Remember that you passing NOT a absolute pointer to your variable, but index in global variable pointer to your variable, i.e
  14.              *
  15.              * Global variable table:
  16.              * <$PLAYER_CHAR> <$PLAYER_ACTOR> <Pointer to your variable> <$SOMETHING_OTHER>
  17.              *
  18.              * While it's simple to use original main.scm global variable table, it's not safe, so i've implemented another "temporary" globals table.
  19.              */
  20.  
  21.             ScriptCommand cmd = new ScriptCommand(opcode, args);
  22.             uint sz = (uint)(cmd.GetArgumentLength() + ScriptContextSize + sizeof(short));
  23.             int scmData = (int)Native.VirtualAllocEx(GTA.GetInstance().Process.Handle, IntPtr.Zero, sz, AllocationType.Commit, MemoryProtection.ReadWrite);
  24.             int scmHeap = (int)Native.VirtualAllocEx(GTA.GetInstance().Process.Handle, IntPtr.Zero, (uint)cmd.GetVariableLength(), AllocationType.Commit, MemoryProtection.ReadWrite);
  25.             int scmGlobalsTable = (int)Native.VirtualAllocEx(GTA.GetInstance().Process.Handle, IntPtr.Zero, (uint)(cmd.GetVariableCount() * 4), AllocationType.Commit, MemoryProtection.ReadWrite);
  26.             int argPtr = scmData + ScriptContextSize + 2;
  27.             int vCount = 0;
  28.             int heapPtr = scmHeap;
  29.             int globalPtr = scmHeap;
  30.  
  31.             if (scmData == 0 || scmHeap == 0)
  32.                 throw new ArgumentException(string.Format("Failed to allocate {0} bytes for SCM context", sz));
  33.  
  34.             Console.WriteLine("Allocated {0} bytes", sz);
  35.  
  36.             GTA.GetInstance().WriteMemory(scmData + 0x10, (argPtr - 2) - OffsetSCMBase);
  37.  
  38.             GTA.GetInstance().WriteMemory(argPtr - 2, (short)opcode);
  39.  
  40.             foreach (object arg in cmd.Arguments)
  41.             {
  42.                 if (arg is byte)
  43.                 {
  44.                     GTA.GetInstance().WriteMemory(argPtr, (byte)0x04);
  45.                     GTA.GetInstance().WriteMemory(argPtr + 1, arg);
  46.  
  47.                     argPtr += 2;
  48.  
  49.                     continue;
  50.                 }
  51.  
  52.                 if (arg is short)
  53.                 {
  54.                     GTA.GetInstance().WriteMemory(argPtr, (byte)0x05);
  55.                     GTA.GetInstance().WriteMemory(argPtr + 1, arg);
  56.  
  57.                     argPtr += 3;
  58.  
  59.                     continue;
  60.                 }
  61.  
  62.                 if (arg is int)
  63.                 {
  64.                     GTA.GetInstance().WriteMemory(argPtr, (byte)0x01);
  65.                     GTA.GetInstance().WriteMemory(argPtr + 1, arg);
  66.  
  67.                     argPtr += 5;
  68.  
  69.                     continue;
  70.                 }
  71.  
  72.                 if (arg is float)
  73.                 {
  74.                     GTA.GetInstance().WriteMemory(argPtr, (byte)0x06);
  75.                     GTA.GetInstance().WriteMemory(argPtr + 1, arg);
  76.  
  77.                     argPtr += 5;
  78.  
  79.                     continue;
  80.                 }
  81.  
  82.                 if (arg is ScriptResult)
  83.                 {
  84.                     GTA.GetInstance().WriteMemory(argPtr, (byte)0x02);
  85.                     GTA.GetInstance().WriteMemory(argPtr + 1, (int)((scmGlobalsTable - OffsetSCMBase) + (vCount * 4))); // Pointer to our variable in globals table
  86.  
  87.                     GTA.GetInstance().WriteMemory(globalPtr, heapPtr);
  88.  
  89.                     heapPtr += Marshal.SizeOf((arg as ScriptResult).ResultType);
  90.                     vCount++;
  91.                     argPtr += 5;
  92.                    
  93.                     continue;
  94.                 }
  95.  
  96.                 throw new ArgumentException(string.Format("Incorrect type specified \"{0}\" in ExecuteCommand(only byte, short, int and float is supported)", arg.GetType().FullName));
  97.             }
  98.  
  99.             heapPtr = scmHeap;
  100.             //vPtr = scmHeap - OffsetSCMBase;
  101.  
  102.             new Shellcode().
  103.                 Append("mov ecx, " + scmData).
  104.                 Append("mov edx, " + OffsetExecuteCommand).
  105.                 Append("call edx").
  106.                 Assemble().
  107.                 Execute();
  108.  
  109.             // Collect result
  110.             foreach (object arg in cmd.Arguments)
  111.             {
  112.                 if (arg is ScriptResult)
  113.                 {
  114.                     ScriptResult res = (arg as ScriptResult);
  115.  
  116.                     res.Data = GTA.GetInstance().ReadMemory(heapPtr, res.ResultType);
  117.                     heapPtr += Marshal.SizeOf(res.ResultType);
  118.                 }
  119.             }
  120.  
  121.             return null;
  122.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement