Advertisement
Imperious123

Untitled

Jun 19th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.83 KB | None | 0 0
  1. #pragma once
  2. #include "retcheck.h"
  3. #include <Windows.h>
  4. #include "globals.h"
  5. #include "memory.h"
  6. extern "C" {
  7. #include "Lua\lua.h"
  8. #include "Lua\lapi.h"
  9. #include "Lua\lualib.h"
  10. #include "Lua\lstate.h"
  11. #include "Lua\lauxlib.h"
  12. #include "Lua\luaconf.h"
  13. #include "Lua\llimits.h"
  14. #include "Lua\lapi.h"
  15. #include "Lua\lfunc.h"
  16. #include "Lua\lobject.h"
  17. }
  18. #define r_lua_tostring(rL,i) r_lua_tolstring(rL, (i), NULL)
  19. #define r_lua_pop(rL,n) r_lua_settop(rL, -(n)-1)
  20. #define r_lua_getglobal(rL,s) r_lua_getfield(rL, LUA_GLOBALSINDEX, (s))
  21. #define r_lua_newtable(rL) r_lua_createtable(rL, 0, 0)
  22. #define LIMPL_ERR(s) r_lua_pushnil(m_rL); r_lua_pushstring(m_rL, s);
  23. #define LIMPL_ERR_RTN(s) LIMPL_ERR(s) return 2;
  24. int ContentProvider;
  25. DWORD ADDRESS_PCALL = x(0x826fc0);
  26. DWORD ADDRESS_TOUSERDATA = x(0x8289e0);
  27. typedef void*(__cdecl* rGetTable)(DWORD rL, int index);
  28. auto r_luad_pcall = (int(__cdecl*)(DWORD, int, int, int))Retcheck::Unprotect(ADDRESS_PCALL, true);
  29. auto r_lua_touserdata = (void*(__cdecl*)(DWORD, int))Retcheck::Unprotect(ADDRESS_TOUSERDATA, true);
  30.  
  31. DWORD unprotect(DWORD addr)
  32. {
  33. BYTE* tAddr = (BYTE*)addr;
  34. do
  35. {
  36. tAddr += 16;
  37. } while (!(tAddr[0] == 0x55 && tAddr[1] == 0x8B && tAddr[2] == 0xEC));
  38.  
  39. DWORD funcSz = tAddr - (BYTE*)addr;
  40.  
  41. PVOID nFunc = VirtualAlloc(NULL, funcSz, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
  42. if (nFunc == NULL)
  43. return addr;
  44.  
  45. memcpy(nFunc, (void*)addr, funcSz);
  46.  
  47. BYTE* pos = (BYTE*)nFunc;
  48. BOOL valid = false;
  49. do
  50. {
  51. if (pos[0] == 0x72 && pos[2] == 0xA1 && pos[7] == 0x8B) {
  52. *(BYTE*)pos = 0xEB;
  53.  
  54. DWORD cByte = (DWORD)nFunc;
  55. do
  56. {
  57. if (*(BYTE*)cByte == 0xE8)
  58. {
  59. DWORD oFuncPos = addr + (cByte - (DWORD)nFunc);
  60. DWORD oFuncAddr = (oFuncPos + *(DWORD*)(oFuncPos + 1)) + 5;
  61.  
  62. if (oFuncAddr % 16 == 0)
  63. {
  64. DWORD relativeAddr = oFuncAddr - cByte - 5;
  65. *(DWORD*)(cByte + 1) = relativeAddr;
  66.  
  67. cByte += 4;
  68. }
  69. }
  70.  
  71. cByte += 1;
  72. } while (cByte - (DWORD)nFunc < funcSz);
  73.  
  74. valid = true;
  75. }
  76. pos += 1;
  77. } while ((DWORD)pos < (DWORD)nFunc + funcSz);
  78.  
  79. if (!valid)
  80. {
  81. VirtualFree(nFunc, funcSz, MEM_RELEASE);
  82. return addr;
  83. }
  84.  
  85. return (DWORD)nFunc;
  86. }
  87. int top = 8, base = 12;
  88. typedef DWORD _DWORD;
  89.  
  90. typedef int(__fastcall*rLuaS_newlstr)(int RL, const char *str, size_t l);
  91. rLuaS_newlstr r_luaS_newlstr = (rLuaS_newlstr)x(0x821730);
  92.  
  93. union r_Value
  94. {
  95. int b;
  96. double n;
  97. void* p;
  98. void* gc;
  99. };
  100.  
  101. struct r_TValue
  102. {
  103. r_Value value;
  104. int tt;
  105. };
  106.  
  107. typedef r_TValue*(__cdecl*rlua_index2adr)(int lst, int idx);
  108. rlua_index2adr r_lua_index2adr = (rlua_index2adr)x(0x81d2c0);
  109.  
  110. void r_lua_pushnil(DWORD a1) {
  111. *(_DWORD *)(*(_DWORD *)(a1 + top) + 8) = 0;
  112. *(_DWORD *)(a1 + top) += 16;
  113. }
  114.  
  115. void r_lua_pushboolean(DWORD a1, int a2) {
  116. _DWORD *v2;
  117. v2 = *(_DWORD **)(a1 + top);
  118. *v2 = a2 != 0;
  119. v2[2] = 3;
  120. *(_DWORD *)(a1 + top) += 16;
  121. }
  122.  
  123. void r_lua_pushstring(int rState, const char *s) {
  124. int v3 = *(DWORD *)(rState + top);
  125. *(DWORD *)v3 = r_luaS_newlstr(rState, s, strlen(s));
  126. *(DWORD *)(v3 + 8) = R_LUA_TSTRING;
  127. *(DWORD *)(rState + top) += 16;
  128. }
  129.  
  130. void r_lua_settop(int a1, int a2) {
  131. int i;
  132. if (a2 < 0)
  133. {
  134. *(_DWORD *)(a1 + top) += 16 * a2 + 16;
  135. }
  136. else
  137. {
  138. for (i = 16 * a2; *(_DWORD *)(a1 + top) < (unsigned int)(i + *(_DWORD *)(a1 + base)); *(_DWORD *)(a1 + top) += 16)
  139. *(_DWORD *)(*(_DWORD *)(a1 + top) + 8) = 0;
  140. *(_DWORD *)(a1 + top) = i + *(_DWORD *)(a1 + base);
  141. }
  142. }
  143.  
  144. void r_lua_pushvalue(DWORD a1, int a2)
  145. {
  146. r_TValue *p = r_lua_index2adr(a1, a2);
  147. int v3 = *(DWORD *)(a1 + top);
  148. *(DWORD *)v3 = *(DWORD *)p;
  149. *(DWORD *)(v3 + 4) = *(DWORD *)(p + 4);
  150. *(DWORD *)(v3 + 8) = p->tt;
  151. *(DWORD *)(a1 + top) += 16;
  152. }
  153.  
  154. int r_lua_gettop(DWORD a1)
  155. {
  156. return (*(_DWORD *)(a1 + top) - *(_DWORD *)(a1 + base)) >> 4;
  157. }
  158.  
  159. typedef void(__fastcall* rgetfield)(DWORD rL, int idx, const char *k);
  160. rgetfield r_lua_getfield = (rgetfield)unprotect(x(0x826140));
  161.  
  162. typedef char*(__stdcall* rtolstring)(DWORD rL, int idx, size_t *size);
  163. rtolstring r_lua_tolstring = (rtolstring)(unprotect(x(0x828770)));
  164.  
  165. typedef bool(__cdecl* toboolean)(DWORD rL, bool idx);
  166. toboolean r_lua_toboolean = (toboolean)(x(0x8286b0));
  167.  
  168. typedef double(__cdecl* pushnumber)(DWORD rL, double idx);
  169. pushnumber r_lua_pushnumber = (pushnumber)(unprotect(x(0x827500)));
  170.  
  171. typedef DWORD(__cdecl* next2)(DWORD rL, int idx);
  172. next2 r_lua_next = (next2)(unprotect(x(0x826e40)));
  173.  
  174. typedef double(__cdecl* rtonumber)(DWORD, int);
  175. rtonumber r_lua_tonumber = (rtonumber)(x(0x8288b0));
  176.  
  177. typedef void(__cdecl* rpushcclosure)(DWORD rL, int fn, int non);
  178. rpushcclosure r_lua_pushcclosure = (rpushcclosure)(unprotect(x(0x827110)));
  179.  
  180. typedef void(__cdecl *rcreatetable)(DWORD rL, int num, int fix);
  181. rcreatetable r_lua_createtable = (rcreatetable)(unprotect(x(0x825dd0)));
  182.  
  183. typedef void*(__cdecl *rnewuserdata)(DWORD rL, int idx);
  184. rnewuserdata r_lua_newuserdata = (rnewuserdata)(unprotect(x(0x826d80)));
  185.  
  186. typedef void*(__fastcall*rgetmetatable)(DWORD rL, int idx);
  187. rgetmetatable r_lua_getmetatable = (rgetmetatable)(unprotect(x(0x8264e0)));
  188.  
  189. typedef void*(__cdecl*rsetmetatable)(DWORD rL, int idx);
  190. rsetmetatable r_lua_setmetatable = (rsetmetatable)(unprotect(x(0x828270)));
  191.  
  192. typedef void*(__fastcall *rsetfield)(DWORD rL, int idx);
  193. rsetfield r_lua_setfield = (rsetfield)(unprotect(x(0x827ff0)));
  194.  
  195. typedef void*(__cdecl *rsettable)(DWORD rL, int);
  196. rsettable r_lua_settable = (rsettable)(unprotect(x(0x8284a0)));
  197.  
  198. typedef DWORD(__cdecl *rtype)(DWORD, int);
  199. rtype r_lua_type = (rtype)(x(0x828a10));
  200.  
  201. typedef DWORD(__cdecl *rnewthread)(DWORD);
  202. rnewthread r_lua_newthread = (rnewthread)unprotect(x(0x826c90));
  203.  
  204. typedef DWORD(__cdecl *rref)(DWORD, DWORD);
  205. #include "retcheck.h"
  206. rref r_luaL_ref = (rref)(Ret::unprotect<DWORD>((byte*)(x(0x820d40))));
  207.  
  208. typedef void(__cdecl *rrawgeti)(DWORD, DWORD, DWORD);
  209. rrawgeti r_lua_rawgeti = (rrawgeti)unprotect(x(0x827920));
  210.  
  211. typedef void(__cdecl *rpushlight)(DWORD, void*);
  212. rpushlight r_lua_pushlightuserdata = (rpushlight)(unprotect(x(0x827370)));
  213.  
  214. void fakeChain(DWORD* chain)
  215. {
  216. chain[1] = 0x1555555;
  217. if ((((DWORD*)chain[0])[1]) != NULL) {
  218. ((DWORD*)chain[0])[1] = 0x1555555;
  219. }
  220. }
  221. void restoreChain(DWORD* chain, DWORD unk, DWORD nextUnk)
  222. {
  223. chain[1] = unk;
  224. if ((((DWORD*)chain[0])[1]) != NULL) {
  225. ((DWORD*)chain[0])[1] = nextUnk;
  226. }
  227. }
  228.  
  229. int r_lua_pcall(DWORD rL, int nargs, int nresults, int errfunc)
  230. {
  231. DWORD* exceptionChain = (DWORD*)__readfsdword(0);
  232. DWORD unk = exceptionChain[1];
  233. ((((DWORD*)exceptionChain[0])[1]) != NULL);
  234. {
  235. DWORD nextUnk = ((DWORD*)exceptionChain[0])[1];
  236. fakeChain(exceptionChain);
  237. int ret = r_luad_pcall(rL, nargs, nresults, errfunc);
  238. restoreChain(exceptionChain, unk, nextUnk);
  239. return ret;
  240. }
  241.  
  242. fakeChain(exceptionChain);
  243. int ret = r_luad_pcall(rL, nargs, nresults, errfunc);
  244. restoreChain(exceptionChain, unk, 0);
  245. return ret;
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement