Advertisement
Guest User

Bridge

a guest
Oct 15th, 2019
977
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.67 KB | None | 0 0
  1. int registry;
  2.  
  3. namespace Bridge
  4. {
  5.     DWORD RLS;
  6.     lua_State* LS;
  7.  
  8.     void unwrap(lua_State* L, DWORD rL, int index);
  9.     void wrap(DWORD rL, lua_State* L, int index);
  10.  
  11.     int rbxFunctionBridge(DWORD rL);
  12.     int vanillaFunctionBridge(lua_State* L);
  13.     int VanillaClosureHandler(lua_State* L);
  14.     LONG WINAPI vehHandler(PEXCEPTION_POINTERS ex);
  15.     VOID VehHandlerpush();
  16.     std::vector<int> int3breakpoints;
  17.     int resumea(DWORD thread);
  18. }
  19.  
  20. DWORD hookStateIndex(DWORD hooklocation, int offset)
  21. {
  22.     DWORD* context = reinterpret_cast<DWORD*>(hooklocation);
  23.     return context[offset] - (DWORD)&context[offset];
  24. }
  25. struct Userdata
  26. {
  27.     int32_t reference;
  28. };
  29.  
  30. struct DataModelUserData {
  31.     int ref;
  32. };
  33.  
  34.  
  35. //Instance caching start
  36. void cacheObject(uintptr_t rL, lua_State* L, int index)
  37. {
  38.     uintptr_t rawInstancePtr = rlua_touserdata(rL, index);
  39.  
  40.     lua_rawgeti(L, LUA_REGISTRYINDEX, rawInstancePtr);
  41.  
  42.     if (!lua_type(L, -1))
  43.     {
  44.         lua_settop(L, -2);
  45.  
  46.         r_lua_pushvalue(rL, index);
  47.         reinterpret_cast<Userdata*>(lua_newuserdata(L, sizeof(Userdata)))->reference = r_luaL_ref(rL, -10000);
  48.  
  49.         r_lua_getmetatable(rL, index);
  50.         Bridge::wrap(rL, L, -1);
  51.         lua_setmetatable(L, -2);
  52.  
  53.         lua_pushvalue(L, -1);
  54.         lua_rawseti(L, -10000, rawInstancePtr);
  55.  
  56.         r_lua_settop(rL, -2);
  57.     }
  58. }
  59. namespace Bridge
  60. {
  61.  
  62.     LONG WINAPI vehHandler(PEXCEPTION_POINTERS ex)
  63.     {
  64.         switch (ex->ExceptionRecord->ExceptionCode)
  65.         {
  66.         case (DWORD)0x80000003L:
  67.         {
  68.             if (ex->ContextRecord->Eip == int3breakpoints[0])
  69.             {
  70.                 ex->ContextRecord->Eip = (DWORD)(Bridge::rbxFunctionBridge);
  71.                 return EXCEPTION_CONTINUE_EXECUTION;
  72.             }
  73.  
  74.             if (ex->ContextRecord->Eip == int3breakpoints[1])
  75.             {
  76.                 ex->ContextRecord->Eip = (DWORD)(Bridge::resumea);
  77.                 return EXCEPTION_CONTINUE_EXECUTION;
  78.             }
  79.             return -1;
  80.         }
  81.         default: return 0;
  82.         }
  83.         return 0;
  84.     }
  85.  
  86.     DWORD locateINT3() {
  87.         DWORD _s = x(0x400000);
  88.         const char i3_8opcode[8] = { 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC };
  89.         for (int i = 0; i < MAX_INT; i++) {
  90.             if (memcmp((void*)(_s + i), i3_8opcode, sizeof(i3_8opcode)) == 0) {
  91.                 return (_s + i);
  92.             }
  93.         }
  94.         return NULL;
  95.     }
  96.  
  97.     VOID VehHandlerpush()
  98.     {
  99.         int3breakpoints.push_back(locateINT3());
  100.         int3breakpoints.push_back(locateINT3());
  101.         AddVectoredExceptionHandler(1, vehHandler);
  102.     }
  103.  
  104.     void unwrap(lua_State* L, DWORD rL, int index)
  105.     {
  106.         //printf("ROBLOX: %d\n", lua_type(L, index));
  107.         switch (lua_type(L, index))
  108.         {
  109.         case LUA_TLIGHTUSERDATA:
  110.        
  111.             r_lua_pushlightuserdata(rL, nullptr);
  112.             break;
  113.         case LUA_TNIL:
  114.    
  115.             r_lua_pushnil(rL);
  116.             break;
  117.         case LUA_TNUMBER:
  118.            
  119.             r_lua_pushnumber(rL, lua_tonumber(L, index));
  120.             break;
  121.         case LUA_TBOOLEAN:
  122.        
  123.             r_lua_pushboolean(rL, lua_toboolean(L, index));
  124.             break;
  125.         case LUA_TSTRING:
  126.  
  127.             r_lua_pushstring(rL, lua_tostring(L, index));
  128.             break;
  129.         case LUA_TTHREAD:
  130.        
  131.             r_lua_newthread(rL);
  132.             break;
  133.         case LUA_TFUNCTION:
  134.        
  135.             lua_pushvalue(L, index);
  136.             r_lua_pushnumber(rL, luaL_ref(L, LUA_REGISTRYINDEX));
  137.             r_lua_pushcclosure(rL, Bridge::int3breakpoints[0], 1);
  138.             break;
  139.         case LUA_TTABLE:
  140.    
  141.             lua_pushvalue(L, index);
  142.             r_lua_newtable(rL);
  143.             lua_pushnil(L);
  144.             while (lua_next(L, -2) != LUA_TNIL)
  145.             {
  146.                 Bridge::unwrap(L, rL, -2);
  147.                 Bridge::unwrap(L, rL, -1);
  148.                 r_lua_settable(rL, -3);
  149.                 lua_pop(L, 1);
  150.             }
  151.             lua_pop(L, 1);
  152.             break;
  153.         case LUA_TUSERDATA:
  154.        
  155.             r_lua_rawgeti(rL, LUA_REGISTRYINDEX, reinterpret_cast<DataModelUserData*>(lua_touserdata(L, index))->ref);
  156.             break;
  157.         default: break;
  158.         }
  159.     }
  160.  
  161.     void wrap(DWORD rL, lua_State* L, int index)
  162.     {
  163.         //printf("VANILLA: %d\r\n", r_lua_type(rL, index));
  164.         switch (r_lua_type(rL, index))
  165.         {
  166.         case R_LUA_TLIGHTUSERDATA:
  167.        
  168.             lua_pushlightuserdata(L, nullptr);
  169.             break;
  170.         case R_LUA_TNIL:
  171.  
  172.             lua_pushnil(L);
  173.             break;
  174.         case R_LUA_TNUMBER:
  175.            
  176.             lua_pushnumber(L, r_lua_tonumber(rL, index));
  177.             break;
  178.         case R_LUA_TBOOLEAN:
  179.        
  180.             lua_pushboolean(L, r_lua_toboolean(rL, index));
  181.             break;
  182.         case R_LUA_TSTRING:
  183.        
  184.             lua_pushstring(L, r_lua_tostring(rL, index));
  185.             break;
  186.         case R_LUA_TTHREAD:
  187.            
  188.             lua_newthread(L);
  189.             break;
  190.         case R_LUA_TFUNCTION:
  191.            
  192.             r_lua_pushvalue(rL, index);
  193.             lua_pushnumber(L, r_luaL_ref(rL, LUA_REGISTRYINDEX));
  194.             lua_pushcclosure(L, vanillaFunctionBridge, 1);
  195.             break;
  196.         case R_LUA_TTABLE:
  197.        
  198.             r_lua_pushvalue(rL, index);
  199.             lua_newtable(L);
  200.             r_lua_pushnil(rL);
  201.             while (r_lua_next(rL, -2) != R_LUA_TNIL)
  202.             {
  203.                 Bridge::wrap(rL, L, -2);
  204.                 Bridge::wrap(rL, L, -1);
  205.                 lua_settable(L, -3);
  206.                 r_lua_pop(rL, 1);
  207.             }
  208.             r_lua_pop(rL, 1);
  209.             break;
  210.         case R_LUA_TUSERDATA:
  211.             cacheObject(rL, L, index);
  212.             break;
  213.         default: break;
  214.         }
  215.     }
  216.  
  217.     static int resume(lua_State* thread)
  218.     {
  219.         lua_State* L = lua_tothread(thread, lua_upvalueindex(1));
  220.         const int nargs = lua_gettop(thread);
  221.         lua_xmove(thread, L, nargs);
  222.         return lua_resume(L, nargs);
  223.         lua_newtable(L);
  224.         lua_pushstring(L, "This metatable is locked");
  225.         lua_setfield(L, -2, "__metatable");
  226.         lua_close(L);
  227.  
  228.     }
  229.  
  230.     int resumea(DWORD thread)
  231.     {
  232.         lua_State* L = (lua_State*)r_lua_touserdata(thread, lua_upvalueindex(1));
  233.         const int nargs = r_lua_gettop(thread);
  234.         for (int arg = 1; arg <= nargs; ++arg)
  235.             Bridge::wrap(thread, L, arg);
  236.         return lua_resume(L, nargs);
  237.     }
  238.  
  239.  
  240.     int vanillaFunctionBridge(lua_State* L)
  241.     {
  242.         r_lua_pushstring(RLS, std::to_string(++registry).c_str());
  243.         DWORD rL = r_lua_newthread(RLS);
  244.         r_lua_settable(RLS, LUA_REGISTRYINDEX);
  245.  
  246.         int key = lua_tonumber(L, lua_upvalueindex(1));
  247.  
  248.         r_lua_rawgeti(rL, LUA_REGISTRYINDEX, key);
  249.  
  250.         for (int arg = 1; arg <= lua_gettop(L); ++arg)
  251.             Bridge::unwrap(L, rL, arg);
  252.  
  253.         if (r_lua_pcall(rL, lua_gettop(L), LUA_MULTRET, 0))
  254.         {
  255.             const char* errormessage = r_lua_tostring(rL, -1, 0);
  256.  
  257.             if (!errormessage || strlen(errormessage) == 0)
  258.                 errormessage = "Error occoured, no output from Lua\n";
  259.  
  260.             if (strcmp(errormessage, "attempt to yield across metamethod/C-call boundary") == 0)
  261.             {
  262.  
  263.                 r_lua_pop(rL, 1);
  264.                 lua_pushthread(L);
  265.                 lua_pushcclosure(L, &resume, 1);
  266.                 Bridge::unwrap(L, rL, -1);
  267.  
  268.                 return lua_yield(L, 0);
  269.             }
  270.            
  271.             return 0;
  272.        
  273.             delete[] errormessage;
  274.         }
  275.  
  276.         int args = 0;
  277.  
  278.         for (int arg = 1; arg <= r_lua_gettop(rL); ++arg, ++args)
  279.             Bridge::wrap(rL, L, arg);
  280.  
  281.         r_lua_settop(rL, 0);
  282.  
  283.         return args;
  284.         lua_close(L);
  285.     }
  286.  
  287.     int rbxFunctionBridge(DWORD rL)
  288.     {
  289.  
  290.         lua_pushstring(LS, std::to_string(++registry).c_str());
  291.         lua_State* L = lua_newthread(LS);
  292.         lua_settable(LS, LUA_REGISTRYINDEX);
  293.  
  294.         int key = r_lua_tonumber(rL, lua_upvalueindex(1));
  295.  
  296.         lua_rawgeti(L, LUA_REGISTRYINDEX, key);
  297.  
  298.         for (int arg = 1; arg <= r_lua_gettop(rL); ++arg)
  299.             Bridge::wrap(rL, L, arg);
  300.  
  301.         switch (lua_pcall(L, r_lua_gettop(rL), LUA_MULTRET, 0))
  302.         {
  303.  
  304.  
  305.         case LUA_YIELD:
  306.  
  307.             r_lua_pushlightuserdata(RLS, (void*)L);
  308.             r_lua_pushcclosure(RLS, Bridge::int3breakpoints[1], 1);
  309.             return -1;
  310.         case LUA_ERRRUN:
  311.    
  312.             return -1;
  313.         default: break;
  314.         }
  315.  
  316.         int args = 0;
  317.  
  318.         for (int arg = 1; arg <= lua_gettop(L); ++arg, ++args)
  319.             Bridge::unwrap(L, rL, arg);
  320.  
  321.         lua_settop(L, 0);
  322.  
  323.         return args;
  324.         lua_close(L);
  325.     }
  326. }
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339. namespace UnisonHomo{
  340.  
  341.     void PushGlobal(DWORD rL, lua_State* L, const char* s)
  342.     {
  343.         r_lua_getglobal(rL, s);
  344.         Bridge::wrap(rL, L, -1);
  345.         lua_setglobal(L, s);
  346.         r_lua_pop(rL, 1);
  347.     }
  348.  
  349.     void InitWrapper(DWORD RLS, lua_State* LS) {
  350.    
  351.         PushGlobal(RLS, LS, "printidentity");
  352.         PushGlobal(RLS, LS, "game");
  353.         PushGlobal(RLS, LS, "Game");
  354.         PushGlobal(RLS, LS, "workspace");
  355.         PushGlobal(RLS, LS, "Workspace");
  356.         PushGlobal(RLS, LS, "Axes");
  357.         PushGlobal(RLS, LS, "BrickColor");
  358.         PushGlobal(RLS, LS, "CFrame");
  359.         PushGlobal(RLS, LS, "Color3");
  360.         PushGlobal(RLS, LS, "ColorSequence");
  361.         PushGlobal(RLS, LS, "ColorSequenceKeypoint");
  362.         PushGlobal(RLS, LS, "NumberRange");
  363.         PushGlobal(RLS, LS, "NumberSequence");
  364.         PushGlobal(RLS, LS, "NumberSequenceKeypoint");
  365.         PushGlobal(RLS, LS, "PhysicalProperties");
  366.         PushGlobal(RLS, LS, "Ray");
  367.         PushGlobal(RLS, LS, "Rect");
  368.         PushGlobal(RLS, LS, "Region3");
  369.         PushGlobal(RLS, LS, "Region3int16");
  370.         PushGlobal(RLS, LS, "TweenInfo");
  371.         PushGlobal(RLS, LS, "UDim");
  372.         PushGlobal(RLS, LS, "UDim2");
  373.         PushGlobal(RLS, LS, "Vector2");
  374.         PushGlobal(RLS, LS, "Vector2int16");
  375.         PushGlobal(RLS, LS, "Vector3");
  376.         PushGlobal(RLS, LS, "Vector3int16");
  377.         PushGlobal(RLS, LS, "Enum");
  378.         PushGlobal(RLS, LS, "Faces");
  379.         PushGlobal(RLS, LS, "Instance");
  380.         PushGlobal(RLS, LS, "math");
  381.         PushGlobal(RLS, LS, "warn");
  382.         PushGlobal(RLS, LS, "typeof");
  383.         PushGlobal(RLS, LS, "type");
  384.         PushGlobal(RLS, LS, "spawn");
  385.         PushGlobal(RLS, LS, "Spawn");
  386.         PushGlobal(RLS, LS, "print");
  387.         PushGlobal(RLS, LS, "printidentity");
  388.         PushGlobal(RLS, LS, "ypcall");
  389.         PushGlobal(RLS, LS, "Wait");
  390.         PushGlobal(RLS, LS, "wait");
  391.         PushGlobal(RLS, LS, "delay");
  392.         PushGlobal(RLS, LS, "Delay");
  393.         PushGlobal(RLS, LS, "tick");
  394.         PushGlobal(RLS, LS, "LoadLibrary");
  395.     }
  396.  
  397.  
  398. }
  399.  
  400. using namespace Bridge;
  401. namespace Wrapper {
  402.  
  403.     std::string ExecuteStr;
  404.  
  405.     void Execute() {
  406.         std::string Script = ExecuteStr;
  407.         Script = "spawn(function() script = Instance.new('LocalScript')\r\n" + Script + "\r\nend)";
  408.  
  409.         if (Script.find(".Name = \"Dex\"") == std::string::npos) {
  410.             Script = ReplaceAll(Script, "game:GetObjects", "GetObjects");
  411.             Script = ReplaceAll(Script, "game:HttpGet", "HttpGet");
  412.         }
  413.         else {
  414.  
  415.         }
  416.  
  417.  
  418.  
  419.  
  420.         if (luaL_loadstring(LS, Script.c_str())) {
  421.             return;
  422.         }
  423.         else {
  424.             lua_pcall(LS, 0, 0, 0);
  425.         }
  426.     }
  427.  
  428. #define CRASH EXCEPTION_EXECUTE_HANDLER
  429.  
  430.     void SecureRun() {
  431.         __try { Execute(); }
  432.         __except (CRASH) {}
  433.     }
  434.     void Execute_Script(std::string Script) {
  435.         ExecuteStr = Script;
  436.         SecureRun();
  437.     }
  438.  
  439. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement