diff --git a/pkg/js/libs/mysql/mysql_private.go b/pkg/js/libs/mysql/mysql_private.go index 42e76a5579..c5f2292170 100644 --- a/pkg/js/libs/mysql/mysql_private.go +++ b/pkg/js/libs/mysql/mysql_private.go @@ -46,6 +46,12 @@ func BuildDSN(opts MySQLOptions) (string, error) { if opts.Protocol == "" { opts.Protocol = "tcp" } + // We're going to use a custom dialer when creating MySQL connections, so if we've been + // given "tcp" as the protocol, then quietly switch it to "nucleitcp", which we have + // already registered. + if opts.Protocol == "tcp" { + opts.Protocol = "nucleitcp" + } if opts.DbName == "" { opts.DbName = "/" } else { diff --git a/pkg/protocols/common/protocolstate/state.go b/pkg/protocols/common/protocolstate/state.go index 5c56fdf77e..f226067cb2 100644 --- a/pkg/protocols/common/protocolstate/state.go +++ b/pkg/protocols/common/protocolstate/state.go @@ -154,8 +154,16 @@ func Init(options *types.Options) error { } Dialer = dialer - // override dialer in mysql - mysql.RegisterDialContext("tcp", func(ctx context.Context, addr string) (net.Conn, error) { + // Set a custom dialer for the "nucleitcp" protocol. This is just plain TCP, but it's registered + // with a different name so that we do not clobber the "tcp" dialer in the event that nuclei is + // being included as a package in another application. + mysql.RegisterDialContext("nucleitcp", func(ctx context.Context, addr string) (net.Conn, error) { + // Because we're not using the default TCP workflow, quietly add the default port + // number if no port number was specified. + if _, _, err := net.SplitHostPort(addr); err != nil { + addr += ":3306" + } + return Dialer.Dial(ctx, "tcp", addr) })