Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Verified Commit c8763adf authored by Marvin W.'s avatar Marvin W. 🐿️
Browse files

Ensure existance of dg.db



Includes parts of #29

Co-authored-by: default avatartacticalDevC <tacticaldevc@tutanota.com>
parent af166696
Loading
Loading
Loading
Loading
+30 −0
Original line number Diff line number Diff line
/*
 * SPDX-FileCopyrightText: 2020, microG Project Team
 * SPDX-License-Identifier: Apache-2.0
 */

package org.microg.gms.droidguard;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DroidGuardDatabase extends SQLiteOpenHelper {
    private static final String DB_NAME = "dg.db";

    public DroidGuardDatabase(Context context) {
        super(context, DB_NAME, null, 2);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // Note: "NON NULL" is actually not a valid sqlite constraint, but this is what we see in the original database *shrug*
        db.execSQL("CREATE TABLE main (a TEXT NOT NULL, b LONG NOT NULL, c LONG NOT NULL, d TEXT NON NULL, e TEXT NON NULL,f BLOB NOT NULL,g BLOB NOT NULL);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS main;");
        onCreate(db);
    }
}
+20 −17
Original line number Diff line number Diff line
@@ -8,9 +8,9 @@ package org.microg.gms.droidguard;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.text.TextUtils;

import com.squareup.wire.Wire;

@@ -41,8 +41,8 @@ import java.util.zip.ZipFile;
import dalvik.system.DexClassLoader;
import okio.ByteString;

public class DroidguardHelper {
    private static final String TAG = "GmsDroidguardHelper";
public class DroidGuardHelper {
    private static final String TAG = "GmsDroidGuardHelper";
    private static final String DG_CLASS_NAME = "com.google.ccc.abuse.droidguard.DroidGuard";
    private static final String DG_URL = "https://www.googleapis.com/androidantiabuse/v1/x/create?alt=PROTO&key=AIzaSyBofcZsgLSS7BOnBjZPEkk4rYwzOIz-lTI";
    private static Map<String, Class<?>> loadedClass = new HashMap<>();
@@ -50,6 +50,9 @@ public class DroidguardHelper {
    public static byte[] guard(Context context, RemoteDroidGuardRequest request) throws Exception {
        int versionCode = context.getPackageManager().getPackageInfo(Constants.GMS_PACKAGE_NAME, 0).versionCode;

        // Ensure database exists
        new DroidGuardDatabase(context).getReadableDatabase().close();

        SignedDGResponse signedResponse = request(new DGRequest.Builder()
                        .usage(new DGUsage(request.reason, request.packageName))
                        .info(getSystemInfo(null))
@@ -101,8 +104,8 @@ public class DroidguardHelper {
        versionCode /= 1000;
        patch = versionCode % 100;
        versionCode /= 100;
        minor = versionCode % 10;
        versionCode /= 10;
        minor = versionCode % 100;
        versionCode /= 100;
        String versionPrefix = versionCode + "." + minor + "." + patch;
        switch (arch) {
            case "arm64-v8a":
@@ -341,7 +344,7 @@ public class DroidguardHelper {
        }

        public final String a(final byte[] array) {
            String guasso = new String(DroidguassoHelper.guasso(array));
            String guasso = new String(DroidGuassoHelper.guasso(array));
            Log.d(TAG, "a: " + Base64.encodeToString(array, Base64.NO_WRAP) + " -> " + guasso);
            return guasso;
        }
+36 −12
Original line number Diff line number Diff line
@@ -20,18 +20,27 @@ import java.util.List;

import okio.ByteString;

public class DroidguassoHelper {
    private static final String TAG = "GmsDroidguassoHelper";
/**
 * DroidGuasso helper class. It hashes some files up to a size of 1024 bytes and feeds them with a digest into DroidGuasso
 */
public class DroidGuassoHelper {
    private static final String TAG = "GmsDroidGuassoHelper";

    private char[] HEX_CHARS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    /**
     * Makes a call to DroidGuasso.
     *
     * @param digest A digest array to feed into DroidGuasso
     * @return DroidGuasso output data to further process
     */
    public static byte[] guasso(byte[] digest) {
        List<String> substrs = new ArrayList<>();
        addFilesInPath("/vendor/lib/egl", substrs);
        addFilesInPath("/system/lib/egl", substrs);
        addFilesInPath("/vendor/lib/egl", substrs); //Hashing all EGL libs in /vendor
        addFilesInPath("/system/lib/egl", substrs); //Hashing all EGL libs in /system
        Collections.sort(substrs);
        substrs.add(initialBytesDigest(new File("/system/lib/egl/egl.cfg")));
        String eglInfo = hexDigest(substrs.toString().getBytes());
        substrs.add(initialBytesDigest(new File("/system/lib/egl/egl.cfg"))); //Hashing the EGL config
        String eglInfo = hexDigest(substrs.toString().getBytes()); //SHA-1 hashing the toString() of an object?


        float[] floats = new float[]{0.35502917f, 0.47196686f, 0.24689609f, 0.66850024f, 0.7746259f, 0.5967446f, 0.06270856f, 0.19201201f, 0.35090452f, 0.5573558f, 0.470259f, 0.9866341f};
@@ -48,6 +57,12 @@ public class DroidguassoHelper {
        return ("5=" + eglInfo + "\n7=" + dg.getGpu() + "\n8=" + dg.getHash1() + "\n9=" + dg.getHash2() + "\n").getBytes();
    }

    /**
     * SHA-1 hashes file contents. Max size is 1024 bytes.
     *
     * @param file A file object which contents to hash
     * @return Returns the hash
     */
    private static String initialBytesDigest(File file) {
        try {
            FileInputStream is = new FileInputStream(file);
@@ -60,6 +75,12 @@ public class DroidguassoHelper {
        }
    }

    /**
     * SHA-1 hashes an byte array
     *
     * @param bytes Bytes to hash
     * @return Returns the hash
     */
    private static String hexDigest(byte[] bytes) {
        try {
            return ByteString.of(MessageDigest.getInstance("SHA-1").digest(bytes)).hex();
@@ -68,15 +89,18 @@ public class DroidguassoHelper {
        }
    }

    /**
     * Finds ".so" files in a directory and adds their SHA-1 hashed content to a given {@link List} object.
     * Representation in the list as follows:
     * {@literal <filename>/<file_length>/<contents_hash>}
     *
     * @param path the parent directory to search for ".so" files
     * @param list the list to add the
     */
    private static void addFilesInPath(String path, List list) {
        final File parent = new File(path);
        if (parent.isDirectory()) {
            final File[] listFiles = parent.listFiles(new FilenameFilter() {
                @Override
                public boolean accept(File dir, String filename) {
                    return filename.endsWith(".so");
                }
            });
            final File[] listFiles = parent.listFiles((dir, filename) -> filename.endsWith(".so"));
            for (File file : listFiles) {
                list.add(file.getName() + "/" + file.length() + "/" + initialBytesDigest(file));
            }
+1 −1
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ public class RemoteDroidGuardService extends Service {
                    @Override
                    public void run() {
                        try {
                            callback.onResult(DroidguardHelper.guard(RemoteDroidGuardService.this, request));
                            callback.onResult(DroidGuardHelper.guard(RemoteDroidGuardService.this, request));
                        } catch (Exception e) {
                            Log.w(TAG, e);
                            try {