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 "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 
41 #include "output.h"
42 #include "app-layer.h"
43 #include "app-layer-parser.h"
44 #include "app-layer-ssh.h"
45 #include "util-privs.h"
46 #include "util-buffer.h"
47 #include "util-proto-name.h"
48 #include "util-logopenfile.h"
49 #include "util-time.h"
50 #include "rust.h"
51 
52 #ifdef HAVE_LUA
53 
54 #include <lua.h>
55 #include <lualib.h>
56 #include <lauxlib.h>
57 
58 #include "util-lua.h"
59 #include "util-lua-common.h"
60 #include "util-lua-ssh.h"
61 
62 static int GetServerProtoVersion(lua_State *luastate, const Flow *f)
63 {
64  void *state = FlowGetAppState(f);
65  if (state == NULL)
66  return LuaCallbackError(luastate, "error: no app layer state");
67  const uint8_t *protocol = NULL;
68  uint32_t b_len = 0;
69 
70  void *tx = rs_ssh_state_get_tx(state, 0);
71  if (rs_ssh_tx_get_protocol(tx, &protocol, &b_len, STREAM_TOCLIENT) != 1)
72  return LuaCallbackError(luastate, "error: no server proto version");
73  if (protocol == NULL || b_len == 0) {
74  return LuaCallbackError(luastate, "error: no server proto version");
75  }
76 
77  return LuaPushStringBuffer(luastate, protocol, b_len);
78 }
79 
80 static int SshGetServerProtoVersion(lua_State *luastate)
81 {
82  int r;
83 
84  if (!(LuaStateNeedProto(luastate, ALPROTO_SSH)))
85  return LuaCallbackError(luastate, "error: protocol not ssh");
86 
87  Flow *f = LuaStateGetFlow(luastate);
88  if (f == NULL)
89  return LuaCallbackError(luastate, "internal error: no flow");
90 
91  r = GetServerProtoVersion(luastate, f);
92 
93  return r;
94 }
95 
96 static int GetServerSoftwareVersion(lua_State *luastate, const Flow *f)
97 {
98  void *state = FlowGetAppState(f);
99  if (state == NULL)
100  return LuaCallbackError(luastate, "error: no app layer state");
101 
102  const uint8_t *software = NULL;
103  uint32_t b_len = 0;
104 
105  void *tx = rs_ssh_state_get_tx(state, 0);
106  if (rs_ssh_tx_get_software(tx, &software, &b_len, STREAM_TOCLIENT) != 1)
107  return LuaCallbackError(luastate, "error: no server software version");
108  if (software == NULL || b_len == 0) {
109  return LuaCallbackError(luastate, "error: no server software version");
110  }
111 
112  return LuaPushStringBuffer(luastate, software, b_len);
113 }
114 
115 static int SshGetServerSoftwareVersion(lua_State *luastate)
116 {
117  int r;
118 
119  if (!(LuaStateNeedProto(luastate, ALPROTO_SSH)))
120  return LuaCallbackError(luastate, "error: protocol not ssh");
121 
122  Flow *f = LuaStateGetFlow(luastate);
123  if (f == NULL)
124  return LuaCallbackError(luastate, "internal error: no flow");
125 
126  r = GetServerSoftwareVersion(luastate, f);
127 
128  return r;
129 }
130 
131 static int GetClientProtoVersion(lua_State *luastate, const Flow *f)
132 {
133  void *state = FlowGetAppState(f);
134  if (state == NULL)
135  return LuaCallbackError(luastate, "error: no app layer state");
136 
137  const uint8_t *protocol = NULL;
138  uint32_t b_len = 0;
139 
140  void *tx = rs_ssh_state_get_tx(state, 0);
141  if (rs_ssh_tx_get_protocol(tx, &protocol, &b_len, STREAM_TOSERVER) != 1)
142  return LuaCallbackError(luastate, "error: no client proto version");
143  if (protocol == NULL || b_len == 0) {
144  return LuaCallbackError(luastate, "error: no client proto version");
145  }
146 
147  return LuaPushStringBuffer(luastate, protocol, b_len);
148 }
149 
150 static int SshGetClientProtoVersion(lua_State *luastate)
151 {
152  int r;
153 
154  if (!(LuaStateNeedProto(luastate, ALPROTO_SSH)))
155  return LuaCallbackError(luastate, "error: protocol not ssh");
156 
157  Flow *f = LuaStateGetFlow(luastate);
158  if (f == NULL)
159  return LuaCallbackError(luastate, "internal error: no flow");
160 
161  r = GetClientProtoVersion(luastate, f);
162 
163  return r;
164 }
165 
166 static int GetClientSoftwareVersion(lua_State *luastate, const Flow *f)
167 {
168  void *state = FlowGetAppState(f);
169  if (state == NULL)
170  return LuaCallbackError(luastate, "error: no app layer state");
171 
172  const uint8_t *software = NULL;
173  uint32_t b_len = 0;
174 
175  void *tx = rs_ssh_state_get_tx(state, 0);
176  if (rs_ssh_tx_get_software(tx, &software, &b_len, STREAM_TOSERVER) != 1)
177  return LuaCallbackError(luastate, "error: no client software version");
178  if (software == NULL || b_len == 0) {
179  return LuaCallbackError(luastate, "error: no client software version");
180  }
181 
182  return LuaPushStringBuffer(luastate, software, b_len);
183 }
184 
185 static int SshGetClientSoftwareVersion(lua_State *luastate)
186 {
187  int r;
188 
189  if (!(LuaStateNeedProto(luastate, ALPROTO_SSH)))
190  return LuaCallbackError(luastate, "error: protocol not ssh");
191 
192  Flow *f = LuaStateGetFlow(luastate);
193  if (f == NULL)
194  return LuaCallbackError(luastate, "internal error: no flow");
195 
196  r = GetClientSoftwareVersion(luastate, f);
197 
198  return r;
199 }
200 
201 /** \brief register ssh lua extensions in a luastate */
202 int LuaRegisterSshFunctions(lua_State *luastate)
203 {
204  /* registration of the callbacks */
205  lua_pushcfunction(luastate, SshGetServerProtoVersion);
206  lua_setglobal(luastate, "SshGetServerProtoVersion");
207 
208  lua_pushcfunction(luastate, SshGetServerSoftwareVersion);
209  lua_setglobal(luastate, "SshGetServerSoftwareVersion");
210 
211  lua_pushcfunction(luastate, SshGetClientProtoVersion);
212  lua_setglobal(luastate, "SshGetClientProtoVersion");
213 
214  lua_pushcfunction(luastate, SshGetClientSoftwareVersion);
215  lua_setglobal(luastate, "SshGetClientSoftwareVersion");
216 
217  return 0;
218 }
219 
220 #endif /* HAVE_LUA */
tm-threads.h
util-lua-ssh.h
app-layer-ssh.h
util-lua-common.h
util-lua.h
threads.h
Flow_
Flow data structure.
Definition: flow.h:343
rust.h
util-privs.h
ALPROTO_SSH
@ ALPROTO_SSH
Definition: app-layer-protos.h:34
util-unittest.h
FlowGetAppState
void * FlowGetAppState(const Flow *f)
Definition: flow.c:1081
protocol
uint16_t protocol
Definition: decode-ppp.h:2
util-debug.h
STREAM_TOSERVER
#define STREAM_TOSERVER
Definition: stream.h:31
util-print.h
detect.h
pkt-var.h
util-time.h
app-layer-parser.h
conf.h
util-proto-name.h
suricata-common.h
STREAM_TOCLIENT
#define STREAM_TOCLIENT
Definition: stream.h:32
lua_State
void lua_State
Definition: suricata-common.h:509
threadvars.h
util-logopenfile.h
util-buffer.h
debug.h
output.h
app-layer.h