Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/js/libs/smb/smbghost.go
2070 views
1
package smb
2
3
import (
4
"bytes"
5
"context"
6
"errors"
7
"fmt"
8
"net"
9
"strconv"
10
"time"
11
12
"github.com/projectdiscovery/nuclei/v3/pkg/js/libs/structs"
13
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/protocolstate"
14
"github.com/projectdiscovery/utils/reader"
15
)
16
17
const (
18
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"
19
)
20
21
// DetectSMBGhost tries to detect SMBGhost vulnerability
22
// by using SMBv3 compression feature.
23
// If the host is vulnerable, it returns true.
24
// @example
25
// ```javascript
26
// const smb = require('nuclei/smb');
27
// const isSMBGhost = smb.DetectSMBGhost('acme.com', 445);
28
// ```
29
func (c *SMBClient) DetectSMBGhost(ctx context.Context, host string, port int) (bool, error) {
30
executionId := ctx.Value("executionId").(string)
31
return memoizeddetectSMBGhost(executionId, host, port)
32
}
33
34
// @memo
35
func detectSMBGhost(executionId string, host string, port int) (bool, error) {
36
if !protocolstate.IsHostAllowed(executionId, host) {
37
// host is not valid according to network policy
38
return false, protocolstate.ErrHostDenied.Msgf(host)
39
}
40
addr := net.JoinHostPort(host, strconv.Itoa(port))
41
dialer := protocolstate.GetDialersWithId(executionId)
42
if dialer == nil {
43
return false, fmt.Errorf("dialers not initialized for %s", executionId)
44
}
45
conn, err := dialer.Fastdialer.Dial(context.TODO(), "tcp", addr)
46
if err != nil {
47
return false, err
48
49
}
50
defer func() {
51
_ = conn.Close()
52
}()
53
54
_, err = conn.Write([]byte(pkt))
55
if err != nil {
56
return false, err
57
}
58
buff, _ := reader.ConnReadNWithTimeout(conn, 4, time.Duration(5)*time.Second)
59
args, err := structs.Unpack(">I", buff)
60
if err != nil {
61
return false, err
62
}
63
if len(args) != 1 {
64
return false, errors.New("invalid response")
65
}
66
67
length := args[0].(int)
68
_ = conn.SetReadDeadline(time.Now().Add(2 * time.Second))
69
data, err := reader.ConnReadNWithTimeout(conn, int64(length), time.Duration(5)*time.Second)
70
if err != nil {
71
return false, err
72
}
73
if len(data) < 72 {
74
return false, errors.New("invalid response expected at least 72 bytes")
75
}
76
77
if !bytes.Equal(data[68:70], []byte("\x11\x03")) || !bytes.Equal(data[70:72], []byte("\x02\x00")) {
78
return false, nil
79
}
80
return true, nil
81
}
82
83