fscan/Plugins/ActiveMQ.go
2025-03-19 12:21:51 +00:00

190 lines
4.9 KiB
Go

package Plugins
import (
"fmt"
"github.com/shadow1ng/fscan/Common"
"net"
"strings"
"time"
)
func ActiveMQScan(info *Common.HostInfo) (tmperr error) {
if Common.DisableBrute {
return
}
maxRetries := Common.MaxRetries
target := fmt.Sprintf("%v:%v", info.Host, info.Ports)
Common.LogDebug(fmt.Sprintf("Starting scan %s", target))
Common.LogDebug("Trying default account admin:admin")
// First test the default account
for retryCount := 0; retryCount < maxRetries; retryCount++ {
if retryCount > 0 {
Common.LogDebug(fmt.Sprintf("Retrying default account for the %d time", retryCount+1))
}
flag, err := ActiveMQConn(info, "admin", "admin")
if flag {
successMsg := fmt.Sprintf("ActiveMQ service %s successfully brute-forced Username: admin Password: admin", target)
Common.LogSuccess(successMsg)
// Save result
result := &Common.ScanResult{
Time: time.Now(),
Type: Common.VULN,
Target: info.Host,
Status: "vulnerable",
Details: map[string]interface{}{
"port": info.Ports,
"service": "activemq",
"username": "admin",
"password": "admin",
"type": "weak-password",
},
}
Common.SaveResult(result)
return nil
}
if err != nil {
errMsg := fmt.Sprintf("ActiveMQ service %s default account attempt failed: %v", target, err)
Common.LogError(errMsg)
if retryErr := Common.CheckErrs(err); retryErr != nil {
if retryCount == maxRetries-1 {
return err
}
continue
}
}
break
}
totalUsers := len(Common.Userdict["activemq"])
totalPass := len(Common.Passwords)
Common.LogDebug(fmt.Sprintf("Starting to try username and password combinations (Total users: %d, Total passwords: %d)", totalUsers, totalPass))
tried := 0
total := totalUsers * totalPass
// Iterate over all username and password combinations
for _, user := range Common.Userdict["activemq"] {
for _, pass := range Common.Passwords {
tried++
pass = strings.Replace(pass, "{user}", user, -1)
Common.LogDebug(fmt.Sprintf("[%d/%d] Trying: %s:%s", tried, total, user, pass))
// Retry loop
for retryCount := 0; retryCount < maxRetries; retryCount++ {
if retryCount > 0 {
Common.LogDebug(fmt.Sprintf("Retrying for the %d time: %s:%s", retryCount+1, user, pass))
}
done := make(chan struct {
success bool
err error
}, 1)
go func(user, pass string) {
flag, err := ActiveMQConn(info, user, pass)
select {
case done <- struct {
success bool
err error
}{flag, err}:
default:
}
}(user, pass)
var err error
select {
case result := <-done:
err = result.err
if result.success {
successMsg := fmt.Sprintf("ActiveMQ service %s successfully brute-forced Username: %v Password: %v", target, user, pass)
Common.LogSuccess(successMsg)
// Save result
vulnResult := &Common.ScanResult{
Time: time.Now(),
Type: Common.VULN,
Target: info.Host,
Status: "vulnerable",
Details: map[string]interface{}{
"port": info.Ports,
"service": "activemq",
"username": user,
"password": pass,
"type": "weak-password",
},
}
Common.SaveResult(vulnResult)
return nil
}
case <-time.After(time.Duration(Common.Timeout) * time.Second):
err = fmt.Errorf("connection timeout")
}
if err != nil {
errMsg := fmt.Sprintf("ActiveMQ service %s attempt failed Username: %v Password: %v Error: %v", target, user, pass, err)
Common.LogError(errMsg)
if retryErr := Common.CheckErrs(err); retryErr != nil {
if retryCount == maxRetries-1 {
continue
}
continue
}
}
break
}
}
}
Common.LogDebug(fmt.Sprintf("Scan completed, tried %d combinations", tried))
return tmperr
}
// ActiveMQConn unified connection test function
func ActiveMQConn(info *Common.HostInfo, user string, pass string) (bool, error) {
timeout := time.Duration(Common.Timeout) * time.Second
addr := fmt.Sprintf("%s:%s", info.Host, info.Ports)
conn, err := net.DialTimeout("tcp", addr, timeout)
if err != nil {
return false, err
}
defer conn.Close()
// STOMP protocol CONNECT command
stompConnect := fmt.Sprintf("CONNECT\naccept-version:1.0,1.1,1.2\nhost:/\nlogin:%s\npasscode:%s\n\n\x00", user, pass)
// Send authentication request
conn.SetWriteDeadline(time.Now().Add(timeout))
if _, err := conn.Write([]byte(stompConnect)); err != nil {
return false, err
}
// Read response
conn.SetReadDeadline(time.Now().Add(timeout))
respBuf := make([]byte, 1024)
n, err := conn.Read(respBuf)
if err != nil {
return false, err
}
// Check authentication result
response := string(respBuf[:n])
if strings.Contains(response, "CONNECTED") {
return true, nil
}
if strings.Contains(response, "Authentication failed") || strings.Contains(response, "ERROR") {
return false, fmt.Errorf("authentication failed")
}
return false, fmt.Errorf("unknown response: %s", response)
}