İçeriğe geç

Artık SSL Pinning Bypass Diye Bir Şey Yok

Android NDK Projesine mbedtls ve libcurl Eklenmesi

Bir NDK projesi oluşturulduğunda eğer http istekleri oluşturmak isterseniz default olarak NDK'nın bunu sağlamadığını görürsünüz. Bu işlemleri gerçekleştirmek için projenize cpp projeleri olan mbedtls ve libcurl'ü eklemeniz gerekebilir. ben burada cmakelists.txt dosyası olduğudnan dolayı opnessl yerine mbedtls projesini seçtim. NDK projesi altında aşağıdaki gibi bir cmakelist.txt dosyası oluşturulduğuda projenize curl ve mbedtls eklenmiş olacaktır.

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.22.1)

# Declares and names the project.

project("myapplication")

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

set(CURL_USE_MBEDTLS ON)
set(MBEDTLS_INCLUDE_DIRS "${CMAKE_BINARY_DIR}/libs/mbedtls/include")
set(MBEDTLS_LIBRARY "${CMAKE_BINARY_DIR}/libs/mbedtls/library/libmbedtls.a")
set(MBEDX509_LIBRARY "${CMAKE_BINARY_DIR}/libs/mbedtls/library/libmbedx509.a")
set(MBEDCRYPTO_LIBRARY "${CMAKE_BINARY_DIR}/libs/mbedtls/library/libmbedcrypto.a")
add_subdirectory(libs/mbedtls)

add_subdirectory(libs/curl)

add_library( # Sets the name of the library.
        myapplication

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        native-lib.cpp)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
        log-lib

        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log)

target_include_directories(myapplication PRIVATE lib/mbedtls/include)
target_include_directories(myapplication PRIVATE lib/curl/include)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
        myapplication
        mbedtls
        libcurl
        ${log-lib})

# Specify mbedtls library output directory
#set_target_properties(mbedtls PROPERTIES ARCHIVE_OUTPUT_DIRECTORY libs/kuday)

message(${CMAKE_BINARY_DIR})
message("kudayy")

Aynı zamanda proje dizinimde cpp klasörünün altında libs isimli bir klasör oluşturdum ve bu klasörün altında curl ile mbedtls projelerini githubtan çekip dahil ettim. daha sornasında aşağıdaki basit cpp kodu ile Android projemden hiçbir şekilde SSL Pinning Bypass yapılamayacak bir http isteği oluşturdum.

#include <jni.h>
#include <string>
#include "curl/curl.h"

// Callback function to handle curl response
size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
    ((std::string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}

// Function to perform a GET request and return response
std::string performGetRequest(const char* url) {
    CURL* curl = curl_easy_init();
    std::string response;

    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);

        CURLcode res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);

        if (res == CURLE_OK) {
            return response;
        }
    }

    return "Curl request failed";
}

// Function to perform a POST request and return response
std::string performPostRequest(const char* url, const char* postData) {
    CURL* curl = curl_easy_init();
    std::string response;

    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_easy_setopt(curl, CURLOPT_POST, 1);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData);

        CURLcode res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);

        if (res == CURLE_OK) {
            return response;
        }
    }

    return "Curl request failed";
}

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_myapplication_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "Hello from C++";

    // Perform GET request
    std::string getResponse = performGetRequest("https://api.ipify.org/?format=txt");

    // Perform POST request
   // std::string postData = "key1=value1&key2=value2";
   // std::string postResponse = performPostRequest("https://example.com/post-endpoint", postData.c_str());

    // Combine responses
    std::string combinedResponse = "GET Response: " + getResponse;

    return env->NewStringUTF(combinedResponse.c_str());
}
Kategori:AndroidAndroid Securitycmakecpp

İlk Yorumu Siz Yapın

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir