在 Linux 下执行平面二进制文件
有没有办法在 Linux 中执行平面二进制映像,使用如下语法:
nasm -f bin -o foo.bin foo.asm
runbinary foo.bin
Is there a way to execute a flat binary image in Linux, using a syntax something like:
nasm -f bin -o foo.bin foo.asm
runbinary foo.bin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否有某种原因不想使用“-f elf”而不是“-f bin”?
我认为 Linux 不会运行非 ELF 格式的二进制文件。 我找不到将平面二进制文件转换为 ELF 的工具,但是您可以通过将 ELF 信息放入 foo.asm 来作弊,
使用此处描述的技术:
Is there some reason you don't want to use "-f elf" instead of "-f bin"?
I think Linux won't run a binary that's not in ELF format. I can't find a tool that converts flat binaries to ELF, but you can cheat by putting the ELF information in foo.asm,
using the technique described here :
至少,Linux 需要弄清楚可执行文件的格式,并且它会从第一个字节中获取该格式。 例如,如果它是一个脚本,则为 #!, shebang。 如果是 ELF,则为 0x7F 'E' 'L' 'F'。 这些神奇的数字将通过查找来确定处理程序。
所以你需要一个带有可识别的幻数的标题。 您可以在/proc/sys/fs/binfmt_misc中获取shebang支持的格式列表。 获取本机二进制格式列表(不幸的是)有点棘手。
bFLT 可能是好的选择。 事实上,它是一种流行的嵌入式可执行格式。 但你也可以将 ELF 压得很低。 本文将 ELF 可执行文件缩小到 45 字节。 也就是说,您主要用手而不是工具将其挤压。
Minimally, Linux will need to figure out the format of the executable and it will get that from the first bytes. For example, if it's a script that will be #!, shebang. If it's ELF that will be 0x7F 'E' 'L' 'F'. Those magic numbers will determine the handler from a lookup.
So you're gonna need a header with a recognized magic number. You can get a list of shebang supported formats in /proc/sys/fs/binfmt_misc. Getting a list of native binary formats is (unfortunately) a little trickier.
bFLT may be a good choice. Indeed, it's a popular embedded executable format. But you can also squeeze ELF down quite far. This article got an ELF executable down to 45 bytes. That said, you'd be squeezing it down mostly by hand rather than by tool.
Linux 内核可以加载多种不同的二进制格式 - ELF 是最常见的,尽管 a.out 格式也很众所周知。
支持的二进制格式由加载或编译到内核的 binfmt 模块控制(它们位于内核配置的文件系统部分)。 uClinux BFLT 平面格式二进制文件有一个
binfmt_flat
,它非常小 - 它们甚至可以被 zlib 压缩,这将使您的二进制文件变得更小,所以这可能是一个不错的选择。看起来 nasm 本身并不支持这种格式,但是像 Jim Lewis 为 ELF 描述的那样,手动添加必要的标头非常容易。 有格式的描述这里< /a>.
The Linux kernel can load several different binary formats - ELF is just the most common, though the a.out format is also pretty well known.
The supported binary formats are controlled by which binfmt modules are loaded or compiled in to the kernel (they're under the Filesystem section of the kernel config). There's a
binfmt_flat
for uClinux BFLT flat format binaries which are pretty minimal - they can even be zlib compressed which will let you make your binary even smaller, so this could be a good choice.It doesn't look like nasm natively supports this format, but it's pretty easy to add the necessary header manually as Jim Lewis describes for ELF. There's a description of the format here.