Detecting System.setProperty method invocations

流过昼夜 提交于 2019-12-03 20:18:20
Michael Konietzka

System.setProperty is checked by a SecurityManager, if installed.

You can create your own MySecurityManager and deploy at runtime. Your own SecurityManager can log some information like the current stacktrace, when the method checkPropertyAccess is called:

public class MySecurityManager extends SecurityManager
{

    public MySecurityManager()
    {
        super();
    }

    @Override
    public void checkPropertyAccess(String key)
    {
        if ("javax.xml.parsers.DocumentBuilderFactory".equals(key))
        {
            System.err.println("checkPropertyAccess(String :" + key + "): ");
            Thread.currentThread().dumpStack(); // or anything useful for
                                                // logging the context.
            new Throwable().printStackTrace(); // whatever, or use it with
            // PrintStream/PrintWriter, or some logging framework if configured.
        }
        super.checkPropertyAccess(key);
    }

    @Override
    public void checkPermission(Permission perm)
    {
        if (perm instanceof PropertyPermission)
        {
            PropertyPermission propPerm = (PropertyPermission) perm;
            System.err.println("checkPropertyAccess(String:" + propPerm.getName() + "):");
            Thread.currentThread().dumpStack(); // or anything useful for
                                                // logging the context.
            new Throwable().printStackTrace(); // whatever, or use it with
            // PrintStream/PrintWriter, or some logging framework if configured.
        }
        super.checkPermission(perm);
    }
}

String literals are UTF8-encoded in the class file format and since there is no reason to assume that the offending invoker is using code to concatenate the property name, I would simply have unpacked all JARs from the classpath into one directory and tried a recursive grep for "javax.xml.parsers.DocumentBuilderFactory" through the class files. You would have at least found all class files containing this String as a literal and hopefully not to many false positives.

If the class name of the incorrect implementation is easier to identify, it would perhaps been more clever to search for that instead.

Just start you application in debug mode, connect to it with eclipse, set breakpoint and look what class do it in call hierarchy http://www.eclipsezone.com/eclipse/forums/t53459.html

AlexR

I know 2 solutions

  1. use aspect framework (aspectJ or so)
  2. Modify System class (add logging to setProperty() method) new Throwable().printStacktrace(); will report you where the method was called from.

Now run your application with option -Xbootclasspath/p and put your version System class there.

-Xbootclasspath/p:<directories and zip/jar files separated by ;> prepend in front of bootstrap class path

I prefer the second solution because it is very simple.

Seems like some use case for aspect frameworks, no ?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!