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 #include "rust.h"
53 
54 #define MODULE_NAME "LogSshLog"
55 
56 typedef struct OutputSshCtx_ {
60 
61 
62 typedef struct JsonSshLogThread_ {
66 
67 
68 static int JsonSshLogger(ThreadVars *tv, void *thread_data, const Packet *p,
69  Flow *f, void *state, void *txptr, uint64_t tx_id)
70 {
71  JsonSshLogThread *aft = (JsonSshLogThread *)thread_data;
72  OutputSshCtx *ssh_ctx = aft->sshlog_ctx;
73 
74  if (unlikely(state == NULL)) {
75  return 0;
76  }
77 
78  json_t *js = CreateJSONHeader(p, LOG_DIR_FLOW, "ssh");
79  if (unlikely(js == NULL))
80  return 0;
81 
82  JsonAddCommonOptions(&ssh_ctx->cfg, p, f, js);
83 
84  /* reset */
85  MemBufferReset(aft->buffer);
86 
87  json_t *tjs = rs_ssh_log_json(txptr);
88  if (unlikely(tjs == NULL)) {
89  free(js);
90  return 0;
91  }
92  json_object_set_new(js, "ssh", tjs);
93 
94  OutputJSONBuffer(js, ssh_ctx->file_ctx, &aft->buffer);
95  json_object_clear(js);
96  json_decref(js);
97 
98  return 0;
99 }
100 
101 static TmEcode JsonSshLogThreadInit(ThreadVars *t, const void *initdata, void **data)
102 {
104  if (unlikely(aft == NULL))
105  return TM_ECODE_FAILED;
106  memset(aft, 0, sizeof(JsonSshLogThread));
107 
108  if(initdata == NULL)
109  {
110  SCLogDebug("Error getting context for EveLogSSH. \"initdata\" argument NULL");
111  SCFree(aft);
112  return TM_ECODE_FAILED;
113  }
114 
115  /* Use the Ouptut Context (file pointer and mutex) */
116  aft->sshlog_ctx = ((OutputCtx *)initdata)->data;
117 
119  if (aft->buffer == NULL) {
120  SCFree(aft);
121  return TM_ECODE_FAILED;
122  }
123 
124  *data = (void *)aft;
125  return TM_ECODE_OK;
126 }
127 
128 static TmEcode JsonSshLogThreadDeinit(ThreadVars *t, void *data)
129 {
130  JsonSshLogThread *aft = (JsonSshLogThread *)data;
131  if (aft == NULL) {
132  return TM_ECODE_OK;
133  }
134 
135  MemBufferFree(aft->buffer);
136  /* clear memory */
137  memset(aft, 0, sizeof(JsonSshLogThread));
138 
139  SCFree(aft);
140  return TM_ECODE_OK;
141 }
142 
143 static void OutputSshLogDeinit(OutputCtx *output_ctx)
144 {
145  OutputSshCtx *ssh_ctx = output_ctx->data;
146  LogFileCtx *logfile_ctx = ssh_ctx->file_ctx;
147  LogFileFreeCtx(logfile_ctx);
148  SCFree(ssh_ctx);
149  SCFree(output_ctx);
150 }
151 
152 #define DEFAULT_LOG_FILENAME "ssh.json"
153 static OutputInitResult OutputSshLogInit(ConfNode *conf)
154 {
155  OutputInitResult result = { NULL, false };
156  LogFileCtx *file_ctx = LogFileNewCtx();
157  if(file_ctx == NULL) {
158  SCLogError(SC_ERR_SSH_LOG_GENERIC, "couldn't create new file_ctx");
159  return result;
160  }
161 
162  if (SCConfLogOpenGeneric(conf, file_ctx, DEFAULT_LOG_FILENAME, 1) < 0) {
163  LogFileFreeCtx(file_ctx);
164  return result;
165  }
166 
167  OutputSshCtx *ssh_ctx = SCMalloc(sizeof(OutputSshCtx));
168  if (unlikely(ssh_ctx == NULL)) {
169  LogFileFreeCtx(file_ctx);
170  return result;
171  }
172 
173  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
174  if (unlikely(output_ctx == NULL)) {
175  LogFileFreeCtx(file_ctx);
176  SCFree(ssh_ctx);
177  return result;
178  }
179 
180  ssh_ctx->file_ctx = file_ctx;
181 
182  output_ctx->data = ssh_ctx;
183  output_ctx->DeInit = OutputSshLogDeinit;
184 
186 
187  result.ctx = output_ctx;
188  result.ok = true;
189  return result;
190 }
191 
192 static void OutputSshLogDeinitSub(OutputCtx *output_ctx)
193 {
194  OutputSshCtx *ssh_ctx = output_ctx->data;
195  SCFree(ssh_ctx);
196  SCFree(output_ctx);
197 }
198 
199 static OutputInitResult OutputSshLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
200 {
201  OutputInitResult result = { NULL, false };
202  OutputJsonCtx *ojc = parent_ctx->data;
203 
204  OutputSshCtx *ssh_ctx = SCMalloc(sizeof(OutputSshCtx));
205  if (unlikely(ssh_ctx == NULL))
206  return result;
207 
208  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
209  if (unlikely(output_ctx == NULL)) {
210  SCFree(ssh_ctx);
211  return result;
212  }
213 
214  ssh_ctx->file_ctx = ojc->file_ctx;
215  ssh_ctx->cfg = ojc->cfg;
216 
217  output_ctx->data = ssh_ctx;
218  output_ctx->DeInit = OutputSshLogDeinitSub;
219 
221 
222  result.ctx = output_ctx;
223  result.ok = true;
224  return result;
225 }
226 
228 {
229  /* register as separate module */
231  "JsonSshLog", "ssh-json-log",
232  OutputSshLogInit, ALPROTO_SSH, JsonSshLogger,
233  SshStateBannerDone, SshStateBannerDone,
234  JsonSshLogThreadInit, JsonSshLogThreadDeinit, NULL);
235 
236  /* also register as child of eve-log */
238  "eve-log", "JsonSshLog", "eve-log.ssh",
239  OutputSshLogInitSub, ALPROTO_SSH, JsonSshLogger,
240  SshStateBannerDone, SshStateBannerDone,
241  JsonSshLogThreadInit, JsonSshLogThreadDeinit, NULL);
242 }
tm-threads.h
OutputSshCtx_
Definition: output-json-ssh.c:56
app-layer-ssh.h
OutputJsonCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json.h:81
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
LogFileNewCtx
LogFileCtx * LogFileNewCtx(void)
LogFileNewCtx() Get a new LogFileCtx.
Definition: util-logopenfile.c:517
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
JsonSshLogThread
struct JsonSshLogThread_ JsonSshLogThread
JSON_OUTPUT_BUFFER_SIZE
#define JSON_OUTPUT_BUFFER_SIZE
Definition: output-json.h:44
threads.h
OutputJsonCtx_
Definition: output-json.h:78
Flow_
Flow data structure.
Definition: flow.h:343
OutputJsonCommonSettings_
Definition: output-json.h:69
LOGGER_JSON_SSH
@ LOGGER_JSON_SSH
Definition: suricata-common.h:458
LogFileCtx_
Definition: util-logopenfile.h:52
rust.h
util-privs.h
JsonSshLogRegister
void JsonSshLogRegister(void)
Definition: output-json-ssh.c:227
ALPROTO_SSH
@ ALPROTO_SSH
Definition: app-layer-protos.h:34
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:79
util-unittest.h
OutputCtx_::data
void * data
Definition: tm-modules.h:81
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:78
OutputCtx_
Definition: tm-modules.h:78
SCConfLogOpenGeneric
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
Definition: util-logopenfile.c:305
SC_ERR_SSH_LOG_GENERIC
@ SC_ERR_SSH_LOG_GENERIC
Definition: util-error.h:316
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:42
output-json.h
DEFAULT_LOG_FILENAME
#define DEFAULT_LOG_FILENAME
Definition: output-json-ssh.c:152
OutputRegisterTxSubModuleWithProgress
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:379
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:471
JsonSshLogThread_
Definition: output-json-ssh.c:62
util-print.h
util-crypt.h
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
pkt-var.h
OutputSshCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json-ssh.c:58
JsonSshLogThread_::sshlog_ctx
OutputSshCtx * sshlog_ctx
Definition: output-json-ssh.c:63
OutputInitResult_::ok
bool ok
Definition: output.h:43
app-layer-parser.h
Packet_
Definition: decode.h:411
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:77
MemBuffer_
Definition: util-buffer.h:27
OutputSshCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-ssh.c:57
MemBufferReset
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
OutputInitResult_
Definition: output.h:41
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
CreateJSONHeader
json_t * CreateJSONHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type)
Definition: output-json.c:710
MemBufferFree
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
LogFileFreeCtx
int LogFileFreeCtx(LogFileCtx *lf_ctx)
LogFileFreeCtx() Destroy a LogFileCtx (Close the file and free memory)
Definition: util-logopenfile.c:539
JsonAddCommonOptions
void JsonAddCommonOptions(const OutputJsonCommonSettings *cfg, const Packet *p, const Flow *f, json_t *js)
Definition: output-json.c:390
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
threadvars.h
LOG_DIR_FLOW
@ LOG_DIR_FLOW
Definition: output-json.h:38
OutputSshCtx
struct OutputSshCtx_ OutputSshCtx
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
util-logopenfile.h
util-buffer.h
JsonSshLogThread_::buffer
MemBuffer * buffer
Definition: output-json-ssh.c:64
OutputRegisterTxModuleWithProgress
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:368
output-json-ssh.h
OutputJsonCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json.h:79
OutputJSONBuffer
int OutputJSONBuffer(json_t *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:809
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
MemBufferCreateNew
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
debug.h
output.h
app-layer.h