Introduce more randomness

- Use C++ random generator instead of old and broken rand()
- Randomize string length to piss off stupid detectors
This commit is contained in:
topjohnwu 2019-07-14 17:41:51 -07:00
parent 188ea2644a
commit 41045b62dc
4 changed files with 35 additions and 29 deletions

View File

@ -22,7 +22,7 @@ socklen_t setup_sockaddr(struct sockaddr_un *sun, const char *name) {
int create_rand_socket(struct sockaddr_un *sun) { int create_rand_socket(struct sockaddr_un *sun) {
memset(sun, 0, sizeof(*sun)); memset(sun, 0, sizeof(*sun));
sun->sun_family = AF_LOCAL; sun->sun_family = AF_LOCAL;
gen_rand_str(sun->sun_path + 1, 9); gen_rand_str(sun->sun_path + 1, sizeof(sun->sun_path) - 1);
int fd = xsocket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0); int fd = xsocket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
xbind(fd, (struct sockaddr*) sun, ABS_SOCKET_LEN(sun)); xbind(fd, (struct sockaddr*) sun, ABS_SOCKET_LEN(sun));
xlisten(fd, 1); xlisten(fd, 1);

View File

@ -19,15 +19,13 @@
using namespace std; using namespace std;
static void patch_socket_name(const char *path) { static void patch_socket_name(const char *path) {
uint8_t *buf; char *buf;
char name[sizeof(MAIN_SOCKET)];
size_t size; size_t size;
mmap_rw(path, buf, size); mmap_rw(path, buf, size);
for (int i = 0; i < size; ++i) { for (int i = 0; i < size; ++i) {
if (memcmp(buf + i, MAIN_SOCKET, sizeof(MAIN_SOCKET)) == 0) { if (memcmp(buf + i, MAIN_SOCKET, sizeof(MAIN_SOCKET)) == 0) {
gen_rand_str(name, sizeof(name)); gen_rand_str(buf + i, sizeof(MAIN_SOCKET));
memcpy(buf + i, name, sizeof(name)); i += sizeof(MAIN_SOCKET);
i += sizeof(name);
} }
} }
munmap(buf, size); munmap(buf, size);
@ -50,14 +48,10 @@ static void patch_init_rc(FILE *rc) {
fprintf(rc, "%s", line.data()); fprintf(rc, "%s", line.data());
return true; return true;
}); });
char pfd_svc[8], ls_svc[8], bc_svc[8]; char pfd_svc[32], ls_svc[32], bc_svc[32];
// Make sure to be unique gen_rand_str(pfd_svc, sizeof(pfd_svc));
pfd_svc[0] = 'a'; gen_rand_str(ls_svc, sizeof(ls_svc));
ls_svc[0] = '0'; gen_rand_str(bc_svc, sizeof(bc_svc));
bc_svc[0] = 'A';
gen_rand_str(pfd_svc + 1, sizeof(pfd_svc) - 1);
gen_rand_str(ls_svc + 1, sizeof(ls_svc) - 1);
gen_rand_str(bc_svc + 1, sizeof(bc_svc) - 1);
LOGD("Inject magisk services: [%s] [%s] [%s]\n", pfd_svc, ls_svc, bc_svc); LOGD("Inject magisk services: [%s] [%s] [%s]\n", pfd_svc, ls_svc, bc_svc);
fprintf(rc, magiskrc, pfd_svc, pfd_svc, ls_svc, bc_svc, bc_svc); fprintf(rc, magiskrc, pfd_svc, pfd_svc, ls_svc, bc_svc, bc_svc);
} }

View File

@ -10,7 +10,6 @@ extern "C" {
unsigned get_shell_uid(); unsigned get_shell_uid();
int fork_dont_care(); int fork_dont_care();
int fork_no_zombie(); int fork_no_zombie();
void gen_rand_str(char *buf, int len);
int strend(const char *s1, const char *s2); int strend(const char *s1, const char *s2);
char *rtrim(char *str); char *rtrim(char *str);
void init_argv0(int argc, char **argv); void init_argv0(int argc, char **argv);
@ -24,6 +23,8 @@ int parse_int(const char *s);
#include <functional> #include <functional>
#include <string_view> #include <string_view>
void gen_rand_str(char *buf, int len, bool varlen = true);
#define str_contains(s, ss) ((ss) != nullptr && (s).find(ss) != std::string::npos) #define str_contains(s, ss) ((ss) != nullptr && (s).find(ss) != std::string::npos)
#define str_starts(s, ss) ((ss) != nullptr && (s).compare(0, strlen(ss), ss) == 0) #define str_starts(s, ss) ((ss) != nullptr && (s).compare(0, strlen(ss), ss) == 0)

View File

@ -1,6 +1,10 @@
/* misc.cpp - Store all functions that are unable to be catagorized clearly /* misc.cpp - Store all functions that are unable to be catagorized clearly
*/ */
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/prctl.h>
#include <sys/sysmacros.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -8,10 +12,7 @@
#include <pwd.h> #include <pwd.h>
#include <unistd.h> #include <unistd.h>
#include <syscall.h> #include <syscall.h>
#include <sys/types.h> #include <random>
#include <sys/wait.h>
#include <sys/prctl.h>
#include <sys/sysmacros.h>
#include <logging.h> #include <logging.h>
#include <utils.h> #include <utils.h>
@ -49,17 +50,27 @@ int fork_no_zombie() {
return 0; return 0;
} }
static bool rand_init = false; constexpr char ALPHANUM[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
static bool seeded = false;
void gen_rand_str(char *buf, int len) { static std::mt19937 gen;
constexpr const char base[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; static std::uniform_int_distribution<int> dist(0, sizeof(ALPHANUM) - 1);
if (!rand_init) { void gen_rand_str(char *buf, int len, bool varlen) {
srand(time(nullptr)); if (!seeded) {
rand_init = true; if (access("/dev/urandom", F_OK) == 0) {
std::random_device rdev;
gen.seed(rdev());
} else {
// In magiskinit
gen.seed(time(nullptr));
} }
for (int i = 0; i < len - 1; ++i) { seeded = true;
buf[i] = base[rand() % (sizeof(base) - 1)];
} }
if (varlen) {
std::uniform_int_distribution<int> len_dist(len / 2, len);
len = len_dist(gen);
}
for (int i = 0; i < len - 1; ++i)
buf[i] = ALPHANUM[dist(gen)];
buf[len - 1] = '\0'; buf[len - 1] = '\0';
} }