suricata
output-json-ssh.c
Go to the documentation of this file.
1 /* Copyright (C) 2014 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  *
23  * Implements SSH JSON logging portion of the engine.
24  */
25 
26 #include "suricata-common.h"
27 #include "debug.h"
28 #include "detect.h"
29 #include "pkt-var.h"
30 #include "conf.h"
31 
32 #include "threads.h"
33 #include "threadvars.h"
34 #include "tm-threads.h"
35 
36 #include "util-print.h"
37 #include "util-unittest.h"
38 
39 #include "util-debug.h"
40 #include "app-layer-parser.h"
41 #include "output.h"
42 #include "app-layer-ssh.h"
43 #include "app-layer.h"
44 #include "util-privs.h"
45 #include "util-buffer.h"
46 
47 #include "util-logopenfile.h"
48 #include "util-crypt.h"
49 
50 #include "output-json.h"
51 #include "output-json-ssh.h"
52 
53 #ifdef HAVE_LIBJANSSON
54 
55 #define MODULE_NAME "LogSshLog"
56 
57 typedef struct OutputSshCtx_ {
58  LogFileCtx *file_ctx;
59  OutputJsonCommonSettings cfg;
60 } OutputSshCtx;
61 
62 
63 typedef struct JsonSshLogThread_ {
64  OutputSshCtx *sshlog_ctx;
65  MemBuffer *buffer;
66 } JsonSshLogThread;
67 
68 
69 void JsonSshLogJSON(json_t *tjs, SshState *ssh_state)
70 {
71  json_t *cjs = json_object();
72  if (cjs != NULL) {
73  json_object_set_new(cjs, "proto_version",
74  SCJsonString((char *)ssh_state->cli_hdr.proto_version));
75 
76  json_object_set_new(cjs, "software_version",
77  SCJsonString((char *)ssh_state->cli_hdr.software_version));
78  }
79  json_object_set_new(tjs, "client", cjs);
80 
81  json_t *sjs = json_object();
82  if (sjs != NULL) {
83  json_object_set_new(sjs, "proto_version",
84  SCJsonString((char *)ssh_state->srv_hdr.proto_version));
85 
86  json_object_set_new(sjs, "software_version",
87  SCJsonString((char *)ssh_state->srv_hdr.software_version));
88  }
89  json_object_set_new(tjs, "server", sjs);
90 
91 }
92 
93 static int JsonSshLogger(ThreadVars *tv, void *thread_data, const Packet *p,
94  Flow *f, void *state, void *txptr, uint64_t tx_id)
95 {
96  JsonSshLogThread *aft = (JsonSshLogThread *)thread_data;
97  OutputSshCtx *ssh_ctx = aft->sshlog_ctx;
98 
99  SshState *ssh_state = (SshState *)state;
100  if (unlikely(ssh_state == NULL)) {
101  return 0;
102  }
103 
104  if (ssh_state->cli_hdr.software_version == NULL ||
105  ssh_state->srv_hdr.software_version == NULL)
106  return 0;
107 
108  json_t *js = CreateJSONHeader(p, LOG_DIR_FLOW, "ssh");
109  if (unlikely(js == NULL))
110  return 0;
111 
112  JsonAddCommonOptions(&ssh_ctx->cfg, p, f, js);
113 
114  json_t *tjs = json_object();
115  if (tjs == NULL) {
116  free(js);
117  return 0;
118  }
119 
120  /* reset */
121  MemBufferReset(aft->buffer);
122 
123  JsonSshLogJSON(tjs, ssh_state);
124 
125  json_object_set_new(js, "ssh", tjs);
126 
127  OutputJSONBuffer(js, ssh_ctx->file_ctx, &aft->buffer);
128  json_object_clear(js);
129  json_decref(js);
130 
131  return 0;
132 }
133 
134 static TmEcode JsonSshLogThreadInit(ThreadVars *t, const void *initdata, void **data)
135 {
136  JsonSshLogThread *aft = SCMalloc(sizeof(JsonSshLogThread));
137  if (unlikely(aft == NULL))
138  return TM_ECODE_FAILED;
139  memset(aft, 0, sizeof(JsonSshLogThread));
140 
141  if(initdata == NULL)
142  {
143  SCLogDebug("Error getting context for EveLogSSH. \"initdata\" argument NULL");
144  SCFree(aft);
145  return TM_ECODE_FAILED;
146  }
147 
148  /* Use the Ouptut Context (file pointer and mutex) */
149  aft->sshlog_ctx = ((OutputCtx *)initdata)->data;
150 
151  aft->buffer = MemBufferCreateNew(JSON_OUTPUT_BUFFER_SIZE);
152  if (aft->buffer == NULL) {
153  SCFree(aft);
154  return TM_ECODE_FAILED;
155  }
156 
157  *data = (void *)aft;
158  return TM_ECODE_OK;
159 }
160 
161 static TmEcode JsonSshLogThreadDeinit(ThreadVars *t, void *data)
162 {
163  JsonSshLogThread *aft = (JsonSshLogThread *)data;
164  if (aft == NULL) {
165  return TM_ECODE_OK;
166  }
167 
168  MemBufferFree(aft->buffer);
169  /* clear memory */
170  memset(aft, 0, sizeof(JsonSshLogThread));
171 
172  SCFree(aft);
173  return TM_ECODE_OK;
174 }
175 
176 static void OutputSshLogDeinit(OutputCtx *output_ctx)
177 {
178  OutputSshCtx *ssh_ctx = output_ctx->data;
179  LogFileCtx *logfile_ctx = ssh_ctx->file_ctx;
180  LogFileFreeCtx(logfile_ctx);
181  SCFree(ssh_ctx);
182  SCFree(output_ctx);
183 }
184 
185 #define DEFAULT_LOG_FILENAME "ssh.json"
186 static OutputInitResult OutputSshLogInit(ConfNode *conf)
187 {
188  OutputInitResult result = { NULL, false };
189  LogFileCtx *file_ctx = LogFileNewCtx();
190  if(file_ctx == NULL) {
191  SCLogError(SC_ERR_SSH_LOG_GENERIC, "couldn't create new file_ctx");
192  return result;
193  }
194 
195  if (SCConfLogOpenGeneric(conf, file_ctx, DEFAULT_LOG_FILENAME, 1) < 0) {
196  LogFileFreeCtx(file_ctx);
197  return result;
198  }
199 
200  OutputSshCtx *ssh_ctx = SCMalloc(sizeof(OutputSshCtx));
201  if (unlikely(ssh_ctx == NULL)) {
202  LogFileFreeCtx(file_ctx);
203  return result;
204  }
205 
206  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
207  if (unlikely(output_ctx == NULL)) {
208  LogFileFreeCtx(file_ctx);
209  SCFree(ssh_ctx);
210  return result;
211  }
212 
213  ssh_ctx->file_ctx = file_ctx;
214 
215  output_ctx->data = ssh_ctx;
216  output_ctx->DeInit = OutputSshLogDeinit;
217 
219 
220  result.ctx = output_ctx;
221  result.ok = true;
222  return result;
223 }
224 
225 static void OutputSshLogDeinitSub(OutputCtx *output_ctx)
226 {
227  OutputSshCtx *ssh_ctx = output_ctx->data;
228  SCFree(ssh_ctx);
229  SCFree(output_ctx);
230 }
231 
232 static OutputInitResult OutputSshLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
233 {
234  OutputInitResult result = { NULL, false };
235  OutputJsonCtx *ojc = parent_ctx->data;
236 
237  OutputSshCtx *ssh_ctx = SCMalloc(sizeof(OutputSshCtx));
238  if (unlikely(ssh_ctx == NULL))
239  return result;
240 
241  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
242  if (unlikely(output_ctx == NULL)) {
243  SCFree(ssh_ctx);
244  return result;
245  }
246 
247  ssh_ctx->file_ctx = ojc->file_ctx;
248  ssh_ctx->cfg = ojc->cfg;
249 
250  output_ctx->data = ssh_ctx;
251  output_ctx->DeInit = OutputSshLogDeinitSub;
252 
254 
255  result.ctx = output_ctx;
256  result.ok = true;
257  return result;
258 }
259 
260 void JsonSshLogRegister (void)
261 {
262  /* register as separate module */
264  "JsonSshLog", "ssh-json-log",
265  OutputSshLogInit, ALPROTO_SSH, JsonSshLogger,
267  JsonSshLogThreadInit, JsonSshLogThreadDeinit, NULL);
268 
269  /* also register as child of eve-log */
271  "eve-log", "JsonSshLog", "eve-log.ssh",
272  OutputSshLogInitSub, ALPROTO_SSH, JsonSshLogger,
274  JsonSshLogThreadInit, JsonSshLogThreadDeinit, NULL);
275 }
276 
277 #else
278 
280 {
281 }
282 
283 #endif
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
SshHeader cli_hdr
Definition: app-layer-ssh.h:74
#define SCLogDebug(...)
Definition: util-debug.h:335
#define unlikely(expr)
Definition: util-optimize.h:35
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
uint8_t * proto_version
Definition: app-layer-ssh.h:60
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
void JsonSshLogRegister(void)
#define SCCalloc(nm, a)
Definition: util-mem.h:253
SshHeader srv_hdr
Definition: app-layer-ssh.h:73
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
void OutputRegisterTxModuleWithProgress(LoggerId id, const char *name, const char *conf_name, OutputInitFunc InitFunc, AppProto alproto, TxLogger TxLogFunc, int tc_log_progress, int ts_log_progress, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit, ThreadExitPrintStatsFunc ThreadExitPrintStats)
Register a tx output module with progress.
Definition: output.c:358
LogFileCtx * LogFileNewCtx(void)
LogFileNewCtx() Get a new LogFileCtx.
uint8_t * software_version
Definition: app-layer-ssh.h:61
int SCConfLogOpenGeneric(ConfNode *conf, LogFileCtx *log_ctx, const char *default_filename, int rotate)
open a generic output "log file", which may be a regular file or a socket
void OutputRegisterTxSubModuleWithProgress(LoggerId id, const char *parent_name, const char *name, const char *conf_name, OutputInitSubFunc InitFunc, AppProto alproto, TxLogger TxLogFunc, int tc_log_progress, int ts_log_progress, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit, ThreadExitPrintStatsFunc ThreadExitPrintStats)
Definition: output.c:369
Definition: conf.h:32
OutputCtx * ctx
Definition: output.h:42
#define DEFAULT_LOG_FILENAME
#define SCMalloc(a)
Definition: util-mem.h:222
int LogFileFreeCtx(LogFileCtx *lf_ctx)
LogFileFreeCtx() Destroy a LogFileCtx (Close the file and free memory)
#define SCFree(a)
Definition: util-mem.h:322
uint16_t tx_id
void * data
Definition: tm-modules.h:81
Per thread variable structure.
Definition: threadvars.h:57
Flow data structure.
Definition: flow.h:325
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82