开发者

Modify a string in Java bytecode (jar)

I want to modify a connection string that's hard-coded in a Java application (jar without source).

I presume it's possible to decompile the jar, then change the source and recompile to a new jar, but is there a quicker way to do this?

EDIT:

  • It's a standalone application, not a jar I'm loading from my own code
  • I doubt it will have been obfuscated: niche scientific application开发者_如何学Python, author AWOL.
  • A solution that entails modifying the string "in memory" while the app is running would also suffice, but not ideal


In your application that uses that jar, you could use reflection to set the connection String. (Reflection can be used even with private setters).

Obviously, if the setter is public, you could just call it without reflection.


I think decompilation is probably the quickest way, provided the code hasn't been obfuscated such that a decompilation/compilation round-trip is not possible. You're going to have to decompile the code anyway to find the connection string, so you're half-way there already.

More importantly, you can take advantage of this method to pull the connection string out into a properties file, and hence (hopefully) only perform the decompilation once!


Since Jars are often not compressed, there's a small chance that the address will be visible as plain text in the Jar. You could try editing the Jar with an editor that's not afraid of binary data (I use vim) and just search for and change the text, provided the old address and the new use the same number of characters. That's primitive but very simple.

If it doesn't work, you'll need one of the other approaches.

Of course you'll want to keep a backup copy of the jar in case this approach fails.


Since jar files are simply ZIP-Files with a certain structure, you can use a zip tool (WinZip etc.) to extract the class files; then you use a hex editor to modfiy the class files (should not be too difficult if the new connection string has the same length like the old one); after that, you zip it back into the jar file.

Of course this should only be your last-ditch attempt when all the other possibilities described above don't work.


I assume you already thought of this, but maybe you could simply use inheritance, if the compiled method in the .jar file looks something like this:

public String getConnectionString(){...

then just extend the class, override the method to return your new string and use your class instead. Obviously this only works if the super class is not final, the method is not final, and it's public or protected (if you can use the same package name).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜