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 #define OUTPUT_BUFFER_SIZE 65535
135 static TmEcode JsonSshLogThreadInit(ThreadVars *t, const void *initdata, void **data)
136 {
137  JsonSshLogThread *aft = SCMalloc(sizeof(JsonSshLogThread));
138  if (unlikely(aft == NULL))
139  return TM_ECODE_FAILED;
140  memset(aft, 0, sizeof(JsonSshLogThread));
141 
142  if(initdata == NULL)
143  {
144  SCLogDebug("Error getting context for EveLogSSH. \"initdata\" argument NULL");
145  SCFree(aft);
146  return TM_ECODE_FAILED;
147  }
148 
149  /* Use the Ouptut Context (file pointer and mutex) */
150  aft->sshlog_ctx = ((OutputCtx *)initdata)->data;
151 
152  aft->buffer = MemBufferCreateNew(OUTPUT_BUFFER_SIZE);
153  if (aft->buffer == NULL) {
154  SCFree(aft);
155  return TM_ECODE_FAILED;
156  }
157 
158  *data = (void *)aft;
159  return TM_ECODE_OK;
160 }
161 
162 static TmEcode JsonSshLogThreadDeinit(ThreadVars *t, void *data)
163 {
164  JsonSshLogThread *aft = (JsonSshLogThread *)data;
165  if (aft == NULL) {
166  return TM_ECODE_OK;
167  }
168 
169  MemBufferFree(aft->buffer);
170  /* clear memory */
171  memset(aft, 0, sizeof(JsonSshLogThread));
172 
173  SCFree(aft);
174  return TM_ECODE_OK;
175 }
176 
177 static void OutputSshLogDeinit(OutputCtx *output_ctx)
178 {
179  OutputSshCtx *ssh_ctx = output_ctx->data;
180  LogFileCtx *logfile_ctx = ssh_ctx->file_ctx;
181  LogFileFreeCtx(logfile_ctx);
182  SCFree(ssh_ctx);
183  SCFree(output_ctx);
184 }
185 
186 #define DEFAULT_LOG_FILENAME "ssh.json"
187 static OutputInitResult OutputSshLogInit(ConfNode *conf)
188 {
189  OutputInitResult result = { NULL, false };
190  LogFileCtx *file_ctx = LogFileNewCtx();
191  if(file_ctx == NULL) {
192  SCLogError(SC_ERR_SSH_LOG_GENERIC, "couldn't create new file_ctx");
193  return result;
194  }
195 
196  if (SCConfLogOpenGeneric(conf, file_ctx, DEFAULT_LOG_FILENAME, 1) < 0) {
197  LogFileFreeCtx(file_ctx);
198  return result;
199  }
200 
201  OutputSshCtx *ssh_ctx = SCMalloc(sizeof(OutputSshCtx));
202  if (unlikely(ssh_ctx == NULL)) {
203  LogFileFreeCtx(file_ctx);
204  return result;
205  }
206 
207  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
208  if (unlikely(output_ctx == NULL)) {
209  LogFileFreeCtx(file_ctx);
210  SCFree(ssh_ctx);
211  return result;
212  }
213 
214  ssh_ctx->file_ctx = file_ctx;
215 
216  output_ctx->data = ssh_ctx;
217  output_ctx->DeInit = OutputSshLogDeinit;
218 
220 
221  result.ctx = output_ctx;
222  result.ok = true;
223  return result;
224 }
225 
226 static void OutputSshLogDeinitSub(OutputCtx *output_ctx)
227 {
228  OutputSshCtx *ssh_ctx = output_ctx->data;
229  SCFree(ssh_ctx);
230  SCFree(output_ctx);
231 }
232 
233 static OutputInitResult OutputSshLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
234 {
235  OutputInitResult result = { NULL, false };
236  OutputJsonCtx *ojc = parent_ctx->data;
237 
238  OutputSshCtx *ssh_ctx = SCMalloc(sizeof(OutputSshCtx));
239  if (unlikely(ssh_ctx == NULL))
240  return result;
241 
242  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
243  if (unlikely(output_ctx == NULL)) {
244  SCFree(ssh_ctx);
245  return result;
246  }
247 
248  ssh_ctx->file_ctx = ojc->file_ctx;
249  ssh_ctx->cfg = ojc->cfg;
250 
251  output_ctx->data = ssh_ctx;
252  output_ctx->DeInit = OutputSshLogDeinitSub;
253 
255 
256  result.ctx = output_ctx;
257  result.ok = true;
258  return result;
259 }
260 
261 void JsonSshLogRegister (void)
262 {
263  /* register as separate module */
265  "JsonSshLog", "ssh-json-log",
266  OutputSshLogInit, ALPROTO_SSH, JsonSshLogger,
268  JsonSshLogThreadInit, JsonSshLogThreadDeinit, NULL);
269 
270  /* also register as child of eve-log */
272  "eve-log", "JsonSshLog", "eve-log.ssh",
273  OutputSshLogInitSub, ALPROTO_SSH, JsonSshLogger,
275  JsonSshLogThreadInit, JsonSshLogThreadDeinit, NULL);
276 }
277 
278 #else
279 
281 {
282 }
283 
284 #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:197
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:355
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:366
Definition: conf.h:32
OutputCtx * ctx
Definition: output.h:42
#define DEFAULT_LOG_FILENAME
#define SCMalloc(a)
Definition: util-mem.h:166
int LogFileFreeCtx(LogFileCtx *lf_ctx)
LogFileFreeCtx() Destroy a LogFileCtx (Close the file and free memory)
#define SCFree(a)
Definition: util-mem.h:228
uint16_t tx_id
void * data
Definition: tm-modules.h:81
Per thread variable structure.
Definition: threadvars.h:57
#define OUTPUT_BUFFER_SIZE
Definition: log-httplog.c:58
Flow data structure.
Definition: flow.h:325
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82