zgrab2/modules/smtp/smtp.go

40 lines
1.1 KiB
Go
Raw Normal View History

2018-03-14 13:18:54 +00:00
package smtp
import (
"net"
"regexp"
"io"
2018-03-14 21:19:27 +00:00
"github.com/zmap/zgrab2"
2018-03-14 13:18:54 +00:00
)
// This is the regex used in zgrab.
// Corner cases like "200 OK\r\nthis is not valid at all\x00\x01\x02\x03\r\n" will be matched.
var smtpEndRegex = regexp.MustCompile(`(?:^\d\d\d\s.*\r\n$)|(?:^\d\d\d-[\s\S]*\r\n\d\d\d\s.*\r\n$)`)
const readBufferSize int = 0x10000
2018-03-14 21:19:27 +00:00
// Connection wraps the state and access to the SMTP connection.
2018-03-14 13:18:54 +00:00
type Connection struct {
Conn net.Conn
}
2018-03-14 21:19:27 +00:00
// ReadResponse reads from the connection until it matches the smtpEndRegex. Copied from the original zgrab.
// TODO: Catch corner cases
2018-03-14 21:19:27 +00:00
func (conn *Connection) ReadResponse() (string, error) {
2018-03-14 13:18:54 +00:00
ret := make([]byte, readBufferSize)
n, err := zgrab2.ReadUntilRegex(conn.Conn, ret, smtpEndRegex)
if err != nil && err != io.EOF && !zgrab2.IsTimeoutError(err) {
return "", err
2018-03-14 13:18:54 +00:00
}
return string(ret[:n]), nil
2018-03-14 13:18:54 +00:00
}
2018-03-14 21:19:27 +00:00
// SendCommand sends a command, followed by a CRLF, then wait for / read the server's response.
2018-03-14 13:18:54 +00:00
func (conn *Connection) SendCommand(cmd string) (string, error) {
if _, err := conn.Conn.Write([]byte(cmd + "\r\n")); err != nil {
2018-03-14 21:19:27 +00:00
return "", err
2018-03-14 13:18:54 +00:00
}
2018-03-14 21:19:27 +00:00
return conn.ReadResponse()
}