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:
parent
188ea2644a
commit
41045b62dc
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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));
|
||||
}
|
||||
for (int i = 0; i < len - 1; ++i) {
|
||||
buf[i] = base[rand() % (sizeof(base) - 1)];
|
||||
seeded = true;
|
||||
}
|
||||
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';
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user