Skip to content

Commit

Permalink
Fix getrandom on older versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Waujito committed Oct 12, 2024
1 parent e9b033c commit 05cc005
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
4 changes: 0 additions & 4 deletions args.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#include "utils.h"
#include "tls.h"

#ifndef ARGS_H
#define ARGS_H

Expand All @@ -11,5 +8,4 @@ int parse_args(int argc, char *argv[]);
/* Prints starting messages */
void print_welcome();


#endif /* ARGS_H */
22 changes: 18 additions & 4 deletions tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include "utils.h"

#ifndef KERNEL_SPACE
#include <stdlib.h>
#include <sys/random.h>
#include <fcntl.h>
#include <unistd.h>
#endif

#define TLS_CONTENT_TYPE_HANDSHAKE 0x16
Expand Down Expand Up @@ -274,6 +274,8 @@ int gen_fake_sni(struct fake_type type,
const struct tcphdr *tcph, uint32_t tcph_len,
uint8_t *buf, uint32_t *buflen) {
uint32_t data_len = type.fake_len;
int ret;

if (type.type == FAKE_PAYLOAD_RANDOM && data_len == 0) {
data_len = (uint32_t)randint() % 1200;
}
Expand Down Expand Up @@ -317,9 +319,21 @@ int gen_fake_sni(struct fake_type type,
default: // FAKE_PAYLOAD_RANDOM
#ifdef KERNEL_SPACE
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);
#endif
#endif /* _NO_GETRANDOM */
#endif /* KERNEL_SPACE */
}

if (ipxv == IP4VERSION) {
Expand Down
6 changes: 6 additions & 0 deletions types.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
#include <stdint.h> // IWYU pragma: export
#include <string.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
#endif

#endif /* SPACES */

Expand Down

0 comments on commit 05cc005

Please sign in to comment.