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 #define MODULE_NAME "LogSshLog"
54 
55 typedef struct OutputSshCtx_ {
58 } OutputSshCtx;
59 
60 
61 typedef struct JsonSshLogThread_ {
65 
66 
67 void JsonSshLogJSON(json_t *tjs, SshState *ssh_state)
68 {
69  json_t *cjs = json_object();
70  if (cjs != NULL) {
71  json_object_set_new(cjs, "proto_version",
72  SCJsonString((char *)ssh_state->cli_hdr.proto_version));
73 
74  json_object_set_new(cjs, "software_version",
75  SCJsonString((char *)ssh_state->cli_hdr.software_version));
76  }
77  json_object_set_new(tjs, "client", cjs);
78 
79  json_t *sjs = json_object();
80  if (sjs != NULL) {
81  json_object_set_new(sjs, "proto_version",
82  SCJsonString((char *)ssh_state->srv_hdr.proto_version));
83 
84  json_object_set_new(sjs, "software_version",
85  SCJsonString((char *)ssh_state->srv_hdr.software_version));
86  }
87  json_object_set_new(tjs, "server", sjs);
88 
89 }
90 
91 static int JsonSshLogger(ThreadVars *tv, void *thread_data, const Packet *p,
92  Flow *f, void *state, void *txptr, uint64_t tx_id)
93 {
94  JsonSshLogThread *aft = (JsonSshLogThread *)thread_data;
95  OutputSshCtx *ssh_ctx = aft->sshlog_ctx;
96 
97  SshState *ssh_state = (SshState *)state;
98  if (unlikely(ssh_state == NULL)) {
99  return 0;
100  }
101 
102  if (ssh_state->cli_hdr.software_version == NULL ||
103  ssh_state->srv_hdr.software_version == NULL)
104  return 0;
105 
106  json_t *js = CreateJSONHeader(p, LOG_DIR_FLOW, "ssh");
107  if (unlikely(js == NULL))
108  return 0;
109 
110  JsonAddCommonOptions(&ssh_ctx->cfg, p, f, js);
111 
112  json_t *tjs = json_object();
113  if (tjs == NULL) {
114  free(js);
115  return 0;
116  }
117 
118  /* reset */
119  MemBufferReset(aft->buffer);
120 
121  JsonSshLogJSON(tjs, ssh_state);
122 
123  json_object_set_new(js, "ssh", tjs);
124 
125  OutputJSONBuffer(js, ssh_ctx->file_ctx, &aft->buffer);
126  json_object_clear(js);
127  json_decref(js);
128 
129  return 0;
130 }
131 
132 static TmEcode JsonSshLogThreadInit(ThreadVars *t, const void *initdata, void **data)
133 {
135  if (unlikely(aft == NULL))
136  return TM_ECODE_FAILED;
137  memset(aft, 0, sizeof(JsonSshLogThread));
138 
139  if(initdata == NULL)
140  {
141  SCLogDebug("Error getting context for EveLogSSH. \"initdata\" argument NULL");
142  SCFree(aft);
143  return TM_ECODE_FAILED;
144  }
145 
146  /* Use the Ouptut Context (file pointer and mutex) */
147  aft->sshlog_ctx = ((OutputCtx *)initdata)->data;
148 
150  if (aft->buffer == NULL) {
151  SCFree(aft);
152  return TM_ECODE_FAILED;
153  }
154 
155  *data = (void *)aft;
156  return TM_ECODE_OK;
157 }
158 
159 static TmEcode JsonSshLogThreadDeinit(ThreadVars *t, void *data)
160 {
161  JsonSshLogThread *aft = (JsonSshLogThread *)data;
162  if (aft == NULL) {
163  return TM_ECODE_OK;
164  }
165 
166  MemBufferFree(aft->buffer);
167  /* clear memory */
168  memset(aft, 0, sizeof(JsonSshLogThread));
169 
170  SCFree(aft);
171  return TM_ECODE_OK;
172 }
173 
174 static void OutputSshLogDeinit(OutputCtx *output_ctx)
175 {
176  OutputSshCtx *ssh_ctx = output_ctx->data;
177  LogFileCtx *logfile_ctx = ssh_ctx->file_ctx;
178  LogFileFreeCtx(logfile_ctx);
179  SCFree(ssh_ctx);
180  SCFree(output_ctx);
181 }
182 
183 #define DEFAULT_LOG_FILENAME "ssh.json"
184 static OutputInitResult OutputSshLogInit(ConfNode *conf)
185 {
186  OutputInitResult result = { NULL, false };
188  if(file_ctx == NULL) {
189  SCLogError(SC_ERR_SSH_LOG_GENERIC, "couldn't create new file_ctx");
190  return result;
191  }
192 
193  if (SCConfLogOpenGeneric(conf, file_ctx, DEFAULT_LOG_FILENAME, 1) < 0) {
194  LogFileFreeCtx(file_ctx);
195  return result;
196  }
197 
198  OutputSshCtx *ssh_ctx = SCMalloc(sizeof(OutputSshCtx));
199  if (unlikely(ssh_ctx == NULL)) {
200  LogFileFreeCtx(file_ctx);
201  return result;
202  }
203 
204  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
205  if (unlikely(output_ctx == NULL)) {
206  LogFileFreeCtx(file_ctx);
207  SCFree(ssh_ctx);
208  return result;
209  }
210 
211  ssh_ctx->file_ctx = file_ctx;
212 
213  output_ctx->data = ssh_ctx;
214  output_ctx->DeInit = OutputSshLogDeinit;
215 
217 
218  result.ctx = output_ctx;
219  result.ok = true;
220  return result;
221 }
222 
223 static void OutputSshLogDeinitSub(OutputCtx *output_ctx)
224 {
225  OutputSshCtx *ssh_ctx = output_ctx->data;
226  SCFree(ssh_ctx);
227  SCFree(output_ctx);
228 }
229 
230 static OutputInitResult OutputSshLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
231 {
232  OutputInitResult result = { NULL, false };
233  OutputJsonCtx *ojc = parent_ctx->data;
234 
235  OutputSshCtx *ssh_ctx = SCMalloc(sizeof(OutputSshCtx));
236  if (unlikely(ssh_ctx == NULL))
237  return result;
238 
239  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
240  if (unlikely(output_ctx == NULL)) {
241  SCFree(ssh_ctx);
242  return result;
243  }
244 
245  ssh_ctx->file_ctx = ojc->file_ctx;
246  ssh_ctx->cfg = ojc->cfg;
247 
248  output_ctx->data = ssh_ctx;
249  output_ctx->DeInit = OutputSshLogDeinitSub;
250 
252 
253  result.ctx = output_ctx;
254  result.ok = true;
255  return result;
256 }
257 
259 {
260  /* register as separate module */
262  "JsonSshLog", "ssh-json-log",
263  OutputSshLogInit, ALPROTO_SSH, JsonSshLogger,
265  JsonSshLogThreadInit, JsonSshLogThreadDeinit, NULL);
266 
267  /* also register as child of eve-log */
269  "eve-log", "JsonSshLog", "eve-log.ssh",
270  OutputSshLogInitSub, ALPROTO_SSH, JsonSshLogger,
272  JsonSshLogThreadInit, JsonSshLogThreadDeinit, NULL);
273 }
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
#define JSON_OUTPUT_BUFFER_SIZE
Definition: output-json.h:44
json_t * CreateJSONHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type)
Definition: output-json.c:710
#define DEFAULT_LOG_FILENAME
SshHeader cli_hdr
Definition: app-layer-ssh.h:74
#define SCLogDebug(...)
Definition: util-debug.h:335
int OutputJSONBuffer(json_t *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:809
struct OutputSshCtx_ OutputSshCtx
#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
struct JsonSshLogThread_ JsonSshLogThread
MemBuffer * buffer
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
void JsonAddCommonOptions(const OutputJsonCommonSettings *cfg, const Packet *p, const Flow *f, json_t *js)
Definition: output-json.c:390
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
OutputJsonCommonSettings cfg
Definition: output-json.h:81
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:360
LogFileCtx * file_ctx
Definition: output-json.h:79
LogFileCtx * LogFileNewCtx(void)
LogFileNewCtx() Get a new LogFileCtx.
uint8_t * software_version
Definition: app-layer-ssh.h:61
json_t * SCJsonString(const char *val)
Definition: output-json.c:107
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
LogFileCtx * file_ctx
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:371
Definition: conf.h:32
OutputCtx * ctx
Definition: output.h:42
#define SCMalloc(a)
Definition: util-mem.h:222
OutputSshCtx * sshlog_ctx
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 JsonSshLogJSON(json_t *tjs, SshState *ssh_state)
void * data
Definition: tm-modules.h:81
Per thread variable structure.
Definition: threadvars.h:57
OutputJsonCommonSettings cfg
Flow data structure.
Definition: flow.h:325
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82