2023-09-16 16:02:17 +05:30
|
|
|
package oracle
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-09-12 10:29:42 +02:00
|
|
|
"database/sql"
|
2025-07-09 14:47:26 -05:00
|
|
|
"fmt"
|
2023-09-16 16:02:17 +05:30
|
|
|
"net"
|
|
|
|
|
"strconv"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/praetorian-inc/fingerprintx/pkg/plugins"
|
|
|
|
|
"github.com/praetorian-inc/fingerprintx/pkg/plugins/services/oracledb"
|
2025-09-12 10:29:42 +02:00
|
|
|
"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"
|
2025-09-12 10:29:42 +02:00
|
|
|
goora "github.com/sijms/go-ora/v2"
|
2023-09-16 16:02:17 +05:30
|
|
|
)
|
|
|
|
|
|
2024-02-07 21:45:40 +05:30
|
|
|
type (
|
|
|
|
|
// IsOracleResponse is the response from the IsOracle function.
|
|
|
|
|
// this is returned by IsOracle function.
|
|
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const oracle = require('nuclei/oracle');
|
|
|
|
|
// const isOracle = oracle.IsOracle('acme.com', 1521);
|
|
|
|
|
// ```
|
|
|
|
|
IsOracleResponse struct {
|
|
|
|
|
IsOracle bool
|
|
|
|
|
Banner string
|
|
|
|
|
}
|
2025-09-12 10:29:42 +02:00
|
|
|
// Client is a client for Oracle database.
|
|
|
|
|
// Internally client uses oracle/godror driver.
|
|
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const oracle = require('nuclei/oracle');
|
|
|
|
|
// const client = new oracle.OracleClient();
|
|
|
|
|
// ```
|
|
|
|
|
OracleClient struct {
|
|
|
|
|
connector *goora.OracleConnector
|
|
|
|
|
}
|
2024-02-07 21:45:40 +05:30
|
|
|
)
|
2023-09-16 16:02:17 +05:30
|
|
|
|
2024-02-07 21:45:40 +05:30
|
|
|
// IsOracle checks if a host is running an Oracle server
|
|
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const oracle = require('nuclei/oracle');
|
|
|
|
|
// const isOracle = oracle.IsOracle('acme.com', 1521);
|
|
|
|
|
// log(toJSON(isOracle));
|
|
|
|
|
// ```
|
2025-09-12 10:29:42 +02:00
|
|
|
func (c *OracleClient) IsOracle(ctx context.Context, host string, port int) (IsOracleResponse, error) {
|
2025-07-09 14:47:26 -05:00
|
|
|
executionId := ctx.Value("executionId").(string)
|
|
|
|
|
return memoizedisOracle(executionId, host, port)
|
2024-03-01 16:10:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @memo
|
2025-07-09 14:47:26 -05:00
|
|
|
func isOracle(executionId string, host string, port int) (IsOracleResponse, error) {
|
2023-09-16 16:02:17 +05:30
|
|
|
resp := IsOracleResponse{}
|
|
|
|
|
|
2025-07-09 14:47:26 -05:00
|
|
|
dialer := protocolstate.GetDialersWithId(executionId)
|
|
|
|
|
if dialer == nil {
|
|
|
|
|
return IsOracleResponse{}, fmt.Errorf("dialers not initialized for %s", executionId)
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-16 16:02:17 +05:30
|
|
|
timeout := 5 * time.Second
|
2025-07-09 14:47:26 -05:00
|
|
|
conn, err := dialer.Fastdialer.Dial(context.TODO(), "tcp", net.JoinHostPort(host, strconv.Itoa(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
|
|
|
|
|
|
|
|
oracledbPlugin := oracledb.ORACLEPlugin{}
|
|
|
|
|
service, err := oracledbPlugin.Run(conn, timeout, plugins.Target{Host: host})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return resp, err
|
|
|
|
|
}
|
|
|
|
|
if service == nil {
|
|
|
|
|
return resp, nil
|
|
|
|
|
}
|
|
|
|
|
resp.Banner = service.Version
|
|
|
|
|
resp.Banner = service.Metadata().(plugins.ServiceOracle).Info
|
|
|
|
|
resp.IsOracle = true
|
|
|
|
|
return resp, nil
|
|
|
|
|
}
|
2025-09-12 10:29:42 +02:00
|
|
|
|
|
|
|
|
func (c *OracleClient) oracleDbInstance(connStr string, executionId string) (*goora.OracleConnector, error) {
|
|
|
|
|
if c.connector != nil {
|
|
|
|
|
return c.connector, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connector := goora.NewConnector(connStr)
|
|
|
|
|
oraConnector, ok := connector.(*goora.OracleConnector)
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, fmt.Errorf("failed to cast connector to OracleConnector")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create custom dialer wrapper
|
|
|
|
|
customDialer := &oracleCustomDialer{
|
|
|
|
|
executionId: executionId,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
oraConnector.Dialer(customDialer)
|
|
|
|
|
|
|
|
|
|
c.connector = oraConnector
|
|
|
|
|
|
|
|
|
|
return oraConnector, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Connect connects to an Oracle database
|
|
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const oracle = require('nuclei/oracle');
|
|
|
|
|
// const client = new oracle.OracleClient;
|
|
|
|
|
// client.Connect('acme.com', 1521, 'XE', 'user', 'password');
|
|
|
|
|
// ```
|
|
|
|
|
func (c *OracleClient) Connect(ctx context.Context, host string, port int, serviceName string, username string, password string) (bool, error) {
|
|
|
|
|
connStr := goora.BuildUrl(host, port, serviceName, username, password, nil)
|
|
|
|
|
|
|
|
|
|
return c.ConnectWithDSN(ctx, connStr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *OracleClient) ConnectWithDSN(ctx context.Context, dsn string) (bool, error) {
|
|
|
|
|
executionId := ctx.Value("executionId").(string)
|
|
|
|
|
|
|
|
|
|
connector, err := c.oracleDbInstance(dsn, executionId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db := sql.OpenDB(connector)
|
|
|
|
|
defer func() {
|
|
|
|
|
_ = db.Close()
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
db.SetMaxOpenConns(1)
|
|
|
|
|
db.SetMaxIdleConns(0)
|
|
|
|
|
|
|
|
|
|
// Test the connection
|
|
|
|
|
err = db.Ping()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ExecuteQuery connects to MS SQL database using given credentials and executes a query.
|
|
|
|
|
// It returns the results of the query or an error if something goes wrong.
|
|
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const oracle = require('nuclei/oracle');
|
|
|
|
|
// const client = new oracle.OracleClient;
|
|
|
|
|
// const result = client.ExecuteQuery('acme.com', 1521, 'username', 'password', 'XE', 'SELECT @@version');
|
|
|
|
|
// log(to_json(result));
|
|
|
|
|
// ```
|
|
|
|
|
func (c *OracleClient) ExecuteQuery(ctx context.Context, host string, port int, username, password, dbName, query string) (*utils.SQLResult, error) {
|
|
|
|
|
if host == "" || port <= 0 {
|
|
|
|
|
return nil, fmt.Errorf("invalid host or port")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isOracleResp, err := c.IsOracle(ctx, host, port)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if !isOracleResp.IsOracle {
|
|
|
|
|
return nil, fmt.Errorf("not a oracle service")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connStr := goora.BuildUrl(host, port, dbName, username, password, nil)
|
|
|
|
|
|
|
|
|
|
return c.ExecuteQueryWithDSN(ctx, connStr, query)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ExecuteQueryWithDSN executes a query on an Oracle database using a DSN
|
|
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const oracle = require('nuclei/oracle');
|
|
|
|
|
// const client = new oracle.OracleClient;
|
|
|
|
|
// const result = client.ExecuteQueryWithDSN('oracle://user:password@host:port/service', 'SELECT @@version');
|
|
|
|
|
// log(to_json(result));
|
|
|
|
|
// ```
|
|
|
|
|
func (c *OracleClient) ExecuteQueryWithDSN(ctx context.Context, dsn string, query string) (*utils.SQLResult, error) {
|
|
|
|
|
executionId := ctx.Value("executionId").(string)
|
|
|
|
|
|
|
|
|
|
connector, err := c.oracleDbInstance(dsn, executionId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
db := sql.OpenDB(connector)
|
|
|
|
|
defer func() {
|
|
|
|
|
_ = db.Close()
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
db.SetMaxOpenConns(1)
|
|
|
|
|
db.SetMaxIdleConns(0)
|
|
|
|
|
|
|
|
|
|
rows, err := db.Query(query)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data, err := utils.UnmarshalSQLRows(rows)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if data != nil && len(data.Rows) > 0 {
|
|
|
|
|
return data, nil
|
|
|
|
|
}
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return data, nil
|
|
|
|
|
}
|