Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Java Decompilation in Perl

by Stegalex (Chaplain)
on Jun 06, 2002 at 13:43 UTC ( [id://172188]=perlquestion: print w/replies, xml ) Need Help??

Stegalex has asked for the wisdom of the Perl Monks concerning the following question:

Months ago I wrote data interface specs for a feed I was expecting a software vendor to send me. Not being a trusting person, I designed an XML DTD and shared a sample data file with them etc (this is not a post about the pros and cons of XML so don't go there please).

Anyway, the first test feed came from the vendor yesterday and the XML file was conspicuously missing. What they did provide us with was a neat little Java applet with all the data built right into the classes. It seems that this vendor does not want us to write programs against their data. Our contracts person says that they are in breach of contract and so I expect that there is about a 50/50 chance that we may get them to pony up the XML or some other kind of flat file.

So my question is, if I can't make the software vendor play nice, I want to decompile what must be a two-dimensional array of data out of their Java class files. Since the rest of my system is written in Perl, I am looking for guidance about how to do this in Perl.

Plus, the sheer irony of munging through Java class files in Perl is just too much to pass up.

~~~~~~~~~~~~~~~
I like chicken.

Replies are listed 'Best First'.
(wil) Re: Java Decompilation in Perl
by wil (Priest) on Jun 06, 2002 at 14:00 UTC
    From my (limited) knoweldge of Java, if they haven't ran the class through some kind of obfuscator, then you should be able to decompile them. If they have obscured them in anyway, then you've got a bigger task on your hand. The process can be reverssed but is a little tricky.

    Here's a list of some decompiler tools that I know of. There are probably a number of others out there, and you will probably have better results doing a google for this one.


    It know there's a module on CPAN to write Perl classes in Java, but I haven't see any that decompiles java classes. I may be wrong however. But it seems to me like you need to decompile first using one of the decompilers above and then use the data in your Perl script.

    You might also be interested in this article which compares 3 of the leading Java decompilers. It could help you determine which, if any, meet your needs.

    Hope this helps.

    - wil
      Note that using such tecnhiques in the US may be a violation of the DMCA. Consult with a lawyer (in house counsel perhaps) before you do use such techniques. Certainly insist on a written formal statement from your boss and or the legal types in your company before you do. Otherwise you may find yourself (as the coder) in jail.

      Yves / DeMerphq
      ---
      Writing a good benchmark isnt as easy as it might look.

Re: Java Decompilation in Perl
by Dog and Pony (Priest) on Jun 06, 2002 at 14:39 UTC
    JAD is insanely good at decompiling class files, and it has lots of frontends and examples etc.

    Just make sure you listen to demerphq and don't do anything illegal. ;-)


    You have moved into a dark place.
    It is pitch black. You are likely to be eaten by a grue.
Re: Java Decompilation in Perl
by agentv (Friar) on Jun 06, 2002 at 16:20 UTC

    ...actually the answer may be easier than this.

    Decompilers are fine, and without this being legal advice, I find (as a human) nothing provocative in digging through class files to expose the data that is being presented. If your purpose is to access the data (which they have given you relative to a contractual obligation), the means by which you gain access to the data are probably your own business.

    "But," (as Vincent Vega said,) "that don't matter, cause check this out..."

    Java classes expose their details through a process known as reflection and introspection. Even if your vendor has obfuscated the class files, the encapsulated data you are trying to gather is probably quite accessible through this mechanism.

    The purpose of reflection is to allow java classes to be used in environments other than more java code. What that means is that we want to be able to manipulate java objects from scripting environments, from tool builders and from other places that don't have us writing more code to use our object. (In fact, a lot of the java "instant application makers" take advantage of this fact.)

    What it means to us here is that we can use the reflection API to query the objects about their characteristics, even if we don't have the original source. This is how decompilers do their thing.

    It sounds like you are trying to get at the data held in the various objects with your own code, and again, I don't see any "reverse engineering" implications in this, any more than if you wrote some code to extract the plain text content of a PDF file or a Word document. (But you should ask an attorney to be safe.)

    What I can't tell you is how to write code in Perl that will allow you to introspect the class files. (I would do that work in Java myself.) I believe (from a glimpse of related discussions) that this may be possible. It certainly is relatively easy to write a little Java that will extract the information you want.

    And it may prove to be instructive as you contemplate how you might do it in Perl. If you take a look at the details of object introspection in Java and discover how you would do this, you may be able to relate that to something that you would do in Perl to accomplish the same effect.

    Thanks for your questions BTW, this will stimulate me to engage in the long overdue reading about the current state of interfacing Perl with Java.

    ---v

Re: Java Decompilation in Perl
by andye (Curate) on Jun 06, 2002 at 14:52 UTC
    Perhaps ("probably", even) I'm missing something obvious here, but couldn't you just call the applet repeatedly with all possible input values, thus getting a dump of all possible output values?

    (/me waits to hear why this suggestion reveals that I have - to quote another current thread - a head made of bone).

    andy.

      That was in fact my first thought also. One could just write a class to dump their data to an XML file..

      Makeshifts last the longest.

Re: Java Decompilation in Perl
by dws (Chancellor) on Jun 06, 2002 at 23:13 UTC
    So my question is, if I can't make the software vendor play nice, I want to decompile ...

    If they're in breach of contract, solve the problem at that level. Reverse enginerring from code can open you up to DCMA problems.

    If you're in doubt, get a legal opinion from your contracts people (or your corporate attorneys). Explain what you propose to do, and make sure they understand it involves reverse engineering. They might say "no problem", or they might say "NO WAY." If the latter, better to know that know, before you invest time in hacking Java bytecodes.

Re: Java Decompilation in Perl
by Anonymous Monk on Jun 06, 2002 at 16:32 UTC
    Isn't there an Inline module for Java?

      ...this is what wil referred to above. (Although both wil's comment and the documentation on the module claim that the module allows you to "write Perl classes in java" when the fact is that the module appears to allow you to write Java classes in a Perl program. Perhaps the statement is accurate, but I found that it left me confused.)

      At any rate, yes, the module allows you to embed some Java into your Perl program and if the module works (an issue the documentation on it leaves in doubt) it would be precisely the solution that would allow your perl program to introspect Java objects.

      ---v

Re: Java Decompilation in Perl
by cjf (Parson) on Jun 07, 2002 at 11:07 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://172188]
Approved by Stegalex
Front-paged by Stegalex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-03-29 07:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found