-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
smtp.go
194 lines (173 loc) · 5.13 KB
/
smtp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package smtp
import (
"context"
"fmt"
"net"
"net/smtp"
"strconv"
"time"
"github.com/dop251/goja"
"github.com/praetorian-inc/fingerprintx/pkg/plugins"
"github.com/projectdiscovery/nuclei/v3/pkg/js/utils"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/protocolstate"
pluginsmtp "github.com/praetorian-inc/fingerprintx/pkg/plugins/services/smtp"
)
type (
// SMTPResponse is the response from the IsSMTP function.
// @example
// ```javascript
// const smtp = require('nuclei/smtp');
// const client = new smtp.Client('acme.com', 25);
// const isSMTP = client.IsSMTP();
// log(isSMTP)
// ```
SMTPResponse struct {
IsSMTP bool
Banner string
}
)
type (
// Client is a minimal SMTP client for nuclei scripts.
// @example
// ```javascript
// const smtp = require('nuclei/smtp');
// const client = new smtp.Client('acme.com', 25);
// ```
Client struct {
nj *utils.NucleiJS
host string
port string
}
)
// Constructor for SMTP Client
// Constructor: constructor(public host: string, public port: string)
func NewSMTPClient(call goja.ConstructorCall, runtime *goja.Runtime) *goja.Object {
// setup nucleijs utils
c := &Client{nj: utils.NewNucleiJS(runtime)}
c.nj.ObjectSig = "Client(host, port)" // will be included in error messages
host, _ := c.nj.GetArg(call.Arguments, 0).(string) // host
port, _ := c.nj.GetArg(call.Arguments, 1).(string) // port
// validate arguments
c.nj.Require(host != "", "host cannot be empty")
c.nj.Require(port != "", "port cannot be empty")
// validate port
portInt, err := strconv.Atoi(port)
c.nj.Require(err == nil && portInt > 0 && portInt < 65536, "port must be a valid number")
c.host = host
c.port = port
// check if this is allowed address
c.nj.Require(protocolstate.IsHostAllowed(host+":"+port), protocolstate.ErrHostDenied.Msgf(host+":"+port).Error())
// Link Constructor to Client and return
return utils.LinkConstructor(call, runtime, c)
}
// IsSMTP checks if a host is running a SMTP server.
// @example
// ```javascript
// const smtp = require('nuclei/smtp');
// const client = new smtp.Client('acme.com', 25);
// const isSMTP = client.IsSMTP();
// log(isSMTP)
// ```
func (c *Client) IsSMTP() (SMTPResponse, error) {
resp := SMTPResponse{}
c.nj.Require(c.host != "", "host cannot be empty")
c.nj.Require(c.port != "", "port cannot be empty")
timeout := 5 * time.Second
conn, err := protocolstate.Dialer.Dial(context.TODO(), "tcp", net.JoinHostPort(c.host, c.port))
if err != nil {
return resp, err
}
defer conn.Close()
smtpPlugin := pluginsmtp.SMTPPlugin{}
service, err := smtpPlugin.Run(conn, timeout, plugins.Target{Host: c.host})
if err != nil {
return resp, err
}
if service == nil {
return resp, nil
}
resp.Banner = service.Version
resp.IsSMTP = true
return resp, nil
}
// IsOpenRelay checks if a host is an open relay.
// @example
// ```javascript
// const smtp = require('nuclei/smtp');
// const message = new smtp.SMTPMessage();
// message.From('xyz@projectdiscovery.io');
// message.To('xyz2@projectdiscoveyr.io');
// message.Subject('hello');
// message.Body('hello');
// const client = new smtp.Client('acme.com', 25);
// const isRelay = client.IsOpenRelay(message);
// ```
func (c *Client) IsOpenRelay(msg *SMTPMessage) (bool, error) {
c.nj.Require(c.host != "", "host cannot be empty")
c.nj.Require(c.port != "", "port cannot be empty")
addr := net.JoinHostPort(c.host, c.port)
conn, err := protocolstate.Dialer.Dial(context.TODO(), "tcp", addr)
if err != nil {
return false, err
}
defer conn.Close()
client, err := smtp.NewClient(conn, c.host)
if err != nil {
return false, err
}
if err := client.Mail(msg.from); err != nil {
return false, err
}
if len(msg.to) == 0 || len(msg.to) > 1 {
return false, fmt.Errorf("invalid number of recipients: required 1, got %d", len(msg.to))
}
if err := client.Rcpt(msg.to[0]); err != nil {
return false, err
}
// Send the email body.
wc, err := client.Data()
if err != nil {
return false, err
}
_, err = wc.Write([]byte(msg.String()))
if err != nil {
return false, err
}
err = wc.Close()
if err != nil {
return false, err
}
// Send the QUIT command and close the connection.
err = client.Quit()
if err != nil {
return false, err
}
return true, nil
}
// SendMail sends an email using the SMTP protocol.
// @example
// ```javascript
// const smtp = require('nuclei/smtp');
// const message = new smtp.SMTPMessage();
// message.From('xyz@projectdiscovery.io');
// message.To('xyz2@projectdiscoveyr.io');
// message.Subject('hello');
// message.Body('hello');
// const client = new smtp.Client('acme.com', 25);
// const isSent = client.SendMail(message);
// log(isSent)
// ```
func (c *Client) SendMail(msg *SMTPMessage) (bool, error) {
c.nj.Require(c.host != "", "host cannot be empty")
c.nj.Require(c.port != "", "port cannot be empty")
var auth smtp.Auth
if msg.user != "" && msg.pass != "" {
auth = smtp.PlainAuth("", msg.user, msg.pass, c.host)
}
// send mail
addr := net.JoinHostPort(c.host, c.port)
if err := smtp.SendMail(addr, auth, msg.from, msg.to, []byte(msg.String())); err != nil {
c.nj.Throw("failed to send mail with message(%s) got %v", msg.String(), err)
}
return true, nil
}