将数据存储在可执行文件中 [英] Store data in executable

查看:55
本文介绍了将数据存储在可执行文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此很好奇.

应用程序是否可以在其自己的可执行文件中存储一些可变数据(如配置和选项)?

Is it possible for an application to store some changeable data (like configurations and options) inside its own executable?

例如:是否可以设计一个可执行文件,如果用户运行,设置一些配置,将其复制到另一台PC,然后该应用程序将按其在新PC中最后设置的配置运行.

for example: is it possible to design a single executable which if a user ran, set some configurations, copied it into another PC, then the application runs by its last set config in new PC.

这有可能吗?

更新:似乎有可能.然后如何?

Update: it seems that it's possible. then How?

推荐答案

是,不是-

  • 是的,可执行映像中有足够的空间可以放置数据.您可以为此添加一个预初始化的数据段,然后将数据写入其中.或资源,或者您可以滥用某些段填充空间来存储值.您可以控制链接器设置,以保证有空间.

  • Yes, there's plenty of space in an executable image you can put data. You can add a pre-initialised data segment for this, say, and write the data into there; or a resource, or you can abuse some of the segment padding space to store values in. You control the linker settings so you can guarantee there will be space.

不,您可能无法在运行时执行此操作:

No, you probably can't do this at run-time:

  1. Windows的缓存机制将锁定任何已加载的可执行文件的磁盘上的文件.这样一来,如果它需要卸载段,就不必担心将数据写到缓存中了-它可以保证它可以从磁盘上的相同位置取回相同的数据.如果操作系统确实遵守此要求,则可以通过运行.exe加载复制到临时标志之一(从CD,从网络)运行来解决此问题,或者可以写一个辅助exe来临时转移控制权要,请先卸载原始文件,然后再修改已卸载的文件.(在Linux等上,这实际上要容易得多,因为inode实际上是参考计数-即使它们具有相同的默认锁定策略,您也可以复制可执行文件,将设置编辑到副本中,然后将其移到原始文件上,同时仍在执行.)

  1. Windows' caching mechanism will lock the files on disk of any executable loaded. This is so that it doesn't need to worry about writing out the data into cache if it ever needs to unload a segment - it can guarantee that it can get the same data back from the same location on disk. You may be able to get around this by running with one of the .exe load copy-to-temp flags (from CD, from Network) if the OS actually respects that, or you can write out a helper exe to temp to transfer control to, unload the original and then modify the unloaded file. (This is much easier on Linux etc. where inodes are effectively a reference count - even if they have the same default locking strategy you can copy your executable, edit the settings into the copy and then move it over the original whilst still executing.)

几乎可以肯定的是,病毒检查程序会为此而跳动.

Virus checkers will almost certainly jump on you for this.

总的来说,我认为将设置写入注册表或某个位置,并在需要时提供和导入/导出设置选项是一个更好的主意.

In general I think it's a much better idea to just write settings to the registry or somewhere and provide and import / export settings option if you think it'd be needed.

扩展操作"部分-

为了知道将数据写入文件的位置,您实际上有两个或三个选项:

In order to know where to write the data into your file you've got two or three options really:

  1. 使用魔术字符串,例如在开始时声明具有已知序列的全局静态变量,例如-我的数据在这里---",然后有足够的空白空间来存储您的设置.打开磁盘上的文件,对其进行扫描,以按顺序进行(请注意,扫描代码实际上并不包含该字符串.片段,也就是说,您找不到扫描代码了)-然后就找到了要写入的缓冲区.执行修改后的副本后,它将在全局静态变量中已有数据.

  1. Use a magic string, e.g. declare a global static variable with a known sequence at the start, e.g. "---my data here---", followed by enough empty space to store your settings in. Open the file on disk, scan it for that sequence (taking care that the scanning code doesn't actually contain the string in one piece, i.e. so you don't find the scanning code instead) - then you've found your buffer to write to. When the modified copy is executed it'll have the data already in your global static.

理解并解析二进制文件中的可执行标头数据,以查找所使用的位置.一种方法是在链接器中将命名部分添加到二进制文件中,例如一个名为"mySettings"的4K部分将其标记为初始化数据.您可以(尽管这是我所不知道的)将其连接为外部缓冲区,您可以在代码中按名称引用以供读取.要进行编写,请在可执行文件头中找到节表,找到一个名为"mySettings"的文件,然后您将需要在二进制文件中添加偏移量.

Understand and parse the executable header data in your binary to find the location you've used. One way would be to add a named section to your binary in the linker, e.g. a 4K section called 'mySettings' flagged it as initialised data. You can (although this is a beyond my knowledge) wire this up as an external buffer you can refer to by name in your code to read from. To write, find the section table in the executable headers, find the one called 'mySettings' and you'll have the offset in the binary that you need to modify.

对需要读取/写入的缓冲区的偏移量进行硬编码.生成文件一次,在十六进制编辑器中找到偏移量,然后将其硬编码到您的程序中.由于程序段通常四舍五入到4K,因此您可能会通过较小的更改而获得相同的硬编码值,尽管它可能只是在您下面更改.

Hard-code the offset of the buffer that you need to read / write. Build the file once, find the offset in a hex editor and then hard-code it into your program. Since program segments are usually rounded up to 4K you'll probably get away with the same hard-coded value through minor changes, though it may well just change underneath you.

这篇关于将数据存储在可执行文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆