package com.sec421.controller.postPentest;
/**
 * @author 0x421
 * @date 2023/12/28 11:20
 * @github https://github.com/0x7eTeam
 */
import com.sec421.controller.ui.MainController;
import com.sec421.core.Constants;
import com.sec421.tools.Tools;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.nio.file.Paths;

public class ExecController {
    private String command;
    private String line;
    private String output;
    private String auth;
    public String exec(String exec_type,String execIP,String execNAME,String execPASS,String execCMD,String execAUTH,String execCHARSET){
        String pyPath = null;
        try {
            pyPath = Paths.get(Tools.getProperty("py_path")).toAbsolutePath().toString();
        } catch (IOException e) {
            return "请配置python.exe的路径";
        }
        if (pyPath==null||pyPath.equals("")){
            Tools.alert("错误","请配置python.exe的路径");
            return "请配置python.exe的路径";
        }
        Proxy proxy = (Proxy) MainController.settingInfo.get("proxy");
        if (proxy != null) {
            InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address();
            String proxyHost = proxyAddress.getHostString();
            int proxyPort = proxyAddress.getPort();
            if (proxy.type() == Proxy.Type.HTTP) {
                Tools.alert("错误","请使用socks代理");
                return "代理错误";
            }else if (proxy.type() == Proxy.Type.SOCKS){
                System.setProperty("socksProxyHost", proxyHost);
                System.setProperty("socksProxyPort", Integer.toString(proxyPort));
            }
        }
        String script_name = Paths.get(Constants.IMPACKET_PATH+exec_type+".py").toAbsolutePath().toString();

        if (execAUTH=="密码"){
            this.auth = execNAME + ":" + execPASS;
        }else{
            this.auth = execNAME;
        }
        if (exec_type=="dcomexec"){
            if (execAUTH=="密码"){
                this.command = pyPath + " " + script_name + " -object MMC20 " + this.auth + "@" + execIP + " " + execCMD + " -codec " + execCHARSET;
            }else {
                this.command = pyPath + " " + script_name + " -object MMC20 " + this.auth + "@" + execIP + " " + execCMD + " -hashes :"+ execPASS + " -codec " + execCHARSET;
            }
        }else{
            if (execAUTH=="密码"){
                this.command = pyPath + " " + script_name + " " +  this.auth + "@" + execIP + " " + execCMD + " -codec " + execCHARSET;
            }else {
                this.command = pyPath + " " + script_name + " " +  this.auth + "@" + execIP + " " + execCMD + " -hashes :"+ execPASS + " -codec " + execCHARSET;
            }
        }
        System.out.println(this.command);
        try {
            Runtime runtime = Runtime.getRuntime();
            // 执行系统命令
            Process process = runtime.exec(this.command);

            // 等待命令执行完成
            int exitCode = process.waitFor();
            System.out.println("命令执行完成。退出代码：" + exitCode);

            // 获取标准输出流
            InputStream inputStream = process.getInputStream();
            BufferedReader inputStreamReader = new BufferedReader(new InputStreamReader(inputStream, "GBK"));

            while ((this.line = inputStreamReader.readLine()) != null) {
                this.output+=this.line+Tools.getLineSeparator();
            }

            // 获取错误输出流
            InputStream errorStream = process.getErrorStream();
            BufferedReader errorStreamReader = new BufferedReader(new InputStreamReader(errorStream, "GBK"));
            while ((this.line = errorStreamReader.readLine()) != null) {
                this.output+=this.line+Tools.getLineSeparator();
            }
            if (exec_type=="smbexec"){
                runtime.exec("cmd.exe");
            }
            return this.output.toString().replace("null","");
        } catch (Exception e) {
            System.out.println(e);
            return "命令执行失败：" + this.command;
        }
    }
}
