|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Extract cli.js from Claude Code native binary |
| 4 | + * Supports ELF (Linux) and Mach-O (macOS) formats |
| 5 | + */ |
| 6 | + |
| 7 | +const LIEF = require("node-lief"); |
| 8 | +const fs = require("fs"); |
| 9 | + |
| 10 | +const BUN_TRAILER = Buffer.from("\n---- Bun! ----\n"); |
| 11 | +const SIZEOF_OFFSETS = 32; |
| 12 | +const SIZEOF_STRING_POINTER = 8; |
| 13 | +const SIZEOF_MODULE_V1 = 4 * SIZEOF_STRING_POINTER + 4; // 36 bytes (Bun < 1.3.9) |
| 14 | +const SIZEOF_MODULE_V2 = 4 * SIZEOF_STRING_POINTER + 4 + 16; // 52 bytes (Bun >= 1.3.9, added extra field) |
| 15 | + |
| 16 | +function detectModuleSize(modulesListLength) { |
| 17 | + if (modulesListLength % SIZEOF_MODULE_V2 === 0) return SIZEOF_MODULE_V2; |
| 18 | + if (modulesListLength % SIZEOF_MODULE_V1 === 0) return SIZEOF_MODULE_V1; |
| 19 | + return SIZEOF_MODULE_V1; // fallback |
| 20 | +} |
| 21 | + |
| 22 | +function parseStringPointer(buffer, offset) { |
| 23 | + return { offset: buffer.readUInt32LE(offset), length: buffer.readUInt32LE(offset + 4) }; |
| 24 | +} |
| 25 | + |
| 26 | +function parseOffsets(buffer) { |
| 27 | + let pos = 0; |
| 28 | + const byteCount = buffer.readBigUInt64LE(pos); pos += 8; |
| 29 | + const modulesPtr = parseStringPointer(buffer, pos); pos += 8; |
| 30 | + const entryPointId = buffer.readUInt32LE(pos); pos += 4; |
| 31 | + const compileExecArgvPtr = parseStringPointer(buffer, pos); |
| 32 | + return { byteCount, modulesPtr, entryPointId, compileExecArgvPtr }; |
| 33 | +} |
| 34 | + |
| 35 | +function getStringPointerContent(buffer, sp) { |
| 36 | + return buffer.subarray(sp.offset, sp.offset + sp.length); |
| 37 | +} |
| 38 | + |
| 39 | +function parseModule(buffer, offset) { |
| 40 | + let pos = offset; |
| 41 | + return { |
| 42 | + name: parseStringPointer(buffer, pos), contents: parseStringPointer(buffer, pos + 8), |
| 43 | + sourcemap: parseStringPointer(buffer, pos + 16), bytecode: parseStringPointer(buffer, pos + 24), |
| 44 | + encoding: buffer.readUInt8(pos + 32), loader: buffer.readUInt8(pos + 33), |
| 45 | + moduleFormat: buffer.readUInt8(pos + 34), side: buffer.readUInt8(pos + 35) |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +function isClaudeModule(name) { |
| 50 | + return name.endsWith("/claude") || name === "claude" || |
| 51 | + name.endsWith("/claude.exe") || name === "claude.exe"; |
| 52 | +} |
| 53 | + |
| 54 | +function extractBunDataFromSection(sectionData) { |
| 55 | + // Try u64 header first (Bun >= 1.3.4), then u32 |
| 56 | + const bunDataSizeU64 = sectionData.length >= 8 ? Number(sectionData.readBigUInt64LE(0)) : 0; |
| 57 | + const bunDataSizeU32 = sectionData.readUInt32LE(0); |
| 58 | + |
| 59 | + let headerSize, bunDataSize; |
| 60 | + if (sectionData.length >= 8 && 8 + bunDataSizeU64 <= sectionData.length && 8 + bunDataSizeU64 >= sectionData.length - 4096) { |
| 61 | + headerSize = 8; bunDataSize = bunDataSizeU64; |
| 62 | + } else if (4 + bunDataSizeU32 <= sectionData.length && 4 + bunDataSizeU32 >= sectionData.length - 4096) { |
| 63 | + headerSize = 4; bunDataSize = bunDataSizeU32; |
| 64 | + } else { |
| 65 | + throw new Error("Cannot determine section header format"); |
| 66 | + } |
| 67 | + |
| 68 | + const bunDataContent = sectionData.subarray(headerSize, headerSize + bunDataSize); |
| 69 | + const trailerStart = bunDataContent.length - BUN_TRAILER.length; |
| 70 | + const offsetsStart = bunDataContent.length - SIZEOF_OFFSETS - BUN_TRAILER.length; |
| 71 | + const offsetsBytes = bunDataContent.subarray(offsetsStart, offsetsStart + SIZEOF_OFFSETS); |
| 72 | + |
| 73 | + return { bunOffsets: parseOffsets(offsetsBytes), bunData: bunDataContent, sectionHeaderSize: headerSize }; |
| 74 | +} |
| 75 | + |
| 76 | +function extractFromELF(binary) { |
| 77 | + if (!binary.hasOverlay) throw new Error("ELF binary has no overlay data"); |
| 78 | + const overlay = binary.overlay; |
| 79 | + const offsetsStart = overlay.length - 8 - BUN_TRAILER.length - SIZEOF_OFFSETS; |
| 80 | + const offsetsBytes = overlay.subarray(offsetsStart, overlay.length - 8 - BUN_TRAILER.length); |
| 81 | + const bunOffsets = parseOffsets(offsetsBytes); |
| 82 | + const tailDataLen = 8 + BUN_TRAILER.length + SIZEOF_OFFSETS; |
| 83 | + const dataStart = overlay.length - tailDataLen - Number(bunOffsets.byteCount); |
| 84 | + const dataRegion = overlay.subarray(dataStart, overlay.length - tailDataLen); |
| 85 | + const trailerBytes = overlay.subarray(overlay.length - 8 - BUN_TRAILER.length, overlay.length - 8); |
| 86 | + return { bunOffsets, bunData: Buffer.concat([dataRegion, offsetsBytes, trailerBytes]) }; |
| 87 | +} |
| 88 | + |
| 89 | +function extractFromMachO(binary) { |
| 90 | + const bunSegment = binary.getSegment("__BUN"); |
| 91 | + if (!bunSegment) throw new Error("__BUN segment not found"); |
| 92 | + const bunSection = bunSegment.getSection("__bun"); |
| 93 | + if (!bunSection) throw new Error("__bun section not found"); |
| 94 | + return extractBunDataFromSection(bunSection.content); |
| 95 | +} |
| 96 | + |
| 97 | +function extract(binaryPath) { |
| 98 | + LIEF.logging.disable(); |
| 99 | + const binary = LIEF.parse(binaryPath); |
| 100 | + |
| 101 | + let bunData, bunOffsets; |
| 102 | + if (binary.format === "ELF") { |
| 103 | + ({ bunData, bunOffsets } = extractFromELF(binary)); |
| 104 | + } else if (binary.format === "MachO") { |
| 105 | + ({ bunData, bunOffsets } = extractFromMachO(binary)); |
| 106 | + } else { |
| 107 | + throw new Error(`Unsupported format: ${binary.format}`); |
| 108 | + } |
| 109 | + |
| 110 | + const modulesListBytes = getStringPointerContent(bunData, bunOffsets.modulesPtr); |
| 111 | + const moduleSize = detectModuleSize(modulesListBytes.length); |
| 112 | + const modulesCount = Math.floor(modulesListBytes.length / moduleSize); |
| 113 | + |
| 114 | + for (let i = 0; i < modulesCount; i++) { |
| 115 | + const module = parseModule(modulesListBytes, i * moduleSize); |
| 116 | + const moduleName = getStringPointerContent(bunData, module.name).toString("utf-8"); |
| 117 | + if (isClaudeModule(moduleName)) { |
| 118 | + return getStringPointerContent(bunData, module.contents); |
| 119 | + } |
| 120 | + } |
| 121 | + throw new Error("Claude module not found"); |
| 122 | +} |
| 123 | + |
| 124 | +// Main |
| 125 | +const binaryPath = process.argv[2] || `${process.env.HOME}/.local/share/claude/versions/2.1.17`; |
| 126 | +const outputPath = process.argv[3] || "/tmp/native-cli.js"; |
| 127 | + |
| 128 | +try { |
| 129 | + console.log(`Extracting from: ${binaryPath}`); |
| 130 | + const cliJs = extract(binaryPath); |
| 131 | + fs.writeFileSync(outputPath, cliJs); |
| 132 | + console.log(`Extracted to: ${outputPath} (${cliJs.length} bytes)`); |
| 133 | +} catch (err) { |
| 134 | + console.error("Error:", err.message); |
| 135 | + process.exit(1); |
| 136 | +} |
0 commit comments