1

I have an object file under Linux with a lot of strings in it. I would like to replace all strings that represent a specific path in this object.

I can see all the strings I need to change using the strings utility.

Is there a way to replace all these strings at once ?

The path I want to change is shorter, so it should be relatively easy, just adding terminating zeros at the end of the strings so it will remain the same length on the file.

I can write a short program that does this, but I wonder if there's already a tool I can use for this.

3
  • 1
    Maybe it's easier if you just create a symlink from "the location the object file uses" to "the path you want" ? Commented Mar 27, 2014 at 7:41
  • The paths exist as strings in the object files. It's an internal data file of some application I want to alter. Commented Mar 27, 2014 at 7:44
  • Did any of the answers here resolve your question? Please mark them accordingly.
    – avgvstvs
    Commented Apr 1, 2014 at 3:13

2 Answers 2

3

Here's a way to do that using Perl:

perl -pi -e 's/pattern/replacement/g' binaryfile

For example, I created this trivial C++ program:

// silly.cpp
#include <iostream>
#include <string>

int main()
{
    std::string name("Mellowcandle");
    std::cout << "The name is " << name << std::endl;
    std::cout << "One could call this person " << name << std::endl;
    return 0;
}

After compiling and linking it, I used this on the binary:

perl -pi -e 's/Mellowcandle/Edward\0\0\0\0\0\0/g' silly

which gives the intended effect. Note that failing to pad the replacement string to the longer string length will cause those bytes to simply be omitted, causing a segfault or worse if you try to run the resulting binary.

1

Edward's Perl answer is great, so normally I wouldn't post another answer, but in response to the other part of your question...

I wonder if there's already a tool I can use for this.

Yes, pretty much every decent hex editor has a global search-and-replace function. 010 Editor runs on Linux and definitely supports this functionality.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.