nuclei/pkg/js/libs/redis/redis.go
Dwi Siswanto 87ed0b2bb9
build: bump all direct modules (#6290)
* chore: fix non-constant fmt string in call

Signed-off-by: Dwi Siswanto <git@dw1.io>

* build: bump all direct modules

Signed-off-by: Dwi Siswanto <git@dw1.io>

* chore(hosterrorscache): update import path

Signed-off-by: Dwi Siswanto <git@dw1.io>

* fix(charts): break changes

Signed-off-by: Dwi Siswanto <git@dw1.io>

* build: pinned `github.com/zmap/zcrypto` to v0.0.0-20240512203510-0fef58d9a9db

Signed-off-by: Dwi Siswanto <git@dw1.io>

* chore: golangci-lint auto fixes

Signed-off-by: Dwi Siswanto <git@dw1.io>

* chore: satisfy lints

Signed-off-by: Dwi Siswanto <git@dw1.io>

* build: migrate `github.com/xanzy/go-gitlab` => `gitlab.com/gitlab-org/api/client-go`

Signed-off-by: Dwi Siswanto <git@dw1.io>

* feat(json): update build constraints

Signed-off-by: Dwi Siswanto <git@dw1.io>

* chore: dont panicking on close err

Signed-off-by: Dwi Siswanto <git@dw1.io>

---------

Signed-off-by: Dwi Siswanto <git@dw1.io>
2025-07-01 00:40:44 +07:00

201 lines
5.1 KiB
Go

package redis
import (
"context"
"fmt"
"time"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/protocolstate"
"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
// @example
// ```javascript
// const redis = require('nuclei/redis');
// const info = redis.GetServerInfo('acme.com', 6379);
// ```
func GetServerInfo(host string, port int) (string, error) {
return memoizedgetServerInfo(host, port)
}
// @memo
func getServerInfo(host string, port int) (string, error) {
if !protocolstate.IsHostAllowed(host) {
// host is not valid according to network policy
return "", protocolstate.ErrHostDenied.Msgf(host)
}
// 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
})
defer func() {
_ = client.Close()
}()
// 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
// @example
// ```javascript
// const redis = require('nuclei/redis');
// const connected = redis.Connect('acme.com', 6379, 'password');
// ```
func Connect(host string, port int, password string) (bool, error) {
return memoizedconnect(host, port, password)
}
// @memo
func connect(host string, port int, password string) (bool, error) {
if !protocolstate.IsHostAllowed(host) {
// host is not valid according to network policy
return false, protocolstate.ErrHostDenied.Msgf(host)
}
// 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
})
defer func() {
_ = client.Close()
}()
_, 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
// @example
// ```javascript
// const redis = require('nuclei/redis');
// const info = redis.GetServerInfoAuth('acme.com', 6379, 'password');
// ```
func GetServerInfoAuth(host string, port int, password string) (string, error) {
return memoizedgetServerInfoAuth(host, port, password)
}
// @memo
func getServerInfoAuth(host string, port int, password string) (string, error) {
if !protocolstate.IsHostAllowed(host) {
// host is not valid according to network policy
return "", protocolstate.ErrHostDenied.Msgf(host)
}
// 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
})
defer func() {
_ = client.Close()
}()
// 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
// @example
// ```javascript
// const redis = require('nuclei/redis');
// const isAuthenticated = redis.IsAuthenticated('acme.com', 6379);
// ```
func IsAuthenticated(host string, port int) (bool, error) {
return memoizedisAuthenticated(host, port)
}
// @memo
func isAuthenticated(host string, port int) (bool, error) {
plugin := pluginsredis.REDISPlugin{}
timeout := 5 * time.Second
conn, err := protocolstate.Dialer.Dial(context.TODO(), "tcp", fmt.Sprintf("%s:%d", host, port))
if err != nil {
return false, err
}
defer func() {
_ = conn.Close()
}()
_, err = plugin.Run(conn, timeout, plugins.Target{Host: host})
if err != nil {
return false, err
}
return true, nil
}
// 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])');
// ```
func RunLuaScript(host string, port int, password string, script string) (interface{}, error) {
if !protocolstate.IsHostAllowed(host) {
// host is not valid according to network policy
return false, protocolstate.ErrHostDenied.Msgf(host)
}
// create a new client
client := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", host, port),
Password: password,
DB: 0, // use default DB
})
defer func() {
_ = client.Close()
}()
// 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
}