the-mac / AndroidJniHelpers

Tools for writing secure Android/JNI code, based upon Spotify's Jni Helpers Library
http://on.fb.me/1MIc51o
MIT License
37 stars 5 forks source link

Convert to multiple modules #6

Open cdm2012 opened 6 years ago

cdm2012 commented 6 years ago

The library module needs to broken apart into:

- The decryption module
- The network module

The library needs to added to all the repos:

- https://jitpack.io
- https://maven.fabric.io/public
- https://jcenter.bintray.com

Publish to JCenter from Android Studio

Android library through jCenter and Maven Central from Android Studio

gradle-bintray-plugin

bintray-examples

cdm2012 commented 6 years ago

Update README to:

Android JNI Helpers

Tools for writing secure Android/JNI code, based upon Spotify's Jni Helpers Library.

The README needs to have an encryption screen shot containing:

The README also needs to have a network screen shot containing:

You may already know, but in Android development we need to hide important details of our apps from basic apk decompilation. Here is a demonstration of how easy apk decompilation truly is, using the online decompiler javadecompilers.

This library allows you to secure your apps with less effort and is fully customizable. It protects your app's resources like inline (Java file) strings, resource (res) strings and network requests beyond basic code shrinking from proguard obfuscation.

Setup

Add C++ Support, but if your project already has it skip this section

Import the module(s) that you need

Import all modules (demo, decryption & network)

compile 'com.github.the-mac:AndroidJNIHelpers:0.1.0'

Import decryption module

compile 'com.github.the-mac.AndroidJNIHelpers:decryption:0.1.0'

Import network module

compile 'com.github.the-mac.AndroidJNIHelpers:network:0.1.0'

Usage

String Decryption

Incorporate String Resource Encryption(s)

<resources>
    <string name="app_name">Android Jni Helpers</string>
    <string name="action_settings">Settings</string>
    <string name="encryptedString">HiYJ+OroGm0hwPv5hz9V0g==</string>
    ...
</resources>

Here we are requesting the decrypted string in an Activity's onCreate method:

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        String decryptedString = decryptString(R.string.encryptedString);
    }

To allow the decryptString method to work, you must declare the JNI method (in Java) then implement it (in C/C++)

package us.the.mac.example.app;
public class ExampleActivity extends Activity {
    ...
    public native String decryptString(int resource);
}

Finally, set up the decryptString native method implementation in C/C++

extern "C"
jstring
Java_us_the_mac_example_app_ExampleActivity_decryptString(JNIEnv* env, jobject java_this, jint resourceId) {
    // REQUEST STRING RESOURCE FROM strings.xml
    jstring stringResource = getStringNative(env, java_this, resourceId);

    // THIS IS YOUR CUSTOM ENCRYPTED STRING CLASS
    YourEncryptedString es = YourEncryptedString(env);
    es.encryptedString = env->GetStringUTFChars(stringResource, JNI_FALSE);

    // USE AN ALGORITHM OF YOUR CHOICE TO DECRYPT HERE
    return es.decryptNative(env, YourEncryptedString::RESOURCE_STRINGS_ALGORITHM);
}

Secure Network Calls

Creating a custom Network subclass

public class MainRequests extends Network {
    ...    
    public static final int HTTP_BIN = ...;
    private static final String API_KEY = "apiKey";
    public static native MainRequests getInstance();

    public MainRequests setKey(String key) {
        put(API_KEY, key);
        return this;
    }
}

Using a MainRequests Java instance could look as follows:

    MainRequests object = MainRequests.getInstance().setKey("1234");
    object.request(MainRequests.HTTP_BIN);

    JSONObject jsonObject = new JSONObject(object.resultString);

Demo Project

the-mac/AndroidJNIHelpers/demo/

ProGuard

Nothing to include

Contributing

Contribution Process

  1. Submit an issue describing your proposed change to the repo in question.
  2. The repo owner will respond to your issue promptly.
  3. If your proposed change is accepted, and you haven't already done so, sign a Contributor License Agreement (see details below).
  4. Fork the desired repo, develop and test your code changes.
  5. Ensure that your code adheres to the existing style of the library to which you are contributing.
  6. Ensure that your code has an appropriate set of unit tests which all pass.
  7. Submit a pull request and cc the repo owner @cdm2012

Contributor License Agreements

We'd love to accept your sample apps and patches! Before we can take them, we have to jump a couple of legal hurdles.

Please fill out either the individual or corporate Contributor License Agreement (CLA).

Follow either of the two links above to access the appropriate CLA and instructions for how to sign and return it. Once we receive it, we'll be able to accept your pull requests.

Installing locally

set(local_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/main/cpp) set(library_JniHelpers_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../AndroidJniHelpers/library/src/main/cpp) set(library_JniHelpersTest_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../AndroidJniHelpers/library/src/androidTest/cpp)

file(GLOB libLocal_SOURCES ${local_DIR}/.cpp) file(GLOB libLocal_HEADERS ${local_DIR}/.h)

file(GLOB libJniHelpers_SOURCES ${library_JniHelpers_DIR}/.cpp) file(GLOB libJniHelpers_HEADERS ${library_JniHelpers_DIR}/.h)

file(GLOB libJniHelpersTest_SOURCES ${library_JniHelpersTest_DIR}/.cpp) file(GLOB libJniHelpersTest_HEADERS ${library_JniHelpersTest_DIR}/.h)

link_directories(${JNI_LIBRARIES}) add_library(JniHelpers STATIC ${libLocal_SOURCES} ${libJniHelpers_SOURCES}) add_library(JniHelpersTest STATIC ${libLocal_SOURCES} ${libJniHelpers_SOURCES} ${libJniHelpersTest_SOURCES}) include_directories(${local_DIR} ${library_JniHelpers_DIR} ${library_JniHelpersTest_DIR})

if(${ANDROID_TESTING}) set(library_NAME "test-lib") set(library_Entry_Point "src/androidTest/cpp") else() set(library_NAME "native-lib") set(library_Entry_Point "src/main/cpp") endif()

add_library( # Sets the name of the library. ${library_NAME}

         # Sets the library as a shared library.
         SHARED

         # The following toggles between the native-lib and test-libs
         # files, based upon whehther a test is currently running.
         ${library_Entry_Point}/${library_NAME}.cpp )

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 )

Only adding JniHelpersTest library if testing

if(${ANDROID_TESTING}) target_link_libraries(${library_NAME} JniHelpers JniHelpersTest ${log-lib} ) else() target_link_libraries(${library_NAME} JniHelpers ${log-lib} ) endif()


**Note: If you're creating JNI helper tests,
you should run them on an Android device and
using the test-lib reference above (inside your
androidTest/cpp folder). Also the
ANDROID_TESTING flag is passed in from the
build.gradle script, check out an example of
that [here](library/build.gradle)**