|
Val's Blog
Lots of stuff for Web 2.0 freaks and Java addicts
|
|
|
Jane Cleland-Huang: "Software development should integrate and consider project metrics that assess its financial impact." |
[ Login ] |
|
Ever wondered what evil power can be unleashed when using reflection? Do you think private methods are really only accessible from within the declaring class? Do you think that a private field can only be modified from within the declaring class? No? That's what I thought!! In this blog, I will try to demonstrate that it is always important to correctly set the security properties of your applications. For instance, let's look at the following example where we successfully retrieve a private password from another class:
Ok, the example is not really sexy. Let's mess up a class that implements the Singleton pattern. In the normal case, a singleton object is supposed to be the only instance of a given class. To achieve this, we usually declare the class constructor private, so that no one can invoke it. Well, as demonstrated below, with reflection we can bypass this restriction and create a second "singleton object".
Using this technique, you can create an instance of any non-abstract class, even if all its constructors are declared private. For instance, below we create an instance of the Math class even though it is useless since the Math class has no instance method. Still, it is possible to do it.
Finally, let's mess with the
All this could have been avoided if the
I'm pretty sure that there is a huge amount of code out there that could be broken this way. Watch out!!
TrackBacks[1]
Comments[14]
Posted by val on May 18, 2004 4:49:53 PM CEST
Reply |
Permalink
Re: Hack any Java class using Reflection
Nice hack! great :-)
Thanks for the tip!
Comment from Armond Avanes on May 19, 2004 11:09:32 AM CEST
this 'hack' is as old as the hills. why not just link the page you got it from instead of writing a whole article?
what a pre-madonna
Comment from T on April 10, 2006 2:05:48 PM CEST
Hi "anonymous coward",
Please note that I did not copy anything from anywhere. When I do so, I usually cite my sources. I didn't know that each piece of information had to come in only one instance on the Internet. I wish you a good hunt for duplicate information on the net. As I see it, uselessness and redundancy is definitely not restricted to information ;)
Comment from Val on April 11, 2006 5:35:03 AM CEST
Time to cite from the C++ FAQ lite [7.6]:
How can I prevent other programmers from violating encapsulation by seeing the private parts of my class? Not worth the effort — encapsulation is for code, not people. [...] Besides, this is rarely if ever a problem. I don't know any programmers who have intentionally tried to access the private parts of a class. "My recommendation in such cases would be to change the programmer, not the code" [...]
Comment from Marc Conrad on May 20, 2004 11:51:48 PM CEST
hey there's a game that runs on java but ive been tryin to find out how to hack for passwords on it,think u can help? well if u can hack this game u can hack any game.
Comment from Anonymous on March 2, 2006 8:06:42 AM CET
"Not worth the effort — encapsulation is for code, not people. [...] Besides, this is rarely if ever a problem. I don't know any programmers who have intentionally tried to access the private parts of a class. "My recommendation in such cases would be to change the programmer, not the code" [...] "
The conclusion above is not true.
In some cases, you can not "change the programmer". One example is Java SDK.
Can you change the programmers over their? But you sometimes do need to
reflect singletons in their API.
Comment from Mark Qian on March 20, 2006 9:48:31 AM CET
Sure its a hack :). But sometimes its useful hack for writing test cases of singletons.
Comment from Anonymous on April 5, 2006 2:26:25 AM CEST
hey how can i hack in to runescape? ive been trying and trying well actually ive never hacked anything could you hack into runescape or mayb tell me how to? please i really need help ive been hacked a millions of times and now i want reevenge so im doing some research on how to hack java games and i guess your site has been the best so far but yeah please help me the site im trying to hack is www.runescape.com
thanks,
eric
Comment from eric on May 11, 2006 10:42:38 PM CEST
Yeah, how do you hack runescape? It's a java based game and i want to know how to hack peoples passwords... please tell me!
Comment from flaming goat on June 15, 2006 10:28:55 AM CEST
Thanks mate! It's proving very useful in a utility class I am writing.
Comment from sanj on July 19, 2006 6:13:07 AM CEST
wat is body(wat dose it mean)
Comment from Anonymous on August 2, 2006 5:34:23 PM CEST
Thanks.This piece of code helped me a lot
Comment from Anonymous on August 3, 2006 9:32:41 PM CEST
ive tried many times...they put too many dead ends and loops in their programming, i find it impossible.
Comment from Anonymous on August 11, 2006 5:21:43 AM CEST
Is it possible to use reflection to extend a class with private constructors, say from within the derived class's constructor ?
Comment from Anonymous on October 30, 2006 2:40:49 AM CET
Reflection is a mechanism in java that allows to to get information about a class without needing to know the type of the class. The program below takes a java class name as a command line argument and shows you all of the methods and field names that...
Read more...
TrackBack from Mark W. Shead on February 23, 2005 4:45:34 AM CET
TrackBack to http://radio.javaranch.com/val/addTrackBack.action?entry=1084891793000
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Content © Val | Powered by Pebble 1.9.1 |