大语言模型能从十六进制转储中逐字节读写可执行文件
摘要
一篇研究博客表明,大语言模型无需编译器或反汇编器,即可从十六进制转储中逐字节读取、修补甚至完整编写 ELF 可执行文件——包括识别程序、发现一个真实存在的统计学缺陷,以及分析一个与沙箱行为相吻合的恶意软件样本。
暂无内容
查看缓存全文
缓存时间: 2026/11/21 15:17
# 在机器码空间中编程:LLM 可以逐字节读写可执行文件
Source: https://sgiannopoulos.github.io/blog/machine-code-programming/
几乎没有人手写机器码。我们编写源码,由编译器和链接器把它变成字节;要反过来,则借助反汇编器和反编译器。这样做有充分的理由:x86-64 指令长度不一,跳转是相对字节距离,头部字段错一个文件就加载不了。那么,当一个 LLM 没有这些工具时,它能拿这些字节做什么?
Takehi 等人最近展示了 LLM 可以直接读写神经网络权重,他们称之为 Weight-Space Programming(权重空间编程)。我们就另一个同样被我们视为"不可读产物"的东西提出同样的问题:编译后的代码。
我们把**Machine Code Programming(机器码编程)**定义为:通用 LLM 直接针对可执行文件的字节进行推理,中间没有编译器、汇编器、反汇编器,也没有执行过程。我们测试了三种能力,外加一个真实案例:
我们把三个自己编写的程序的十六进制转储,加上一个来自 FreeBSD 的真实二进制文件`ministat`,喂给模型。LLM**只以字节为输入,就正确识别出了全部四个程序分别是什么**。
最难的案例是`ministat`。我们用`-O3`构建了 FreeBSD 的这个工具,静态链接 musl,strip 掉符号,并移除全部自有文本,然后把最后三个字符串的每个字节的最高位置 1 来隐藏它们。LLM**找到了解码循环,恢复了选项列表,并描述出了全部八个选项**。
让我们意外的是,LLM 甚至**仅凭字节就发现了一个真实的 bug**,全程没有访问源码。该二进制文件没有符号,也没有任何解释该表的文本。把*t*表当作原始 64 位浮点数读取时,它注意到**标注为 99.5% 置信度的选项实际使用的是约 99.8% 的临界值**,因此`-c 99.5`施加的检验比它声称的更严格。我们用 Claude 在原始源码中确认了这个 bug。
对每个程序,我们要求添加一个新功能。模型回复一串字节编辑,每处编辑与其替换的字节长度相同。随后的隐藏测试会检查旧功能是否仍然正常、新功能是否生效,以及长输入是否被正确处理。**三个打过补丁的程序全部通过了每一项测试。**
**它是怎么做到的?**Sol 6.1 的做法就像人类给二进制文件打补丁。它找到填充区域,通过修改段头使该填充区域可执行,在那里写入新代码,然后用跳转到该处的调用覆盖已有指令。下面是完整的`wordcount`补丁:
现在连二进制文件都没有了。这一次我们要求 LLM 依据一份行为规格说明,产出一个功能完整的 ELF64 程序。它没有汇编器也没有编译器,必须回复文件的每一个字节,包括头部。**三个文件全部运行成功,且在每个用例上都与参考实现一致。**
Sol 6.1 在没有汇编器的情况下,自行推导出了每一个跳转、调用和头部字段。扩展一个程序所用的输出 token 比从头写一个程序少 1.8 到 3.7 倍。
恶意软件分析师会花上数小时,用反汇编器和反编译器把样本还原成汇编或 C 代码。我们把一个真实样本交给 Sol 6.1,使用与实验一相同的提示词,且不透露它是恶意的。这是一个 4,608 字节的 PE32+ 文件,唯一可见的导入是`Sleep`。它的其他 API 调用隐藏在名称哈希之后,字符串则是在栈上构造的。211 秒内,Sol 6.1 得出结论:它**下载机器码并在内存中运行**。它的报告指出了一个硬编码的服务器——`165.154.205.34`,端口 443。随后我们把它与同一文件的一份公开 VMRay 动态分析结果进行比对(通过 SHA-256 匹配)。**沙箱访问了同一个地址,并请求了同一个 URL。**
这些比对是在运行之后进行的,且只覆盖了一个样本和一次运行。沙箱证实了地址、请求和下载行为,但没有证实 10 秒重试和 XOR 密钥本身。右移 13 位的哈希在公开工具中也很常见,因此识别过程可能也从中受益。我们没有要求 Sol 6.1 修改或编写任何恶意软件。
这些是在小程序上得到的早期结果,但它们**可能很快会动摇逆向工程的根基**。反汇编器、反编译器以及它们产出的汇编代码,存在的意义是把字节变成人能读懂的东西。在我们的实验中,LLM 根本不需要这一步:它直接读取、修补并写出机器码。这些工具对人类分析师仍然必不可少,但**对 LLM 来说,它们可能最终是可有可无的**。
恶意软件分析也会受到影响。**一个直接在机器码空间中工作的模型**,可以在不先把它提升为汇编或源码的情况下分析样本。这去掉了分诊流程中最慢、最依赖工具的环节,因此可以用更少的精力分析更多样本。我们的结果表明,**通用 LLM 无需专门训练就能做到这一点**,这可能让检测和分析都变得更高效。
**上下文长度是硬性限制。**十六进制转储每字节大约消耗 2.4 个 token,因此我们那个 67 KB 的二进制约用了 163,000 个 token。一个 10 MB 的程序将需要约 2,400 万个 token。所以这种一次性方法无法读取非常大的程序,而且由于我们最大的测试只有 67 KB,我们并不知道质量在逼近这个极限的过程中会如何变化。
一条出路是基于 Recursive Language Model(递归语言模型)的框架。RLM 把长提示词保存在模型上下文之外,作为 REPL 中的一个变量,让模型可以检查其中的片段并对它们调用自身。对于二进制文件,模型可以先读取头部,按偏移量追踪调用和跳转,然后把每个函数交给一次子调用。
读取原始字节是恶意软件检测中的一个成熟问题,已有为这项任务训练的模型(如 MalConv)来应对。近期的 Large Byte Model 用字节 token 扩展了 LLM 的词表,其前提是 LLM"无法处理原始字节表示"。我们这个小样本表明,通用前沿模型现在能做到的比这暗示的更多——至少在字节以十六进制写出时是如此。
其他工作把模型应用于汇编,包括 SLaDe 反编译器以及面向汇编的生成模型 Nova。BinMetric 基准测试发现,LLM 面临的挑战依然存在,尤其是在二进制提升(binary lifting)和汇编合成方面。Project Ire 使用 Ghidra、angr 等工具大规模地逆向分析并对恶意软件分类;它是一个比我们"无工具"测试庞大得多的系统,两者不可比较。
我们没有找到先前关于通用 LLM 在无工具情况下修补并写出可运行可执行文件的记述,尽管我们的检索并不全面。
我们用另一个框架 opencode、另一个模型 DeepSeek V4.1 Flash(其权重以 MIT 许可证公开:https://huggingface.co/deepseek-ai/DeepSeek-V4.1-Flash)重做了实验四。它在高推理强度下收到同样的样本和同样的请求,只有一次回复,且未记录任何工具调用。这是**该能力并不局限于闭源前沿模型的初步证据**。
**它是怎么做到的?**追踪记录显示 DeepSeek 把字节当作十六进制来读。我们把三条推导与可执行文件进行了核对。DeepSeek 得出了相同结论,但用了约 17 倍的推理 token。它反复修正自己的读取结果,并明确回忆起熟悉的 API 哈希。13 个隐藏函数名中它答对了 9 个,其中两处命名错误导致它误读了标记文件检查。
阅读 DeepSeek 的思考过程
以下是提供的推理记录,包括它的错误起点、修正和不准确的猜测。这是模型输出,不是经过验证的分析。文本原样复制。
```
We need answer detailed report no tools. Need analyze binary hex. It's Windows PE x64, likely shellcode loader / meterpreter-ish? Need reverse engineer. We must not use tools; only given hex. Need produce detailed report. Let's decode. PE header: MZ, PE at 0xc0. Machine 0x8664 (x64). Number sections 6? SizeOfOptionalHeader 0xf0? Characteristics 0x22 (executable, large address aware? 0x22 = executable image, large address aware). Magic 0x20b PE32+. Sections: .text VA 0x1000 size raw 0x831? virtual 0x831? Let's parse. At 0x1c8 section table. First: Name .text, VirtualSize 0x831, VirtualAddress 0x1000, SizeOfRawData 0xa00? At 0x1d0: 31 08 00 00 = 0x831; 00 10 00 00 VA 0x1000; 00 0a 00 00 raw size 0xa00; 00 04 00 00 pointer raw 0x400; relocs 0; linenums 0; numreloc 0; characteristics 0x60000020 code execute read. .rdata at 0x1f0? Name at 0x1f8? Actually section 2 starts 0x1e8? Let's compute: section table starts 0xc0+4+20+0xf0 = 0x1c8? PE offset 0xc0. PE sig 4, COFF 20 -> 0xd8. Optional header 0xf0? At 0xd8 optional starts 0xe0? Wait optional header starts 0xd8? Let's recalc. At 0xc0 PE\0\0. COFF header at 0xc4: machine 64 86, numsections 03 00 (3), timestamp fe 75 3a 65, ptr sym 0, num sym 0, size opt 0xf0 at 0xd4? COFF bytes: 0xc4-0xc5 machine 0xc6-0xc7 numsections = 03 00 (3 sections) 0xc8-0xcb timestamp 0xcc-0xcf pointer symbol 0xd0-0xd3 number symbols 0xd4-0xd5 size optional 0xf0 0xd6-0xd7 characteristics 0x22 Optional at 0xd8, 0xf0 bytes -> ends 0x1c8. Section headers at 0x1c8. But data at 0x1c8 shows ".text"? Actually at 0x1c0 row: 00 00 00 00 00 00 00 00 2e 74 65 78 74 00 00 00. So section at 0x1c8 indeed name ".text". Good. Sections: .text VA 0x1000 vsize 0x831 rawsize 0xa00 rawptr 0x400. .rdata at 0x1f0? Name `2e 72 64 61 74 61 00 00` starting 0x1f0? Wait 0x1e8 row has 00 00 00 00 00 00 00 00 2e 72... Actually row 0x1e0: 00 00 00 00 00 00 00 00 00 00 00 00 20 00 00 60. So first section header spans 0x1c8-0x1ef. Second at 0x1f0: 2e 72 64 61 74 61 00 00. vsize be 01 00 00=0x1be, VA 0x2000, rawsize 0x200, rawptr 0xe00, characteristics 0x40000040 = initialized data read. Third .pdata at 0x218? Actually at 0x218 row? Name .pdata: 2e 70 64 61 74 61 00 00 at 0x218. vsize 0x18? bytes 18 00 00 00 at 0x220? VA 0x3000, rawsize 0x200, rawptr 0x1000, chars 0x40000040. Wait Data at 0x240 onwards zeros. Hmm but 0x1000 in hex dump contains .pdata (exception directory) with entries, yes raw pdata at 0x1000? But rawsize 0x200. Actually header at 0x218 says ptr raw 0x1000? At 0x228: 00 00 00 00? Let's parse: Section .pdata header at 0x218: 0x218-0x21f name 0x220-0x223 vsize = 0x18 (24) 0x224-0x227 VA = 0x3000 0x228-0x22b rawsize = 0x200 0x22c-0x22f rawptr = 0x1000? Bytes at 0x220: 18 00 00 00 00 30 00 00 00 02 00 00 00 10 00 00 => yes VA 0x3000, size 0x200, ptr 0x1000. Wait rawptr 0x1000 overlaps? The .text raw is 0x400-0xdff, .rdata raw 0xe00-0xfff, .pdata raw 0x1000-0x11ff. But hexdump at 0x1000 starts pdata entries and 0x1100 zeros. However actual code extends to 0xb... within .text raw up to 0xdff? code at 0x400-0xc3f; zeros rest. .rdata at 0xe00? At 0xe00 there seems data/cert? Let's look: at 0xe00 starts `a8 21 00 00...` and 0xe60 `47 43 54 4c` etc. That's .rdata containing exception data? Wait .rdata raw 0xe00 size 0x200. It contains load config? Actually at 0xe60 "GCTL" indicates debug directory type? `47 43 54 4c` is "GCTL" (not standard debug type? 0x4c544347 = "GCTL"). Hmm could be .rdata at 0xe00. Let's analyze. At 0xe00: a8 21 00 00 = RVA 0x21a8 maybe pointer to something. Then 0xe10: 00 ... fe 75 3a 65 timestamp. At 0xe18? 0d 00 00 00 0xdc... This looks debug directory entry: characteristics 0, timestamp, major 0, minor 0, type? Wait debug directory entries are 28 bytes. At 0xe08? Let's align: maybe .rdata starts at 0xe00 raw but with data? Hmm section raw pointer could be 0xe00 and .rdata data starts with something like import directory? At 0xe00 there is `00 00 00 00 00 00 00 00 a8 21 00 00...`? Let's check 0xe00 row: `a8 21 00 00 00 00 00 00 00 00 00 00 00 00 00 00`. 0xe10: `00 00 00 00 fe 75 3a 65 00 00 00 00 0d 00 00 00`. 0xe20 `dc 00 00 00 60 20 00 00 60 0e 00 00 00 00 00 00`. This actually debug directory maybe at 0xe08: Characteristics 0, TimeDateStamp 0x653a75fe at 0xe0c? Wait bytes: 0xe08-0xe0b = 00 00 00 00; 0xe0c-0xe0f = 00 00 00 00; 0xe10-0xe13 = 00 00 00 00; 0xe14-0xe17 = fe 75 3a 65. Not. Maybe section raw pointer for .rdata is 0xe00, and at RVA 0x2000 data starts at file 0xe00. The content should include import directory, exception directory, load config, etc. But typical .rdata begins with IAT or debug? Let's parse optional data directories from optional header at 0xe0 onwards to know RVAs. Optional header PE32+: 0xe0 magic 0x20b (bytes 0b 02) 0xe2 linker version 0e 24 (14.36?). 0xe4 SizeOfCode 0x00000a00? bytes 00 0a 00 00 0xe8 SizeOfInitializedData 0x400? bytes 00 04 00 00 0xec SizeOfUninitializedData 0 0xf0 AddressOfEntryPoint 0x1000? bytes 00 10 00 00 yes. 0xf4 BaseOfCode 0x1000. 0xf8 ImageBase 0x140000000? bytes at 0xf8: 00 00 40 01 00 00 00 00 = 0x140000000. 0x100 SectionAlignment 0x1000. 0x104 FileAlignment 0x200. 0x108 OS version 6.0 0x10c image version 0 0x110 subsystem version 6.0 0x114 Win32Version 0 0x118 SizeOfImage 0x4000 0x11c SizeOfHeaders 0x400 0x120 Checksum 0 0x124 Subsystem 0x2? Wait bytes at 0x118 row: 00 40 00 00 00 04 00 00 00 00 00 00 02 00 60 81. 0x118 00 40 00 00 (SizeOfImage 0x4000), 0x11c 00 04 00 00 (headers 0x400), 0x120 00 00 00 00 checksum, 0x124 02 00 subsystem = GUI? 2 = Windows GUI, 0x126 DllCharacteristics 0x8160 (high entropy VA, dynamic base, NX, terminal server aware?). 0x128 SizeOfStackReserve 0x100000, 0x130 stack commit 0x1000, 0x138 heap reserve 0x100000, 0x140 heap commit 0x1000, 0x148 loader flags 0, 0x14c NumberOfRvaAndSizes 0x10. Data directories start 0x150: 0x150 export RVA 0? size 0? `6c 21 00 00 28 00 00 00` wait at 0x150 is `6c 21 00 00 28 00 00 00` yes! Export directory RVA 0x216c, size 0x28. Hmm .rdata at VA 0x2000, export at 0x216c. Interesting. It exports something. 0x158 import RVA 0? Let's parse rows: 0x150: 6c 21 00 00 28 00 00 00 = export dir RVA 0x216c size 0x28 0x158: 00 00 00 00 00 00 00 00 import none? 0x160: 00 30 00 00 18 00 00 00 = exception table RVA 0x3000 size 0x18 0x168: 00 00 00 00 00 00 00 00 security none 0x170: 00 00 00 00 00 00 00 00 reloc none 0x178: 00 00 00 00 00 00 00 00 debug? Wait at 0x170 row: `00 00 00 00 00 00 00 00 00 10 20 00 00 38 00 00`. Let's map: 0x170-0x177 = 0, 0x178-0x17f = 0x201000? Actually bytes 0x178: 00 10 20 00 00 38 00 00 => RVA 0x201000? That seems unlikely. Let's recalc data dirs start after 0x14c NumberOfRvaAndSizes: 0x14c-0x14f = 10 00 00 00. First dir at 0x150. Yes: 0x150-0x157 export: 6c 21 00 00 28 00 00 00 => RVA 0x216c,size 0x28. 0x158-0x15f import: 00 00 00 00 00 00 00 00. 0x160-0x167 exception: 00 30 00 00 18 00 00 00 => RVA 0x3000,size 0x18. 0x168-0x16f security: 0. 0x170-0x177 base reloc: 0. 0x178-0x17f debug: 00 00 00 00 00 00 00 00? Wait row 0x170: 00 00 00 00 00 00 00 00 00 10 20 00 00 38 00 00. Row 0x180: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00. Rows: 0x170 bytes = 00 00 00 00 00 00 00 00 00 10 20 00 00 38 00 00. So 0x170-0x177 = 0 (reloc), 0x178-0x17f = `00 10 20 00 00 38 00 00`? Wait that's 8 bytes starting at 0x178: 00 10 20 00 00 38 00 00. But actually 0x170 row 16 bytes: indices 0-7 = 0, indices 8-15 = 00 10 20 00 00 38 00 00. Hmm debug RVA 0x201000? Did I mis-shift? Let's check optional header data directory count. Maybe 0x14c is number of dirs but directories start 0x150? Yes. 16 entries * 8 = 128, ends 0x1d0. But section headers at 0x1c8; no, 0x150+128=0x1d0. Yet section headers at 0x1c8 per our earlier calc. Contradiction: section headers likely at 0x1d0? Let's recalc PE offset maybe 0xc0, COFF=0xc4, optional header size 0xe0? Wait at 0xd4? Let's carefully parse bytes at 0xd0: 0xd0: 00 00 00 00 f0 00 22 00 0b 02 0e 24 00 0a 00 00 So 0xd0-0xd3 = 00 00 00 00; 0xd4-0xd5 = f0 00; 0xd6-0xd7 = 22 00; optional starts 0xd8 = 0b 02. Yes optional header size 0xf0 ending 0x1c8. Directories at optional+112? PE32+ optional standard fields 24? Actually data directories start at optional header offset 112 (0x70), i.e. 0xd8+0x70=0x148, not 0x150! Let's recalc. PE32+ optional header before data dirs: magic 2 + linker 2 + size code 4 + i
相似文章
LLMs可以通过利用推理引擎控制宿主机器
本文探讨了恶意LLMs如何利用推理引擎如vLLM中的漏洞来执行任意代码并控制宿主机器,并引用真实世界的CVEs。
我意外地将 LLM 记忆变成了程序分析
作者开发了一个基于 Datalog 的 LLM 记忆系统,用于在漏洞研究等调查中保持准确状态,并能在事实变化时自动更新结论。
大语言模型辅助逆向工程
一位拥有二十年经验的软硬件黑客介绍了 AI 智能体如何在 2026 年大幅加快逆向工程,并详细讲解了一套工作流程,涵盖反编译器、Markdown 文档记录习惯、调试技巧,以及 Ghidra MCP 桥接和假设调试等专用智能体技能。
大语言模型编写的 PLC 代码即使能编译通过,仍然可能不安全,所以我用真正的编译器和软 PLC 测试框架为它把关
一位实践者构建了一套确定性的四关验证框架(图结构检查、安全审计、无界面 CODESYS 编译、软 PLC 扫描周期测试),用于验证大语言模型生成的 IEC 61131-3 PLC 程序,表明对于安全关键代码,厂商编译器和仿真器比充当评判者的大语言模型更可靠。
LLMs 在读写神经网络权重方面并非超人(4 分钟阅读)
Sean Goedecke 认为,近期一篇论文声称前沿 LLM 能够读取和编辑神经网络权重,但其表现并非如宣称的那般超人,因为一位聪明的 ML 研究员就能通过基础分析复现这些任务。