我不能使用恶意反射查看私有字段的值 [英] I can't use malicous reflection to view values of private fields

查看:50
本文介绍了我不能使用恶意反射查看私有字段的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以获取受保护字段的值,但是私有字段会抛出java.lang.IllegalAccessException.我想我知道为什么我会收到这个异常,但是如何使用反射来利用私有字段的内容,我该如何解决这个问题?

I can get the value of the protected field, but the private field throws java.lang.IllegalAccessException. I think I know why I'm getting this exception, but how is reflection used to exploit the contents of private fields, how do I get around this?

Programmer Hat is on
我在 netbeans 项目中创建了以下易受攻击的类.我已经制作了一个 Jar 文件来分发它.

Programmer Hat is on
I have created the following Vulnerable class in a netbeans project. I have made a Jar file to distribute it.

public class Vulnerable {
    private int privateSecret;
    protected int protectedSecret;
    int secret;

    public Vulnerable() {
    this.protectedSecret = 11;
    this.privateSecret = 22;
    this.secret = 33;
    }
}

恶意黑客帽现已启用
我想知道私有隐藏字段,我想知道它们包含什么.
我有 Jar 文件并将其导入到我的 Exploit 项目中.

Malicious Hacker Hat is now on
I want to know private hidden fields and I want to know what they contain.
I have the Jar file and I have imported it into my Exploit project.

以下类扩展了 Vulnerable 并使用反射来列出字段并尝试访问值.

The following class extends Vulnerable and uses reflection to list fields and try to access the values.

public class ExpliotSubClass extends VulnerableCode.Vulnerable {

    public List<Field> protectedList = new LinkedList<Field>();
    public List<Field> privateList = new LinkedList<Field>();

    public void lists() {
        Field[] declaredFields = this.getClass().getSuperclass().getDeclaredFields();

        for (Field field : declaredFields) {
            int modifiers = field.getModifiers();
            if (Modifier.isPrivate(modifiers)) {
                privateList.add(field);
                System.out.println("Private = " + field.getName());
            } else if (Modifier.isProtected(modifiers)) {
                protectedList.add(field);
                System.out.println("Protected= " + field.getName());
            }
        }
    }


    public Object get(Field field) {
        try {
            return field.get(this);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(ExpliotSubClass.class.getName()).log(Level.SEVERE,
                                                                  null,
                                                                  ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(ExpliotSubClass.class.getName()).log(Level.SEVERE,
                                                                  null,
                                                                  ex);
        }
        return null;
    }
}

推荐答案

为了访问私有字段,您必须将其设置为可访问:

In order to access private field you have to set it as accessible:

field.setAccessible(true);

这篇关于我不能使用恶意反射查看私有字段的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆