2023-09-16 16:02:17 +05:30
|
|
|
package smtp
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2023-12-21 13:34:22 +01:00
|
|
|
"fmt"
|
2023-09-16 16:02:17 +05:30
|
|
|
"net"
|
2023-12-21 13:34:22 +01:00
|
|
|
"net/smtp"
|
2023-09-16 16:02:17 +05:30
|
|
|
"strconv"
|
|
|
|
|
"time"
|
|
|
|
|
|
2024-03-01 16:38:56 +05:30
|
|
|
"github.com/dop251/goja"
|
2023-09-16 16:02:17 +05:30
|
|
|
"github.com/praetorian-inc/fingerprintx/pkg/plugins"
|
2024-03-01 16:38:56 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/js/utils"
|
2023-10-17 17:44:13 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/protocolstate"
|
2023-12-21 13:34:22 +01:00
|
|
|
|
|
|
|
|
pluginsmtp "github.com/praetorian-inc/fingerprintx/pkg/plugins/services/smtp"
|
2023-09-16 16:02:17 +05:30
|
|
|
)
|
|
|
|
|
|
2024-02-07 21:45:40 +05:30
|
|
|
type (
|
2024-03-01 16:38:56 +05:30
|
|
|
// SMTPResponse is the response from the IsSMTP function.
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const smtp = require('nuclei/smtp');
|
2024-03-01 16:38:56 +05:30
|
|
|
// const client = new smtp.Client('acme.com', 25);
|
|
|
|
|
// const isSMTP = client.IsSMTP();
|
|
|
|
|
// log(isSMTP)
|
2024-02-07 21:45:40 +05:30
|
|
|
// ```
|
2024-03-01 16:38:56 +05:30
|
|
|
SMTPResponse struct {
|
|
|
|
|
IsSMTP bool
|
|
|
|
|
Banner string
|
|
|
|
|
}
|
2024-02-07 21:45:40 +05:30
|
|
|
)
|
2023-09-16 16:02:17 +05:30
|
|
|
|
2024-02-07 21:45:40 +05:30
|
|
|
type (
|
2024-03-01 16:38:56 +05:30
|
|
|
// Client is a minimal SMTP client for nuclei scripts.
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const smtp = require('nuclei/smtp');
|
2024-03-01 16:38:56 +05:30
|
|
|
// const client = new smtp.Client('acme.com', 25);
|
2024-02-07 21:45:40 +05:30
|
|
|
// ```
|
2024-03-01 16:38:56 +05:30
|
|
|
Client struct {
|
|
|
|
|
nj *utils.NucleiJS
|
|
|
|
|
host string
|
|
|
|
|
port string
|
2024-02-07 21:45:40 +05:30
|
|
|
}
|
|
|
|
|
)
|
2023-09-16 16:02:17 +05:30
|
|
|
|
2024-03-01 16:38:56 +05:30
|
|
|
// 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)
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-16 16:02:17 +05:30
|
|
|
// IsSMTP checks if a host is running a SMTP server.
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const smtp = require('nuclei/smtp');
|
2024-03-01 16:38:56 +05:30
|
|
|
// const client = new smtp.Client('acme.com', 25);
|
|
|
|
|
// const isSMTP = client.IsSMTP();
|
|
|
|
|
// log(isSMTP)
|
2024-02-07 21:45:40 +05:30
|
|
|
// ```
|
2024-03-01 16:38:56 +05:30
|
|
|
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")
|
2023-09-16 16:02:17 +05:30
|
|
|
|
|
|
|
|
timeout := 5 * time.Second
|
2024-03-01 16:38:56 +05:30
|
|
|
conn, err := protocolstate.Dialer.Dial(context.TODO(), "tcp", net.JoinHostPort(c.host, c.port))
|
2023-09-16 16:02:17 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return resp, err
|
|
|
|
|
}
|
2025-07-01 00:40:44 +07:00
|
|
|
defer func() {
|
|
|
|
|
_ = conn.Close()
|
|
|
|
|
}()
|
2023-09-16 16:02:17 +05:30
|
|
|
|
2023-12-21 13:34:22 +01:00
|
|
|
smtpPlugin := pluginsmtp.SMTPPlugin{}
|
2024-03-01 16:38:56 +05:30
|
|
|
service, err := smtpPlugin.Run(conn, timeout, plugins.Target{Host: c.host})
|
2023-09-16 16:02:17 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return resp, err
|
|
|
|
|
}
|
|
|
|
|
if service == nil {
|
|
|
|
|
return resp, nil
|
|
|
|
|
}
|
|
|
|
|
resp.Banner = service.Version
|
|
|
|
|
resp.IsSMTP = true
|
|
|
|
|
return resp, nil
|
|
|
|
|
}
|
2023-12-21 13:34:22 +01:00
|
|
|
|
2024-02-07 21:45:40 +05:30
|
|
|
// 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');
|
2024-03-01 16:38:56 +05:30
|
|
|
// const client = new smtp.Client('acme.com', 25);
|
|
|
|
|
// const isRelay = client.IsOpenRelay(message);
|
2024-02-07 21:45:40 +05:30
|
|
|
// ```
|
2024-03-01 16:38:56 +05:30
|
|
|
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")
|
2023-12-21 13:34:22 +01:00
|
|
|
|
2024-03-01 16:38:56 +05:30
|
|
|
addr := net.JoinHostPort(c.host, c.port)
|
2023-12-21 13:34:22 +01:00
|
|
|
conn, err := protocolstate.Dialer.Dial(context.TODO(), "tcp", addr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
2025-07-01 00:40:44 +07:00
|
|
|
defer func() {
|
|
|
|
|
_ = conn.Close()
|
|
|
|
|
}()
|
2024-03-01 16:38:56 +05:30
|
|
|
client, err := smtp.NewClient(conn, c.host)
|
2023-12-21 13:34:22 +01:00
|
|
|
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.
|
2024-02-07 21:45:40 +05:30
|
|
|
// @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');
|
2024-03-01 16:38:56 +05:30
|
|
|
// const client = new smtp.Client('acme.com', 25);
|
|
|
|
|
// const isSent = client.SendMail(message);
|
|
|
|
|
// log(isSent)
|
2024-02-07 21:45:40 +05:30
|
|
|
// ```
|
2024-03-01 16:38:56 +05:30
|
|
|
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")
|
2023-12-21 13:34:22 +01:00
|
|
|
|
|
|
|
|
var auth smtp.Auth
|
|
|
|
|
if msg.user != "" && msg.pass != "" {
|
2024-03-01 16:38:56 +05:30
|
|
|
auth = smtp.PlainAuth("", msg.user, msg.pass, c.host)
|
2023-12-21 13:34:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// send mail
|
2024-03-01 16:38:56 +05:30
|
|
|
addr := net.JoinHostPort(c.host, c.port)
|
2023-12-21 13:34:22 +01:00
|
|
|
if err := smtp.SendMail(addr, auth, msg.from, msg.to, []byte(msg.String())); err != nil {
|
2024-03-01 16:38:56 +05:30
|
|
|
c.nj.Throw("failed to send mail with message(%s) got %v", msg.String(), err)
|
2023-12-21 13:34:22 +01:00
|
|
|
}
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|