2023-09-16 16:02:17 +05:30
|
|
|
package redis
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"time"
|
|
|
|
|
|
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
|
|
|
"github.com/redis/go-redis/v9"
|
|
|
|
|
|
|
|
|
|
"github.com/praetorian-inc/fingerprintx/pkg/plugins"
|
|
|
|
|
pluginsredis "github.com/praetorian-inc/fingerprintx/pkg/plugins/services/redis"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// GetServerInfo returns the server info for a redis server
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const redis = require('nuclei/redis');
|
|
|
|
|
// const info = redis.GetServerInfo('acme.com', 6379);
|
|
|
|
|
// ```
|
2025-07-09 14:47:26 -05:00
|
|
|
func GetServerInfo(ctx context.Context, host string, port int) (string, error) {
|
|
|
|
|
executionId := ctx.Value("executionId").(string)
|
|
|
|
|
return memoizedgetServerInfo(executionId, host, port)
|
2024-03-01 16:10:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @memo
|
2025-07-09 14:47:26 -05:00
|
|
|
func getServerInfo(executionId string, host string, port int) (string, error) {
|
|
|
|
|
if !protocolstate.IsHostAllowed(executionId, host) {
|
2023-09-16 16:02:17 +05:30
|
|
|
// host is not valid according to network policy
|
2025-08-25 15:06:58 +07:00
|
|
|
return "", protocolstate.ErrHostDenied.Msgf(host)
|
2023-09-16 16:02:17 +05:30
|
|
|
}
|
|
|
|
|
// create a new client
|
|
|
|
|
client := redis.NewClient(&redis.Options{
|
|
|
|
|
Addr: fmt.Sprintf("%s:%d", host, port),
|
|
|
|
|
Password: "", // no password set
|
|
|
|
|
DB: 0, // use default DB
|
|
|
|
|
})
|
2025-07-01 00:40:44 +07:00
|
|
|
defer func() {
|
2025-07-09 14:47:26 -05:00
|
|
|
_ = client.Close()
|
|
|
|
|
}()
|
2023-09-16 16:02:17 +05:30
|
|
|
|
|
|
|
|
// Ping the Redis server
|
|
|
|
|
_, err := client.Ping(context.TODO()).Result()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get Redis server info
|
|
|
|
|
infoCmd := client.Info(context.TODO())
|
|
|
|
|
if infoCmd.Err() != nil {
|
|
|
|
|
return "", infoCmd.Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return infoCmd.Val(), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Connect tries to connect redis server with password
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const redis = require('nuclei/redis');
|
|
|
|
|
// const connected = redis.Connect('acme.com', 6379, 'password');
|
|
|
|
|
// ```
|
2025-07-09 14:47:26 -05:00
|
|
|
func Connect(ctx context.Context, host string, port int, password string) (bool, error) {
|
|
|
|
|
executionId := ctx.Value("executionId").(string)
|
|
|
|
|
return memoizedconnect(executionId, host, port, password)
|
2024-03-01 16:10:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @memo
|
2025-07-09 14:47:26 -05:00
|
|
|
func connect(executionId string, host string, port int, password string) (bool, error) {
|
|
|
|
|
if !protocolstate.IsHostAllowed(executionId, host) {
|
2023-09-16 16:02:17 +05:30
|
|
|
// host is not valid according to network policy
|
2025-08-25 15:06:58 +07:00
|
|
|
return false, protocolstate.ErrHostDenied.Msgf(host)
|
2023-09-16 16:02:17 +05:30
|
|
|
}
|
|
|
|
|
// create a new client
|
|
|
|
|
client := redis.NewClient(&redis.Options{
|
|
|
|
|
Addr: fmt.Sprintf("%s:%d", host, port),
|
|
|
|
|
Password: password, // no password set
|
|
|
|
|
DB: 0, // use default DB
|
|
|
|
|
})
|
2025-07-01 00:40:44 +07:00
|
|
|
defer func() {
|
2025-07-09 14:47:26 -05:00
|
|
|
_ = client.Close()
|
|
|
|
|
}()
|
2024-03-01 16:10:18 +03:00
|
|
|
|
2023-09-16 16:02:17 +05:30
|
|
|
_, err := client.Ping(context.TODO()).Result()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
// Get Redis server info
|
|
|
|
|
infoCmd := client.Info(context.TODO())
|
|
|
|
|
if infoCmd.Err() != nil {
|
|
|
|
|
return false, infoCmd.Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetServerInfoAuth returns the server info for a redis server
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const redis = require('nuclei/redis');
|
|
|
|
|
// const info = redis.GetServerInfoAuth('acme.com', 6379, 'password');
|
|
|
|
|
// ```
|
2025-07-09 14:47:26 -05:00
|
|
|
func GetServerInfoAuth(ctx context.Context, host string, port int, password string) (string, error) {
|
|
|
|
|
executionId := ctx.Value("executionId").(string)
|
|
|
|
|
return memoizedgetServerInfoAuth(executionId, host, port, password)
|
2024-03-01 16:10:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @memo
|
2025-07-09 14:47:26 -05:00
|
|
|
func getServerInfoAuth(executionId string, host string, port int, password string) (string, error) {
|
|
|
|
|
if !protocolstate.IsHostAllowed(executionId, host) {
|
2023-09-16 16:02:17 +05:30
|
|
|
// host is not valid according to network policy
|
2025-08-25 15:06:58 +07:00
|
|
|
return "", protocolstate.ErrHostDenied.Msgf(host)
|
2023-09-16 16:02:17 +05:30
|
|
|
}
|
|
|
|
|
// create a new client
|
|
|
|
|
client := redis.NewClient(&redis.Options{
|
|
|
|
|
Addr: fmt.Sprintf("%s:%d", host, port),
|
|
|
|
|
Password: password, // no password set
|
|
|
|
|
DB: 0, // use default DB
|
|
|
|
|
})
|
2025-07-01 00:40:44 +07:00
|
|
|
defer func() {
|
2025-07-09 14:47:26 -05:00
|
|
|
_ = client.Close()
|
|
|
|
|
}()
|
2023-09-16 16:02:17 +05:30
|
|
|
|
|
|
|
|
// Ping the Redis server
|
|
|
|
|
_, err := client.Ping(context.TODO()).Result()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get Redis server info
|
|
|
|
|
infoCmd := client.Info(context.TODO())
|
|
|
|
|
if infoCmd.Err() != nil {
|
|
|
|
|
return "", infoCmd.Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return infoCmd.Val(), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsAuthenticated checks if the redis server requires authentication
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const redis = require('nuclei/redis');
|
|
|
|
|
// const isAuthenticated = redis.IsAuthenticated('acme.com', 6379);
|
|
|
|
|
// ```
|
2025-07-09 14:47:26 -05:00
|
|
|
func IsAuthenticated(ctx context.Context, host string, port int) (bool, error) {
|
|
|
|
|
executionId := ctx.Value("executionId").(string)
|
|
|
|
|
return memoizedisAuthenticated(executionId, host, port)
|
2024-03-01 16:10:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @memo
|
2025-07-09 14:47:26 -05:00
|
|
|
func isAuthenticated(executionId string, host string, port int) (bool, error) {
|
2023-09-16 16:02:17 +05:30
|
|
|
plugin := pluginsredis.REDISPlugin{}
|
|
|
|
|
timeout := 5 * time.Second
|
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", fmt.Sprintf("%s:%d", host, 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
|
|
|
|
|
|
|
|
_, err = plugin.Run(conn, timeout, plugins.Target{Host: host})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-07 21:45:40 +05:30
|
|
|
// RunLuaScript runs a lua script on the redis server
|
|
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const redis = require('nuclei/redis');
|
|
|
|
|
// const result = redis.RunLuaScript('acme.com', 6379, 'password', 'return redis.call("get", KEYS[1])');
|
|
|
|
|
// ```
|
2025-07-09 14:47:26 -05:00
|
|
|
func RunLuaScript(ctx context.Context, host string, port int, password string, script string) (interface{}, error) {
|
|
|
|
|
executionId := ctx.Value("executionId").(string)
|
|
|
|
|
if !protocolstate.IsHostAllowed(executionId, host) {
|
2023-09-16 16:02:17 +05:30
|
|
|
// host is not valid according to network policy
|
2025-08-25 15:06:58 +07:00
|
|
|
return false, protocolstate.ErrHostDenied.Msgf(host)
|
2023-09-16 16:02:17 +05:30
|
|
|
}
|
|
|
|
|
// create a new client
|
|
|
|
|
client := redis.NewClient(&redis.Options{
|
|
|
|
|
Addr: fmt.Sprintf("%s:%d", host, port),
|
|
|
|
|
Password: password,
|
|
|
|
|
DB: 0, // use default DB
|
|
|
|
|
})
|
2025-07-01 00:40:44 +07:00
|
|
|
defer func() {
|
2025-07-09 14:47:26 -05:00
|
|
|
_ = client.Close()
|
|
|
|
|
}()
|
2023-09-16 16:02:17 +05:30
|
|
|
|
|
|
|
|
// Ping the Redis server
|
|
|
|
|
_, err := client.Ping(context.TODO()).Result()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get Redis server info
|
|
|
|
|
infoCmd := client.Eval(context.Background(), script, []string{})
|
|
|
|
|
|
|
|
|
|
if infoCmd.Err() != nil {
|
|
|
|
|
return "", infoCmd.Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return infoCmd.Val(), nil
|
|
|
|
|
}
|