在 Linux 下执行平面二进制文件

发布于 2024-08-01 18:54:00 字数 113 浏览 3 评论 0原文

有没有办法在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

驱逐舰岛风号 2024-08-08 18:54:01

您是否有某种原因不想使用“-f elf”而不是“-f bin”?

我认为 Linux 不会运行非 ELF 格式的二进制文件。 我找不到将平面二进制文件转换为 ELF 的工具,但是您可以通过将 ELF 信息放入 foo.asm 来作弊,
使用此处描述的技术:

我们可以看看ELF
规格,以及
/usr/include/linux/elf.h 和
由标准创建的可执行文件
工具,找出我们的空
ELF 可执行文件应该如下所示。 但,
如果你是不耐烦的类型,你可以
只需使用我在这里提供的:

<前><代码> 位 32

组织 0x08048000

欧洲人类发展报告:; Elf32_Ehdr
db 0x7F, "ELF", 1, 1, 1, 0 ; e_ident
乘以 8 分贝 0
dw 2 ; e_类型
dw 3 ; 电子机器
dd 1 ; e_版本
dd_start; 电子条目
dd phdr - $$; e_phoff
dd 0 ; 埃_肖夫
dd 0 ; e_flags
dw ehdr 大小; e_ehsize
dw phdr大小; e_phentsize
dw 1 ; e_phnum
dw 0 ; e_shentsize
dw 0 ; e_shnum
dw 0 ; e_shstrndx

ehdrsize equ $ - ehdr

博士:; Elf32_博士
dd 1 ; p_类型
dd 0 ; p_偏移量
dd $$; p_vaddr
dd $$; p_paddr
dd 文件大小; p_filesz
dd 文件大小; p_memsz
dd 5 ; p_flags
dd 0x1000; p_align

phdrsize equ $ - phdr

_开始:

; 你的程序在这里

文件大小等于 $ - $$

该图像包含 ELF 标头,
将该文件识别为 Intel 386
可执行文件,没有节头
表和程序头表
包含一个条目。 所说条目
指示程序加载器加载
整个文件进入内存(它是
程序的正常行为
包括它的 ELF 头文件和程序
其内存映像中的头表)
从内存地址0x08048000开始
(这是默认地址
要加载的可执行文件),然后开始
执行_start处的代码,其中
紧接在节目之后出现
头表。 没有.data段,没有
.bss 段,无注释 — 无
但最基本的必需品。

那么,让我们添加我们的小程序:

<前><代码>; 微小的asm
组织 0x08048000

;
; (如上)
;

_start: mov bl, 42 xor eax, eax inc eax int 0x80 文件大小 equ $ - $$

并尝试一下:

 $ nasm -f bin -o a.out tiny.asm 
   $ chmod +x a.out 
   $ ./a.out ;   回声$? 
   42 
  

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 :

We can look at the ELF
specification, and
/usr/include/linux/elf.h, and
executables created by the standard
tools, to figure out what our empty
ELF executable should look like. But,
if you're the impatient type, you can
just use the one I've supplied here:

 BITS 32

               org     0x08048000

 ehdr:                                                 ; Elf32_Ehdr
               db      0x7F, "ELF", 1, 1, 1, 0         ;   e_ident
       times 8 db      0
               dw      2                               ;   e_type
               dw      3                               ;   e_machine
               dd      1                               ;   e_version
               dd      _start                          ;   e_entry
               dd      phdr - $                       ;   e_phoff
               dd      0                               ;   e_shoff
               dd      0                               ;   e_flags
               dw      ehdrsize                        ;   e_ehsize
               dw      phdrsize                        ;   e_phentsize
               dw      1                               ;   e_phnum
               dw      0                               ;   e_shentsize
               dw      0                               ;   e_shnum
               dw      0                               ;   e_shstrndx

 ehdrsize      equ     $ - ehdr

 phdr:                                                 ; Elf32_Phdr
               dd      1                               ;   p_type
               dd      0                               ;   p_offset
               dd      $                              ;   p_vaddr
               dd      $                              ;   p_paddr
               dd      filesize                        ;   p_filesz
               dd      filesize                        ;   p_memsz
               dd      5                               ;   p_flags
               dd      0x1000                          ;   p_align

 phdrsize      equ     $ - phdr

 _start:

 ; your program here

  filesize      equ     $ - $

This image contains an ELF header,
identifying the file as an Intel 386
executable, with no section header
table and a program header table
containing one entry. Said entry
instructs the program loader to load
the entire file into memory (it's
normal behavior for a program to
include its ELF header and program
header table in its memory image)
starting at memory address 0x08048000
(which is the default address for
executables to load), and to begin
executing the code at _start, which
appears immediately after the program
header table. No .data segment, no
.bss segment, no commentary — nothing
but the bare necessities.

So, let's add in our little program:

 ; tiny.asm
               org     0x08048000

 ;
 ; (as above)
 ;

_start: mov bl, 42 xor eax, eax inc eax int 0x80 filesize equ $ - $

and try it out:

 $ nasm -f bin -o a.out tiny.asm
 $ chmod +x a.out
 $ ./a.out ; echo $?
 42
少年亿悲伤 2024-08-08 18:54:01

至少,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.

自由如风 2024-08-08 18:54:00

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文