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 "detect.h"
28 #include "pkt-var.h"
29 #include "conf.h"
30 
31 #include "threads.h"
32 #include "threadvars.h"
33 #include "tm-threads.h"
34 
35 #include "util-print.h"
36 #include "util-unittest.h"
37 
38 #include "util-debug.h"
39 
40 #include "output.h"
41 #include "app-layer.h"
42 #include "app-layer-parser.h"
43 #include "app-layer-ssl.h"
44 #include "util-privs.h"
45 #include "util-buffer.h"
46 #include "util-proto-name.h"
47 #include "util-logopenfile.h"
48 #include "util-time.h"
49 
50 #ifdef HAVE_LUA
51 
52 #include <lua.h>
53 #include <lualib.h>
54 #include <lauxlib.h>
55 
56 #include "util-lua.h"
57 #include "util-lua-common.h"
58 #include "util-lua-ja3.h"
59 
60 static int Ja3GetHash(lua_State *luastate)
61 {
62  if (!(LuaStateNeedProto(luastate, ALPROTO_TLS)))
63  return LuaCallbackError(luastate, "error: protocol is not tls");
64 
65  Flow *f = LuaStateGetFlow(luastate);
66  if (f == NULL)
67  return LuaCallbackError(luastate, "internal error: no flow");
68 
69  void *state = FlowGetAppState(f);
70  if (state == NULL)
71  return LuaCallbackError(luastate, "error: no app layer state");
72 
73  SSLState *ssl_state = (SSLState *)state;
74 
75  if (ssl_state->client_connp.ja3_hash == NULL)
76  return LuaCallbackError(luastate, "error: no JA3 hash");
77 
78  return LuaPushStringBuffer(luastate,
79  (uint8_t *)ssl_state->client_connp.ja3_hash,
80  strlen(ssl_state->client_connp.ja3_hash));
81 }
82 
83 static int Ja3GetString(lua_State *luastate)
84 {
85  if (!(LuaStateNeedProto(luastate, ALPROTO_TLS)))
86  return LuaCallbackError(luastate, "error: protocol is not tls");
87 
88  Flow *f = LuaStateGetFlow(luastate);
89  if (f == NULL)
90  return LuaCallbackError(luastate, "internal error: no flow");
91 
92  void *state = FlowGetAppState(f);
93  if (state == NULL)
94  return LuaCallbackError(luastate, "error: no app layer state");
95 
96  SSLState *ssl_state = (SSLState *)state;
97 
98  if (ssl_state->client_connp.ja3_str == NULL ||
99  ssl_state->client_connp.ja3_str->data == NULL)
100  return LuaCallbackError(luastate, "error: no JA3 str");
101 
102  return LuaPushStringBuffer(luastate,
103  (uint8_t *)ssl_state->client_connp.ja3_str->data,
104  ssl_state->client_connp.ja3_str->used);
105 }
106 
107 static int Ja3SGetHash(lua_State *luastate)
108 {
109  if (!(LuaStateNeedProto(luastate, ALPROTO_TLS)))
110  return LuaCallbackError(luastate, "error: protocol is not tls");
111 
112  Flow *f = LuaStateGetFlow(luastate);
113  if (f == NULL)
114  return LuaCallbackError(luastate, "internal error: no flow");
115 
116  void *state = FlowGetAppState(f);
117  if (state == NULL)
118  return LuaCallbackError(luastate, "error: no app layer state");
119 
120  SSLState *ssl_state = (SSLState *)state;
121 
122  if (ssl_state->server_connp.ja3_hash == NULL)
123  return LuaCallbackError(luastate, "error: no JA3S hash");
124 
125  return LuaPushStringBuffer(luastate,
126  (uint8_t *)ssl_state->server_connp.ja3_hash,
127  strlen(ssl_state->server_connp.ja3_hash));
128 }
129 
130 static int Ja3SGetString(lua_State *luastate)
131 {
132  if (!(LuaStateNeedProto(luastate, ALPROTO_TLS)))
133  return LuaCallbackError(luastate, "error: protocol is not tls");
134 
135  Flow *f = LuaStateGetFlow(luastate);
136  if (f == NULL)
137  return LuaCallbackError(luastate, "internal error: no flow");
138 
139  void *state = FlowGetAppState(f);
140  if (state == NULL)
141  return LuaCallbackError(luastate, "error: no app layer state");
142 
143  SSLState *ssl_state = (SSLState *)state;
144 
145  if (ssl_state->server_connp.ja3_str == NULL ||
146  ssl_state->server_connp.ja3_str->data == NULL)
147  return LuaCallbackError(luastate, "error: no JA3S str");
148 
149  return LuaPushStringBuffer(luastate,
150  (uint8_t *)ssl_state->server_connp.ja3_str->data,
151  ssl_state->server_connp.ja3_str->used);
152 }
153 
154 /** *\brief Register JA3 Lua extensions */
155 int LuaRegisterJa3Functions(lua_State *luastate)
156 {
157  lua_pushcfunction(luastate, Ja3GetHash);
158  lua_setglobal(luastate, "Ja3GetHash");
159 
160  lua_pushcfunction(luastate, Ja3GetString);
161  lua_setglobal(luastate, "Ja3GetString");
162 
163  lua_pushcfunction(luastate, Ja3SGetHash);
164  lua_setglobal(luastate, "Ja3SGetHash");
165 
166  lua_pushcfunction(luastate, Ja3SGetString);
167  lua_setglobal(luastate, "Ja3SGetString");
168 
169  return 0;
170 }
171 
172 #endif /* HAVE_LUA */
tm-threads.h
SSLState_
SSLv[2.0|3.[0|1|2|3]] state structure.
Definition: app-layer-ssl.h:288
util-lua-common.h
SSLState_::client_connp
SSLStateConnp client_connp
Definition: app-layer-ssl.h:306
ALPROTO_TLS
@ ALPROTO_TLS
Definition: app-layer-protos.h:33
SSLState_::server_connp
SSLStateConnp server_connp
Definition: app-layer-ssl.h:307
util-lua.h
SSLStateConnp_::ja3_hash
char * ja3_hash
Definition: app-layer-ssl.h:270
threads.h
Flow_
Flow data structure.
Definition: flow.h:351
util-lua-ja3.h
JA3Buffer_::data
char * data
Definition: util-ja3.h:32
util-privs.h
util-unittest.h
JA3Buffer_::used
size_t used
Definition: util-ja3.h:34
util-debug.h
util-print.h
detect.h
pkt-var.h
util-time.h
app-layer-parser.h
conf.h
util-proto-name.h
suricata-common.h
lua_State
void lua_State
Definition: suricata-common.h:500
threadvars.h
util-logopenfile.h
util-buffer.h
SSLStateConnp_::ja3_str
JA3Buffer * ja3_str
Definition: app-layer-ssl.h:269
app-layer-ssl.h
output.h
app-layer.h