Is it possible to replace the "lines" inside the .class file? Or is it better to recompile?

The main question is whether it is safe to change some "string" information inside a file *.class(java compiled classes).

What am I doing:

using regexp I'm trying to find all the mouth IP addresses inside some compiled project

find . -type f | xargs grep -E "[0-9]{3}\.[0-9]{3}\.1\.[0-9]{0,3}"

This command tells me that some binaries match my regular expression. And this indicates that these binaries .jarand .class. When I open these .classfiles with vi, I see a lot of strange characters, as well as something like http://192.168.1.111 "among this binary garbage.

My final task is to replace all IP addresses (using sed -i) with a fully qualified domain name.

It is safe? Or just a working solution to disassemble the whole project and recompile?


Added after replies.

I test it like this:

Create Change.java

public class Change {

    public static String CHANGE_ME="SOME_TEST_STRING";

    public static void main (String[] argv){
        System.out.println(CHANGE_ME);
    }
}

Run it and see in the terminal

$ java Change
SOME_TEST_STRING

Compile it and run the replacement:

sed -i 's/SOME_TEST_STRING/AAAAA/g' Change.class

After this restart

$ java Change 
Exception in thread "main" java.lang.ClassFormatError: Illegal UTF8 string in constant pool in class file Change
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)
+4
source share
3 answers

No, this is not safe. It is so safe that it is outside the solar system. Recompile the project.

However, you can use sed to replace the IP in the source code. This has a good chance of work.

+7
source

Not safe.

If this is our project, you should have all the variables (e.g. IP, Ports, etc.) in an external txt file.

java-.

+2

.class 2 , , UTF-8. uniqoue ASCII . . , , .

, :

$ cat Test.java
public class Test {
  public static void main(String[] args) {
    System.out.println("Hello, World!");
  }
}

$ javac Test.java
$ java Test
Hello, World!
$ hexdump -C Test.class
...
00000060  2f 53 74 72 69 6e 67 3b  29 56 01 00 0a 53 6f 75  |/String;)V...Sou|
00000070  72 63 65 46 69 6c 65 01  00 09 54 65 73 74 2e 6a  |rceFile...Test.j|
00000080  61 76 61 0c 00 07 00 08  07 00 17 0c 00 18 00 19  |ava.............|
00000090  01 00 0d 48 65 6c 6c 6f  2c 20 57 6f 72 6c 64 21  |...Hello, World!|
000000a0  07 00 1a 0c 00 1b 00 1c  01 00 04 54 65 73 74 01  |...........Test.|
000000b0  00 10 6a 61 76 61 2f 6c  61 6e 67 2f 4f 62 6a 65  |..java/lang/Obje|
000000c0  63 74 01 00 10 6a 61 76  61 2f 6c 61 6e 67 2f 53  |ct...java/lang/S|
...

, "Hello, World!" 0x93. "Hello, Earth!".

$ echo -n Hello, Earth! | dd conv=notrunc of=Test.class bs=1 seek=$((0x93))
$ java Test
Hello, Earth!

echo -n Hello Earth! 12 stdout. dd conv=notrunc of=Test.class bs=1 seek=$((0x93)) Test.class 0x93 (Hello Earth ).

: http://pastebin.com/rJi4cRX1

+1

All Articles