增加将结果输出到redis

This commit is contained in:
root 2018-04-08 04:23:13 -04:00
commit da0b28c119
4 changed files with 2835 additions and 2725 deletions

68
hydra.c
View file

@ -2060,6 +2060,7 @@ int main(int argc, char *argv[]) {
char *proxy_string = NULL, *device = NULL, *memcheck, *cmdtarget = NULL; char *proxy_string = NULL, *device = NULL, *memcheck, *cmdtarget = NULL;
char *outfile_format_tmp; char *outfile_format_tmp;
char *output; char *output;
char *redis_output_tmp;
FILE *lfp = NULL, *pfp = NULL, *cfp = NULL, *ifp = NULL, *rfp = NULL, *proxyfp; FILE *lfp = NULL, *pfp = NULL, *cfp = NULL, *ifp = NULL, *rfp = NULL, *proxyfp;
size_t countinfile = 1, sizeinfile = 0; size_t countinfile = 1, sizeinfile = 0;
uint64_t math2; uint64_t math2;
@ -2159,6 +2160,8 @@ int main(int argc, char *argv[]) {
#endif #endif
output = malloc(sizeof(char) * 4096); output = malloc(sizeof(char) * 4096);
memset(output, 0, sizeof(char) * 4096); memset(output, 0, sizeof(char) * 4096);
redis_output_tmp = malloc(sizeof(char) * 4096);
memset(redis_output_tmp, 0, sizeof(char) * 4096);
(void) setvbuf(stdout, NULL, _IONBF, 0); (void) setvbuf(stdout, NULL, _IONBF, 0);
(void) setvbuf(stderr, NULL, _IONBF, 0); (void) setvbuf(stderr, NULL, _IONBF, 0);
// set defaults // set defaults
@ -2192,7 +2195,7 @@ int main(int argc, char *argv[]) {
help(1); help(1);
if (argc < 2) if (argc < 2)
help(0); help(0);
while ((i = getopt(argc, argv, "hIq64Rde:vVl:fFg:L:p:OP:o:b:M:C:t:T:m:w:W:s:SUux:yc:")) >= 0) { while ((i = getopt(argc, argv, "hIq64Rde:vVl:fFg:L:p:OP:o:b:k:M:C:t:T:m:w:W:s:SUux:yc:")) >= 0) {
switch (i) { switch (i) {
case 'h': case 'h':
help(1); help(1);
@ -2284,12 +2287,24 @@ int main(int argc, char *argv[]) {
hydra_options.outfile_format = FORMAT_JSONV1; hydra_options.outfile_format = FORMAT_JSONV1;
else if (0==strcasecmp(outfile_format_tmp,"jsonv1")) else if (0==strcasecmp(outfile_format_tmp,"jsonv1"))
hydra_options.outfile_format = FORMAT_JSONV1; hydra_options.outfile_format = FORMAT_JSONV1;
else { else if (0==strcasecmp(outfile_format_tmp,"redis")){
hydra_options.outfile_format = FORMAT_REDIS;
}else {
fprintf(stderr, "[ERROR] Output file format must be (text, json, jsonv1)\n"); fprintf(stderr, "[ERROR] Output file format must be (text, json, jsonv1)\n");
exit(-1); exit(-1);
} }
// colored_output = 0; // colored_output = 0;
break; break;
case 'k':
hydra_options.key = optarg;
hydra_options.map_key = malloc(sizeof(char) * 36);
memset(hydra_options.map_key, 0, sizeof(char) * 36);
if(strlen(optarg) >= 36){
fprintf(stderr, "[ERROR] Redis key length <= 36\n");
exit(-1);
}
key(hydra_options.map_key, optarg);
break;
case 'M': case 'M':
hydra_options.infile_ptr = optarg; hydra_options.infile_ptr = optarg;
break; break;
@ -2406,9 +2421,12 @@ int main(int argc, char *argv[]) {
bail("You can only use -L OR -l, not both\n"); bail("You can only use -L OR -l, not both\n");
if (hydra_options.pass != NULL && hydra_options.passfile != NULL) if (hydra_options.pass != NULL && hydra_options.passfile != NULL)
bail("You can only use -P OR -p, not both\n"); bail("You can only use -P OR -p, not both\n");
if (hydra_options.outfile_format != FORMAT_PLAIN_TEXT && hydra_options.outfile_ptr == NULL) if (hydra_options.outfile_format == FORMAT_JSONV1 && hydra_options.outfile_format != FORMAT_PLAIN_TEXT && hydra_options.outfile_ptr == NULL)
fprintf(stderr, "[WARNING] output file format specified (-b) - but no output file (-o)\n"); fprintf(stderr, "[WARNING] output file format specified (-b json) - but no output file (-o)\n");
if (hydra_options.outfile_format == FORMAT_REDIS && hydra_options.key == NULL){
fprintf(stderr, "[ERROR] redis output format specified (-b redis) - but no key (-k)\n");
exit(1);
}
if (hydra_options.restore) { if (hydra_options.restore) {
// hydra_restore_read(); // hydra_restore_read();
// stuff we have to copy from the non-restore part // stuff we have to copy from the non-restore part
@ -3507,22 +3525,18 @@ int main(int argc, char *argv[]) {
//service %s on port %d%s\n", hydra_options.service, port, hydra_options.ssl == 1 ? " with SSL" : ""); //service %s on port %d%s\n", hydra_options.service, port, hydra_options.ssl == 1 ? " with SSL" : "");
// if (hydra_options.miscptr != NULL && hydra_options.miscptr[0] != 0) // if (hydra_options.miscptr != NULL && hydra_options.miscptr[0] != 0)
// printf("[DATA] with additional data %s\n", hydra_options.miscptr); // printf("[DATA] with additional data %s\n", hydra_options.miscptr);
if (hydra_options.outfile_ptr != NULL) { if (hydra_options.outfile_ptr != NULL) {
if ((hydra_brains.ofp = fopen(hydra_options.outfile_ptr, "a+")) == NULL) { if ((hydra_brains.ofp = fopen(hydra_options.outfile_ptr, "a+")) == NULL) {
perror("[ERROR] Error creating outputfile"); perror("[ERROR] Error creating outputfile");
exit(-1); exit(-1);
} }
if (hydra_options.outfile_format == FORMAT_JSONV1) { if (hydra_options.outfile_format == FORMAT_JSONV1) {
//sprintf(output, "{\"test\":\"test\"}");
fprintf(hydra_brains.ofp, "{ \"generator\": {\n" fprintf(hydra_brains.ofp, "{ \"generator\": {\n"
"\t\"software\": \"%s\", \"version\": \"%s\", \"built\": \"%s\",\n" "\t\"software\": \"%s\", \"version\": \"%s\", \"built\": \"%s\",\n"
"\t\"server\": \"%s\", \"service\": \"%s\", \"jsonoutputversion\": \"1.00\",\n" "\t\"server\": \"%s\", \"service\": \"%s\", \"jsonoutputversion\": \"1.00\",\n"
"\t\"commandline\": \"%s", "\t\"commandline\": \"%s",
PROGRAM, VERSION, hydra_build_time(), PROGRAM, VERSION, hydra_build_time(),
hydra_options.server == NULL ? hydra_options.infile_ptr : hydra_options.server, hydra_options.service, prg); hydra_options.server == NULL ? hydra_options.infile_ptr : hydra_options.server, hydra_options.service, prg);
set("a", "2");
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
char *t = hydra_string_replace(argv[i],"\"","\\\""); char *t = hydra_string_replace(argv[i],"\"","\\\"");
fprintf(hydra_brains.ofp, " %s", t); fprintf(hydra_brains.ofp, " %s", t);
@ -3536,6 +3550,20 @@ int main(int argc, char *argv[]) {
fprintf(hydra_brains.ofp, " %s", argv[i]); fprintf(hydra_brains.ofp, " %s", argv[i]);
fprintf(hydra_brains.ofp, ")\n"); fprintf(hydra_brains.ofp, ")\n");
} }
}else if (hydra_options.outfile_format == FORMAT_REDIS) {
sprintf(redis_output_tmp, "{ \"generator\": {"
"\"software\": \"%s\", \"version\": \"%s\", \"built\": \"%s\","
"\"server\": \"%s\", \"service\": \"%s\", \"jsonoutputversion\": \"1.00\","
"\"commandline\": \"%s",
PROGRAM, VERSION, hydra_build_time(),
hydra_options.server == NULL ? hydra_options.infile_ptr : hydra_options.server, hydra_options.service, prg);
for (i = 1; i < argc; i++) {
char *t = hydra_string_replace(argv[i],"\"","\\\"");
sprintf(redis_output_tmp + strlen(redis_output_tmp), " %s", t);
free(t);
}
sprintf(redis_output_tmp + strlen(redis_output_tmp), "\"},\"results\": \"%s\", \"status\":\"running\"}", hydra_options.map_key);
set(hydra_options.key, redis_output_tmp);
} }
// we have to flush all writeable buffered file pointers before forking // we have to flush all writeable buffered file pointers before forking
// set appropriate signals for mother // set appropriate signals for mother
@ -3793,7 +3821,6 @@ int main(int argc, char *argv[]) {
hydra_heads[head_no]->current_pass_ptr != NULL ? hydra_string_replace(hydra_heads[head_no]->current_pass_ptr,"\"","\\\"") : "" hydra_heads[head_no]->current_pass_ptr != NULL ? hydra_string_replace(hydra_heads[head_no]->current_pass_ptr,"\"","\\\"") : ""
); );
fflush(hydra_brains.ofp); fflush(hydra_brains.ofp);
set("a", "3");
} else if (hydra_options.outfile_ptr != NULL && hydra_brains.ofp != NULL) { // else output format == 0 aka text } else if (hydra_options.outfile_ptr != NULL && hydra_brains.ofp != NULL) { // else output format == 0 aka text
if (hydra_heads[head_no]->current_login_ptr == NULL || strlen(hydra_heads[head_no]->current_login_ptr) == 0) { if (hydra_heads[head_no]->current_login_ptr == NULL || strlen(hydra_heads[head_no]->current_login_ptr) == 0) {
if (hydra_heads[head_no]->current_pass_ptr == NULL || strlen(hydra_heads[head_no]->current_pass_ptr) == 0) if (hydra_heads[head_no]->current_pass_ptr == NULL || strlen(hydra_heads[head_no]->current_pass_ptr) == 0)
@ -3803,13 +3830,24 @@ int main(int argc, char *argv[]) {
fprintf(hydra_brains.ofp, "[%d][%s] host: %s password: %s\n", hydra_targets[hydra_heads[head_no]->target_no]->port, hydra_options.service, fprintf(hydra_brains.ofp, "[%d][%s] host: %s password: %s\n", hydra_targets[hydra_heads[head_no]->target_no]->port, hydra_options.service,
hydra_targets[hydra_heads[head_no]->target_no]->target, hydra_heads[head_no]->current_pass_ptr); hydra_targets[hydra_heads[head_no]->target_no]->target, hydra_heads[head_no]->current_pass_ptr);
} else if (hydra_heads[head_no]->current_pass_ptr == NULL || strlen(hydra_heads[head_no]->current_pass_ptr) == 0) { } else if (hydra_heads[head_no]->current_pass_ptr == NULL || strlen(hydra_heads[head_no]->current_pass_ptr) == 0) {
fprintf(hydra_brains.ofp, "[%d][%s] host: %s login: %s\n", hydra_targets[hydra_heads[head_no]->target_no]->port, hydra_options.service, fprintf(hydra_brains.ofp, "[%d][%s] host: %s login: %s\n", hydra_targets[hydra_heads[head_no]->target_no]->port,hydra_options.service,
hydra_targets[hydra_heads[head_no]->target_no]->target, hydra_heads[head_no]->current_login_ptr); hydra_targets[hydra_heads[head_no]->target_no]->target, hydra_heads[head_no]->current_login_ptr);
} else }else
fprintf(hydra_brains.ofp, "[%d][%s] host: %s login: %s password: %s\n", hydra_targets[hydra_heads[head_no]->target_no]->port, hydra_options.service, fprintf(hydra_brains.ofp, "[%d][%s] host: %s login: %s password: %s\n", hydra_targets[hydra_heads[head_no]->target_no]->port, hydra_options.service,
hydra_targets[hydra_heads[head_no]->target_no]->target, hydra_heads[head_no]->current_login_ptr, hydra_heads[head_no]->current_pass_ptr); hydra_targets[hydra_heads[head_no]->target_no]->target, hydra_heads[head_no]->current_login_ptr, hydra_heads[head_no]->current_pass_ptr);
fflush(hydra_brains.ofp); fflush(hydra_brains.ofp);
} }
if (hydra_options.key != NULL && hydra_options.outfile_format == FORMAT_REDIS){
sprintf(redis_output_tmp, "{\"port\": %d, \"service\": \"%s\", \"host\": \"%s\", \"login\": \"%s\", \"password\": \"%s\"}",
hydra_targets[hydra_heads[head_no]->target_no]->port,
hydra_options.service,
hydra_targets[hydra_heads[head_no]->target_no]->target !=NULL ? hydra_targets[hydra_heads[head_no]->target_no]->target : "",
hydra_heads[head_no]->current_login_ptr !=NULL ? hydra_string_replace(hydra_heads[head_no]->current_login_ptr,"\"","\\\"") : "",
hydra_heads[head_no]->current_pass_ptr != NULL ? hydra_string_replace(hydra_heads[head_no]->current_pass_ptr,"\"","\\\"") : ""
);
lpush(hydra_options.map_key, redis_output_tmp);
}
if (hydra_options.exit_found) { // option set says quit target after on valid login/pass pair is found if (hydra_options.exit_found) { // option set says quit target after on valid login/pass pair is found
if (hydra_targets[hydra_heads[head_no]->target_no]->done == TARGET_ACTIVE) { if (hydra_targets[hydra_heads[head_no]->target_no]->done == TARGET_ACTIVE) {
hydra_targets[hydra_heads[head_no]->target_no]->done = TARGET_FINISHED; // mark target as done hydra_targets[hydra_heads[head_no]->target_no]->done = TARGET_FINISHED; // mark target as done
@ -4059,7 +4097,13 @@ int main(int argc, char *argv[]) {
(error ? "false" : "true"), json_error, hydra_brains.found); (error ? "false" : "true"), json_error, hydra_brains.found);
} }
fclose(hydra_brains.ofp); fclose(hydra_brains.ofp);
}else if (hydra_options.outfile_format == FORMAT_REDIS && hydra_options.key != NULL) {
get(redis_output_tmp, hydra_options.key);
sprintf(redis_output_tmp + strlen(redis_output_tmp) - 1,",\"success\": \"%s\",\"errormessages\": [ %s ],\"quantityfound\": %lu }", (error ? "false" : "true"), json_error, hydra_brains.found);
redis_output_tmp = replace(redis_output_tmp, "running", "finished");
set(hydra_options.key, redis_output_tmp);
} }
//set("output", output); //set("output", output);
redis_free(); redis_free();
fflush(NULL); fflush(NULL);

View file

@ -177,6 +177,7 @@ typedef enum {
FORMAT_PLAIN_TEXT, FORMAT_PLAIN_TEXT,
FORMAT_JSONV1, FORMAT_JSONV1,
FORMAT_JSONV2, FORMAT_JSONV2,
FORMAT_REDIS,
FORMAT_XMLV1 FORMAT_XMLV1
} output_format_t; } output_format_t;
@ -211,6 +212,8 @@ typedef struct {
char *server; char *server;
char *service; char *service;
char bfg; char bfg;
char *key; //redis save key
char *map_key; //redis save map key
} hydra_option; } hydra_option;
#define _HYDRA_H #define _HYDRA_H

View file

@ -12,27 +12,79 @@ void set(const unsigned char *key, const unsigned char *value)
{ {
if (conn == NULL) if (conn == NULL)
{ {
conn = redisConnect("127.0.0.1", 6379); conn = redisConnect(REDIS_IP, REDIS_PORT);
} }
if(conn != NULL && conn->err) if(conn != NULL && conn->err)
{ {
printf("connection error: %s\n",conn->errstr); printf("connection error: %s\n",conn->errstr);
} }
redisReply *reply = (redisReply*)redisCommand(conn,"set %s %s", key, value); redisReply *reply = (redisReply*)redisCommand(conn,"set %s %s", key, value);
freeReplyObject(reply); freeReplyObject(reply);
}
/*reply = redisCommand(conn,"get %s", key); void get(char *value, const unsigned char *key)
printf("%s\n",reply->str); {
freeReplyObject(reply); */ if (conn == NULL)
{
conn = redisConnect(REDIS_IP, REDIS_PORT);
}
if(conn != NULL && conn->err)
{
printf("connection error: %s\n",conn->errstr);
}
redisReply *reply = redisCommand(conn,"get %s", key);
sprintf(value, "%s", reply->str);
freeReplyObject(reply);
}
void lpush(const unsigned char *key, const unsigned char *value)
{
if (conn == NULL)
{
conn = redisConnect("127.0.0.1", 6379);
}
if(conn != NULL && conn->err)
{
printf("connection error: %s\n",conn->errstr);
}
redisReply *reply = (redisReply*)redisCommand(conn,"lpush %s %s", key, value);
freeReplyObject(reply);
}
void key(char *key, const unsigned char *str){
//随机种子
srand(time(0));
sprintf(key, "%s-%d", str, rand());
} }
void redis_free() void redis_free()
{ {
redisFree(conn); redisFree(conn);
} }
char *replace(char *src, char *sub, char *dst)
{
int pos = 0;
int offset = 0;
int srcLen, subLen, dstLen;
char *pRet = NULL;
srcLen = strlen(src);
subLen = strlen(sub);
dstLen = strlen(dst);
pRet = (char *)malloc(srcLen + dstLen - subLen + 1);//(外部是否该空间)
if (NULL != pRet)
{
pos = strstr(src, sub) - src;
memcpy(pRet, src, pos);
offset += pos;
memcpy(pRet + offset, dst, dstLen);
offset += dstLen;
memcpy(pRet + offset, src + pos + subLen, srcLen - pos - subLen);
offset += srcLen - pos - subLen;
*(pRet + offset) = '\0';
}
return pRet;
}
/*int main() /*int main()
{ {
set("a", "1"); set("a", "1");

View file

@ -1,15 +1,26 @@
// //
// hydra-rtsp.c // output-redis.c
// hydra-rtsp // output-redis
// //
// Created by Javier zhukun on 03/04/18. // Created by zhukun on 03/04/18.
// //
// //
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "lib/hiredis/hiredis.h" #include "lib/hiredis/hiredis.h"
#define REDIS_IP "127.0.0.1"
#define REDIS_PORT 6379
redisContext *conn; redisContext *conn;
void set(const unsigned char *key, const unsigned char *value); void set(const unsigned char *key, const unsigned char *value);
void get(char *value, const unsigned char *key);
void lpush(const unsigned char *key, const unsigned char *value);
void key(char *map_key, const unsigned char *str);
void redis_free(); void redis_free();
char *replace(char *src, char *sub, char *dst);