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) {
memset(sun, 0, sizeof(*sun));
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);
xbind(fd, (struct sockaddr*) sun, ABS_SOCKET_LEN(sun));
xlisten(fd, 1);

View File

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

View File

@ -10,7 +10,6 @@ extern "C" {
unsigned get_shell_uid();
int fork_dont_care();
int fork_no_zombie();
void gen_rand_str(char *buf, int len);
int strend(const char *s1, const char *s2);
char *rtrim(char *str);
void init_argv0(int argc, char **argv);
@ -24,6 +23,8 @@ int parse_int(const char *s);
#include <functional>
#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_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
*/
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/prctl.h>
#include <sys/sysmacros.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
@ -8,10 +12,7 @@
#include <pwd.h>
#include <unistd.h>
#include <syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/prctl.h>
#include <sys/sysmacros.h>
#include <random>
#include <logging.h>
#include <utils.h>
@ -49,17 +50,27 @@ int fork_no_zombie() {
return 0;
}
static bool rand_init = false;
void gen_rand_str(char *buf, int len) {
constexpr const char base[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
if (!rand_init) {
srand(time(nullptr));
rand_init = true;
constexpr char ALPHANUM[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
static bool seeded = false;
static std::mt19937 gen;
static std::uniform_int_distribution<int> dist(0, sizeof(ALPHANUM) - 1);
void gen_rand_str(char *buf, int len, bool varlen) {
if (!seeded) {
if (access("/dev/urandom", F_OK) == 0) {
std::random_device rdev;
gen.seed(rdev());
} else {
// In magiskinit
gen.seed(time(nullptr));
}
seeded = true;
}
for (int i = 0; i < len - 1; ++i) {
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';
}