2

mbedtls lib link failed,

undefined symbol: mbedtls_aes_init

g++ -Wall fpic -c jar.cpp -o libjar.o -I /usr/local/include/mbedtls -L /usr/local/lib -lmbedtls -lmbedcrypto -lmbedx509

g++ -shared -o libjar.so libjar.o

Steps

  1. cd /home/xxx/mbedtls
  2. git clone https://github.com/ARMmbed/mbedtls.git
  3. cmake -DUSE_SHARED_MBEDTLS_LIBRARY=On .
  4. make
  5. sudo make install
  6. copy (jvmti.h ..) into /usr/local/inclde
  7. vim jar.cpp
#include <iostream>
#include <string.h>

#include "jni.h"
#include <jvmti.h>
#include <jni_md.h>

#include "mbedtsl/aes.h"

void decrypt(int *destLen, char *src, int srcLen, unsigned char** new_class_data)
{
    const  char *key = "xxxxxxxxxxxxxxxx";

    mbedtls_aes_context aes_ctx; 
    mbedtls_aes_init( &aes_ctx );  

    mbedtls_aes_setkey_dec( &aes_ctx, (unsigned char *)key, 128);

    int block = srcLen / 16;

    unsigned char* dest = *new_class_data;

    memset(dest, 0, *destLen);

    unsigned char input[16];
    unsigned char outout[16];

    int b = 0;
    while( b < block ){
        int offset = b * 16;
        int len = 0;
        if((srcLen - 16 - offset) > 0 ){
            len = 16;
        }else{
            len = srcLen-offset;
        }

        memset(input, 0 ,16);
        memset(outout, 0, 16);
        memcpy(input, &src[offset], len);

        mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_DECRYPT, input, outout ); 
        if (b == 381) {

            memcpy(&dest[6096], outout, 9);
        } else {
            memcpy(&dest[b * 16], outout, 16);
        }
        b++;
    }

    mbedtls_aes_free(&aes_ctx);
}

void JNICALL ClassDecryptHook(
    jvmtiEnv *jvmti_env,
    JNIEnv* jni_env,
    jclass class_being_redefined,
    jobject loader,
    const char* name,
    jobject protection_domain,
    jint class_data_len,
    const unsigned char* class_data,
    jint* new_class_data_len,
    unsigned char** new_class_data
    )
{
    if (name && strncmp(name, "io/xjar/XLauncher", 17) == 0) {

        *new_class_data_len = 6105;
        jvmti_env->Allocate(*new_class_data_len, new_class_data);
        decrypt((int *)new_class_data_len, (char *)class_data, class_data_len, new_class_data);
    }
}

JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *vm, char *options, void *reserved)
{
    jvmtiEnv *jvmti;
    // Create the JVM TI environment(jvmti)
    jint ret = vm->GetEnv((void **)&jvmti, JVMTI_VERSION);
    if (JNI_OK != ret)
    {
        printf("ERROR: Unable to access JVMTI!\n");
        return ret;
    }
    jvmtiCapabilities capabilities;
    (void)memset(&capabilities, 0, sizeof(capabilities));

    capabilities.can_generate_all_class_hook_events = 1;
    capabilities.can_tag_objects = 1;
    capabilities.can_generate_object_free_events = 1;
    capabilities.can_get_source_file_name = 1;
    capabilities.can_get_line_numbers = 1;
    capabilities.can_generate_vm_object_alloc_events = 1;

    jvmtiError error = jvmti->AddCapabilities(&capabilities);
    if (JVMTI_ERROR_NONE != error)
    {
        printf("ERROR: Unable to AddCapabilities JVMTI!\n");
        return error;
    }

    jvmtiEventCallbacks callbacks;
    (void)memset(&callbacks, 0, sizeof(callbacks));

    callbacks.ClassFileLoadHook = &ClassDecryptHook;
    error = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
    if (JVMTI_ERROR_NONE != error) {
        printf("ERROR: Unable to SetEventCallbacks JVMTI!\n");
        return error;
    }

    error = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, 0);
    if (JVMTI_ERROR_NONE != error) {
        printf("ERROR: Unable to SetEventNotificationMode JVMTI!\n");
        return error;
    }

    return JNI_OK;
}
  1. g++ -Wall fpic -c jar.cpp -o libjar.o -I /usr/local/include/mbedtls -L /usr/local/lib -lmbedtls -lmbedcrypto -lmbedx509
  2. g++ -shared -o libjar.so libjar.o

run agent: undefined symbol: mbedtls_aes_init

nm libjar.so -r:

                     U strncmp@@GLIBC_2.2.5
    0000000000000ca0 t register_tm_clones
                     U puts@@GLIBC_2.2.5
                     U memset@@GLIBC_2.2.5
                     U memcpy@@GLIBC_2.14
                     U mbedtls_aes_setkey_dec
                     U mbedtls_aes_init
                     U mbedtls_aes_free
                     U mbedtls_aes_crypt_ecb
    0000000000000d30 t frame_dummy

ldd libjar.so:

linux-vdso.so.1 (0x00007fffd6a93000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007ff139f00000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff139b00000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007ff139760000)
    /lib64/ld-linux-x86-64.so.2 (0x00007ff13a600000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007ff139540000)
ejderuby
  • 710
  • 5
  • 21
Ftrybe
  • 21
  • 2

0 Answers0