|
|
|
|
@ -18,9 +18,9 @@
|
|
|
|
|
/**
|
|
|
|
|
* \file
|
|
|
|
|
*
|
|
|
|
|
* \author Victor Julien <victor@inliniac.net>
|
|
|
|
|
* \author Tom DeCanio <td@npulsetech.com>
|
|
|
|
|
*
|
|
|
|
|
* Implements dns logging portion of the engine.
|
|
|
|
|
* Implements JSON DNS logging portion of the engine.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "suricata-common.h"
|
|
|
|
|
@ -48,69 +48,15 @@
|
|
|
|
|
#include "util-logopenfile.h"
|
|
|
|
|
#include "util-time.h"
|
|
|
|
|
|
|
|
|
|
#include "alert-json.h"
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBJANSSON
|
|
|
|
|
#include <jansson.h>
|
|
|
|
|
|
|
|
|
|
#define DEFAULT_DNS_SYSLOG_FACILITY_STR "local0"
|
|
|
|
|
#define DEFAULT_DNS_SYSLOG_FACILITY LOG_LOCAL0
|
|
|
|
|
#define DEFAULT_DNS_SYSLOG_LEVEL LOG_INFO
|
|
|
|
|
|
|
|
|
|
#ifndef OS_WIN32
|
|
|
|
|
static int dns_syslog_level = DEFAULT_DNS_SYSLOG_LEVEL;
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#define DEFAULT_LOG_FILENAME "dns.log"
|
|
|
|
|
|
|
|
|
|
#define MODULE_NAME "DnsJson"
|
|
|
|
|
|
|
|
|
|
#define OUTPUT_BUFFER_SIZE 65535
|
|
|
|
|
|
|
|
|
|
/* we can do query logging as well, but it's disabled for now as the
|
|
|
|
|
* TX id handling doesn't expect it */
|
|
|
|
|
#define QUERY 0
|
|
|
|
|
|
|
|
|
|
TmEcode DnsJson (ThreadVars *, Packet *, void *, PacketQueue *, PacketQueue *);
|
|
|
|
|
TmEcode DnsJsonIPv4(ThreadVars *, Packet *, void *, PacketQueue *, PacketQueue *);
|
|
|
|
|
TmEcode DnsJsonIPv6(ThreadVars *, Packet *, void *, PacketQueue *, PacketQueue *);
|
|
|
|
|
TmEcode DnsJsonThreadInit(ThreadVars *, void *, void **);
|
|
|
|
|
TmEcode DnsJsonThreadDeinit(ThreadVars *, void *);
|
|
|
|
|
void DnsJsonExitPrintStats(ThreadVars *, void *);
|
|
|
|
|
static void DnsJsonDeInitCtx(OutputCtx *);
|
|
|
|
|
|
|
|
|
|
void TmModuleDnsJsonRegister (void) {
|
|
|
|
|
tmm_modules[TMM_LOGDNSJSON].name = MODULE_NAME;
|
|
|
|
|
tmm_modules[TMM_LOGDNSJSON].ThreadInit = DnsJsonThreadInit;
|
|
|
|
|
tmm_modules[TMM_LOGDNSJSON].Func = DnsJson;
|
|
|
|
|
tmm_modules[TMM_LOGDNSJSON].ThreadExitPrintStats = DnsJsonExitPrintStats;
|
|
|
|
|
tmm_modules[TMM_LOGDNSJSON].ThreadDeinit = DnsJsonThreadDeinit;
|
|
|
|
|
tmm_modules[TMM_LOGDNSJSON].RegisterTests = NULL;
|
|
|
|
|
tmm_modules[TMM_LOGDNSJSON].cap_flags = 0;
|
|
|
|
|
|
|
|
|
|
OutputRegisterModule(MODULE_NAME, "dns-log-json", DnsJsonInitCtx);
|
|
|
|
|
|
|
|
|
|
/* enable the logger for the app layer */
|
|
|
|
|
AppLayerRegisterLogger(ALPROTO_DNS_UDP);
|
|
|
|
|
AppLayerRegisterLogger(ALPROTO_DNS_TCP);
|
|
|
|
|
SCLogDebug("registered %s", MODULE_NAME);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef struct LogDnsFileCtx_ {
|
|
|
|
|
LogFileCtx *file_ctx;
|
|
|
|
|
uint32_t flags; /** Store mode */
|
|
|
|
|
} LogDnsFileCtx;
|
|
|
|
|
|
|
|
|
|
#define LOG_DNS_DEFAULT 0
|
|
|
|
|
#define LOG_DNS_JSON_SYSLOG 2 /* JSON output via syslog */
|
|
|
|
|
|
|
|
|
|
typedef struct LogDnsLogThread_ {
|
|
|
|
|
LogDnsFileCtx *dnslog_ctx;
|
|
|
|
|
/** LogFileCtx has the pointer to the file and a mutex to allow multithreading */
|
|
|
|
|
uint32_t dns_cnt;
|
|
|
|
|
|
|
|
|
|
MemBuffer *buffer;
|
|
|
|
|
} LogDnsLogThread;
|
|
|
|
|
|
|
|
|
|
static void CreateTypeString(uint16_t type, char *str, size_t str_size) {
|
|
|
|
|
if (type == DNS_RECORD_TYPE_A) {
|
|
|
|
|
snprintf(str, str_size, "A");
|
|
|
|
|
@ -139,17 +85,13 @@ static void CreateTypeString(uint16_t type, char *str, size_t str_size) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void LogQuery(LogDnsLogThread *aft, char *timebuf, char *srcip, char *dstip, Port sp, Port dp, char *proto, DNSTransaction *tx, DNSQueryEntry *entry) {
|
|
|
|
|
LogDnsFileCtx *hlog = aft->dnslog_ctx;
|
|
|
|
|
static void LogQuery(AlertJsonThread/*LogDnsLogThread*/ *aft, json_t *js, /*char *timebuf, char *srcip, char *dstip, Port sp, Port dp, char *proto, */ DNSTransaction *tx, DNSQueryEntry *entry) {
|
|
|
|
|
MemBuffer *buffer = (MemBuffer *)aft->buffer;
|
|
|
|
|
|
|
|
|
|
SCLogDebug("got a DNS request and now logging !!");
|
|
|
|
|
|
|
|
|
|
/* reset */
|
|
|
|
|
MemBufferReset(aft->buffer);
|
|
|
|
|
|
|
|
|
|
json_t *js = json_object();
|
|
|
|
|
if (js == NULL)
|
|
|
|
|
return;
|
|
|
|
|
MemBufferReset(buffer);
|
|
|
|
|
|
|
|
|
|
json_t *djs = json_object();
|
|
|
|
|
if (djs == NULL) {
|
|
|
|
|
@ -157,16 +99,6 @@ static void LogQuery(LogDnsLogThread *aft, char *timebuf, char *srcip, char *dst
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* time & tx */
|
|
|
|
|
json_object_set_new(js, "time", json_string(timebuf));
|
|
|
|
|
|
|
|
|
|
/* tuple */
|
|
|
|
|
json_object_set_new(js, "srcip", json_string(srcip));
|
|
|
|
|
json_object_set_new(js, "sp", json_integer(sp));
|
|
|
|
|
json_object_set_new(js, "dstip", json_string(dstip));
|
|
|
|
|
json_object_set_new(js, "dp", json_integer(dp));
|
|
|
|
|
json_object_set_new(js, "proto", json_string(proto));
|
|
|
|
|
|
|
|
|
|
/* type */
|
|
|
|
|
json_object_set_new(djs, "type", json_string("query"));
|
|
|
|
|
|
|
|
|
|
@ -188,120 +120,10 @@ static void LogQuery(LogDnsLogThread *aft, char *timebuf, char *srcip, char *dst
|
|
|
|
|
|
|
|
|
|
/* dns */
|
|
|
|
|
json_object_set_new(js, "dns", djs);
|
|
|
|
|
char *s = json_dumps(js, JSON_PRESERVE_ORDER|JSON_COMPACT|JSON_ENSURE_ASCII);
|
|
|
|
|
MemBufferWriteString(aft->buffer, "%s", s);
|
|
|
|
|
free(s);
|
|
|
|
|
free(djs);
|
|
|
|
|
free(js);
|
|
|
|
|
|
|
|
|
|
aft->dns_cnt++;
|
|
|
|
|
|
|
|
|
|
SCMutexLock(&hlog->file_ctx->fp_mutex);
|
|
|
|
|
#ifdef HAVE_LIBJANSSON
|
|
|
|
|
if (hlog->flags & LOG_DNS_JSON_SYSLOG) {
|
|
|
|
|
syslog(dns_syslog_level, "%s", (char *)aft->buffer->buffer);
|
|
|
|
|
} else {
|
|
|
|
|
MemBufferWriteString(aft->buffer, "\n");
|
|
|
|
|
#endif
|
|
|
|
|
(void)MemBufferPrintToFPAsString(aft->buffer, hlog->file_ctx->fp);
|
|
|
|
|
fflush(hlog->file_ctx->fp);
|
|
|
|
|
#ifdef HAVE_LIBJANSSON
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
SCMutexUnlock(&hlog->file_ctx->fp_mutex);
|
|
|
|
|
OutputJSON(js, aft, &aft->dns_cnt);
|
|
|
|
|
json_object_del(js, "dns");
|
|
|
|
|
}
|
|
|
|
|
#if 0
|
|
|
|
|
static void LogAnswer(LogDnsLogThread *aft, char *timebuf, char *srcip, char *dstip, Port sp, Port dp, DNSTransaction *tx, DNSAnswerEntry *entry) {
|
|
|
|
|
LogDnsFileCtx *hlog = aft->dnslog_ctx;
|
|
|
|
|
|
|
|
|
|
SCLogDebug("got a DNS response and now logging !!");
|
|
|
|
|
|
|
|
|
|
/* reset */
|
|
|
|
|
MemBufferReset(aft->buffer);
|
|
|
|
|
|
|
|
|
|
json_t *js = json_object();
|
|
|
|
|
if (js == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
json_t *djs = json_object();
|
|
|
|
|
if (djs == NULL) {
|
|
|
|
|
free(js);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* time & tx */
|
|
|
|
|
json_object_set_new(js, "time", json_string(timebuf));
|
|
|
|
|
|
|
|
|
|
/* tuple */
|
|
|
|
|
json_object_set_new(js, "srcip", json_string(srcip));
|
|
|
|
|
json_object_set_new(js, "sp", json_integer(sp));
|
|
|
|
|
json_object_set_new(js, "dstip", json_string(dstip));
|
|
|
|
|
json_object_set_new(js, "dp", json_integer(dp));
|
|
|
|
|
|
|
|
|
|
/* type */
|
|
|
|
|
json_object_set_new(djs, "type", json_string("answer"));
|
|
|
|
|
|
|
|
|
|
/* id */
|
|
|
|
|
json_object_set_new(djs, "id", json_integer(tx->tx_id));
|
|
|
|
|
|
|
|
|
|
if (entry != NULL) {
|
|
|
|
|
/* query */
|
|
|
|
|
if (entry->fqdn_len > 0) {
|
|
|
|
|
char *c;
|
|
|
|
|
json_object_set_new(djs, "query",
|
|
|
|
|
json_string(c = strndup(
|
|
|
|
|
(char *)((char *)entry + sizeof(DNSAnswerEntry)),
|
|
|
|
|
entry->fqdn_len)));
|
|
|
|
|
if (c) free(c);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* name */
|
|
|
|
|
char record[16] = "";
|
|
|
|
|
CreateTypeString(entry->type, record, sizeof(record));
|
|
|
|
|
json_object_set_new(djs, "record", json_string(record));
|
|
|
|
|
|
|
|
|
|
/* ttl */
|
|
|
|
|
json_object_set_new(djs, "ttl", json_integer(entry->ttl));
|
|
|
|
|
|
|
|
|
|
uint8_t *ptr = (uint8_t *)((uint8_t *)entry + sizeof(DNSAnswerEntry)+ entry->fqdn_len);
|
|
|
|
|
if (entry->type == DNS_RECORD_TYPE_A) {
|
|
|
|
|
char a[16] = "";
|
|
|
|
|
PrintInet(AF_INET, (const void *)ptr, a, sizeof(a));
|
|
|
|
|
json_object_set_new(djs, "addr", json_string(a));
|
|
|
|
|
} else if (entry->type == DNS_RECORD_TYPE_AAAA) {
|
|
|
|
|
char a[46] = "";
|
|
|
|
|
PrintInet(AF_INET6, (const void *)ptr, a, sizeof(a));
|
|
|
|
|
json_object_set_new(djs, "addr", json_string(a));
|
|
|
|
|
} else if (entry->data_len == 0) {
|
|
|
|
|
json_object_set_new(djs, "addr", json_string(""));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* dns */
|
|
|
|
|
json_object_set_new(js, "dns", djs);
|
|
|
|
|
char *s = json_dumps(js, JSON_PRESERVE_ORDER|JSON_COMPACT|JSON_ENSURE_ASCII);
|
|
|
|
|
MemBufferWriteString(aft->buffer, "%s", s);
|
|
|
|
|
free(s);
|
|
|
|
|
free(djs);
|
|
|
|
|
free(js);
|
|
|
|
|
|
|
|
|
|
aft->dns_cnt++;
|
|
|
|
|
|
|
|
|
|
SCMutexLock(&hlog->file_ctx->fp_mutex);
|
|
|
|
|
#ifdef HAVE_LIBJANSSON
|
|
|
|
|
if (hlog->flags & LOG_DNS_JSON_SYSLOG) {
|
|
|
|
|
syslog(dns_syslog_level, "%s", (char *)aft->buffer->buffer);
|
|
|
|
|
} else {
|
|
|
|
|
MemBufferWriteString(aft->buffer, "\n");
|
|
|
|
|
#endif
|
|
|
|
|
(void)MemBufferPrintToFPAsString(aft->buffer, hlog->file_ctx->fp);
|
|
|
|
|
fflush(hlog->file_ctx->fp);
|
|
|
|
|
#ifdef HAVE_LIBJANSSON
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
SCMutexUnlock(&hlog->file_ctx->fp_mutex);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
static void AppendAnswer(json_t *djs, DNSTransaction *tx, DNSAnswerEntry *entry) {
|
|
|
|
|
json_t *js = json_object();
|
|
|
|
|
if (js == NULL)
|
|
|
|
|
@ -348,17 +170,13 @@ static void AppendAnswer(json_t *djs, DNSTransaction *tx, DNSAnswerEntry *entry)
|
|
|
|
|
json_array_append_new(djs, js);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void LogAnswers(LogDnsLogThread *aft, char *timebuf, char *srcip, char *dstip, Port sp, Port dp, char *proto, DNSTransaction *tx) {
|
|
|
|
|
LogDnsFileCtx *hlog = aft->dnslog_ctx;
|
|
|
|
|
static void LogAnswers(AlertJsonThread/*LogDnsLogThread*/ *aft, json_t *js, /*char *timebuf, char *srcip, char *dstip, Port sp, Port dp, char *proto,*/ DNSTransaction *tx) {
|
|
|
|
|
MemBuffer *buffer = (MemBuffer *)aft->buffer;
|
|
|
|
|
|
|
|
|
|
SCLogDebug("got a DNS response and now logging !!");
|
|
|
|
|
|
|
|
|
|
/* reset */
|
|
|
|
|
MemBufferReset(aft->buffer);
|
|
|
|
|
|
|
|
|
|
json_t *js = json_object();
|
|
|
|
|
if (js == NULL)
|
|
|
|
|
return;
|
|
|
|
|
MemBufferReset(buffer);
|
|
|
|
|
|
|
|
|
|
json_t *djs = json_array();
|
|
|
|
|
if (djs == NULL) {
|
|
|
|
|
@ -366,17 +184,6 @@ static void LogAnswers(LogDnsLogThread *aft, char *timebuf, char *srcip, char *d
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* time & tx */
|
|
|
|
|
json_object_set_new(js, "time", json_string(timebuf));
|
|
|
|
|
|
|
|
|
|
/* tuple */
|
|
|
|
|
json_object_set_new(js, "srcip", json_string(srcip));
|
|
|
|
|
json_object_set_new(js, "sp", json_integer(sp));
|
|
|
|
|
json_object_set_new(js, "dstip", json_string(dstip));
|
|
|
|
|
json_object_set_new(js, "dp", json_integer(dp));
|
|
|
|
|
json_object_set_new(js, "proto", json_string(proto));
|
|
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
|
if (tx->no_such_name) {
|
|
|
|
|
AppendAnswer(djs, tx, NULL);
|
|
|
|
|
}
|
|
|
|
|
@ -390,70 +197,11 @@ static void LogAnswers(LogDnsLogThread *aft, char *timebuf, char *srcip, char *d
|
|
|
|
|
TAILQ_FOREACH(entry, &tx->authority_list, next) {
|
|
|
|
|
AppendAnswer(djs, tx, entry);
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
/* type */
|
|
|
|
|
json_object_set_new(djs, "type", json_string("answer"));
|
|
|
|
|
|
|
|
|
|
/* id */
|
|
|
|
|
json_object_set_new(djs, "id", json_integer(tx->tx_id));
|
|
|
|
|
|
|
|
|
|
if (entry != NULL) {
|
|
|
|
|
/* query */
|
|
|
|
|
if (entry->fqdn_len > 0) {
|
|
|
|
|
char *c;
|
|
|
|
|
json_object_set_new(djs, "query",
|
|
|
|
|
json_string(c = strndup(
|
|
|
|
|
(char *)((char *)entry + sizeof(DNSAnswerEntry)),
|
|
|
|
|
entry->fqdn_len)));
|
|
|
|
|
if (c) free(c);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* name */
|
|
|
|
|
char record[16] = "";
|
|
|
|
|
CreateTypeString(entry->type, record, sizeof(record));
|
|
|
|
|
json_object_set_new(djs, "record", json_string(record));
|
|
|
|
|
|
|
|
|
|
/* ttl */
|
|
|
|
|
json_object_set_new(djs, "ttl", json_integer(entry->ttl));
|
|
|
|
|
|
|
|
|
|
uint8_t *ptr = (uint8_t *)((uint8_t *)entry + sizeof(DNSAnswerEntry)+ entry->fqdn_len);
|
|
|
|
|
if (entry->type == DNS_RECORD_TYPE_A) {
|
|
|
|
|
char a[16] = "";
|
|
|
|
|
PrintInet(AF_INET, (const void *)ptr, a, sizeof(a));
|
|
|
|
|
json_object_set_new(djs, "addr", json_string(a));
|
|
|
|
|
} else if (entry->type == DNS_RECORD_TYPE_AAAA) {
|
|
|
|
|
char a[46] = "";
|
|
|
|
|
PrintInet(AF_INET6, (const void *)ptr, a, sizeof(a));
|
|
|
|
|
json_object_set_new(djs, "addr", json_string(a));
|
|
|
|
|
} else if (entry->data_len == 0) {
|
|
|
|
|
json_object_set_new(djs, "addr", json_string(""));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* dns */
|
|
|
|
|
json_object_set_new(js, "dns", djs);
|
|
|
|
|
char *s = json_dumps(js, JSON_PRESERVE_ORDER|JSON_COMPACT|JSON_ENSURE_ASCII);
|
|
|
|
|
MemBufferWriteString(aft->buffer, "%s", s);
|
|
|
|
|
free(s);
|
|
|
|
|
free(djs);
|
|
|
|
|
free(js);
|
|
|
|
|
|
|
|
|
|
aft->dns_cnt++;
|
|
|
|
|
|
|
|
|
|
SCMutexLock(&hlog->file_ctx->fp_mutex);
|
|
|
|
|
#ifdef HAVE_LIBJANSSON
|
|
|
|
|
if (hlog->flags & LOG_DNS_JSON_SYSLOG) {
|
|
|
|
|
syslog(dns_syslog_level, "%s", (char *)aft->buffer->buffer);
|
|
|
|
|
} else {
|
|
|
|
|
MemBufferWriteString(aft->buffer, "\n");
|
|
|
|
|
#endif
|
|
|
|
|
(void)MemBufferPrintToFPAsString(aft->buffer, hlog->file_ctx->fp);
|
|
|
|
|
fflush(hlog->file_ctx->fp);
|
|
|
|
|
#ifdef HAVE_LIBJANSSON
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
SCMutexUnlock(&hlog->file_ctx->fp_mutex);
|
|
|
|
|
OutputJSON(js, aft, &aft->dns_cnt);
|
|
|
|
|
json_object_del(js, "dns");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static TmEcode DnsJsonIPWrapper(ThreadVars *tv, Packet *p, void *data, PacketQueue *pq,
|
|
|
|
|
@ -461,8 +209,7 @@ static TmEcode DnsJsonIPWrapper(ThreadVars *tv, Packet *p, void *data, PacketQue
|
|
|
|
|
{
|
|
|
|
|
SCEnter();
|
|
|
|
|
|
|
|
|
|
LogDnsLogThread *aft = (LogDnsLogThread *)data;
|
|
|
|
|
char timebuf[64];
|
|
|
|
|
AlertJsonThread *aft = (AlertJsonThread *)data;
|
|
|
|
|
|
|
|
|
|
/* no flow, no htp state */
|
|
|
|
|
if (p->flow == NULL) {
|
|
|
|
|
@ -489,48 +236,9 @@ static TmEcode DnsJsonIPWrapper(ThreadVars *tv, Packet *p, void *data, PacketQue
|
|
|
|
|
//int tx_progress_done_value_ts = AppLayerGetAlstateProgressCompletionStatus(proto, 0);
|
|
|
|
|
//int tx_progress_done_value_tc = AppLayerGetAlstateProgressCompletionStatus(proto, 1);
|
|
|
|
|
|
|
|
|
|
SCLogDebug("pcap_cnt %"PRIu64, p->pcap_cnt);
|
|
|
|
|
CreateTimeString(&p->ts, timebuf, sizeof(timebuf));
|
|
|
|
|
|
|
|
|
|
char srcip[46], dstip[46];
|
|
|
|
|
Port sp, dp;
|
|
|
|
|
if ((PKT_IS_TOCLIENT(p))) {
|
|
|
|
|
switch (ipproto) {
|
|
|
|
|
case AF_INET:
|
|
|
|
|
PrintInet(AF_INET, (const void *)GET_IPV4_SRC_ADDR_PTR(p), srcip, sizeof(srcip));
|
|
|
|
|
PrintInet(AF_INET, (const void *)GET_IPV4_DST_ADDR_PTR(p), dstip, sizeof(dstip));
|
|
|
|
|
break;
|
|
|
|
|
case AF_INET6:
|
|
|
|
|
PrintInet(AF_INET6, (const void *)GET_IPV6_SRC_ADDR(p), srcip, sizeof(srcip));
|
|
|
|
|
PrintInet(AF_INET6, (const void *)GET_IPV6_DST_ADDR(p), dstip, sizeof(dstip));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
sp = p->sp;
|
|
|
|
|
dp = p->dp;
|
|
|
|
|
} else {
|
|
|
|
|
switch (ipproto) {
|
|
|
|
|
case AF_INET:
|
|
|
|
|
PrintInet(AF_INET, (const void *)GET_IPV4_DST_ADDR_PTR(p), srcip, sizeof(srcip));
|
|
|
|
|
PrintInet(AF_INET, (const void *)GET_IPV4_SRC_ADDR_PTR(p), dstip, sizeof(dstip));
|
|
|
|
|
break;
|
|
|
|
|
case AF_INET6:
|
|
|
|
|
PrintInet(AF_INET6, (const void *)GET_IPV6_DST_ADDR(p), srcip, sizeof(srcip));
|
|
|
|
|
PrintInet(AF_INET6, (const void *)GET_IPV6_SRC_ADDR(p), dstip, sizeof(dstip));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
sp = p->dp;
|
|
|
|
|
dp = p->sp;
|
|
|
|
|
}
|
|
|
|
|
char proto_s[16];
|
|
|
|
|
if (SCProtoNameValid(IPV4_GET_IPPROTO(p)) == TRUE) {
|
|
|
|
|
strlcpy(proto_s, known_proto[IPV4_GET_IPPROTO(p)], sizeof(proto_s));
|
|
|
|
|
} else {
|
|
|
|
|
snprintf(proto_s, sizeof(proto), "PROTO:%03" PRIu32, IPV4_GET_IPPROTO(p));
|
|
|
|
|
}
|
|
|
|
|
json_t *js = CreateJSONHeader(p, 1);
|
|
|
|
|
if (unlikely(js == NULL))
|
|
|
|
|
return TM_ECODE_OK;
|
|
|
|
|
|
|
|
|
|
#if QUERY
|
|
|
|
|
if (PKT_IS_TOSERVER(p)) {
|
|
|
|
|
@ -553,26 +261,10 @@ static TmEcode DnsJsonIPWrapper(ThreadVars *tv, Packet *p, void *data, PacketQue
|
|
|
|
|
|
|
|
|
|
DNSQueryEntry *query = NULL;
|
|
|
|
|
TAILQ_FOREACH(query, &tx->query_list, next) {
|
|
|
|
|
LogQuery(aft, timebuf, dstip, srcip, dp, sp, proto_s, tx, query);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
|
LogAnswers(aft, timebuf, srcip, dstip, sp, dp, proto_s, tx);
|
|
|
|
|
#else
|
|
|
|
|
if (tx->no_such_name) {
|
|
|
|
|
LogAnswer(aft, timebuf, srcip, dstip, sp, dp, tx, NULL);
|
|
|
|
|
LogQuery(aft, js, /*timebuf, dstip, srcip, dp, sp, proto_s,*/ tx, query);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DNSAnswerEntry *entry = NULL;
|
|
|
|
|
TAILQ_FOREACH(entry, &tx->answer_list, next) {
|
|
|
|
|
LogAnswer(aft, timebuf, srcip, dstip, sp, dp, tx, entry);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
entry = NULL;
|
|
|
|
|
TAILQ_FOREACH(entry, &tx->authority_list, next) {
|
|
|
|
|
LogAnswer(aft, timebuf, srcip, dstip, sp, dp, tx, entry);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
LogAnswers(aft, js, /*timebuf, srcip, dstip, sp, dp, proto_s,*/ tx);
|
|
|
|
|
|
|
|
|
|
SCLogDebug("calling AppLayerTransactionUpdateLoggedId");
|
|
|
|
|
AppLayerTransactionUpdateLogId(ALPROTO_DNS_UDP, p->flow);
|
|
|
|
|
@ -584,147 +276,44 @@ end:
|
|
|
|
|
SCReturnInt(TM_ECODE_OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TmEcode DnsJsonIPv4(ThreadVars *tv, Packet *p, void *data, PacketQueue *pq, PacketQueue *postpq)
|
|
|
|
|
{
|
|
|
|
|
return DnsJsonIPWrapper(tv, p, data, pq, postpq, AF_INET);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TmEcode DnsJsonIPv6(ThreadVars *tv, Packet *p, void *data, PacketQueue *pq, PacketQueue *postpq)
|
|
|
|
|
{
|
|
|
|
|
return DnsJsonIPWrapper(tv, p, data, pq, postpq, AF_INET6);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TmEcode DnsJson (ThreadVars *tv, Packet *p, void *data, PacketQueue *pq, PacketQueue *postpq)
|
|
|
|
|
int OutputDnsNeedsLog(Packet *p)
|
|
|
|
|
{
|
|
|
|
|
SCEnter();
|
|
|
|
|
|
|
|
|
|
SCLogDebug("pcap_cnt %"PRIu64, p->pcap_cnt);
|
|
|
|
|
/* no flow, no htp state */
|
|
|
|
|
if (p->flow == NULL) {
|
|
|
|
|
SCReturnInt(TM_ECODE_OK);
|
|
|
|
|
SCReturnInt(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!(PKT_IS_UDP(p)) && !(PKT_IS_TCP(p))) {
|
|
|
|
|
SCReturnInt(TM_ECODE_OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (PKT_IS_IPV4(p)) {
|
|
|
|
|
int r = DnsJsonIPv4(tv, p, data, pq, postpq);
|
|
|
|
|
SCReturnInt(r);
|
|
|
|
|
} else if (PKT_IS_IPV6(p)) {
|
|
|
|
|
int r = DnsJsonIPv6(tv, p, data, pq, postpq);
|
|
|
|
|
SCReturnInt(r);
|
|
|
|
|
SCReturnInt(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SCReturnInt(TM_ECODE_OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TmEcode DnsJsonThreadInit(ThreadVars *t, void *initdata, void **data)
|
|
|
|
|
{
|
|
|
|
|
LogDnsLogThread *aft = SCMalloc(sizeof(LogDnsLogThread));
|
|
|
|
|
if (unlikely(aft == NULL))
|
|
|
|
|
return TM_ECODE_FAILED;
|
|
|
|
|
memset(aft, 0, sizeof(LogDnsLogThread));
|
|
|
|
|
|
|
|
|
|
if(initdata == NULL)
|
|
|
|
|
{
|
|
|
|
|
SCLogDebug("Error getting context for DNSLog. \"initdata\" argument NULL");
|
|
|
|
|
SCFree(aft);
|
|
|
|
|
return TM_ECODE_FAILED;
|
|
|
|
|
/* check if we have DNS state or not */
|
|
|
|
|
FLOWLOCK_WRLOCK(p->flow);
|
|
|
|
|
uint16_t proto = AppLayerGetProtoFromPacket(p);
|
|
|
|
|
if (proto != ALPROTO_DNS_UDP && proto != ALPROTO_DNS_TCP) {
|
|
|
|
|
SCLogDebug("proto not ALPROTO_DNS_UDP: %u", proto);
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
aft->buffer = MemBufferCreateNew(OUTPUT_BUFFER_SIZE);
|
|
|
|
|
if (aft->buffer == NULL) {
|
|
|
|
|
SCFree(aft);
|
|
|
|
|
return TM_ECODE_FAILED;
|
|
|
|
|
DNSState *dns_state = (DNSState *)AppLayerGetProtoStateFromPacket(p);
|
|
|
|
|
if (dns_state == NULL) {
|
|
|
|
|
SCLogDebug("no dns state, so no request logging");
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
end:
|
|
|
|
|
FLOWLOCK_UNLOCK(p->flow);
|
|
|
|
|
|
|
|
|
|
/* Use the Ouptut Context (file pointer and mutex) */
|
|
|
|
|
aft->dnslog_ctx= ((OutputCtx *)initdata)->data;
|
|
|
|
|
|
|
|
|
|
*data = (void *)aft;
|
|
|
|
|
return TM_ECODE_OK;
|
|
|
|
|
SCReturnInt(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TmEcode DnsJsonThreadDeinit(ThreadVars *t, void *data)
|
|
|
|
|
TmEcode OutputDnsLog(ThreadVars *tv, Packet *p, void *data, PacketQueue *pq, PacketQueue *postpq)
|
|
|
|
|
{
|
|
|
|
|
LogDnsLogThread *aft = (LogDnsLogThread *)data;
|
|
|
|
|
if (aft == NULL) {
|
|
|
|
|
return TM_ECODE_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MemBufferFree(aft->buffer);
|
|
|
|
|
/* clear memory */
|
|
|
|
|
memset(aft, 0, sizeof(LogDnsLogThread));
|
|
|
|
|
SCEnter();
|
|
|
|
|
|
|
|
|
|
SCFree(aft);
|
|
|
|
|
return TM_ECODE_OK;
|
|
|
|
|
}
|
|
|
|
|
DnsJsonIPWrapper(tv, p, data, pq, postpq, AF_INET);
|
|
|
|
|
|
|
|
|
|
void DnsJsonExitPrintStats(ThreadVars *tv, void *data) {
|
|
|
|
|
LogDnsLogThread *aft = (LogDnsLogThread *)data;
|
|
|
|
|
if (aft == NULL) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SCLogInfo("DNS logger logged %" PRIu32 " requests", aft->dns_cnt);
|
|
|
|
|
SCReturnInt(TM_ECODE_OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \brief Create a new dns log LogFileCtx.
|
|
|
|
|
* \param conf Pointer to ConfNode containing this loggers configuration.
|
|
|
|
|
* \return NULL if failure, LogFileCtx* to the file_ctx if succesful
|
|
|
|
|
* */
|
|
|
|
|
OutputCtx *DnsJsonInitCtx(ConfNode *conf)
|
|
|
|
|
{
|
|
|
|
|
LogFileCtx* file_ctx = LogFileNewCtx();
|
|
|
|
|
|
|
|
|
|
if(file_ctx == NULL) {
|
|
|
|
|
SCLogError(SC_ERR_DNS_LOG_GENERIC, "couldn't create new file_ctx");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (SCConfLogOpenGeneric(conf, file_ctx, DEFAULT_LOG_FILENAME) < 0) {
|
|
|
|
|
LogFileFreeCtx(file_ctx);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LogDnsFileCtx *dnslog_ctx = SCMalloc(sizeof(LogDnsFileCtx));
|
|
|
|
|
if (unlikely(dnslog_ctx == NULL)) {
|
|
|
|
|
LogFileFreeCtx(file_ctx);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
memset(dnslog_ctx, 0x00, sizeof(LogDnsFileCtx));
|
|
|
|
|
|
|
|
|
|
dnslog_ctx->file_ctx = file_ctx;
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBJANSSON
|
|
|
|
|
const char *json = ConfNodeLookupChildValue(conf, "json");
|
|
|
|
|
if (json) {
|
|
|
|
|
if (strcmp(json, "syslog") == 0) {
|
|
|
|
|
dnslog_ctx->flags |= LOG_DNS_JSON_SYSLOG;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
|
|
|
|
|
if (unlikely(output_ctx == NULL)) {
|
|
|
|
|
LogFileFreeCtx(file_ctx);
|
|
|
|
|
SCFree(dnslog_ctx);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
output_ctx->data = dnslog_ctx;
|
|
|
|
|
output_ctx->DeInit = DnsJsonDeInitCtx;
|
|
|
|
|
|
|
|
|
|
SCLogDebug("DNS json output initialized");
|
|
|
|
|
|
|
|
|
|
return output_ctx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void DnsJsonDeInitCtx(OutputCtx *output_ctx)
|
|
|
|
|
{
|
|
|
|
|
LogDnsFileCtx *dnslog_ctx = (LogDnsFileCtx *)output_ctx->data;
|
|
|
|
|
LogFileFreeCtx(dnslog_ctx->file_ctx);
|
|
|
|
|
SCFree(dnslog_ctx);
|
|
|
|
|
SCFree(output_ctx);
|
|
|
|
|
}
|
|
|
|
|
|