suricata
output-json-ssh.c
Go to the documentation of this file.
1 /* Copyright (C) 2014-2020 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_ {
67 
68 
69 static int JsonSshLogger(ThreadVars *tv, void *thread_data, const Packet *p,
70  Flow *f, void *state, void *txptr, uint64_t tx_id)
71 {
72  JsonSshLogThread *aft = (JsonSshLogThread *)thread_data;
73  OutputSshCtx *ssh_ctx = aft->sshlog_ctx;
74 
75  if (unlikely(state == NULL)) {
76  return 0;
77  }
78 
79  JsonBuilder *js = CreateEveHeaderWithTxId(p, LOG_DIR_FLOW, "ssh", NULL, tx_id);
80  if (unlikely(js == NULL))
81  return 0;
82 
83  EveAddCommonOptions(&ssh_ctx->cfg, p, f, js);
84 
85  /* reset */
86  MemBufferReset(aft->buffer);
87 
88  jb_open_object(js, "ssh");
89  if (!rs_ssh_log_json(txptr, js)) {
90  goto end;
91  }
92  jb_close(js);
93  OutputJsonBuilderBuffer(js, aft->file_ctx, &aft->buffer);
94 
95 end:
96  jb_free(js);
97  return 0;
98 }
99 
100 static TmEcode JsonSshLogThreadInit(ThreadVars *t, const void *initdata, void **data)
101 {
102  if (initdata == NULL) {
103  SCLogDebug("Error getting context for EveLogSSH. \"initdata\" argument NULL");
104  return TM_ECODE_FAILED;
105  }
106 
107  JsonSshLogThread *aft = SCCalloc(1, sizeof(JsonSshLogThread));
108  if (unlikely(aft == NULL))
109  return TM_ECODE_FAILED;
110 
111  /* Use the Output Context (file pointer and mutex) */
112  aft->sshlog_ctx = ((OutputCtx *)initdata)->data;
113 
115  if (aft->buffer == NULL) {
116  goto error_exit;
117  }
118 
120  if (!aft->file_ctx) {
121  goto error_exit;
122  }
123 
124  *data = (void *)aft;
125  return TM_ECODE_OK;
126 
127 error_exit:
128  if (aft->buffer != NULL) {
129  MemBufferFree(aft->buffer);
130  }
131  SCFree(aft);
132  return TM_ECODE_FAILED;
133 }
134 
135 static TmEcode JsonSshLogThreadDeinit(ThreadVars *t, void *data)
136 {
137  JsonSshLogThread *aft = (JsonSshLogThread *)data;
138  if (aft == NULL) {
139  return TM_ECODE_OK;
140  }
141 
142  MemBufferFree(aft->buffer);
143  /* clear memory */
144  memset(aft, 0, sizeof(JsonSshLogThread));
145 
146  SCFree(aft);
147  return TM_ECODE_OK;
148 }
149 
150 static void OutputSshLogDeinit(OutputCtx *output_ctx)
151 {
152  OutputSshCtx *ssh_ctx = output_ctx->data;
153  LogFileCtx *logfile_ctx = ssh_ctx->file_ctx;
154  LogFileFreeCtx(logfile_ctx);
155  SCFree(ssh_ctx);
156  SCFree(output_ctx);
157 }
158 
159 #define DEFAULT_LOG_FILENAME "ssh.json"
160 static OutputInitResult OutputSshLogInit(ConfNode *conf)
161 {
162  OutputInitResult result = { NULL, false };
163  LogFileCtx *file_ctx = LogFileNewCtx();
164  if(file_ctx == NULL) {
165  SCLogError(SC_ERR_SSH_LOG_GENERIC, "couldn't create new file_ctx");
166  return result;
167  }
168 
169  if (SCConfLogOpenGeneric(conf, file_ctx, DEFAULT_LOG_FILENAME, 1) < 0) {
170  LogFileFreeCtx(file_ctx);
171  return result;
172  }
173 
174  OutputSshCtx *ssh_ctx = SCMalloc(sizeof(OutputSshCtx));
175  if (unlikely(ssh_ctx == NULL)) {
176  LogFileFreeCtx(file_ctx);
177  return result;
178  }
179 
180  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
181  if (unlikely(output_ctx == NULL)) {
182  LogFileFreeCtx(file_ctx);
183  SCFree(ssh_ctx);
184  return result;
185  }
186 
187  ssh_ctx->file_ctx = file_ctx;
188 
189  output_ctx->data = ssh_ctx;
190  output_ctx->DeInit = OutputSshLogDeinit;
191 
193 
194  result.ctx = output_ctx;
195  result.ok = true;
196  return result;
197 }
198 
199 static void OutputSshLogDeinitSub(OutputCtx *output_ctx)
200 {
201  OutputSshCtx *ssh_ctx = output_ctx->data;
202  SCFree(ssh_ctx);
203  SCFree(output_ctx);
204 }
205 
206 static OutputInitResult OutputSshLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
207 {
208  OutputInitResult result = { NULL, false };
209  OutputJsonCtx *ojc = parent_ctx->data;
210 
211  OutputSshCtx *ssh_ctx = SCMalloc(sizeof(OutputSshCtx));
212  if (unlikely(ssh_ctx == NULL))
213  return result;
214 
215  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
216  if (unlikely(output_ctx == NULL)) {
217  SCFree(ssh_ctx);
218  return result;
219  }
220 
221  ssh_ctx->file_ctx = ojc->file_ctx;
222  ssh_ctx->cfg = ojc->cfg;
223 
224  output_ctx->data = ssh_ctx;
225  output_ctx->DeInit = OutputSshLogDeinitSub;
226 
228 
229  result.ctx = output_ctx;
230  result.ok = true;
231  return result;
232 }
233 
235 {
236  /* register as separate module */
238  "JsonSshLog", "ssh-json-log",
239  OutputSshLogInit, ALPROTO_SSH, JsonSshLogger,
240  SSHTxLogCondition, JsonSshLogThreadInit, JsonSshLogThreadDeinit, NULL);
241 
242  /* also register as child of eve-log */
244  "eve-log", "JsonSshLog", "eve-log.ssh",
245  OutputSshLogInitSub, ALPROTO_SSH, JsonSshLogger,
246  SSHTxLogCondition, JsonSshLogThreadInit, JsonSshLogThreadDeinit, NULL);
247 }
tm-threads.h
OutputSshCtx_
Definition: output-json-ssh.c:56
app-layer-ssh.h
OutputJsonCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json.h:104
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
LogFileNewCtx
LogFileCtx * LogFileNewCtx(void)
LogFileNewCtx() Get a new LogFileCtx.
Definition: util-logopenfile.c:614
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:63
threads.h
OutputJsonCtx_
Definition: output-json.h:101
Flow_
Flow data structure.
Definition: flow.h:347
OutputJsonCommonSettings_
Definition: output-json.h:91
LOGGER_JSON_SSH
@ LOGGER_JSON_SSH
Definition: suricata-common.h:458
LogFileCtx_
Definition: util-logopenfile.h:60
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:978
rust.h
util-privs.h
EveAddCommonOptions
void EveAddCommonOptions(const OutputJsonCommonSettings *cfg, const Packet *p, const Flow *f, JsonBuilder *js)
Definition: output-json.c:444
JsonSshLogRegister
void JsonSshLogRegister(void)
Definition: output-json-ssh.c:234
ALPROTO_SSH
@ ALPROTO_SSH
Definition: app-layer-protos.h:34
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:81
util-unittest.h
OutputCtx_::data
void * data
Definition: tm-modules.h:81
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:80
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:397
SSHTxLogCondition
int SSHTxLogCondition(ThreadVars *tv, const Packet *p, void *state, void *tx, uint64_t tx_id)
Definition: app-layer-ssh.c:74
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:44
output-json.h
DEFAULT_LOG_FILENAME
#define DEFAULT_LOG_FILENAME
Definition: output-json-ssh.c:159
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:457
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
LogFileEnsureExists
LogFileCtx * LogFileEnsureExists(LogFileCtx *parent_ctx, int thread_id)
LogFileEnsureExists() Ensure a log file context for the thread exists.
Definition: util-logopenfile.c:633
JsonSshLogThread_::sshlog_ctx
OutputSshCtx * sshlog_ctx
Definition: output-json-ssh.c:63
OutputInitResult_::ok
bool ok
Definition: output.h:45
app-layer-parser.h
ThreadVars_::id
int id
Definition: threadvars.h:87
Packet_
Definition: decode.h:412
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:79
JsonSshLogThread_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-ssh.c:64
MemBuffer_
Definition: util-buffer.h:27
OutputSshCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-ssh.c:57
OutputRegisterTxSubModuleWithCondition
void OutputRegisterTxSubModuleWithCondition(LoggerId id, const char *parent_name, const char *name, const char *conf_name, OutputInitSubFunc InitFunc, AppProto alproto, TxLogger TxLogFunc, TxLoggerCondition TxLogCondition, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit, ThreadExitPrintStatsFunc ThreadExitPrintStats)
Definition: output.c:347
MemBufferReset
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
OutputInitResult_
Definition: output.h:43
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
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:737
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
threadvars.h
LOG_DIR_FLOW
@ LOG_DIR_FLOW
Definition: output-json.h:40
OutputSshCtx
struct OutputSshCtx_ OutputSshCtx
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
SCFree
#define SCFree(p)
Definition: util-mem.h:61
CreateEveHeaderWithTxId
JsonBuilder * CreateEveHeaderWithTxId(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, uint64_t tx_id)
Definition: output-json.c:923
ConfNode_
Definition: conf.h:32
util-logopenfile.h
util-buffer.h
JsonSshLogThread_::buffer
MemBuffer * buffer
Definition: output-json-ssh.c:65
output-json-ssh.h
OutputJsonCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json.h:102
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
OutputRegisterTxModuleWithCondition
void OutputRegisterTxModuleWithCondition(LoggerId id, const char *name, const char *conf_name, OutputInitFunc InitFunc, AppProto alproto, TxLogger TxLogFunc, TxLoggerCondition TxLogCondition, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit, ThreadExitPrintStatsFunc ThreadExitPrintStats)
Register a tx output module with condition.
Definition: output.c:336
MemBufferCreateNew
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
debug.h
output.h
app-layer.h