Fix getrandom on older versions

This commit is contained in:
Vadim Vetrov 2024-10-12 16:55:27 +03:00
parent e9b033ccca
commit 05cc0054d8
No known key found for this signature in database
GPG Key ID: E8A308689D7A73A5
3 changed files with 24 additions and 8 deletions

4
args.h
View File

@ -1,6 +1,3 @@
#include "utils.h"
#include "tls.h"
#ifndef ARGS_H #ifndef ARGS_H
#define ARGS_H #define ARGS_H
@ -11,5 +8,4 @@ int parse_args(int argc, char *argv[]);
/* Prints starting messages */ /* Prints starting messages */
void print_welcome(); void print_welcome();
#endif /* ARGS_H */ #endif /* ARGS_H */

22
tls.c
View File

@ -5,8 +5,8 @@
#include "utils.h" #include "utils.h"
#ifndef KERNEL_SPACE #ifndef KERNEL_SPACE
#include <stdlib.h> #include <fcntl.h>
#include <sys/random.h> #include <unistd.h>
#endif #endif
#define TLS_CONTENT_TYPE_HANDSHAKE 0x16 #define TLS_CONTENT_TYPE_HANDSHAKE 0x16
@ -274,6 +274,8 @@ int gen_fake_sni(struct fake_type type,
const struct tcphdr *tcph, uint32_t tcph_len, const struct tcphdr *tcph, uint32_t tcph_len,
uint8_t *buf, uint32_t *buflen) { uint8_t *buf, uint32_t *buflen) {
uint32_t data_len = type.fake_len; uint32_t data_len = type.fake_len;
int ret;
if (type.type == FAKE_PAYLOAD_RANDOM && data_len == 0) { if (type.type == FAKE_PAYLOAD_RANDOM && data_len == 0) {
data_len = (uint32_t)randint() % 1200; data_len = (uint32_t)randint() % 1200;
} }
@ -317,9 +319,21 @@ int gen_fake_sni(struct fake_type type,
default: // FAKE_PAYLOAD_RANDOM default: // FAKE_PAYLOAD_RANDOM
#ifdef KERNEL_SPACE #ifdef KERNEL_SPACE
get_random_bytes(bfdptr, data_len); get_random_bytes(bfdptr, data_len);
#else #else /* KERNEL_SPACE */
#if _NO_GETRANDOM
ret = open("/dev/urandom", O_RDONLY);
if (ret < 0) {
lgerror("Unable to open /dev/urandom", ret);
return ret;
}
read(ret, bfdptr, data_len);
close(ret);
#else /* _NO_GETRANDOM */
getrandom(bfdptr, data_len, 0); getrandom(bfdptr, data_len, 0);
#endif #endif /* _NO_GETRANDOM */
#endif /* KERNEL_SPACE */
} }
if (ipxv == IP4VERSION) { if (ipxv == IP4VERSION) {

View File

@ -14,7 +14,13 @@
#include <stdint.h> // IWYU pragma: export #include <stdint.h> // IWYU pragma: export
#include <string.h> // IWYU pragma: export #include <string.h> // IWYU pragma: export
#include <stdlib.h> // IWYU pragma: export #include <stdlib.h> // IWYU pragma: export
#define _NO_GETRANDOM ((__GLIBC__ <= 2 && __GLIBC_MINOR__ < 25))
#if !_NO_GETRANDOM
#include <sys/random.h> // IWYU pragma: export #include <sys/random.h> // IWYU pragma: export
#endif
#endif /* SPACES */ #endif /* SPACES */