Created
May 8, 2018 15:21
-
-
Save Luiz-Monad/218cd2b6195eaabecdc110020e9cd523 to your computer and use it in GitHub Desktop.
load JNI ELF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdbool.h> | |
#include <stdint.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include "jni.h" | |
int __system_property_get(const char* name, char* value) { strcpy(value, "8.0.0"); return 0; } | |
const char* (JNICALL fakeGetStringUTFChars) (JNIEnv *env, jstring str, jboolean *isCopy) { | |
if (strcmp((char*)str, "getPackageName") == 0) return "my.some_package"; | |
return (char*)str; | |
} | |
jmethodID(JNICALL fakeGetMethodID) (JNIEnv *env, jclass clazz, const char *name, const char *sig) { | |
return (jmethodID)name; | |
} | |
jobject(JNICALL fakeCallObjectMethod) (JNIEnv *env, jobject obj, jmethodID methodID, ...) { | |
return (jobject)methodID; | |
} | |
jstring(JNICALL fakeNewStringUTF) (JNIEnv *env, const char *utf) { | |
auto s = (jstring)malloc(512); | |
strcpy((char*)s, utf); | |
return s; | |
} | |
jclass(JNICALL fakeGetObjectClass) (JNIEnv *env, jobject obj) { | |
return nullptr; | |
} | |
extern "C" { | |
int elf_main(int argc, char **argv, char **envp); | |
void *elfload_dlopen(const char *filename, int flag); | |
void *elfload_dlsym(void *handle, const char *symbol); | |
void relocateELFs(); | |
int (*_native_jni)(JNINativeInterface_ **a1, void *a2, void *a3); | |
void* __cdecl _malloc(size_t _Size) { | |
return malloc(_Size); | |
} | |
char* __cdecl _strcpy(char* _Dest, char const* _Source) { | |
return strcpy(_Dest, _Source); | |
} | |
void *elfload_static(const char *fname) { | |
if (strcmp(fname, "__system_property_get") == 0) { | |
return (void *)__system_property_get; | |
} | |
else if (strcmp(fname, "memset") == 0) { | |
return (void *)memset; | |
} | |
else if (strcmp(fname, "strncat") == 0) { | |
return (void *)strncat; | |
} | |
else if (strcmp(fname, "strchr") == 0) { | |
typedef char *(*fn)(char* const, int const); | |
return (void *)(fn)strchr; | |
} | |
else if (strcmp(fname, "strlen") == 0) { | |
return (void *)strlen; | |
} | |
else if (strcmp(fname, "strcpy") == 0) { | |
return (void *)_strcpy; | |
} | |
else if (strcmp(fname, "strcat") == 0) { | |
return (void *)strcat; | |
} | |
else if (strcmp(fname, "strcmp") == 0) { | |
return (void *)strcmp; | |
} | |
else if (strcmp(fname, "malloc") == 0) { | |
return (void *)_malloc; | |
} | |
else if (strcmp(fname, "free") == 0) { | |
return (void *)free; | |
} | |
return NULL; | |
} | |
} | |
int main(int argc, char **argv) { | |
JNINativeInterface_ n; | |
JNINativeInterface_* pn = &n; | |
n.GetStringUTFChars = fakeGetStringUTFChars; | |
n.GetMethodID = fakeGetMethodID; | |
n.CallObjectMethod = fakeCallObjectMethod; | |
n.NewStringUTF = fakeNewStringUTF; | |
n.GetObjectClass = fakeGetObjectClass; | |
char* sargv[] = { argv[0], "" }; | |
elf_main(2, sargv, nullptr); | |
auto dll = elfload_dlopen("lib-native.so", 0x29A); | |
relocateELFs(); | |
_native_jni = | |
(void * (*)(JNINativeInterface_ **, void *, void *)) | |
elfload_dlsym(dll, "_native_jni"); | |
return (*_native_jni)(&pn, nullptr, argv[1]); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment