2023-09-16 16:02:17 +05:30
|
|
|
package mysql
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"database/sql"
|
|
|
|
|
"fmt"
|
2024-01-31 02:32:23 +05:30
|
|
|
"io"
|
|
|
|
|
"log"
|
2023-09-16 16:02:17 +05:30
|
|
|
"net"
|
|
|
|
|
"time"
|
|
|
|
|
|
2024-01-31 02:32:23 +05:30
|
|
|
"github.com/go-sql-driver/mysql"
|
2023-09-16 16:02:17 +05:30
|
|
|
"github.com/praetorian-inc/fingerprintx/pkg/plugins"
|
|
|
|
|
mysqlplugin "github.com/praetorian-inc/fingerprintx/pkg/plugins/services/mysql"
|
2024-02-02 02:22:04 +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-09-16 16:02:17 +05:30
|
|
|
)
|
|
|
|
|
|
2024-02-07 21:45:40 +05:30
|
|
|
type (
|
|
|
|
|
// MySQLClient is a client for MySQL database.
|
|
|
|
|
// Internally client uses go-sql-driver/mysql driver.
|
|
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const mysql = require('nuclei/mysql');
|
2024-03-01 16:38:56 +05:30
|
|
|
// const client = new mysql.MySQLClient;
|
2024-02-07 21:45:40 +05:30
|
|
|
// ```
|
|
|
|
|
MySQLClient struct{}
|
|
|
|
|
)
|
2023-09-16 16:02:17 +05:30
|
|
|
|
|
|
|
|
// IsMySQL checks if the given host is running MySQL database.
|
|
|
|
|
// If the host is running MySQL database, it returns true.
|
|
|
|
|
// If the host is not running MySQL database, it returns false.
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const mysql = require('nuclei/mysql');
|
|
|
|
|
// const isMySQL = mysql.IsMySQL('acme.com', 3306);
|
|
|
|
|
// ```
|
2025-07-09 14:47:26 -05:00
|
|
|
func (c *MySQLClient) IsMySQL(ctx context.Context, host string, port int) (bool, error) {
|
|
|
|
|
executionId := ctx.Value("executionId").(string)
|
2024-07-08 13:38:29 +02:00
|
|
|
// todo: why this is exposed? Service fingerprint should be automatic
|
2025-07-09 14:47:26 -05:00
|
|
|
return memoizedisMySQL(executionId, host, port)
|
2024-03-01 16:10:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @memo
|
2025-07-09 14:47:26 -05:00
|
|
|
func isMySQL(executionId string, host string, port int) (bool, error) {
|
|
|
|
|
if !protocolstate.IsHostAllowed(executionId, host) {
|
2023-09-16 16:02:17 +05:30
|
|
|
// host is not valid according to network policy
|
|
|
|
|
return false, protocolstate.ErrHostDenied.Msgf(host)
|
|
|
|
|
}
|
2025-07-09 14:47:26 -05:00
|
|
|
dialer := protocolstate.GetDialersWithId(executionId)
|
|
|
|
|
if dialer == nil {
|
|
|
|
|
return false, fmt.Errorf("dialers not initialized for %s", executionId)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conn, err := dialer.Fastdialer.Dial(context.TODO(), "tcp", net.JoinHostPort(host, fmt.Sprintf("%d", port)))
|
2023-09-16 16:02:17 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
2025-07-01 00:40:44 +07:00
|
|
|
defer func() {
|
2025-07-09 14:47:26 -05:00
|
|
|
_ = conn.Close()
|
|
|
|
|
}()
|
2023-09-16 16:02:17 +05:30
|
|
|
|
|
|
|
|
plugin := &mysqlplugin.MYSQLPlugin{}
|
|
|
|
|
service, err := plugin.Run(conn, 5*time.Second, plugins.Target{Host: host})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
if service == nil {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-02 02:22:04 +05:30
|
|
|
// Connect connects to MySQL database using given credentials.
|
2023-09-16 16:02:17 +05:30
|
|
|
// If connection is successful, it returns true.
|
|
|
|
|
// If connection is unsuccessful, it returns false and error.
|
|
|
|
|
// The connection is closed after the function returns.
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const mysql = require('nuclei/mysql');
|
2024-03-01 16:38:56 +05:30
|
|
|
// const client = new mysql.MySQLClient;
|
2024-02-07 21:45:40 +05:30
|
|
|
// const connected = client.Connect('acme.com', 3306, 'username', 'password');
|
|
|
|
|
// ```
|
2025-07-09 14:47:26 -05:00
|
|
|
func (c *MySQLClient) Connect(ctx context.Context, host string, port int, username, password string) (bool, error) {
|
|
|
|
|
executionId := ctx.Value("executionId").(string)
|
|
|
|
|
if !protocolstate.IsHostAllowed(executionId, host) {
|
2024-02-02 02:22:04 +05:30
|
|
|
// host is not valid according to network policy
|
|
|
|
|
return false, protocolstate.ErrHostDenied.Msgf(host)
|
2024-01-31 02:32:23 +05:30
|
|
|
}
|
2024-07-08 13:38:29 +02:00
|
|
|
|
|
|
|
|
// executing queries implies the remote mysql service
|
2025-07-09 14:47:26 -05:00
|
|
|
ok, err := c.IsMySQL(ctx, host, port)
|
2024-07-08 13:38:29 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
if !ok {
|
|
|
|
|
return false, fmt.Errorf("not a mysql service")
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-02 02:22:04 +05:30
|
|
|
dsn, err := BuildDSN(MySQLOptions{
|
|
|
|
|
Host: host,
|
|
|
|
|
Port: port,
|
|
|
|
|
DbName: "INFORMATION_SCHEMA",
|
|
|
|
|
Protocol: "tcp",
|
|
|
|
|
Username: username,
|
|
|
|
|
Password: password,
|
|
|
|
|
})
|
2024-01-31 02:32:23 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
2024-02-02 02:22:04 +05:30
|
|
|
return connectWithDSN(dsn)
|
2024-01-31 02:32:23 +05:30
|
|
|
}
|
|
|
|
|
|
2024-02-07 21:45:40 +05:30
|
|
|
type (
|
|
|
|
|
// MySQLInfo contains information about MySQL server.
|
|
|
|
|
// this is returned when fingerprint is successful
|
|
|
|
|
MySQLInfo struct {
|
|
|
|
|
Host string `json:"host,omitempty"`
|
|
|
|
|
IP string `json:"ip"`
|
|
|
|
|
Port int `json:"port"`
|
|
|
|
|
Protocol string `json:"protocol"`
|
|
|
|
|
TLS bool `json:"tls"`
|
|
|
|
|
Transport string `json:"transport"`
|
|
|
|
|
Version string `json:"version,omitempty"`
|
|
|
|
|
Debug plugins.ServiceMySQL `json:"debug,omitempty"`
|
|
|
|
|
Raw string `json:"metadata"`
|
|
|
|
|
}
|
|
|
|
|
)
|
2023-09-16 16:02:17 +05:30
|
|
|
|
2024-02-02 02:22:04 +05:30
|
|
|
// returns MySQLInfo when fingerpint is successful
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const mysql = require('nuclei/mysql');
|
|
|
|
|
// const info = mysql.FingerprintMySQL('acme.com', 3306);
|
|
|
|
|
// log(to_json(info));
|
|
|
|
|
// ```
|
2025-07-09 14:47:26 -05:00
|
|
|
func (c *MySQLClient) FingerprintMySQL(ctx context.Context, host string, port int) (MySQLInfo, error) {
|
|
|
|
|
executionId := ctx.Value("executionId").(string)
|
|
|
|
|
return memoizedfingerprintMySQL(executionId, host, port)
|
2024-03-01 16:10:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @memo
|
2025-07-09 14:47:26 -05:00
|
|
|
func fingerprintMySQL(executionId string, host string, port int) (MySQLInfo, error) {
|
2024-02-02 02:22:04 +05:30
|
|
|
info := MySQLInfo{}
|
2025-07-09 14:47:26 -05:00
|
|
|
if !protocolstate.IsHostAllowed(executionId, host) {
|
2023-09-16 16:02:17 +05:30
|
|
|
// host is not valid according to network policy
|
2024-02-02 02:22:04 +05:30
|
|
|
return info, protocolstate.ErrHostDenied.Msgf(host)
|
2023-09-16 16:02:17 +05:30
|
|
|
}
|
2025-07-09 14:47:26 -05:00
|
|
|
dialer := protocolstate.GetDialersWithId(executionId)
|
|
|
|
|
if dialer == nil {
|
|
|
|
|
return MySQLInfo{}, fmt.Errorf("dialers not initialized for %s", executionId)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conn, err := dialer.Fastdialer.Dial(context.TODO(), "tcp", net.JoinHostPort(host, fmt.Sprintf("%d", port)))
|
2023-09-16 16:02:17 +05:30
|
|
|
if err != nil {
|
2024-02-02 02:22:04 +05:30
|
|
|
return info, err
|
2023-09-16 16:02:17 +05:30
|
|
|
}
|
2025-07-01 00:40:44 +07:00
|
|
|
defer func() {
|
2025-07-09 14:47:26 -05:00
|
|
|
_ = conn.Close()
|
|
|
|
|
}()
|
2023-09-16 16:02:17 +05:30
|
|
|
|
2024-02-02 02:22:04 +05:30
|
|
|
plugin := &mysqlplugin.MYSQLPlugin{}
|
|
|
|
|
service, err := plugin.Run(conn, 5*time.Second, plugins.Target{Host: host})
|
2023-09-16 16:02:17 +05:30
|
|
|
if err != nil {
|
2024-02-02 02:22:04 +05:30
|
|
|
return info, err
|
2023-09-16 16:02:17 +05:30
|
|
|
}
|
2024-02-02 02:22:04 +05:30
|
|
|
if service == nil {
|
|
|
|
|
return info, fmt.Errorf("something went wrong got null output")
|
|
|
|
|
}
|
|
|
|
|
// fill all fields
|
|
|
|
|
info.Host = service.Host
|
|
|
|
|
info.IP = service.IP
|
|
|
|
|
info.Port = service.Port
|
|
|
|
|
info.Protocol = service.Protocol
|
|
|
|
|
info.TLS = service.TLS
|
|
|
|
|
info.Transport = service.Transport
|
|
|
|
|
info.Version = service.Version
|
|
|
|
|
info.Debug = service.Metadata().(plugins.ServiceMySQL)
|
|
|
|
|
bin, _ := service.Raw.MarshalJSON()
|
|
|
|
|
info.Raw = string(bin)
|
|
|
|
|
return info, nil
|
2023-09-16 16:02:17 +05:30
|
|
|
}
|
|
|
|
|
|
2024-02-02 02:22:04 +05:30
|
|
|
// ConnectWithDSN connects to MySQL database using given DSN.
|
|
|
|
|
// we override mysql dialer with fastdialer so it respects network policy
|
2024-02-07 21:45:40 +05:30
|
|
|
// If connection is successful, it returns true.
|
|
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const mysql = require('nuclei/mysql');
|
2024-03-01 16:38:56 +05:30
|
|
|
// const client = new mysql.MySQLClient;
|
2024-02-07 21:45:40 +05:30
|
|
|
// const connected = client.ConnectWithDSN('username:password@tcp(acme.com:3306)/');
|
|
|
|
|
// ```
|
2024-02-02 02:22:04 +05:30
|
|
|
func (c *MySQLClient) ConnectWithDSN(dsn string) (bool, error) {
|
2024-03-01 16:10:18 +03:00
|
|
|
return memoizedconnectWithDSN(dsn)
|
2024-02-02 02:22:04 +05:30
|
|
|
}
|
2023-09-16 16:02:17 +05:30
|
|
|
|
2024-02-07 21:45:40 +05:30
|
|
|
// ExecuteQueryWithOpts connects to Mysql database using given credentials
|
|
|
|
|
// and executes a query on the db.
|
|
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const mysql = require('nuclei/mysql');
|
|
|
|
|
// const options = new mysql.MySQLOptions();
|
|
|
|
|
// options.Host = 'acme.com';
|
|
|
|
|
// options.Port = 3306;
|
|
|
|
|
// const result = mysql.ExecuteQueryWithOpts(options, 'SELECT * FROM users');
|
|
|
|
|
// log(to_json(result));
|
|
|
|
|
// ```
|
2025-07-09 14:47:26 -05:00
|
|
|
func (c *MySQLClient) ExecuteQueryWithOpts(ctx context.Context, opts MySQLOptions, query string) (*utils.SQLResult, error) {
|
|
|
|
|
executionId := ctx.Value("executionId").(string)
|
|
|
|
|
if !protocolstate.IsHostAllowed(executionId, opts.Host) {
|
2023-09-16 16:02:17 +05:30
|
|
|
// host is not valid according to network policy
|
2024-02-02 02:22:04 +05:30
|
|
|
return nil, protocolstate.ErrHostDenied.Msgf(opts.Host)
|
|
|
|
|
}
|
2024-07-08 13:38:29 +02:00
|
|
|
|
|
|
|
|
// executing queries implies the remote mysql service
|
2025-07-09 14:47:26 -05:00
|
|
|
ok, err := c.IsMySQL(ctx, opts.Host, opts.Port)
|
2024-07-08 13:38:29 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, fmt.Errorf("not a mysql service")
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-02 02:22:04 +05:30
|
|
|
dsn, err := BuildDSN(opts)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2023-09-16 16:02:17 +05:30
|
|
|
}
|
|
|
|
|
|
2024-02-02 02:22:04 +05:30
|
|
|
db, err := sql.Open("mysql", dsn)
|
2023-09-16 16:02:17 +05:30
|
|
|
if err != nil {
|
2024-02-02 02:22:04 +05:30
|
|
|
return nil, err
|
2023-09-16 16:02:17 +05:30
|
|
|
}
|
2025-07-01 00:40:44 +07:00
|
|
|
defer func() {
|
2025-07-09 14:47:26 -05:00
|
|
|
_ = db.Close()
|
|
|
|
|
}()
|
2024-01-31 02:32:23 +05:30
|
|
|
db.SetMaxOpenConns(1)
|
|
|
|
|
db.SetMaxIdleConns(0)
|
2023-09-16 16:02:17 +05:30
|
|
|
|
|
|
|
|
rows, err := db.Query(query)
|
|
|
|
|
if err != nil {
|
2024-02-02 02:22:04 +05:30
|
|
|
return nil, err
|
2023-09-16 16:02:17 +05:30
|
|
|
}
|
2024-02-02 02:22:04 +05:30
|
|
|
|
|
|
|
|
data, err := utils.UnmarshalSQLRows(rows)
|
2023-09-16 16:02:17 +05:30
|
|
|
if err != nil {
|
2024-02-02 02:22:04 +05:30
|
|
|
if len(data.Rows) > 0 {
|
|
|
|
|
// allow partial results
|
|
|
|
|
return data, nil
|
|
|
|
|
}
|
|
|
|
|
return nil, err
|
2023-09-16 16:02:17 +05:30
|
|
|
}
|
2024-02-02 02:22:04 +05:30
|
|
|
return data, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ExecuteQuery connects to Mysql database using given credentials
|
|
|
|
|
// and executes a query on the db.
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const mysql = require('nuclei/mysql');
|
|
|
|
|
// const result = mysql.ExecuteQuery('acme.com', 3306, 'username', 'password', 'SELECT * FROM users');
|
|
|
|
|
// log(to_json(result));
|
|
|
|
|
// ```
|
2025-07-09 14:47:26 -05:00
|
|
|
func (c *MySQLClient) ExecuteQuery(ctx context.Context, host string, port int, username, password, query string) (*utils.SQLResult, error) {
|
2024-07-08 13:38:29 +02:00
|
|
|
// executing queries implies the remote mysql service
|
2025-07-09 14:47:26 -05:00
|
|
|
ok, err := c.IsMySQL(ctx, host, port)
|
2024-07-08 13:38:29 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, fmt.Errorf("not a mysql service")
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-09 14:47:26 -05:00
|
|
|
return c.ExecuteQueryWithOpts(ctx, MySQLOptions{
|
2024-02-02 02:22:04 +05:30
|
|
|
Host: host,
|
|
|
|
|
Port: port,
|
|
|
|
|
Protocol: "tcp",
|
|
|
|
|
Username: username,
|
|
|
|
|
Password: password,
|
|
|
|
|
}, query)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ExecuteQuery connects to Mysql database using given credentials
|
|
|
|
|
// and executes a query on the db.
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const mysql = require('nuclei/mysql');
|
|
|
|
|
// const result = mysql.ExecuteQueryOnDB('acme.com', 3306, 'username', 'password', 'dbname', 'SELECT * FROM users');
|
|
|
|
|
// log(to_json(result));
|
|
|
|
|
// ```
|
2025-07-09 14:47:26 -05:00
|
|
|
func (c *MySQLClient) ExecuteQueryOnDB(ctx context.Context, host string, port int, username, password, dbname, query string) (*utils.SQLResult, error) {
|
|
|
|
|
return c.ExecuteQueryWithOpts(ctx, MySQLOptions{
|
2024-02-02 02:22:04 +05:30
|
|
|
Host: host,
|
|
|
|
|
Port: port,
|
|
|
|
|
Protocol: "tcp",
|
|
|
|
|
Username: username,
|
|
|
|
|
Password: password,
|
|
|
|
|
DbName: dbname,
|
|
|
|
|
}, query)
|
2023-09-16 16:02:17 +05:30
|
|
|
}
|
2024-01-31 02:32:23 +05:30
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
_ = mysql.SetLogger(log.New(io.Discard, "", 0))
|
|
|
|
|
}
|