exposing Java private fields with reflection

This commit is contained in:
Luciano Ramalho 2014-10-19 09:00:19 -02:00
parent 926bb370a0
commit 1d48cdbde5
11 changed files with 114 additions and 0 deletions

Binary file not shown.

View File

@ -0,0 +1,9 @@
public class Confidential {
private String secret = "";
private String hidden = "burn after reading";
public Confidential(String text) {
this.secret = text;
}
}

Binary file not shown.

View File

@ -0,0 +1,25 @@
import java.lang.reflect.Field;
public class Expose {
public static void main(String[] args) {
Confidential message = new Confidential("text you shoudn't see");
Field privateField = null;
try {
privateField = Confidential.class.getDeclaredField("secret");
}
catch (NoSuchFieldException e) {
System.err.println(e);
System.exit(1);
}
privateField.setAccessible(true); // break the lock!
try {
String wasHidden = (String) privateField.get(message);
System.out.println("message.secret = " + wasHidden);
}
catch (IllegalAccessException e) {
// this will not happen after setAcessible(true)
System.err.println(e);
}
}
}

View File

@ -0,0 +1,6 @@
import Confidential
message = Confidential("text you shoudn't see")
private_field = Confidential.getDeclaredField('secret')
private_field.setAccessible(True) # break the lock!
print 'message.secret =', private_field.get(message)

View File

@ -0,0 +1,11 @@
from java.lang.reflect import Modifier
import Confidential
message = Confidential('never expose this')
fields = Confidential.getDeclaredFields()
for field in fields:
# list private fields only
if Modifier.isPrivate(field.getModifiers()):
field.setAccessible(True) # break the lock
print 'field:', field
print '\t', field.getName(), '=', field.get(message)

View File

@ -0,0 +1,12 @@
# In the Jython registry:
# python.security.respectJavaAccessibility = false
# Setting this to false will allow Jython to provide access to
# non-public fields, methods, and constructors of Java objects.
import Confidential
message = Confidential("text you shoudn't see")
for name in dir(message):
attr = getattr(message, name)
if not callable(attr): # ignore methods
print name, '=', attr

View File

@ -0,0 +1,25 @@
import java.lang.reflect.Field;
public class AcessaPrivado {
public static void main(String[] args) {
ObjetoSecreto oSecreto = new ObjetoSecreto("senha super secreta");
Field campoPrivado = null;
try {
campoPrivado = ObjetoSecreto.class.getDeclaredField("escondido");
}
catch (NoSuchFieldException e) {
System.err.println(e);
System.exit(1);
}
campoPrivado.setAccessible(true); // arrombamos a porta
try {
String tavaEscondido = (String) campoPrivado.get(oSecreto);
System.out.println("oSecreto.escondido = " + tavaEscondido);
}
catch (IllegalAccessException e) {
// esta exceção nao acontece porque fizemos setAcessible(true)
System.err.println(e);
}
}
}

View File

@ -0,0 +1,9 @@
public class ObjetoSecreto {
private String escondido = "";
private String oculto = "dado ultra secreto";
public ObjetoSecreto(String texto) {
this.escondido = texto;
}
}

View File

@ -0,0 +1,6 @@
import ObjetoSecreto
oSecreto = ObjetoSecreto('senha super secreta')
campoPrivado = ObjetoSecreto.getDeclaredField('escondido')
campoPrivado.setAccessible(True) # arrombamos a porta
print 'oSecreto.escondido =', campoPrivado.get(oSecreto)

View File

@ -0,0 +1,11 @@
from java.lang.reflect import Modifier
import ObjetoSecreto
oSecreto = ObjetoSecreto('senha super secreta')
campos = ObjetoSecreto.getDeclaredFields()
for campo in campos:
# so campos privados!
if Modifier.isPrivate(campo.getModifiers()):
print campo
campo.setAccessible(True) # arrombamos a porta
print '\t', campo.getName(), '=', campo.get(oSecreto)