Magisk Hide Massive Update

This commit is contained in:
topjohnwu 2016-11-01 04:21:43 +08:00
parent 7fbfa6a52b
commit a34ea8f131
6 changed files with 154 additions and 106 deletions

View File

@ -6,6 +6,7 @@ include $(CLEAR_VARS)
LOCAL_MODULE := magiskhide LOCAL_MODULE := magiskhide
LOCAL_MODULE_TAGS := optional LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := magiskhide.c LOCAL_SRC_FILES := magiskhide.c
LOCAL_CFLAGS += -std=gnu11
include $(BUILD_EXECUTABLE) include $(BUILD_EXECUTABLE)
include $(CLEAR_VARS) include $(CLEAR_VARS)

View File

@ -1,24 +1,21 @@
typedef unsigned short int sa_family_t;
//Linux includes
#define _LINUX_TIME_H
#define _GNU_SOURCE #define _GNU_SOURCE
#include <sys/types.h>
#include <string.h> #include <string.h>
#include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <linux/connector.h>
#include <linux/cn_proc.h>
#include <linux/netlink.h>
#include <linux/fs.h>
#include <sys/socket.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/time.h> #include <sched.h>
#include <sys/resource.h> #include <pthread.h>
#include <unistd.h> #include <unistd.h>
#include <sys/syscall.h> #include <signal.h>
#include <asm/unistd.h>
#include <sys/mount.h> #include <sys/mount.h>
#include <sys/inotify.h>
#include <sys/wait.h>
FILE *logfile;
int i, list_size;
char **hide_list = NULL;
pthread_mutex_t mutex;
char **file_to_str_arr(FILE *fp, int *size) { char **file_to_str_arr(FILE *fp, int *size) {
int allocated = 16; int allocated = 16;
@ -46,6 +43,13 @@ char **file_to_str_arr(FILE *fp, int *size) {
return array; return array;
} }
void lazy_unmount(const char* mountpoint) {
if (umount2(mountpoint, MNT_DETACH) != -1)
fprintf(logfile, "MagiskHide: Unmounted (%s)\n", mountpoint);
else
fprintf(logfile, "MagiskHide: Unmount Failed (%s)\n", mountpoint);
}
//WARNING: Calling this will change our current namespace //WARNING: Calling this will change our current namespace
//We don't care because we don't want to run from here anyway //We don't care because we don't want to run from here anyway
int hideMagisk(int pid) { int hideMagisk(int pid) {
@ -53,15 +57,14 @@ int hideMagisk(int pid) {
asprintf(&path, "/proc/%d/ns/mnt", pid); asprintf(&path, "/proc/%d/ns/mnt", pid);
int fd = open(path, O_RDONLY); int fd = open(path, O_RDONLY);
if(fd == -1) return 2; if(fd == -1) return 2;
int res = syscall(SYS_setns, fd, 0); if(setns(fd, 0) == -1) return 3;
if(res == -1) return 3;
free(path); free(path);
path = NULL; path = NULL;
asprintf(&path, "/proc/%d/mounts", pid); asprintf(&path, "/proc/%d/mounts", pid);
FILE *mount_fp = fopen(path, "r"); FILE *mount_fp = fopen(path, "r");
if (mount_fp == NULL) { if (mount_fp == NULL) {
fprintf(stderr, "Error opening mount list!\n"); fprintf(logfile, "Error opening mount list!\n");
return 1; return 1;
} }
free(path); free(path);
@ -70,23 +73,14 @@ int hideMagisk(int pid) {
char **mount_list = file_to_str_arr(mount_fp, &mount_size), mountpoint[256], *sbstr; char **mount_list = file_to_str_arr(mount_fp, &mount_size), mountpoint[256], *sbstr;
fclose(mount_fp); fclose(mount_fp);
int i, unmount = 0;
for(i = mount_size - 1; i >= 0; --i) { for(i = mount_size - 1; i >= 0; --i) {
if((strstr(mount_list[i], "/dev/block/loop") != NULL)) { if(strstr(mount_list[i], "/dev/block/loop")) {
sscanf(mount_list[i], "%256s %256s", mountpoint, mountpoint); sscanf(mount_list[i], "%256s %256s", mountpoint, mountpoint);
if (strstr(mountpoint, "/.core/dummy") != NULL) if (!strstr(mountpoint, "/dev/magisk/dummy"))
unmount = 0; lazy_unmount(mountpoint);
else } else if ((sbstr = strstr(mount_list[i], "/dev/magisk/dummy"))) {
unmount = 1; sscanf(sbstr, "/dev/magisk/dummy%256s", mountpoint);
} else if ((sbstr = strstr(mount_list[i], "/.core/dummy")) != NULL) { lazy_unmount(mountpoint);
sscanf(sbstr, "/.core/dummy%256s", mountpoint);
unmount = 1;
}
if(unmount) {
unmount = 0;
res = umount2(mountpoint, MNT_DETACH);
if (res != -1) printf("Unmounted: %s\n", mountpoint);
else printf("Failed: %s\n", mountpoint);
} }
free(mount_list[i]); free(mount_list[i]);
} }
@ -96,36 +90,89 @@ int hideMagisk(int pid) {
return 0; return 0;
} }
int main(int argc, char **argv, char **envp) { void update_list(const char *listpath) {
if (argc != 2) { FILE *hide_fp = fopen((char*) listpath, "r");
fprintf(stderr, "%s <process/package name list>\n", argv[0]);
return 1;
}
int i, hide_size;
char **hide_list;
FILE *hide_fp = fopen(argv[1], "r");
if (hide_fp == NULL) { if (hide_fp == NULL) {
fprintf(stderr, "Error opening hide list\n"); fprintf(logfile, "MagiskHide: Error opening hide list\n");
return 1; exit(1);
}
pthread_mutex_lock(&mutex);
if (hide_list) {
// Free memory
for(i = 0; i < list_size; ++i)
free(hide_list[i]);
free(hide_list);
}
hide_list = file_to_str_arr(hide_fp, &list_size);
pthread_mutex_unlock(&mutex);
fclose(hide_fp);
fprintf(logfile, "MagiskHide: Update process/package list:\n");
for(i = 0; i < list_size; i++)
fprintf(logfile, "MagiskHide: %s\n", hide_list[i]);
} }
hide_list = file_to_str_arr(hide_fp, &hide_size); void quit_pthread(int sig) {
fclose(hide_fp); // Free memory
for(i = 0; i < list_size; ++i)
free(hide_list[i]);
free(hide_list);
pthread_exit(NULL);
}
printf("Get process / package name from config:\n"); void *monitor_list(void *listpath) {
for(i = 0; i < hide_size; i++) signal(SIGQUIT, quit_pthread);
printf("%s\n", hide_list[i]); // Initial load
printf("\n"); update_list((char*) listpath);
int inotifyFd;
char buffer[512];
inotifyFd = inotify_init();
if (inotifyFd == -1)
exit(1);
if (inotify_add_watch(inotifyFd, (char*) listpath, IN_ATTRIB | IN_MODIFY) == -1)
exit(1);
while(1) {
if (read(inotifyFd, buffer, 512) == -1)
exit(1);
update_list((char*) listpath);
}
return NULL;
}
int main(int argc, char **argv, char **envp) {
pid_t forkpid = fork();
if (forkpid < 0)
return 1;
if (forkpid == 0) {
if (setsid() < 0)
return 1;
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
logfile = fopen("/cache/magisk.log", "a+");
setbuf(logfile, NULL);
pthread_t list_monitor, unmount_thread;
pthread_mutex_init(&mutex, NULL);
pthread_create(&list_monitor, NULL, monitor_list, "/magisk/.core/magiskhide/hidelist");
char buffer[512]; char buffer[512];
FILE *p = popen("while true; do logcat -b events -v raw -s am_proc_start; sleep 1; done", "r"); FILE *p = popen("while true; do logcat -b events -v raw -s am_proc_start; sleep 1; done", "r");
while(!feof(p)) { while(!feof(p)) {
//Format of am_proc_start is (as of Android 5.1 and 6.0) //Format of am_proc_start is (as of Android 5.1 and 6.0)
//UserID, pid, unix uid, processName, hostingType, hostingName //UserID, pid, unix uid, processName, hostingType, hostingName
fgets(buffer, sizeof(buffer), p); fgets(buffer, sizeof(buffer), p);
{
char *pos = buffer; char *pos = buffer;
while(1) { while(1) {
pos = strchr(pos, ','); pos = strchr(pos, ',');
@ -133,7 +180,6 @@ int main(int argc, char **argv, char **envp) {
break; break;
pos[0] = ' '; pos[0] = ' ';
} }
}
int user, pid, uid; int user, pid, uid;
char processName[256], hostingType[16], hostingName[256]; char processName[256], hostingType[16], hostingName[256];
@ -141,14 +187,21 @@ int main(int argc, char **argv, char **envp) {
&user, &pid, &uid, &user, &pid, &uid,
processName, hostingType, hostingName); processName, hostingType, hostingName);
if(ret != 6)
if(ret != 6) {
continue; continue;
}
for (i = 0; i < hide_size; ++i) { for (i = 0; i < list_size; ++i) {
if(strstr(processName, hide_list[i]) != NULL) { if(strstr(processName, hide_list[i])) {
printf("Disabling for process = %s, PID = %d, UID = %d\n", processName, pid, uid); fprintf(logfile, "MagiskHide: Disabling for process = %s, PID = %d, UID = %d\n", processName, pid, uid);
forkpid = fork();
if (forkpid < 0)
break;
if (forkpid == 0) {
hideMagisk(pid); hideMagisk(pid);
return 0;
}
wait(&i);
kill(forkpid, SIGTERM);
break; break;
} }
} }
@ -156,10 +209,15 @@ int main(int argc, char **argv, char **envp) {
pclose(p); pclose(p);
// Free memory pthread_kill(list_monitor, SIGQUIT);
for(i = 0; i < hide_size; ++i) pthread_mutex_destroy(&mutex);
free(hide_list[i]);
free(hide_list); fprintf(logfile, "MagiskHide: Error occurred...\n");
fclose(logfile);
return 1;
}
return 0; return 0;
} }

View File

@ -21,8 +21,8 @@ export OLDPATH=$PATH
log_print() { log_print() {
echo $1 echo "$1"
echo $1 >> $LOGFILE echo "$1" >> $LOGFILE
log -p i -t Magisk "$1" log -p i -t Magisk "$1"
} }
@ -224,7 +224,7 @@ case $1 in
touch $LOGFILE touch $LOGFILE
chmod 644 $LOGFILE chmod 644 $LOGFILE
log_print "Magisk post-fs mode running..." log_print "** Magisk post-fs mode running..."
# No more cache mods! # No more cache mods!
# Only for multirom! # Only for multirom!
@ -246,7 +246,7 @@ case $1 in
# Don't run twice # Don't run twice
if [ "$(getprop magisk.restart_pfsd)" != "1" ]; then if [ "$(getprop magisk.restart_pfsd)" != "1" ]; then
log_print "Magisk post-fs-data mode running..." log_print "** Magisk post-fs-data mode running..."
# Live patch sepolicy # Live patch sepolicy
/data/magisk/sepolicy-inject --live -s su /data/magisk/sepolicy-inject --live -s su
@ -397,24 +397,18 @@ case $1 in
service ) service )
# Version info # Version info
MAGISK_VERSION_STUB MAGISK_VERSION_STUB
log_print "Magisk late_start service mode running..." log_print "** Magisk late_start service mode running..."
run_scripts service run_scripts service
[ -f "$COREDIR/magiskhide/enable" ] && setprop magisk.hide 1
;;
hide ) # MagiskHide
# Enable magiskhide if [ -f "$COREDIR/magiskhide/enable" ]; then
[ ! -f "$COREDIR/magiskhide/hidelist" ] && mktouch $COREDIR/magiskhide/hidelist [ ! -f "$COREDIR/magiskhide/hidelist" ] && mktouch $COREDIR/magiskhide/hidelist
# Add preset for Safety Net chmod -R 755 $COREDIR/magiskhide
if [ $(grep -c "com.google.android.gms.unstable" $COREDIR/magiskhide/hidelist) -eq "0" ]; then # Add Safety Net preset
mv $COREDIR/magiskhide/hidelist $COREDIR/magiskhide/hidelist.tmp $COREDIR/magiskhide/add com.google.android.gms.unstable
echo "com.google.android.gms.unstable" > $COREDIR/magiskhide/hidelist log_print "** Starting Magisk Hide"
cat $COREDIR/magiskhide/hidelist.tmp >> $COREDIR/magiskhide/hidelist /data/magisk/magiskhide
rm -f $COREDIR/magiskhide/hidelist.tmp
fi fi
chmod 755 $COREDIR/magiskhide $COREDIR/magiskhide/*
log_print "Starting Magisk Hide"
exec /data/magisk/magiskhide $COREDIR/magiskhide/hidelist
;; ;;
esac esac

View File

@ -1,12 +1,9 @@
#!/system/bin/sh #!/system/bin/sh
HIDELIST=$MOUNTPOINT/.core/magiskhide/hidelist HIDELIST=/magisk/.core/magiskhide/hidelist
if [ ! -z "$1" ]; then if [ ! -z "$1" ]; then
if [ $(grep -c "^$1$" $HIDELIST) -eq "0" ]; then if [ $(grep -c "^$1$" $HIDELIST) -eq "0" ]; then
echo "$1" >> $HIDELIST echo "$1" >> $HIDELIST
fi fi
fi fi
# Reload the list
setprop magisk.hide 1

View File

@ -1,4 +1,5 @@
#!/system/bin/sh #!/system/bin/sh
HIDELIST=$MOUNTPOINT/.core/magiskhide/hidelist HIDELIST=/magisk/.core/magiskhide/hidelist
cat $HIDELIST cat $HIDELIST

View File

@ -1,12 +1,9 @@
#!/system/bin/sh #!/system/bin/sh
HIDELIST=$MOUNTPOINT/.core/magiskhide/hidelist HIDELIST=/magisk/.core/magiskhide/hidelist
if [ ! -z "$1" ]; then if [ ! -z "$1" ]; then
mv $HIDELIST $HIDELIST.tmp mv $HIDELIST $HIDELIST.tmp
cat $HIDELIST.tmp | grep -v "^$1$" > $HIDELIST cat $HIDELIST.tmp | grep -v "^$1$" > $HIDELIST
rm -f $HIDELIST.tmp rm -f $HIDELIST.tmp
fi fi
# Reload the list
setprop magisk.hide 1