suricata
util-lua-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 /**
20  * \file
21  *
22  * \author Mats Klepsland <mats.klepsland@gmail.com>
23  *
24  */
25 
26 #include "suricata-common.h"
27 #include "util-lua-ssh.h"
28 #include "util-lua.h"
29 #include "util-lua-common.h"
30 #include "rust.h"
31 
32 // #define SSH_MT "suricata:ssh:tx"
33 static const char ssh_tx[] = "suricata:ssh:tx";
34 
35 struct LuaTx {
36  SCLuaTxRef ref; // (flow, tx id); the SSHTransaction is resolved on demand
37 };
38 
39 static int LuaSshGetTx(lua_State *L)
40 {
41  if (!(LuaStateNeedProto(L, ALPROTO_SSH))) {
42  return LuaCallbackError(L, "error: protocol not ssh");
43  }
44  void *tx = LuaStateGetTX(L);
45  if (tx == NULL) {
46  return LuaCallbackError(L, "error: no tx available");
47  }
48  struct LuaTx *ltx = SCLuaNewUserData(L, SCLUA_TUD_SSH_TX, sizeof(*ltx));
49  if (ltx == NULL) {
50  return LuaCallbackError(L, "error: fail to allocate user data");
51  }
52  SCLuaTxRefInit(L, &ltx->ref, LuaStateGetTxId(L));
53 
54  luaL_getmetatable(L, ssh_tx);
55  lua_setmetatable(L, -2);
56 
57  return 1;
58 }
59 
60 static int LuaSshTxGetProto(lua_State *L, uint8_t flags)
61 {
62  const uint8_t *buf = NULL;
63  uint32_t b_len = 0;
64  struct LuaTx *ltx = SCLuaTestUserData(L, 1, ssh_tx, SCLUA_TUD_SSH_TX);
65  if (ltx == NULL) {
66  lua_pushnil(L);
67  return 1;
68  }
69  SCLUA_RESOLVE_TX(L, &ltx->ref, txptr);
70  if (!SCSshTxGetProtocol(txptr, flags, &buf, &b_len)) {
71  lua_pushnil(L);
72  return 1;
73  }
74  return LuaPushStringBuffer(L, buf, b_len);
75 }
76 
77 static int LuaSshTxGetServerProto(lua_State *L)
78 {
79  return LuaSshTxGetProto(L, STREAM_TOCLIENT);
80 }
81 
82 static int LuaSshTxGetClientProto(lua_State *L)
83 {
84  return LuaSshTxGetProto(L, STREAM_TOSERVER);
85 }
86 
87 static int LuaSshTxGetSoftware(lua_State *L, uint8_t flags)
88 {
89  const uint8_t *buf = NULL;
90  uint32_t b_len = 0;
91  struct LuaTx *ltx = SCLuaTestUserData(L, 1, ssh_tx, SCLUA_TUD_SSH_TX);
92  if (ltx == NULL) {
93  lua_pushnil(L);
94  return 1;
95  }
96  SCLUA_RESOLVE_TX(L, &ltx->ref, txptr);
97  if (!SCSshTxGetSoftware(txptr, flags, &buf, &b_len)) {
98  lua_pushnil(L);
99  return 1;
100  }
101  return LuaPushStringBuffer(L, buf, b_len);
102 }
103 
104 static int LuaSshTxGetServerSoftware(lua_State *L)
105 {
106  return LuaSshTxGetSoftware(L, STREAM_TOCLIENT);
107 }
108 
109 static int LuaSshTxGetClientSoftware(lua_State *L)
110 {
111  return LuaSshTxGetSoftware(L, STREAM_TOSERVER);
112 }
113 
114 static int LuaSshTxGetHassh(lua_State *L, uint8_t flags)
115 {
116  const uint8_t *buf = NULL;
117  uint32_t b_len = 0;
118  struct LuaTx *ltx = SCLuaTestUserData(L, 1, ssh_tx, SCLUA_TUD_SSH_TX);
119  if (ltx == NULL) {
120  lua_pushnil(L);
121  return 1;
122  }
123  SCLUA_RESOLVE_TX(L, &ltx->ref, txptr);
124  if (SCSshTxGetHassh(txptr, flags, &buf, &b_len) != 1) {
125  lua_pushnil(L);
126  return 1;
127  }
128  return LuaPushStringBuffer(L, buf, b_len);
129 }
130 
131 static int LuaSshTxGetClientHassh(lua_State *L)
132 {
133  return LuaSshTxGetHassh(L, STREAM_TOSERVER);
134 }
135 
136 static int LuaSshTxGetServerHassh(lua_State *L)
137 {
138  return LuaSshTxGetHassh(L, STREAM_TOCLIENT);
139 }
140 
141 static int LuaSshTxGetHasshString(lua_State *L, uint8_t flags)
142 {
143  const uint8_t *buf = NULL;
144  uint32_t b_len = 0;
145  struct LuaTx *ltx = SCLuaTestUserData(L, 1, ssh_tx, SCLUA_TUD_SSH_TX);
146  if (ltx == NULL) {
147  lua_pushnil(L);
148  return 1;
149  }
150  SCLUA_RESOLVE_TX(L, &ltx->ref, txptr);
151  if (SCSshTxGetHasshString(txptr, flags, &buf, &b_len) != 1) {
152  lua_pushnil(L);
153  return 1;
154  }
155  return LuaPushStringBuffer(L, buf, b_len);
156 }
157 
158 static int LuaSshTxGetClientHasshString(lua_State *L)
159 {
160  return LuaSshTxGetHasshString(L, STREAM_TOSERVER);
161 }
162 
163 static int LuaSshTxGetServerHasshString(lua_State *L)
164 {
165  return LuaSshTxGetHasshString(L, STREAM_TOCLIENT);
166 }
167 
168 static const struct luaL_Reg txlib[] = {
169  // clang-format off
170  { "server_proto", LuaSshTxGetServerProto },
171  { "server_software", LuaSshTxGetServerSoftware },
172  { "client_proto", LuaSshTxGetClientProto },
173  { "client_software", LuaSshTxGetClientSoftware },
174  { "client_hassh", LuaSshTxGetClientHassh },
175  { "server_hassh", LuaSshTxGetServerHassh },
176  { "client_hassh_string", LuaSshTxGetClientHasshString },
177  { "server_hassh_string", LuaSshTxGetServerHasshString },
178  { NULL, NULL, }
179  // clang-format on
180 };
181 
182 static int LuaSshEnableHassh(lua_State *L)
183 {
184  SCSshEnableHassh();
185  /* global, parse-time switch: no result to return */
186  return 0;
187 }
188 
189 static const struct luaL_Reg sshlib[] = {
190  // clang-format off
191  { "get_tx", LuaSshGetTx },
192  { "enable_hassh", LuaSshEnableHassh },
193  { NULL, NULL,},
194  // clang-format on
195 };
196 
198 {
199  luaL_newmetatable(L, ssh_tx);
200  lua_pushvalue(L, -1);
201  lua_setfield(L, -2, "__index");
202  luaL_setfuncs(L, txlib, 0);
203 
204  luaL_newlib(L, sshlib);
205  return 1;
206 }
LuaTx::ref
SCLuaTxRef ref
Definition: util-lua-dns.c:35
util-lua-ssh.h
util-lua-common.h
util-lua.h
LuaCallbackError
int LuaCallbackError(lua_State *luastate, const char *msg)
Definition: util-lua-common.c:218
SCLuaTxRef_
Definition: util-lua-common.h:92
rust.h
ALPROTO_SSH
@ ALPROTO_SSH
Definition: app-layer-protos.h:40
lua_State
struct lua_State lua_State
Definition: suricata-common.h:527
SCLUA_TUD_SSH_TX
@ SCLUA_TUD_SSH_TX
Definition: util-lua-common.h:65
LuaTx
Definition: util-lua-dns.c:34
SCLuaTestUserData
void * SCLuaTestUserData(lua_State *L, int idx, const char *mt, uint32_t type_id)
Definition: util-lua-common.c:123
SCLuaNewUserData
void * SCLuaNewUserData(lua_State *L, uint32_t type_id, size_t size)
Definition: util-lua-common.c:42
LuaStateGetTX
void * LuaStateGetTX(lua_State *luastate)
get tx pointer from the lua state
Definition: util-lua.c:234
flags
uint8_t flags
Definition: decode-gre.h:0
LuaStateGetTxId
uint64_t LuaStateGetTxId(lua_State *luastate)
get tx id from the lua state
Definition: util-lua.c:240
suricata-common.h
SCLUA_RESOLVE_TX
#define SCLUA_RESOLVE_TX(L, refp, txvar)
Definition: util-lua-common.h:128
SCLuaLoadSshLib
int SCLuaLoadSshLib(lua_State *L)
Definition: util-lua-ssh.c:197
SCLuaTxRefInit
void SCLuaTxRefInit(lua_State *L, SCLuaTxRef *ref, uint64_t tx_id)
Definition: util-lua-common.c:164
LuaStateNeedProto
int LuaStateNeedProto(lua_State *luastate, AppProto alproto)
Definition: util-lua-common.c:289
LuaPushStringBuffer
int LuaPushStringBuffer(lua_State *luastate, const uint8_t *input, size_t input_len)
Definition: util-lua.c:380