Hello I connected to the Magisk Unix Socket but I'm having trouble reading the incoming data to su

56 Views Asked by At

Hello I connected to the Magisk Unix Socket, but I am having trouble reading the data coming to su. I want to take the incoming data and write it to the screen, for example, I want to capture and manage this data when the command comes to the unix socket as adb shell su. My goal is to make my own water management application, but you understand that creating a su binary is quite complex and difficult.

In "TiSu.cpp", I established a UNIX socket on the C++ side and connected it to the Magisk UNIX socket. However, I'm encountering issues when reading the incoming data. Below are my C++ codes. I've also provided the process for displaying the data on the Kotlin side.

#include <jni.h>
#include <string>
#include <iostream>
#include <sys/socket.h>
#include <filesystem>
#include <sys/un.h>
#include <fstream>
#include <locale>
#include <unistd.h>

jobject globalCallback = nullptr; // Global callback reference

extern "C" JNIEXPORT void JNICALL
Java_com_mebubilisim_tisu_MainActivity_registerCallback(JNIEnv *env, jobject /* this */, jobject callback) {
    globalCallback = env->NewGlobalRef(callback);
}

extern "C" JNIEXPORT void JNICALL
Java_com_mebubilisim_tisu_MainActivity_listenSocket(JNIEnv *env, jobject /* this */) {
    int sockfd;
    sockaddr_un addr = {};
    const char* socketPath = "/debug_ramdisk/.magisk/socket";
    char buffer[1024] = {0};

    sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
    if (sockfd < 0) {
        // Error handling or logging here
        return;
    }

    addr.sun_family = AF_LOCAL;
    strncpy(addr.sun_path, socketPath, sizeof(addr.sun_path) - 1);

    if (connect(sockfd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
        close(sockfd);
        // Error handling or logging here
        return;
    }

    while (true) {
        int bytesRead = read(sockfd, buffer, sizeof(buffer) - 1);
        if (bytesRead < 0) {
            // Error handling or logging here
            break;
        }

        buffer[bytesRead] = '\0';

        jclass callbackClass = env->GetObjectClass(globalCallback);
        jmethodID methodId = env->GetMethodID(callbackClass, "onDataReceived", "(Ljava/lang/String;)V");
        env->CallVoidMethod(globalCallback, methodId, env->NewStringUTF(buffer));
    }

    close(sockfd);
}

On the Kotlin side, I access my CPP file as shown below. I want to display the captured data as a Toast message, but unfortunately, the application closes and doesn't work.

package com.mebubilisim.tisu

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import okio.Okio
import java.io.File
import java.io.FileDescriptor
import kotlin.concurrent.thread

class MainActivity : AppCompatActivity() {


    // JNI Fonksiyonları
   // external fun createUnixDomainSocket(): String
    external fun registerCallback(callback: SocketDataCallback)
    external fun listenSocket()
    // Callback interface
    interface SocketDataCallback {
        fun onDataReceived(data: String)
    }

    private val socketDataCallback = object : SocketDataCallback {
        override fun onDataReceived(data: String) {
                runOnUiThread {
                    Toast.makeText(this@MainActivity, "Alınan veri: $data", Toast.LENGTH_LONG).show()
                }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        System.loadLibrary("native-lib")
        registerCallback(socketDataCallback)
        // Asenkron bir şekilde soketi dinlemeye başlayın.

            listenSocket()
    }
}

Thanks in advance to everyone who contributes.

0

There are 0 best solutions below