【问题标题】:Edit an executable with bash without recompiling it?使用 bash 编辑可执行文件而不重新编译它?
【发布时间】:2018-10-02 18:31:29
【问题描述】:

我正在尝试以这种方式编写用于编辑可执行文件(例如 /bin/bash)的脚本:

搜索一个字符串 - 用新字符串替换旧字符串 - 保存对原始文件的更改(显然,出于安全原因,我正在尝试使用 /bin/bash 的副本)

我的字符串是一个单词。我该怎么做?

【问题讨论】:

  • 如果两个字符串的长度相同:sed
  • 但是 sed 不会将我的替换保存在原始文件中,对吗?我想编辑我的可执行文件
  • 您可以使用重定向command ... infile > outfile 自己保存结果,或者使用某些sed 实现的-i 选项。

标签: bash executable binaryfiles editing


【解决方案1】:

您可以使用sed 轻松修改二进制文件中的字符串,但如 在 cmets 中指出,它通常仅在新旧字符串具有相同长度时才有效。以bash 为例:

$ cp /bin/bash .
$ strings bash  | grep such
describe_pid: %ld: no such pid
    it creates, on systems that allow such control.
          such as cd which change the current directory.
%s: no such job
$ sed 's,no such job,no such boj,' -i bash
$ ./bash --noprofile --norc
bash-4.3$ kill %3333
bash: kill: %3333: no such boj

-i for sed 不在中 POSIX 所以如果你想实现最大的便携性:

$ cp /bin/bash .
$ sed 's,no such job,no such boj,' bash > bash.bak
$ mv bash.bak bash
$ chmod +x ./bash
$ ./bash --noprofile --norc
bash-4.3$ kill %3333
bash: kill: %3333: no such boj

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    • 2015-01-19
    相关资源
    最近更新 更多