Skip to content

Commit e4a0b8e

Browse files
Do not clobber the "tcp" dialer for MySQL (#5681)
This introduces a "nucleitcp" protocol that Nuclei will use when making MySQL connections as part of its templates. Previously, this would register (and de-register!) a custom "tcp" dialer, and that applied globally, so any piece of software that used a MySQL database and included nuclei in SDK mode would have its database connections ripped out from under it due to the dialer hijacking. By using "nucleitcp" as the protocol, we are free to do whatever we want with the dialer and not impact any other packages. Within our `BuildDSN` function, we quietly replace the protocol to "nucleitcp" if it was "tcp", so nuclei developers don't have to do anything special to use this functionality; it will always do it.
1 parent 08c46ff commit e4a0b8e

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

pkg/js/libs/mysql/mysql_private.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ func BuildDSN(opts MySQLOptions) (string, error) {
4646
if opts.Protocol == "" {
4747
opts.Protocol = "tcp"
4848
}
49+
// We're going to use a custom dialer when creating MySQL connections, so if we've been
50+
// given "tcp" as the protocol, then quietly switch it to "nucleitcp", which we have
51+
// already registered.
52+
if opts.Protocol == "tcp" {
53+
opts.Protocol = "nucleitcp"
54+
}
4955
if opts.DbName == "" {
5056
opts.DbName = "/"
5157
} else {

pkg/protocols/common/protocolstate/state.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,16 @@ func Init(options *types.Options) error {
154154
}
155155
Dialer = dialer
156156

157-
// override dialer in mysql
158-
mysql.RegisterDialContext("tcp", func(ctx context.Context, addr string) (net.Conn, error) {
157+
// Set a custom dialer for the "nucleitcp" protocol. This is just plain TCP, but it's registered
158+
// with a different name so that we do not clobber the "tcp" dialer in the event that nuclei is
159+
// being included as a package in another application.
160+
mysql.RegisterDialContext("nucleitcp", func(ctx context.Context, addr string) (net.Conn, error) {
161+
// Because we're not using the default TCP workflow, quietly add the default port
162+
// number if no port number was specified.
163+
if _, _, err := net.SplitHostPort(addr); err != nil {
164+
addr += ":3306"
165+
}
166+
159167
return Dialer.Dial(ctx, "tcp", addr)
160168
})
161169

0 commit comments

Comments
 (0)