Skip to content

Instantly share code, notes, and snippets.

@numinit
Created June 19, 2017 00:37
Show Gist options
  • Save numinit/50baac71d8bd1b9f532763868138f7f8 to your computer and use it in GitHub Desktop.
Save numinit/50baac71d8bd1b9f532763868138f7f8 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <stdint.h>
#include <unistd.h>
#include <stddef.h>
#include <stdbool.h>
#include <signal.h>
#include <jni.h>
static void go(jint (*JNI_CreateJavaVM)(JavaVM**, JNIEnv**, void*), int (*JNI_OnLoad)(JavaVM *)) {
JavaVM *jvm = NULL;
JNIEnv *env = NULL;
JavaVMOption opt[4];
opt[0].optionString = "-Djava.class.path=/data/local/tmp/food.apk";
opt[1].optionString = "-agentlib:jdwp=transport=dt_android_adb,suspend=n,server=y";
opt[2].optionString = "-Djava.library.path=/data/local/tmp";
opt[3].optionString = "-verbose:jni";
JavaVMInitArgs args = {
.version = JNI_VERSION_1_6,
.options = opt,
.nOptions = 4,
.ignoreUnrecognized = JNI_FALSE
};
printf("Creating Java VM\n");
JNI_CreateJavaVM(&jvm, &env, &args);
printf("Waiting for a debugger\n");
raise(SIGTRAP);
printf("Calling JNI_OnLoad\n");
jint result = JNI_OnLoad(jvm);
printf("Done with status code %d; destroying JVM\n", result);
(*jvm)->DestroyJavaVM(jvm);
}
int main(int argc, const char *argv[]) {
if (argc != 2) {
fprintf(stderr, "usage: %s [path]\n", argv[0]);
return -1;
}
void *handle = dlopen(argv[1], RTLD_NOW | RTLD_LOCAL);
if (handle == NULL) {
fprintf(stderr, "error loading `%s`\n", argv[1]);
return -1;
} else {
fprintf(stderr, "loaded `%s`\n", argv[1]);
}
int (*JNI_OnLoad)(JavaVM *) = dlsym(handle, "JNI_OnLoad");
if (JNI_OnLoad == NULL) {
fprintf(stderr, "couldn't find JNI_OnLoad\n");
return -1;
}
// load Dalvik
void *dalvik = dlopen("libdvm.so", RTLD_NOW);
if (dalvik == NULL) {
fprintf(stderr, "error loading libdvm\n");
return -1;
} else {
fprintf(stderr, "loaded libdvm.so\n");
}
jint (*JNI_CreateJavaVM)(JavaVM**, JNIEnv**, void*) = dlsym(dalvik, "JNI_CreateJavaVM");
if (JNI_CreateJavaVM == NULL) {
fprintf(stderr, "couldn't find JNI_CreateJavaVM\n");
return -1;
}
go(JNI_CreateJavaVM, JNI_OnLoad);
dlclose(handle);
dlclose(dalvik);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment