在执行时,是否有一种方法可以向二进制文件中添加/删除资源?

huangapple go评论82阅读模式
英文:

Is there a way to append/remove a resource to a binary at execution time?

问题

在执行时是否可以将资源文件追加/删除到二进制文件中?

我用Go语言编写了一个应用程序,它从数据库文件中保存/搜索数据,我希望这个数据库文件能够被嵌入到二进制文件中,并由应用程序自身进行更新。

这样,应用程序就可以自包含地携带其数据库。

英文:

Is it possible to append/remove a ressource file to a binary at execution time?

I have an application written with go, which saves/searches data from a database file, and i would like this database file to be embedded to the binary, and updated by the application itself.

This way the application would be self contained with its database.

答案1

得分: 2

从技术上讲,这是可能的,但这是一个不好的主意。用户可能没有对你的二进制文件具有写权限的运行应用程序。

如果你在谈论一个便携式应用程序,你最好的选择可能是使用与二进制文件位于同一目录中的文件,否则 - 根据你运行的操作系统的约定,使用用户的主目录。你可以使用os/user包来找到主目录。

英文:

Technically, this is possible, but this is a bad idea. Your application could be run by users not having write permissions to your binary.

If you're talking about a portable app, your best option might be using a file in the same directory the binary is located, otherwise - use the user's home directory according to the conventions of the OS you're running on. You can use the os/user package to find the home directory.

答案2

得分: 2

修改可执行文件,这通常是一个非常糟糕的主意。

我脑海中立刻浮现出几个问题,比如:

  1. 当前用户是否具有足够的权限?
  2. 在执行期间文件是否被锁定?
  3. 应用程序是否有多个运行实例?

即使你设法做到了,想想杀毒软件和防火墙应用程序会对此说些什么:大多数情况下,当它们检测到变化时,会标记可执行文件和/或将其隔离,或拒绝运行它,甚至有些可能会将其删除。这是合理的,因为这正是许多病毒所做的:修改现有的可执行文件。

此外,病毒扫描器数据库会根据文件内容的哈希值来识别文件(其内容)。修改可执行文件将自然改变文件内容的哈希值,从而使文件对这些数据库来说变得未知/可疑。

如前所述,只需将数据写入/缓存到单独的文件中,最好是在用户的主文件夹或应用程序文件夹中(紧邻可执行文件,可选地放在子文件夹中)。或者将缓存文件/文件夹作为可更改的选项(命令行标志)。

英文:

Modifying the executable, this is generally a very bad idea.

Several issues pop right into my head, such as:

  1. Does the current user have sufficient permissions?
  2. Is the file locked during execution?
  3. What about multiple running instances of the application?

Even if you manage to do just that, think of what anti-virus and firewall applications will say to it: most when they detect the change will flag the executable and/or contain it, or deny running it, or some may even delete it. Rightfully, as this is what many viruses do: modify existing executables.

Also virus scanner databases maintain reports where files (their contents) are identified based on the hash of their content. Modifying the executable will naturally change the file content hash, thus render the file unknown / suspicious to these databases.

As mentioned, just write / cache data in separate file(s), preferably in user's home folder or in the application folder (next to the executable, optionally in sub-folders). Or make the cache file / folder a changeable option (command line flags).

huangapple
  • 本文由 发表于 2015年7月29日 22:56:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/31704083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定