suricata
util-lua-ja3.c
Go to the documentation of this file.
1 /* Copyright (C) 2017 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-ssl.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 
51 #ifdef HAVE_LUA
52 
53 #include <lua.h>
54 #include <lualib.h>
55 #include <lauxlib.h>
56 
57 #include "util-lua.h"
58 #include "util-lua-common.h"
59 #include "util-lua-ja3.h"
60 
61 static int Ja3GetHash(lua_State *luastate)
62 {
63  if (!(LuaStateNeedProto(luastate, ALPROTO_TLS)))
64  return LuaCallbackError(luastate, "error: protocol is not tls");
65 
66  Flow *f = LuaStateGetFlow(luastate);
67  if (f == NULL)
68  return LuaCallbackError(luastate, "internal error: no flow");
69 
70  void *state = FlowGetAppState(f);
71  if (state == NULL)
72  return LuaCallbackError(luastate, "error: no app layer state");
73 
74  SSLState *ssl_state = (SSLState *)state;
75 
76  if (ssl_state->client_connp.ja3_hash == NULL)
77  return LuaCallbackError(luastate, "error: no JA3 hash");
78 
79  return LuaPushStringBuffer(luastate,
80  (uint8_t *)ssl_state->client_connp.ja3_hash,
81  strlen(ssl_state->client_connp.ja3_hash));
82 }
83 
84 static int Ja3GetString(lua_State *luastate)
85 {
86  if (!(LuaStateNeedProto(luastate, ALPROTO_TLS)))
87  return LuaCallbackError(luastate, "error: protocol is not tls");
88 
89  Flow *f = LuaStateGetFlow(luastate);
90  if (f == NULL)
91  return LuaCallbackError(luastate, "internal error: no flow");
92 
93  void *state = FlowGetAppState(f);
94  if (state == NULL)
95  return LuaCallbackError(luastate, "error: no app layer state");
96 
97  SSLState *ssl_state = (SSLState *)state;
98 
99  if (ssl_state->client_connp.ja3_str == NULL ||
100  ssl_state->client_connp.ja3_str->data == NULL)
101  return LuaCallbackError(luastate, "error: no JA3 str");
102 
103  return LuaPushStringBuffer(luastate,
104  (uint8_t *)ssl_state->client_connp.ja3_str->data,
105  ssl_state->client_connp.ja3_str->used);
106 }
107 
108 static int Ja3SGetHash(lua_State *luastate)
109 {
110  if (!(LuaStateNeedProto(luastate, ALPROTO_TLS)))
111  return LuaCallbackError(luastate, "error: protocol is not tls");
112 
113  Flow *f = LuaStateGetFlow(luastate);
114  if (f == NULL)
115  return LuaCallbackError(luastate, "internal error: no flow");
116 
117  void *state = FlowGetAppState(f);
118  if (state == NULL)
119  return LuaCallbackError(luastate, "error: no app layer state");
120 
121  SSLState *ssl_state = (SSLState *)state;
122 
123  if (ssl_state->server_connp.ja3_hash == NULL)
124  return LuaCallbackError(luastate, "error: no JA3S hash");
125 
126  return LuaPushStringBuffer(luastate,
127  (uint8_t *)ssl_state->server_connp.ja3_hash,
128  strlen(ssl_state->server_connp.ja3_hash));
129 }
130 
131 static int Ja3SGetString(lua_State *luastate)
132 {
133  if (!(LuaStateNeedProto(luastate, ALPROTO_TLS)))
134  return LuaCallbackError(luastate, "error: protocol is not tls");
135 
136  Flow *f = LuaStateGetFlow(luastate);
137  if (f == NULL)
138  return LuaCallbackError(luastate, "internal error: no flow");
139 
140  void *state = FlowGetAppState(f);
141  if (state == NULL)
142  return LuaCallbackError(luastate, "error: no app layer state");
143 
144  SSLState *ssl_state = (SSLState *)state;
145 
146  if (ssl_state->server_connp.ja3_str == NULL ||
147  ssl_state->server_connp.ja3_str->data == NULL)
148  return LuaCallbackError(luastate, "error: no JA3S str");
149 
150  return LuaPushStringBuffer(luastate,
151  (uint8_t *)ssl_state->server_connp.ja3_str->data,
152  ssl_state->server_connp.ja3_str->used);
153 }
154 
155 /** *\brief Register JA3 Lua extensions */
156 int LuaRegisterJa3Functions(lua_State *luastate)
157 {
158  lua_pushcfunction(luastate, Ja3GetHash);
159  lua_setglobal(luastate, "Ja3GetHash");
160 
161  lua_pushcfunction(luastate, Ja3GetString);
162  lua_setglobal(luastate, "Ja3GetString");
163 
164  lua_pushcfunction(luastate, Ja3SGetHash);
165  lua_setglobal(luastate, "Ja3SGetHash");
166 
167  lua_pushcfunction(luastate, Ja3SGetString);
168  lua_setglobal(luastate, "Ja3SGetString");
169 
170  return 0;
171 }
172 
173 #endif /* HAVE_LUA */
SSLStateConnp server_connp
char * data
Definition: util-ja3.h:30
SSLv[2.0|3.[0|1|2|3]] state structure.
size_t used
Definition: util-ja3.h:32
void * FlowGetAppState(const Flow *f)
Definition: flow.c:1065
JA3Buffer * ja3_str
Flow data structure.
Definition: flow.h:325
SSLStateConnp client_connp