give me eglswapbuffer imgui hook for android game

Создано: 18 августа 2025 г.

Отвечено с использованием GPT-4o от Chat01

Вопрос

give me eglswapbuffer imgui hook for android game

Ответ

Hooking into eglSwapBuffers for an Android game to implement an ImGui overlay is a technique commonly used in game modding and reverse engineering. However, it requires low-level access, and it may violate the terms of service of the game or platform. Proceed only if you have legal permission to do so.

That said, here’s a general approach to hooking eglSwapBuffers and integrating ImGui on Android (especially via an injected shared library):


📌 Prerequisites

  • Rooted device or app with LD_PRELOAD injection
  • Android NDK
  • C/C++ knowledge
  • JNI experience
  • A working ImGui implementation (ported for OpenGL ES)
  • Frida (optional) or manual .so injection

🔧 Hooking eglSwapBuffers

Below is a simplified example using inline hooking via MHook, PLT hooking, or Dobby (alternatively use dlsym if you’re not using a hooking framework).

cpp
#include <EGL/egl.h> #include <GLES2/gl2.h> #include <dlfcn.h> typedef EGLBoolean (*eglSwapBuffers_t)(EGLDisplay display, EGLSurface surface); eglSwapBuffers_t orig_eglSwapBuffers = nullptr; EGLBoolean hooked_eglSwapBuffers(EGLDisplay display, EGLSurface surface) { static bool initialized = false; if (!initialized) { // Setup ImGui and OpenGL state here ImGui_ImplAndroid_Init(); ImGui_ImplOpenGL3_Init("#version 100"); initialized = true; } // Start new frame ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplAndroid_NewFrame(); ImGui::NewFrame(); // Your overlay UI here ImGui::Begin("Injected Overlay"); ImGui::Text("Hello from ImGui!"); ImGui::End(); // Render UI ImGui::Render(); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); // Call the original function return orig_eglSwapBuffers(display, surface); } __attribute__((constructor)) void lib_main() { void* handle = dlopen("libEGL.so", RTLD_LAZY); void* sym = dlsym(handle, "eglSwapBuffers"); if (sym) { orig_eglSwapBuffers = (eglSwapBuffers_t)sym; // Replace with a hooking framework of choice hook_function(sym, (void*)hooked_eglSwapBuffers, (void**)&orig_eglSwapBuffers); } }

🧩 ImGui Android Notes

  • Use the imgui + imgui_impl_android.cpp + imgui_impl_opengl3.cpp backend.
  • Initialize once during the first eglSwapBuffers call.
  • Ensure OpenGL context is current before rendering ImGui.

⚠️ Warnings

  • Hooking system libraries (e.g., libEGL.so) can break apps or crash games if not handled carefully.
  • Anti-cheat systems can detect hooks and ban your account.
  • Ensure ImGui rendering does not overwrite game buffers if it uses framebuffer rendering.
  • You must manage OpenGL state: save/restore if the target app uses custom GL setup.

🛠 Tools You Can Use

  • Dobby, Substrate, or Riru + LSPosed for hooking
  • ImGui port for GLES (OpenGL ES 2.0/3.0)
  • Frida for dynamic analysis

If you want a full project template (CMake + JNI + ImGui + hook setup), let me know — I can generate a boilerplate setup.

Do you have any of the following already set up?

  • Target architecture (armv7/arm64)?
  • Game uses GLES2 or GLES3?
  • Do you want to inject via Frida, Magisk, or custom loader?

Let me know and I can tailor the solution.

Вопрос

use lsposed for hooking

Ответ

Using LSPosed (based on Riru or Zygisk) to hook eglSwapBuffers and render ImGui overlays in an Android game is a powerful approach. Here's a full guide and code template to get you started.


🧩 Overview

You're going to:

  1. Build an LSPosed module (Zygisk or Riru based).
  2. Hook eglSwapBuffers() using inline or PLT hooking.
  3. Initialize and render ImGui each frame via OpenGL ES.
  4. Inject this overlay into the target game or app.

📁 Project Structure

text
lsposed-imgui-overlay/ ├── module/ │ ├── Android.mk / CMakeLists.txt │ ├── entry.cpp <-- LSPosed hook entry │ ├── imgui/ <-- ImGui sources │ ├── hooks/ │ │ └── egl_hook.cpp <-- eglSwapBuffers hook │ └── include/

🔧 1. LSPosed Hook Entry (entry.cpp)

This is the entry point for your LSPosed module:

cpp
#include <xhook.h> #include <jni.h> #include <unistd.h> #include "hooks/egl_hook.h" extern "C" void onLoad() { // Hook eglSwapBuffers from libEGL xhook_register(".*libEGL.so$", "eglSwapBuffers", (void*)hooked_eglSwapBuffers, (void**)&orig_eglSwapBuffers); xhook_refresh(0); } extern "C" __attribute__((visibility("default"))) JNIEXPORT void JNICALL Java_com_example_lsposed_Overlay_init(JNIEnv* env, jobject clazz) { onLoad(); }

🧷 2. Hook eglSwapBuffers (egl_hook.cpp)

cpp
#include <EGL/egl.h> #include <GLES2/gl2.h> #include "imgui.h" #include "backends/imgui_impl_android.h" #include "backends/imgui_impl_opengl3.h" typedef EGLBoolean (*eglSwapBuffers_t)(EGLDisplay display, EGLSurface surface); eglSwapBuffers_t orig_eglSwapBuffers = nullptr; EGLBoolean hooked_eglSwapBuffers(EGLDisplay display, EGLSurface surface) { static bool initialized = false; if (!initialized) { ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); io.DisplaySize = ImVec2(1080, 1920); // Customize based on resolution ImGui_ImplAndroid_Init(nullptr); // Pass JNIEnv + Surface if needed ImGui_ImplOpenGL3_Init("#version 100"); initialized = true; } ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplAndroid_NewFrame(); ImGui::NewFrame(); ImGui::Begin("LSPosed Overlay"); ImGui::Text("Hello from ImGui LSPosed!"); ImGui::End(); ImGui::Render(); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); return orig_eglSwapBuffers(display, surface); }

🧰 3. Build with CMake (CMakeLists.txt)

cmake
cmake_minimum_required(VERSION 3.6) project(lsposed_imgui) add_library(lsposed_imgui SHARED entry.cpp hooks/egl_hook.cpp imgui/imgui.cpp imgui/imgui_draw.cpp imgui/imgui_widgets.cpp imgui/backends/imgui_impl_android.cpp imgui/backends/imgui_impl_opengl3.cpp ) find_library(log-lib log) target_link_libraries(lsposed_imgui ${log-lib} EGL GLESv2)

📦 4. LSPosed Module Template (module.prop)

Create a file module.prop:

text
id=imgui.overlay name=ImGui Overlay version=1.0 author=you description=ImGui overlay for Android games via LSPosed

⚙️ 5. Permissions and Installation

  • Ensure you whitelist your target app in LSPosed manager.
  • Make sure ImGui_ImplAndroid_Init() gets the proper ANativeWindow if rendering fails.
  • Use xhook (or Dobby, or PLTHook) to reliably hook symbols.

✅ Final Notes

  • 🧠 OpenGL Context Awareness: Ensure eglMakeCurrent is called before rendering ImGui, or your overlay will not show.
  • 🚨 Anti-Cheat Caution: This approach can trigger bans in protected games.
  • 🔄 Resolution-Aware UI: Dynamically fetch screen size via glGetIntegerv() or JNI window access.
  • 🎯 Target Filtering: Hook only specific apps via package filtering in LSPosed.

Would you like me to generate a full zip of this LSPosed module ready to build, or help with a specific hooking method (e.g., Dobby instead of xhook)?

Поделиться этим Q&A