Skip to content

Commit

Permalink
Create module for aliuhook library
Browse files Browse the repository at this point in the history
This was done to prevent a policy violation in F-Droid that restricts usage of such repositories for security concerns.

Tested-by: Pranav Purwar <[email protected]>
Signed-off-by: PranavPurwar <[email protected]>
  • Loading branch information
PranavPurwar committed Sep 25, 2023
1 parent 48a38ac commit ef55d23
Show file tree
Hide file tree
Showing 24 changed files with 1,662 additions and 9 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
* You should have received a copy of the GNU General Public License along with Cosmic IDE. If not, see <https://www.gnu.org/licenses/>.
*/

/*
* This file is part of Cosmic IDE.
* Cosmic IDE is a free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* Cosmic IDE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with Cosmic IDE. If not, see <https://www.gnu.org/licenses/>.
*/

plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
Expand Down Expand Up @@ -183,7 +190,7 @@ dependencies {
implementation("io.noties.markwon:html:$markwonVersion")
implementation("io.noties.markwon:linkify:$markwonVersion")

implementation("com.aliucord:Aliuhook:main-SNAPSHOT")
implementation(projects.feature.aliuhook)
implementation("de.maxr1998:modernandroidpreferences:2.3.2")

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3")
Expand Down
1 change: 1 addition & 0 deletions feature/aliuhook/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
18 changes: 18 additions & 0 deletions feature/aliuhook/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
The code in this module is part of Aliucord, and licensed under the OSL-3.0 license.
The code was copied due to F-Droid's policy of not allowing apps to use insecure maven repositories.

No major changes have been to the [upstream](https://github.com/Aliucord/hook/tree/main) code.

License header:

```agsl
/*
* This file is part of AliuHook, a library providing XposedAPI bindings to LSPlant
* Copyright (c) 2021 Juby210 & Vendicated
* Licensed under the Open Software License version 3.0
*
* Originally written by rovo89 as part of the original Xposed
* Copyright 2013 rovo89, Tungstwenty
* Licensed under the Apache License, Version 2.0, see http://www.apache.org/licenses/LICENSE-2.0
*/
```
56 changes: 56 additions & 0 deletions feature/aliuhook/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* This file is part of Cosmic IDE.
* Cosmic IDE is a free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* Cosmic IDE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with Cosmic IDE. If not, see <https://www.gnu.org/licenses/>.
*/

/*
* This file is part of Cosmic IDE.
* Cosmic IDE is a free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* Cosmic IDE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with Cosmic IDE. If not, see <https://www.gnu.org/licenses/>.
*/

plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
}

android {
namespace = "com.aliucord.aliuhook"
compileSdk = 34

defaultConfig {
minSdk = 26

externalNativeBuild {
cmake {
arguments("-DANDROID_STL=c++_shared")
}
}
}

buildFeatures {
prefab = true
}

externalNativeBuild {
cmake {
path = file("src/main/cpp/CMakeLists.txt")
}
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "17"
}
}

dependencies {
implementation("io.github.vvb2060.ndk:dobby:1.2")
implementation("org.lsposed.lsplant:lsplant:5.2")
}
17 changes: 17 additions & 0 deletions feature/aliuhook/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ This file is part of Cosmic IDE.
~ Cosmic IDE is a free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
~ Cosmic IDE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
~ You should have received a copy of the GNU General Public License along with Cosmic IDE. If not, see <https://www.gnu.org/licenses/>.
-->

<!--
~ This file is part of Cosmic IDE.
~ Cosmic IDE is a free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
~ Cosmic IDE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
~ You should have received a copy of the GNU General Public License along with Cosmic IDE. If not, see <https://www.gnu.org/licenses/>.
-->

<manifest>

</manifest>
15 changes: 15 additions & 0 deletions feature/aliuhook/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

cmake_minimum_required(VERSION 3.22.1)
project("aliuhook")

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_library(aliuhook SHARED aliuhook.cpp elf_img.cpp profile_saver.cpp hidden_api.cpp)

find_package(lsplant REQUIRED CONFIG)
find_package(dobby REQUIRED CONFIG)

target_link_libraries(aliuhook log lsplant::lsplant dobby::dobby)
174 changes: 174 additions & 0 deletions feature/aliuhook/src/main/cpp/aliuhook.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* This file is part of AliuHook, a library providing XposedAPI bindings to LSPlant
* Copyright (c) 2021 Juby210 & Vendicated
* Licensed under the Open Software License version 3.0
*/

#include <jni.h>
#include <string>
#include <lsplant.hpp>
#include <dobby.h>
#include <sys/mman.h>
#include <bits/sysconf.h>
#include "elf_img.h"
#include "log.h"
#include "profile_saver.h"
#include "hidden_api.h"
#include <sys/system_properties.h>
#include <cstdlib>
#include <cerrno>
#include "aliuhook.h"

int AliuHook::android_version = -1;
pine::ElfImg AliuHook::elf_img; // NOLINT(cert-err58-cpp)

void AliuHook::init(int version) {
elf_img.Init("libart.so", version);
android_version = version;
}

static size_t page_size_;

// Macros to align addresses to page boundaries
#define ALIGN_DOWN(addr, page_size) ((addr) & -(page_size))
#define ALIGN_UP(addr, page_size) (((addr) + ((page_size) - 1)) & ~((page_size) - 1))

static bool Unprotect(void *addr) {
auto addr_uint = reinterpret_cast<uintptr_t>(addr);
auto page_aligned_prt = reinterpret_cast<void *>(ALIGN_DOWN(addr_uint, page_size_));
size_t size = page_size_;
if (ALIGN_UP(addr_uint + page_size_, page_size_) != ALIGN_UP(addr_uint, page_size_)) {
size += page_size_;
}

int result = mprotect(page_aligned_prt, size, PROT_READ | PROT_WRITE | PROT_EXEC);
if (result == -1) {
LOGE("mprotect failed for %p: %s (%d)", addr, strerror(errno), errno);
return false;
}
return true;
}

void *InlineHooker(void *address, void *replacement) {
if (!Unprotect(address)) {
return nullptr;
}

void *origin_call;
if (DobbyHook(address, replacement, &origin_call) == RS_SUCCESS) {
return origin_call;
} else {
return nullptr;
}
}

bool InlineUnhooker(void *func) {
return DobbyDestroy(func) == RT_SUCCESS;
}

extern "C"
JNIEXPORT jboolean

JNICALL
Java_de_robv_android_xposed_XposedBridge_isHooked0(JNIEnv *env, jclass, jobject method) {
return lsplant::IsHooked(env, method);
}

extern "C"
JNIEXPORT jobject

JNICALL
Java_de_robv_android_xposed_XposedBridge_hook0(JNIEnv *env, jclass, jobject context,
jobject original,
jobject callback) {
return lsplant::Hook(env, original, context, callback);
}

extern "C"
JNIEXPORT jboolean

JNICALL
Java_de_robv_android_xposed_XposedBridge_unhook0(JNIEnv *env, jclass, jobject target) {
return lsplant::UnHook(env, target);
}

extern "C"
JNIEXPORT jboolean

JNICALL
Java_de_robv_android_xposed_XposedBridge_deoptimize0(JNIEnv *env, jclass, jobject method) {
return lsplant::Deoptimize(env, method);
}

extern "C"
JNIEXPORT jboolean

JNICALL
Java_de_robv_android_xposed_XposedBridge_makeClassInheritable0(JNIEnv *env, jclass, jclass clazz) {
return lsplant::MakeClassInheritable(env, clazz);
}

extern "C"
JNIEXPORT jboolean

JNICALL
Java_de_robv_android_xposed_XposedBridge_disableProfileSaver(JNIEnv *, jclass) {
return disable_profile_saver();
}

extern "C"
JNIEXPORT jboolean

JNICALL
Java_de_robv_android_xposed_XposedBridge_disableHiddenApiRestrictions(JNIEnv *env, jclass) {
return disable_hidden_api(env);
}

JNIEXPORT jint

JNICALL
JNI_OnLoad(JavaVM *vm, void *) {
JNIEnv *env;
if (vm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) {
return JNI_ERR;
}

page_size_ = static_cast<const size_t>(sysconf(_SC_PAGESIZE));

{
char version_str[PROP_VALUE_MAX];
if (!__system_property_get("ro.build.version.sdk", version_str)) {
LOGE("Failed to obtain SDK int");
return JNI_ERR;
}
long version = std::strtol(version_str, nullptr, 10);

if (version == 0) {
LOGE("Invalid SDK int %s", version_str);
return JNI_ERR;
}

AliuHook::init(static_cast<int>(version));
}

lsplant::InitInfo initInfo{
.inline_hooker = InlineHooker,
.inline_unhooker = InlineUnhooker,
.art_symbol_resolver = [](std::string_view symbol) -> void * {
return AliuHook::elf_img.GetSymbolAddress(symbol, false, false);
},
.art_symbol_prefix_resolver = [](std::string_view symbol) -> void * {
return AliuHook::elf_img.GetSymbolAddress(symbol, false, true);
}
};

bool res = lsplant::Init(env, initInfo);
if (!res) {
LOGE("lsplant init failed");
return JNI_ERR;
}

LOGI("lsplant init finished");

return JNI_VERSION_1_6;
}
22 changes: 22 additions & 0 deletions feature/aliuhook/src/main/cpp/aliuhook.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Created by ven on 24/03/2022.
//

#ifndef ALIUHOOK_ALIUHOOK_H
#define ALIUHOOK_ALIUHOOK_H

#include "elf_img.h"

void *InlineHooker(void *, void *);

bool InlineUnhooker(void *);

class AliuHook {
public:
static pine::ElfImg elf_img;
static int android_version;

static void init(int version);
};

#endif //ALIUHOOK_ALIUHOOK_H
Loading

0 comments on commit ef55d23

Please sign in to comment.