exposing Java private fields with reflection
This commit is contained in:
BIN
classes/private/Confidential.class
Normal file
BIN
classes/private/Confidential.class
Normal file
Binary file not shown.
9
classes/private/Confidential.java
Normal file
9
classes/private/Confidential.java
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
public class Confidential {
|
||||||
|
|
||||||
|
private String secret = "";
|
||||||
|
private String hidden = "burn after reading";
|
||||||
|
|
||||||
|
public Confidential(String text) {
|
||||||
|
this.secret = text;
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
classes/private/Expose.class
Normal file
BIN
classes/private/Expose.class
Normal file
Binary file not shown.
25
classes/private/Expose.java
Normal file
25
classes/private/Expose.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
6
classes/private/expose.py
Normal file
6
classes/private/expose.py
Normal 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)
|
||||||
11
classes/private/leakprivate.py
Normal file
11
classes/private/leakprivate.py
Normal 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)
|
||||||
12
classes/private/no_respect.py
Normal file
12
classes/private/no_respect.py
Normal 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
|
||||||
25
classes/private/pt-br/AcessaPrivado.java
Normal file
25
classes/private/pt-br/AcessaPrivado.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
9
classes/private/pt-br/ObjetoSecreto.java
Normal file
9
classes/private/pt-br/ObjetoSecreto.java
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
public class ObjetoSecreto {
|
||||||
|
|
||||||
|
private String escondido = "";
|
||||||
|
private String oculto = "dado ultra secreto";
|
||||||
|
|
||||||
|
public ObjetoSecreto(String texto) {
|
||||||
|
this.escondido = texto;
|
||||||
|
}
|
||||||
|
}
|
||||||
6
classes/private/pt-br/acessapriv.py
Normal file
6
classes/private/pt-br/acessapriv.py
Normal 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)
|
||||||
11
classes/private/pt-br/listapriv.py
Normal file
11
classes/private/pt-br/listapriv.py
Normal 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)
|
||||||
Reference in New Issue
Block a user