Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/js/libs/rdp/rdp.go
2070 views
1
package rdp
2
3
import (
4
"context"
5
"fmt"
6
"time"
7
8
"github.com/praetorian-inc/fingerprintx/pkg/plugins"
9
"github.com/praetorian-inc/fingerprintx/pkg/plugins/services/rdp"
10
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/protocolstate"
11
)
12
13
type (
14
// IsRDPResponse is the response from the IsRDP function.
15
// this is returned by IsRDP function.
16
// @example
17
// ```javascript
18
// const rdp = require('nuclei/rdp');
19
// const isRDP = rdp.IsRDP('acme.com', 3389);
20
// log(toJSON(isRDP));
21
// ```
22
IsRDPResponse struct {
23
IsRDP bool
24
OS string
25
}
26
)
27
28
// IsRDP checks if the given host and port are running rdp server.
29
// If connection is successful, it returns true.
30
// If connection is unsuccessful, it returns false and error.
31
// The Name of the OS is also returned if the connection is successful.
32
// @example
33
// ```javascript
34
// const rdp = require('nuclei/rdp');
35
// const isRDP = rdp.IsRDP('acme.com', 3389);
36
// log(toJSON(isRDP));
37
// ```
38
func IsRDP(ctx context.Context, host string, port int) (IsRDPResponse, error) {
39
executionId := ctx.Value("executionId").(string)
40
return memoizedisRDP(executionId, host, port)
41
}
42
43
// @memo
44
func isRDP(executionId string, host string, port int) (IsRDPResponse, error) {
45
resp := IsRDPResponse{}
46
47
dialer := protocolstate.GetDialersWithId(executionId)
48
if dialer == nil {
49
return IsRDPResponse{}, fmt.Errorf("dialers not initialized for %s", executionId)
50
}
51
52
timeout := 5 * time.Second
53
conn, err := dialer.Fastdialer.Dial(context.TODO(), "tcp", fmt.Sprintf("%s:%d", host, port))
54
if err != nil {
55
return resp, err
56
}
57
defer func() {
58
_ = conn.Close()
59
}()
60
61
server, isRDP, err := rdp.DetectRDP(conn, timeout)
62
if err != nil {
63
return resp, err
64
}
65
if !isRDP {
66
return resp, nil
67
}
68
resp.IsRDP = true
69
resp.OS = server
70
return resp, nil
71
}
72
73
type (
74
// CheckRDPAuthResponse is the response from the CheckRDPAuth function.
75
// this is returned by CheckRDPAuth function.
76
// @example
77
// ```javascript
78
// const rdp = require('nuclei/rdp');
79
// const checkRDPAuth = rdp.CheckRDPAuth('acme.com', 3389);
80
// log(toJSON(checkRDPAuth));
81
// ```
82
CheckRDPAuthResponse struct {
83
PluginInfo *plugins.ServiceRDP
84
Auth bool
85
}
86
)
87
88
// CheckRDPAuth checks if the given host and port are running rdp server
89
// with authentication and returns their metadata.
90
// If connection is successful, it returns true.
91
// @example
92
// ```javascript
93
// const rdp = require('nuclei/rdp');
94
// const checkRDPAuth = rdp.CheckRDPAuth('acme.com', 3389);
95
// log(toJSON(checkRDPAuth));
96
// ```
97
func CheckRDPAuth(ctx context.Context, host string, port int) (CheckRDPAuthResponse, error) {
98
executionId := ctx.Value("executionId").(string)
99
return memoizedcheckRDPAuth(executionId, host, port)
100
}
101
102
// @memo
103
func checkRDPAuth(executionId string, host string, port int) (CheckRDPAuthResponse, error) {
104
resp := CheckRDPAuthResponse{}
105
106
dialer := protocolstate.GetDialersWithId(executionId)
107
if dialer == nil {
108
return CheckRDPAuthResponse{}, fmt.Errorf("dialers not initialized for %s", executionId)
109
}
110
timeout := 5 * time.Second
111
conn, err := dialer.Fastdialer.Dial(context.TODO(), "tcp", fmt.Sprintf("%s:%d", host, port))
112
if err != nil {
113
return resp, err
114
}
115
defer func() {
116
_ = conn.Close()
117
}()
118
119
pluginInfo, auth, err := rdp.DetectRDPAuth(conn, timeout)
120
if err != nil {
121
return resp, err
122
}
123
if !auth {
124
return resp, nil
125
}
126
resp.Auth = true
127
resp.PluginInfo = pluginInfo
128
return resp, nil
129
}
130
131