2

When I use strings filename, it lists all strings in a binary file. And now, I want to modify the strings that listed. But, how?

5
  • Do you just want to overtype one or more of the strings that are present, or do you want to replace a shorter string with a longer one? Commented Nov 26, 2021 at 19:36
  • unix.stackexchange.com/a/673242/325065
    – user313992
    Commented Nov 26, 2021 at 19:37
  • 1
    Does this answer your question? CLI: Editing binary strings Commented Nov 26, 2021 at 19:38
  • I want to replace the strings in a binary file. For example: replace abc with xyz in my program. So, my program will print xyz instead of abc.
    – Arian
    Commented Nov 26, 2021 at 19:38
  • You said that already. But you haven't answered the question I asked Commented Nov 27, 2021 at 23:15

1 Answer 1

4

If you replace a string with a string with the same length, you can edit a binary file with sed, and it will work in the context that you describe in the question and your comment.

I do it with iso files of Linux operating systems to make a persistent live drive by replacing 'quiet splash' with 'persistent ' (12 characters) in the grub menuentry. (See this link and scroll down to 'Manual method' in my answer.)

First check with strings that the string you want to replace does not appear somewhere, that should not be modified (some binary sequence, that happens to contain that string), and then run for example

< original-file sed 's/abc/xyz/g' > modified-file
7
  • How can I replace with a non-same length string?
    – Arian
    Commented Nov 26, 2021 at 20:05
  • In general you can do it (for example with sed 's/abc/wxyz/g'), and it may or may not work depending on the kind of binary file that you modify. For example, an iso file with Linux will probably be corrupted. A program file will probably also be corrupted. Some kind of data file might still work, depending on how it is used.
    – sudodus
    Commented Nov 26, 2021 at 20:10
  • 1
    I tried the same length string in my program, and it worked. But it's doesn't works when I try the non-same length string.
    – Arian
    Commented Nov 26, 2021 at 21:16
  • 1
    If you change the length of a string without "padding" it to the same length you will inevitably destroy a executable file (that is not built from text, as bash or python scripts). Pad with space (SPC) characters or NUL will often work.
    – Hannu
    Commented Nov 26, 2021 at 22:17
  • @Arian, If your program is a compiled program, you should modify the source code, compile and link it in order to modify it. If it is an interpreted program (as the examples bash or python scripts by Hannu), then you can modify it freely directly in the program code, because the source code is the executable program.
    – sudodus
    Commented Nov 26, 2021 at 23:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .