2022-11-19 17:04:13 +08:00
|
|
|
|
package Plugins
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"fmt"
|
2024-12-18 22:00:18 +08:00
|
|
|
|
"github.com/shadow1ng/fscan/Common"
|
2024-12-18 21:56:08 +08:00
|
|
|
|
"github.com/shadow1ng/fscan/Config"
|
2022-11-19 17:04:13 +08:00
|
|
|
|
"net"
|
|
|
|
|
|
"os"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/hirochachacha/go-smb2"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2024-12-18 15:19:47 +08:00
|
|
|
|
// SmbScan2 执行SMB2服务的认证扫描,支持密码和哈希两种认证方式
|
2024-12-18 21:56:08 +08:00
|
|
|
|
func SmbScan2(info *Config.HostInfo) (tmperr error) {
|
2024-12-19 15:13:38 +08:00
|
|
|
|
|
2024-12-18 15:19:47 +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 15:13:38 +08:00
|
|
|
|
fmt.Println("[+] Smb2扫描模块开始...")
|
2024-12-18 15:19:47 +08:00
|
|
|
|
|
2022-11-19 17:04:13 +08:00
|
|
|
|
hasprint := false
|
2024-12-18 15:19:47 +08:00
|
|
|
|
startTime := time.Now().Unix()
|
|
|
|
|
|
|
|
|
|
|
|
// 使用哈希认证模式
|
2024-12-18 22:00:18 +08:00
|
|
|
|
if len(Common.HashBytes) > 0 {
|
2024-12-18 15:19:47 +08:00
|
|
|
|
return smbHashScan(info, hasprint, startTime)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 使用密码认证模式
|
|
|
|
|
|
return smbPasswordScan(info, hasprint, startTime)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// smbHashScan 使用哈希进行认证扫描
|
2024-12-18 21:56:08 +08:00
|
|
|
|
func smbHashScan(info *Config.HostInfo, hasprint bool, startTime int64) error {
|
2024-12-18 22:00:18 +08:00
|
|
|
|
for _, user := range Common.Userdict["smb"] {
|
|
|
|
|
|
for _, hash := range Common.HashBytes {
|
2024-12-18 15:19:47 +08:00
|
|
|
|
success, err, printed := Smb2Con(info, user, "", hash, hasprint)
|
|
|
|
|
|
if printed {
|
|
|
|
|
|
hasprint = true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if success {
|
|
|
|
|
|
logSuccessfulAuth(info, user, "", hash)
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
logFailedAuth(info, user, "", hash, err)
|
|
|
|
|
|
|
2024-12-18 22:00:18 +08:00
|
|
|
|
if shouldStopScan(err, startTime, len(Common.Userdict["smb"])*len(Common.HashBytes)) {
|
2024-12-18 15:19:47 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-18 22:00:18 +08:00
|
|
|
|
if len(Common.Hash) > 0 {
|
2024-12-18 15:19:47 +08:00
|
|
|
|
break
|
2024-08-29 09:50:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-12-18 15:19:47 +08:00
|
|
|
|
}
|
2024-12-19 15:13:38 +08:00
|
|
|
|
fmt.Println("[+] Smb2扫描模块结束...")
|
2024-12-18 15:19:47 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// smbPasswordScan 使用密码进行认证扫描
|
2024-12-18 21:56:08 +08:00
|
|
|
|
func smbPasswordScan(info *Config.HostInfo, hasprint bool, startTime int64) error {
|
2024-12-18 22:00:18 +08:00
|
|
|
|
for _, user := range Common.Userdict["smb"] {
|
|
|
|
|
|
for _, pass := range Common.Passwords {
|
2024-12-18 15:19:47 +08:00
|
|
|
|
pass = strings.ReplaceAll(pass, "{user}", user)
|
|
|
|
|
|
success, err, printed := Smb2Con(info, user, pass, []byte{}, hasprint)
|
|
|
|
|
|
if printed {
|
|
|
|
|
|
hasprint = true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if success {
|
|
|
|
|
|
logSuccessfulAuth(info, user, pass, []byte{})
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
logFailedAuth(info, user, pass, []byte{}, err)
|
|
|
|
|
|
|
2024-12-18 22:00:18 +08:00
|
|
|
|
if shouldStopScan(err, startTime, len(Common.Userdict["smb"])*len(Common.Passwords)) {
|
2024-12-18 15:19:47 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-18 22:00:18 +08:00
|
|
|
|
if len(Common.Hash) > 0 {
|
2024-12-18 15:19:47 +08:00
|
|
|
|
break
|
2022-11-19 17:04:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-12-19 15:13:38 +08:00
|
|
|
|
fmt.Println("[+] Smb2扫描模块结束...")
|
2024-12-18 15:19:47 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// logSuccessfulAuth 记录成功的认证
|
2024-12-18 21:56:08 +08:00
|
|
|
|
func logSuccessfulAuth(info *Config.HostInfo, user, pass string, hash []byte) {
|
2024-12-18 15:19:47 +08:00
|
|
|
|
var result string
|
2024-12-18 22:00:18 +08:00
|
|
|
|
if Common.Domain != "" {
|
2024-12-18 15:19:47 +08:00
|
|
|
|
result = fmt.Sprintf("[✓] SMB2认证成功 %v:%v Domain:%v\\%v ",
|
2024-12-18 22:00:18 +08:00
|
|
|
|
info.Host, info.Ports, Common.Domain, user)
|
2024-12-18 15:19:47 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
result = fmt.Sprintf("[✓] SMB2认证成功 %v:%v User:%v ",
|
|
|
|
|
|
info.Host, info.Ports, user)
|
|
|
|
|
|
}
|
2024-08-29 09:50:32 +08:00
|
|
|
|
|
2024-12-18 15:19:47 +08:00
|
|
|
|
if len(hash) > 0 {
|
2024-12-18 22:00:18 +08:00
|
|
|
|
result += fmt.Sprintf("Hash:%v", Common.Hash)
|
2024-12-18 15:19:47 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
result += fmt.Sprintf("Pass:%v", pass)
|
|
|
|
|
|
}
|
2024-12-18 22:00:18 +08:00
|
|
|
|
Common.LogSuccess(result)
|
2022-11-19 17:04:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-18 15:19:47 +08:00
|
|
|
|
// logFailedAuth 记录失败的认证
|
2024-12-18 21:56:08 +08:00
|
|
|
|
func logFailedAuth(info *Config.HostInfo, user, pass string, hash []byte, err error) {
|
2024-12-18 15:19:47 +08:00
|
|
|
|
var errlog string
|
|
|
|
|
|
if len(hash) > 0 {
|
|
|
|
|
|
errlog = fmt.Sprintf("[x] SMB2认证失败 %v:%v User:%v Hash:%v Err:%v",
|
2024-12-18 22:00:18 +08:00
|
|
|
|
info.Host, info.Ports, user, Common.Hash, err)
|
2024-12-18 15:19:47 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
errlog = fmt.Sprintf("[x] SMB2认证失败 %v:%v User:%v Pass:%v Err:%v",
|
|
|
|
|
|
info.Host, info.Ports, user, pass, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
errlog = strings.ReplaceAll(errlog, "\n", " ")
|
2024-12-18 22:00:18 +08:00
|
|
|
|
Common.LogError(errlog)
|
2024-12-18 15:19:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// shouldStopScan 检查是否应该停止扫描
|
|
|
|
|
|
func shouldStopScan(err error, startTime int64, totalAttempts int) bool {
|
2024-12-18 22:00:18 +08:00
|
|
|
|
if Common.CheckErrs(err) {
|
2024-12-18 15:19:47 +08:00
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-18 22:00:18 +08:00
|
|
|
|
if time.Now().Unix()-startTime > (int64(totalAttempts) * Common.Timeout) {
|
2024-12-18 15:19:47 +08:00
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Smb2Con 尝试SMB2连接并进行认证,检查共享访问权限
|
2024-12-18 21:56:08 +08:00
|
|
|
|
func Smb2Con(info *Config.HostInfo, user string, pass string, hash []byte, hasprint bool) (flag bool, err error, flag2 bool) {
|
2024-12-18 15:19:47 +08:00
|
|
|
|
// 建立TCP连接
|
|
|
|
|
|
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:445", info.Host),
|
2024-12-18 22:00:18 +08:00
|
|
|
|
time.Duration(Common.Timeout)*time.Second)
|
2022-11-19 17:04:13 +08:00
|
|
|
|
if err != nil {
|
2024-12-18 15:19:47 +08:00
|
|
|
|
return false, fmt.Errorf("连接失败: %v", err), false
|
2022-11-19 17:04:13 +08:00
|
|
|
|
}
|
2023-11-13 16:23:19 +08:00
|
|
|
|
defer conn.Close()
|
2024-12-18 15:19:47 +08:00
|
|
|
|
|
|
|
|
|
|
// 配置NTLM认证
|
2022-11-19 17:04:13 +08:00
|
|
|
|
initiator := smb2.NTLMInitiator{
|
|
|
|
|
|
User: user,
|
2024-12-18 22:00:18 +08:00
|
|
|
|
Domain: Common.Domain,
|
2022-11-19 17:04:13 +08:00
|
|
|
|
}
|
2024-12-18 15:19:47 +08:00
|
|
|
|
|
|
|
|
|
|
// 设置认证方式(哈希或密码)
|
2022-11-19 17:04:13 +08:00
|
|
|
|
if len(hash) > 0 {
|
|
|
|
|
|
initiator.Hash = hash
|
|
|
|
|
|
} else {
|
|
|
|
|
|
initiator.Password = pass
|
|
|
|
|
|
}
|
2024-12-18 15:19:47 +08:00
|
|
|
|
|
|
|
|
|
|
// 创建SMB2会话
|
2022-11-19 17:04:13 +08:00
|
|
|
|
d := &smb2.Dialer{
|
|
|
|
|
|
Initiator: &initiator,
|
|
|
|
|
|
}
|
2024-12-18 15:19:47 +08:00
|
|
|
|
session, err := d.Dial(conn)
|
2022-11-19 17:04:13 +08:00
|
|
|
|
if err != nil {
|
2024-12-18 15:19:47 +08:00
|
|
|
|
return false, fmt.Errorf("SMB2会话建立失败: %v", err), false
|
2022-11-19 17:04:13 +08:00
|
|
|
|
}
|
2024-12-18 15:19:47 +08:00
|
|
|
|
defer session.Logoff()
|
|
|
|
|
|
|
|
|
|
|
|
// 获取共享列表
|
|
|
|
|
|
shares, err := session.ListSharenames()
|
2022-11-19 17:04:13 +08:00
|
|
|
|
if err != nil {
|
2024-12-18 15:19:47 +08:00
|
|
|
|
return false, fmt.Errorf("获取共享列表失败: %v", err), false
|
2022-11-19 17:04:13 +08:00
|
|
|
|
}
|
2024-12-18 15:19:47 +08:00
|
|
|
|
|
|
|
|
|
|
// 打印共享信息(如果未打印过)
|
2022-11-19 17:04:13 +08:00
|
|
|
|
if !hasprint {
|
2024-12-18 15:19:47 +08:00
|
|
|
|
logShareInfo(info, user, pass, hash, shares)
|
2022-11-19 17:04:13 +08:00
|
|
|
|
flag2 = true
|
|
|
|
|
|
}
|
2024-12-18 15:19:47 +08:00
|
|
|
|
|
|
|
|
|
|
// 尝试访问C$共享以验证管理员权限
|
|
|
|
|
|
fs, err := session.Mount("C$")
|
2022-11-19 17:04:13 +08:00
|
|
|
|
if err != nil {
|
2024-12-18 15:19:47 +08:00
|
|
|
|
return false, fmt.Errorf("挂载C$失败: %v", err), flag2
|
2022-11-19 17:04:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
defer fs.Umount()
|
2024-12-18 15:19:47 +08:00
|
|
|
|
|
|
|
|
|
|
// 尝试读取系统文件以验证权限
|
2022-11-19 17:04:13 +08:00
|
|
|
|
path := `Windows\win.ini`
|
|
|
|
|
|
f, err := fs.OpenFile(path, os.O_RDONLY, 0666)
|
|
|
|
|
|
if err != nil {
|
2024-12-18 15:19:47 +08:00
|
|
|
|
return false, fmt.Errorf("访问系统文件失败: %v", err), flag2
|
2022-11-19 17:04:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
2024-12-18 15:19:47 +08:00
|
|
|
|
return true, nil, flag2
|
2022-11-19 17:04:13 +08:00
|
|
|
|
}
|
2023-11-13 11:24:44 +08:00
|
|
|
|
|
2024-12-18 15:19:47 +08:00
|
|
|
|
// logShareInfo 记录SMB共享信息
|
2024-12-18 21:56:08 +08:00
|
|
|
|
func logShareInfo(info *Config.HostInfo, user string, pass string, hash []byte, shares []string) {
|
2024-12-18 15:19:47 +08:00
|
|
|
|
var result string
|
|
|
|
|
|
|
|
|
|
|
|
// 构建基础信息
|
2024-12-18 22:00:18 +08:00
|
|
|
|
if Common.Domain != "" {
|
2024-12-18 15:19:47 +08:00
|
|
|
|
result = fmt.Sprintf("[*] SMB2共享信息 %v:%v Domain:%v\\%v ",
|
2024-12-18 22:00:18 +08:00
|
|
|
|
info.Host, info.Ports, Common.Domain, user)
|
2024-12-18 15:19:47 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
result = fmt.Sprintf("[*] SMB2共享信息 %v:%v User:%v ",
|
|
|
|
|
|
info.Host, info.Ports, user)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 添加认证信息
|
|
|
|
|
|
if len(hash) > 0 {
|
2024-12-18 22:00:18 +08:00
|
|
|
|
result += fmt.Sprintf("Hash:%v ", Common.Hash)
|
2024-12-18 15:19:47 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
result += fmt.Sprintf("Pass:%v ", pass)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 添加共享列表
|
|
|
|
|
|
result += fmt.Sprintf("可用共享: %v", shares)
|
2024-12-18 22:00:18 +08:00
|
|
|
|
Common.LogSuccess(result)
|
2024-12-18 15:19:47 +08:00
|
|
|
|
}
|