package com.drops.exp.util;

import cn.hutool.http.HttpResponse;
import com.drops.utils.HTTPUtils;
import com.drops.utils.ResponseUtil;

/**
 * @ClassName: JolokiaUtil
 * @Description: TODO
 * @Author: Summer
 * @Date: 2021/7/29 14:24
 * @Version: v1.0.0
 * @Description:
 **/
public class JolokiaUtil {
    static String keyV1 = "ch.qos.logback.classic.jmx.JMXConfigurator";
    static String keyV2 = "reloadByURL";
    static String keyV3 = "type=MBeanFactory";
    static String keyV4 = "createJNDIRealm";



    /**
     * @Description: jolokia logback JNDI RCE
     * @param target
     *
     * @return:
     */
    public static boolean hasMbeans(String target){
        String result = HTTPUtils.getRequest(target + "/jolokia/list").body();
        if (result.contains(keyV1)){
            if (result.contains(keyV2)){
                return true;
            }
        }

        return false;
    }

    public static boolean hasMbeansV2(String target){
        String result = HTTPUtils.getRequest(target +  "/actuator/jolokia/list").body();
        if (result.contains(keyV1)){
            if (result.contains(keyV2)){
                return true;
            }
        }
        return false;
    }
    /**
     * @Description:    jolokia Realm JNDI RCE
     * @param target
     *
     * @return:
     */
    public static boolean hasMbeansV3(String target){
        String result = HTTPUtils.getRequest(target +  "/jolokia/list").body();
        if (result.contains(keyV3)) {
            if (result.contains(keyV4)) {
                return true;
            }
        }

        return false;
    }


    public static boolean hasMbeansV4(String target){
        String result = HTTPUtils.getRequest(target +  "/actuator/jolokia/list").body();
        if (result.contains(keyV3)){
            if (result.contains(keyV4)){
                return true;
            }
        }

        return false;
    }

}
