2023-09-16 16:02:17 +05:30
|
|
|
package smb
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"net"
|
|
|
|
|
"strconv"
|
|
|
|
|
"time"
|
|
|
|
|
|
2023-10-17 17:44:13 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/js/libs/structs"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/protocolstate"
|
2023-11-02 13:33:40 +05:30
|
|
|
"github.com/projectdiscovery/utils/reader"
|
2023-09-16 16:02:17 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
pkt = "\x00\x00\x00\xc0\xfeSMB@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00$\x00\x08\x00\x01\x00\x00\x00\x7f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00x\x00\x00\x00\x02\x00\x00\x00\x02\x02\x10\x02\"\x02$\x02\x00\x03\x02\x03\x10\x03\x11\x03\x00\x00\x00\x00\x01\x00&\x00\x00\x00\x00\x00\x01\x00 \x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\n\x00\x00\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// DetectSMBGhost tries to detect SMBGhost vulnerability
|
|
|
|
|
// by using SMBv3 compression feature.
|
|
|
|
|
func (c *SMBClient) DetectSMBGhost(host string, port int) (bool, error) {
|
|
|
|
|
addr := net.JoinHostPort(host, strconv.Itoa(port))
|
|
|
|
|
conn, err := protocolstate.Dialer.Dial(context.TODO(), "tcp", addr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
|
|
_, err = conn.Write([]byte(pkt))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
2023-11-02 13:33:40 +05:30
|
|
|
buff, _ := reader.ConnReadNWithTimeout(conn, 4, time.Duration(5)*time.Second)
|
|
|
|
|
args, err := structs.Unpack(">I", buff)
|
2023-09-16 16:02:17 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
if len(args) != 1 {
|
|
|
|
|
return false, errors.New("invalid response")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
length := args[0].(int)
|
|
|
|
|
_ = conn.SetReadDeadline(time.Now().Add(2 * time.Second))
|
2023-11-02 13:33:40 +05:30
|
|
|
data, err := reader.ConnReadNWithTimeout(conn, int64(length), time.Duration(5)*time.Second)
|
2023-09-16 16:02:17 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
2023-11-02 13:33:40 +05:30
|
|
|
if len(data) < 72 {
|
|
|
|
|
return false, errors.New("invalid response expected at least 72 bytes")
|
|
|
|
|
}
|
2023-09-16 16:02:17 +05:30
|
|
|
|
|
|
|
|
if !bytes.Equal(data[68:70], []byte("\x11\x03")) || !bytes.Equal(data[70:72], []byte("\x02\x00")) {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|