From 154af56b453a481c7987e4bd19df2bf07debdee1 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Sat, 18 Feb 2012 17:56:38 +0100 Subject: [PATCH] Add a print function specially for json output that escapes all characters json requires to be escaped. --- src/log-file.c | 14 ++++++++------ src/util-print.c | 19 +++++++++++++++++++ src/util-print.h | 1 + 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/log-file.c b/src/log-file.c index 36f7137510..3d24058d23 100644 --- a/src/log-file.c +++ b/src/log-file.c @@ -108,7 +108,7 @@ static void LogFileMetaGetUri(FILE *fp, Packet *p, File *ff) { if (htp_state != NULL) { htp_tx_t *tx = list_get(htp_state->connp->conn->transactions, ff->txid); if (tx != NULL && tx->request_uri_normalized != NULL) { - PrintRawUriFp(fp, (uint8_t *)bstr_ptr(tx->request_uri_normalized), + PrintRawJsonFp(fp, (uint8_t *)bstr_ptr(tx->request_uri_normalized), bstr_len(tx->request_uri_normalized)); return; } @@ -129,7 +129,7 @@ static void LogFileMetaGetHost(FILE *fp, Packet *p, File *ff) { table_iterator_reset(headers); while (table_iterator_next(headers, (void **)&h) != NULL) { if (strcasecmp("Host", bstr_tocstr(h->name)) == 0) { - PrintRawUriFp(fp, (uint8_t *)bstr_ptr(h->value), + PrintRawJsonFp(fp, (uint8_t *)bstr_ptr(h->value), bstr_len(h->value)); return; } @@ -152,7 +152,7 @@ static void LogFileMetaGetReferer(FILE *fp, Packet *p, File *ff) { table_iterator_reset(headers); while (table_iterator_next(headers, (void **)&h) != NULL) { if (strcasecmp("Referer", bstr_tocstr(h->name)) == 0) { - PrintRawUriFp(fp, (uint8_t *)bstr_ptr(h->value), + PrintRawJsonFp(fp, (uint8_t *)bstr_ptr(h->value), bstr_len(h->value)); return; } @@ -275,7 +275,9 @@ static void LogFileWriteJsonRecord(LogFileLogThread *aft, Packet *p, File *ff, i CreateTimeString(&p->ts, timebuf, sizeof(timebuf)); fprintf(fp, "{ \"id\": %u, ", ff->file_id); - fprintf(fp, "\"timestamp\": \"%s\", ", timebuf); + fprintf(fp, "\"timestamp\": \""); + PrintRawJsonFp(fp, (uint8_t *)timebuf, strlen(timebuf)); + fprintf(fp, "\", "); if (p->pcap_cnt > 0) { fprintf(fp, "\"pcap_pkt_num\": %"PRIu64", ", p->pcap_cnt); } @@ -322,12 +324,12 @@ static void LogFileWriteJsonRecord(LogFileLogThread *aft, Packet *p, File *ff, i fprintf(fp, "\", "); fprintf(fp, "\"filename\": \""); - PrintRawUriFp(fp, ff->name, ff->name_len); + PrintRawJsonFp(fp, ff->name, ff->name_len); fprintf(fp, "\", "); fprintf(fp, "\"magic\": \""); if (ff->magic) { - PrintRawUriFp(fp, (uint8_t *)ff->magic, strlen(ff->magic)); + PrintRawJsonFp(fp, (uint8_t *)ff->magic, strlen(ff->magic)); } else { fprintf(fp, "unknown"); } diff --git a/src/util-print.c b/src/util-print.c index 9fc2c581f8..fbda45e1f7 100644 --- a/src/util-print.c +++ b/src/util-print.c @@ -73,6 +73,25 @@ void PrintRawLineHexBuf(char *retbuf, uint32_t retbuflen, uint8_t *buf, uint32_t } } +void PrintRawJsonFp(FILE *fp, uint8_t *buf, uint32_t buflen) +{ + char nbuf[2048] = ""; + char temp[5] = ""; + uint32_t u = 0; + + for (u = 0; u < buflen; u++) { + if (buf[u] == '\\' || buf[u] == '/' || buf[u] == '\"') { + snprintf(temp, sizeof(temp), "\\%c", buf[u]); + } else if (isprint(buf[u])) { + snprintf(temp, sizeof(temp), "%c", buf[u]); + } else { + snprintf(temp, sizeof(temp), "\\\\x%02X", buf[u]); + } + strlcat(nbuf, temp, sizeof(nbuf)); + } + fprintf(fp, "%s", nbuf); +} + void PrintRawUriFp(FILE *fp, uint8_t *buf, uint32_t buflen) { char nbuf[2048] = ""; diff --git a/src/util-print.h b/src/util-print.h index b3abfab3d3..8df4cf3cd8 100644 --- a/src/util-print.h +++ b/src/util-print.h @@ -28,6 +28,7 @@ void PrintRawLineHexFp(FILE *, uint8_t *, uint32_t); void PrintRawUriFp(FILE *, uint8_t *, uint32_t); +void PrintRawJsonFp(FILE *, uint8_t *, uint32_t); void PrintRawDataFp(FILE *, uint8_t *, uint32_t); void PrintRawLineHexBuf(char *, uint32_t, uint8_t *, uint32_t ); const char *PrintInet(int , const void *, char *, socklen_t);