0

I wanted to know which of the below solutions is better: I want to modify some .class files and as I have realized there is two ways to do that:

  • Decomplile the .class file, modify it and finally compile it again. -
  • Directly modify it with a hex editor.

Thanks

7
  • What's the motivation for this, some hack-playground or a real use case that involves this? Commented Feb 24, 2013 at 6:57
  • No its real use, I have to do some modification and I wanted to know which is better to consider. Commented Feb 24, 2013 at 7:00
  • 1
    It would be better if you explain the problem so people could give you a hand. Take into account that we're not wizards with a magic crystal ball and we can guess your needs or read your mind. Commented Feb 24, 2013 at 7:01
  • I've got JSP web applications and there class files. I am working with Soot and it only gets class or java files as input. I need to modify the code so that some checks are made before further execution. Therefore I wanted to know which is better: decompiling my class files then modifying them or the other way. I myself somehow think that decompiling is better but, because I was not sure I wanted to ask the experts like you for help. Commented Feb 24, 2013 at 7:07
  • Maybe I explained wrong, I want to modify the web application that I have not Soot. Commented Feb 24, 2013 at 7:11

7 Answers 7

1

Of those two bad choices, decompiling, modifying and recompiling is better. Changing anything in binary is likely to change offsets of other stuff and far more error prone.

.class files don't have any checksums or error checking as far as I know, although the bytecode does need to be validated. Even so, output from a compiler is safer.

Sign up to request clarification or add additional context in comments.

4 Comments

Maybe OP can just extend the class and thus avoiding modify it. But we can't be sure until OP explains the real problem he/she's facing.
@evil otto is there a better choice?
There almost certainly is. Like Luiggi is asking, it depends on what it is that you're trying to do. You are asking if your solution to an unstated problem is good. Actually asking about the problem you are facing will be much more productive.
I thought that the last explanation was enough. I have class files ok. I have to do some inspections to find the specific places that I am looking for, so far I'm ok. After finding the places I need to modify my code to do some checking. For this part I need your help to choose the right way to do so. As I'm using Soot and it accepts java and class files, I can decompile the class files (as you said its better between the two choices) and go through the process I need and get what I want. Is there a better way?
1

option #3 - if youre going to be messing around with bytecode, the least you could do is use some library designed for it to avoid re-inventing the wheel. see asm for example

Comments

1

It turned out that a JSP source is available for the classes. So a much better approach is to make the modification in the JSP source and then use a JSP compiler (like this one).

3 Comments

I think that this might not be suitable for me because I need to use Soot and it does not work with JSP. I need to use Soot before the modifications to see where needs to be modified.
It is still not clear to me what exactly you want to achieve. Keep in mind that making manual modifications to generated code is a maintenance nightmare.
I need to use Soot to identify the parts which need modification (Soot is essential), therefore I can not use JSP since Soot only gets java and class files as input.
0

If you don't have the source code, and you are sure that you want to modify .class files, I recomment using Javassist library (for byte-code modification) in combination with Java Decomiler (to inspect the source).

3 Comments

I have the source but its JSP and Soot does not work with JSP, it gets java or class files as input.
@developer If you have the JSP there are far better options. You see why people asked for the real problem?
Could you help me further @Henry.
0

I am for option #1. Use JAD or some other decompiler to make .java, make changes, compile and replace the old .class with the new version.

1 Comment

Thanks @Evgeniy Dorofeev. It seems as if this solution is the best as others have suggested this one too.
0

No solution which involves messing around with bytecode files is a good solution.

The good solutions are:

  • Get hold of the source code and make the changes you need to in the source code. Then recompile, run the unit tests, build a WAR, redeploy, etcetera.

  • If you can't get the source code, get the people who wrote the source code to fix the problem.

  • If you can't get them to fix the problem for free, pay them.

  • If they won't fix it even if you pay them ... ditch the product, and look for an alternative.

Only consider the bad solutions of decompiling and/or bytecode engineering if none of the above is a viable option. And even then, you need to start planning a way to get yourself out of the whole you are currently in. 'Cos it will only get deeper with the bad solutions.


I can't honestly say which is the best of the bad solutions. It depends on:

  • the nature of the code,
  • the nature of the changes that you have to make to the code, and
  • your skills at reverse engineering bytecodes.

Comments

0

I have used cavaj, JAD and JD-GUI and they gave quite the same decompiled results. But JD-GUI had better results in comparison to the other two and therefore was a better choice for me. I wanted to share this in case anyone was looking for a decompiler to choose.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.